As of PostgreSQL 10.x, foreign tables support aggregate pushdown.
I noticed that sum and count operations are being pushed to the remote server but min and max are not pushed the same way.
select min(col) from foreign_table
shows a foreign scan with an ORDER BY, followed by a LIMIT applied locally.
In my case, the remote server is actually Redshift rather than another PostgreSQL instance, so this is going to be slower than issuing the min
right on the query the same way a sum or count would have been, and oddly, if you do a query like
select count(col), min(col) from foreign_table
the min
will be pushed on the query.
Why is postgres_fdw treating min/max differently and is there an optimizer setting that will get min/max to be treated same as other aggregate functions?