Re: postgres_fdw aggregate pushdown for group by with expressions

From: Michał Kłeczek <michal(at)kleczek(dot)org>
To: PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: postgres_fdw aggregate pushdown for group by with expressions
Date: 2024-03-04 12:35:33
Message-ID: 6B0B7CCB-2325-42BE-9240-45B26C74576D@kleczek.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi All,

> On 3 Mar 2024, at 10:34, Michał Kłeczek <michal(at)kleczek(dot)org> wrote:
>
> Hi,
>
> I have the following foreign table:
>
> CREATE FOREIGN TABLE t1 (
> grouping_column text,
> date_column date,
> whatever_data int
> );
>
> The query is:
>
> SELECT
> sum(whatever_data)
> FROM
> t1
> GROUP BY
> grouping_colulmn, extract(YEAR FROM date_column), extract(MONTH FROM date_column);
>
> From my (preliminary) testing postgres_fdw will not push down this aggregate query
> - it will happily push down query with only “grouping_column” or “grouping_column, date_column" in GROUP BY

I was able to make it work by creating an extension and installing it on both sides with functions:

year(date) IMMUTABLE
month(date) IMMUTABLE

But it looks there are more issues - the following queries work fine (ie. are pushed down to remote server):

SELECT * FROM t1 WHERE grouping_column LIKE ‘abcd%’;
and
SELECT sum(whatever_data) FROM t1 GROUP BY grouping_column, year(date_column), month(date_column)

But together - unfortunately not:

SELECT grouping_column, sum(whatever_data) FROM t1 WHERE grouping_column LIKE ‘abcd%' GROUP BY grouping_column, year(date_column)

In this case aggregate is performed locally.
Not sure if this is because of statistics (but I set fdw_tuple_cost to a very high value 99999 to try to force planner to push down aggregates)


Michal

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Francisco Olarte 2024-03-04 12:45:48 Re: Inconsistent results in timestamp/interval comparison
Previous Message albrecht.dress 2024-03-04 12:09:47 Inconsistent results in timestamp/interval comparison