pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexander Pyhalov <a.pyhalov@postgrespro.ru>
To: Kirill Reshke <reshkekirill@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Cc: Alexander Korotkov <aekorotkov@gmail.com>
Subject: Re: FDW RTE join pushdown fails to create plan with aggregates
Date: Fri, 25 Sep 2026 10:20:25 +0300
Message-ID: <8972614da36fcb0745e8060acf528cb2@postgrespro.ru> (raw)
In-Reply-To: <CALdSSPh9SYVD3LRot6_fVtSt=jW=S=iVPEU-onJ-5ozn64n=Qw@mail.gmail.com>
References: <CALdSSPh9SYVD3LRot6_fVtSt=jW=S=iVPEU-onJ-5ozn64n=Qw@mail.gmail.com>

Kirill Reshke писал(а) 2026-09-25 07:51:
> On head, planner fails to build a plan for foreign relation joined
> with RTE in cases where Eager Aggregation optimization is applicable.
> 
> This issue exists starting at 0ee83dd4a99.
> 
> repro:
> 
> CREATE SCHEMA rmt;
> CREATE SERVER srv FOREIGN DATA WRAPPER postgres_fdw
>   OPTIONS (dbname 'postgres', host 'localhost', port '5432');
> CREATE USER MAPPING FOR CURRENT_USER SERVER srv;
> CREATE FOREIGN TABLE rmt.a (id integer, k integer, v text)
>   SERVER srv OPTIONS (schema_name 'r', table_name 'a');
> 
> 
> EXPLAIN
> SELECT count(1) FROM rmt.a, generate_series(1,1) GROUP BY id;
> ERROR:  Aggref found where not expected
> 
> 
> bt:
> ```
> #0  errstart_cold (elevel=elevel@entry=21, domain=domain@entry=0x0) at
> elog.c:340
> #1  0x00005c3e6badfa57 in pull_var_clause_walker (node=<optimized
> out>, context=0x7ffce16693c0) at var.c:699
> #2  0x00005c3e6bda543b in expression_tree_walker_impl (node=<optimized
> out>, walker=0x5c3e6be5f3a0 <pull_var_clause_walker>,
> context=0x7ffce16693c0) at nodeFuncs.c:2544
> #3  0x00005c3e6be605d2 in pull_var_clause (node=<optimized out>,
> flags=flags@entry=32) at var.c:668
> #4  0x00007344a35d4f10 in build_tlist_to_deparse
> (foreignrel=foreignrel@entry=0x5c3ea98f9918) at deparse.c:1250
> #5  0x00007344a35e1922 in postgresGetForeignPlan (root=0x5c3ea98f3e88,
> foreignrel=0x5c3ea98f9918, foreigntableid=<optimized out>,
> best_path=0x5c3ea98fa558, tlist=0x0, scan_clauses=0x0, outer_plan=0x0)
>     at postgres_fdw.c:1471
> #6  0x00005c3e6be1f268 in create_foreignscan_plan
> (scan_clauses=<optimized out>, tlist=0x0, best_path=0x5c3ea98fa558,
> root=0x5c3ea98f3e88) at createplan.c:4011
> #7  create_scan_plan (root=0x5c3ea98f3e88, best_path=0x5c3ea98fa558,
> flags=<optimized out>) at createplan.c:785
> #8  0x00005c3e6be1ad70 in create_projection_plan (root=0x5c3ea98f3e88,
> best_path=0x5c3ea98fbfa0, flags=6) at createplan.c:1907
> #9  0x00005c3e6be1bab6 in create_sort_plan (flags=4,
> best_path=0x5c3ea98fc6b0, root=0x5c3ea98f3e88) at createplan.c:2037
> #10 create_plan_recurse (root=0x5c3ea98f3e88,
> best_path=0x5c3ea98fc6b0, flags=4) at createplan.c:487
> ```
> 
> So, eager aggregation optimization tries to pushdown relations with
> partial agg tle, which postgresGetForeignJoinPaths couldn't deparse,
> so there is an error.
> 
> PFA simple patch adding guard for this exact case.
> 
> In principle, we can make deparse more smarter and do actually
> pushdown partial agg, but looks like this is less likely to land in a
> short time.
> 
> 
> +CC  Alexander Korotkov as committer of 0ee83dd4a99
> +CC Author  Alexander Pyhalov as author

Hi.

Yes, there's an issue, but it seems to be not specific to function 
pushdown.
For example, the following test case

EXPLAIN (VERBOSE, COSTS OFF)
SELECT count(1) FROM remote_tbl r1, remote_tbl r2 GROUP BY r1.a;
ERROR:  Aggref found where not expected

if we emit foreign path with low cost for grouped_rel (to test this I've 
just set low Foreign Path cost in
make_grouped_join_rel() after populate_joinrel_with_paths()).

And yes, we can't deparse partial aggregates now. The last discussion of 
the issue was here[1], but had been before
eager aggregation was introduced.

I'm not sure, does issue affect only joinrels? Can't we encounter other 
grouped rels while building foreign paths?

[1] 
https://www.postgresql.org/message-id/flat/TYRPR01MB13941CEA16574771B1BFD130595A02@TYRPR01MB13941.jp...

-- 
Best regards,
Alexander Pyhalov,
Postgres Professional





view thread (5+ messages)  latest in thread

Message-ID: <8972614da36fcb0745e8060acf528cb2@postgrespro.ru>
Permalink:  ../8972614da36fcb0745e8060acf528cb2@postgrespro.ru/
Also on:    postgresql.org/message-id/8972614da36fcb0745e8060acf528cb2@postgrespro.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: a.pyhalov@postgrespro.ru, reshkekirill@gmail.com, aekorotkov@gmail.com
  Subject: Re: FDW RTE join pushdown fails to create plan with aggregates
  In-Reply-To: <8972614da36fcb0745e8060acf528cb2@postgrespro.ru>
  References: <CALdSSPh9SYVD3LRot6_fVtSt=jW=S=iVPEU-onJ-5ozn64n=Qw@mail.gmail.com> <8972614da36fcb0745e8060acf528cb2@postgrespro.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox