title: Postgres "first" aggregation function
body: <p>I am aggregation a table using file ID field. Each file has a name which matched exactly one (his) file id.</p>
  
  <pre><code>select file_key, min(fullfilepath)
  from table
  group by file_key 
  </code></pre>
  
  <p>Because I know the structure of the table, I know that I need ANY fullfilepath. The min and the max are ok, but it requires a lot of time.</p>
  
  <p>I came across <a href="https://wiki.postgresql.org/wiki/First/last_(aggregate)" rel="nofollow noreferrer">this</a> aggregation function which returns the first value. Unfortunately, this function takes a long time, because it scans the whole table. For example, this is very slow:</p>
  
  <pre><code>select first(file_id) from table;
  </code></pre>
  
  <p>What is the fastest way to do that? With or without aggregation function.</p>