title: PostgreSQL HashAggregate lack of performance
body: <p>I have this pretty simple query on a large <em>foreign (columnstore)</em> table:</p>
  
  <pre><code>select distinct "Business Unit", "Application", "Application Suite", "Account Name" 
      from __tmp_l1_11259 
      where "Date" between '2017-10-01' and '2017-10-31';
  </code></pre>
  
  <p>So, I launch this query in SQL Server using hint <code>hash group</code> and it performs  <strong>in less than two seconds</strong>. PostgreSQL goes two ways: it uses HashAggregate or it groups rows by sorting them and perfroming Unique. Here's plans:</p>
  
  <pre><code>                                                                    QUERY PLAN
  -----------------------------------------------------------------------------------------------------------------------------------------------------
   HashAggregate  (cost=2169041.54..2169041.55 rows=1 width=128) (actual time=11671.983..11672.065 rows=407 loops=1)
    Group Key: "Business Unit", "Application", "Application Suite", "Account Name"
    -&gt;  Foreign Scan on tmp_l1_  (cost=0.00..2164695.79 rows=434575 width=128) (actual time=6.576..4830.866 rows=14237546 loops=1)
          Filter: (("Date" &gt;= '2017-10-01 00:00:00'::timestamp without time zone) AND ("Date" &lt;= '2017-10-31 00:00:00'::timestamp without time zone))
          Rows Removed by Filter: 8702454
          CStore File: /datadrive/postgresql/cstore_fdw/16507/16540
          CStore File Size: 87457953966
   Planning time: 15.914 ms
   Execution time: 11672.927 ms
  (9 rows)
  
                                                                      QUERY PLAN
  -----------------------------------------------------------------------------------------------------------------------------------------------------------
   Unique  (cost=2261840.10..2267272.29 rows=1 width=128) (actual time=44412.373..57237.559 rows=407 loops=1)
     -&gt;  Sort  (cost=2261840.10..2262926.54 rows=434575 width=128) (actual time=44412.371..53115.637 rows=14237546 loops=1)
           Sort Key: "Business Unit", "Application", "Application Suite", "Account Name"
           Sort Method: external merge  Disk: 804440kB
           -&gt;  Foreign Scan on tmp_l1_  (cost=0.00..2164695.79 rows=434575 width=128) (actual time=6.209..5488.539 rows=14237546 loops=1)
                 Filter: (("Date" &gt;= '2017-10-01 00:00:00'::timestamp without time zone) AND ("Date" &lt;= '2017-10-31 00:00:00'::timestamp without time zone))
                 Rows Removed by Filter: 8702454
                 CStore File: /datadrive/postgresql/cstore_fdw/16507/16540
                 CStore File Size: 87457953966
   Planning time: 19.011 ms
   Execution time: 76676.073 ms
  (11 rows)
  </code></pre>
  
  <p>Can I improve perfromance of this query without changing algorithm of HashAggregate in PostgreSQL sources? If yes, how?</p>