Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xA0ES-00000002Ogd-0CnO for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Sep 2026 07:20:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xA0EQ-0000000GxcM-2j4I for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Sep 2026 07:20:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xA0EQ-0000000GxcD-0p5C for pgsql-hackers@lists.postgresql.org; Fri, 25 Sep 2026 07:20:30 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xA0EM-00000001CCj-3wdr for pgsql-hackers@lists.postgresql.org; Fri, 25 Sep 2026 07:20:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1790320825; bh=0oezTVXxKlbI5JnhrH1SURQ9JxOAwKuG6kM+aZXoKCs=; h=Date:From:To:Cc:Subject:In-Reply-To:References:Message-ID:From; b=hQSJPvdXORByMTXLcx5/VvWNDbpgQLi2kqbnEdL4sYp61BXX+T81Ks/Zn5q5e1OSP XYNXRM/kHlKnRPmZrFXsyP5yAD34WApR9bvTGjDMMH4xWNcx/z7mWS+TRWb8RtyP43 nypXXZL/txFhdlDxPFYr1EWB3uNd7fIKNzt8pM/ODnL3gWaccnIVUqE13Vvo++dCeT Fx7pMU/7vO9BAuZ0P1lKTIbpMHslUVyrEiZLEjoL8RBDg7gnZ/ss1yMHE3kEMhjLy5 656OSLwVkjdB5jSsRvT7A+9n1kHYvPL0qt8TOH3iCp0+IbVgEi2InnCVx/zEu6uL8q x0sVc1KIMWzfQ== Received: from mail.l.postgrespro.ru (webmail-master-mstn.l.postgrespro.ru [192.168.2.26]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: a.pyhalov@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id 9C1045FD77; Fri, 25 Sep 2026 10:20:25 +0300 (MSK) MIME-Version: 1.0 Date: Fri, 25 Sep 2026 10:20:25 +0300 From: Alexander Pyhalov To: Kirill Reshke Cc: PostgreSQL Hackers , Alexander Korotkov Subject: Re: FDW RTE join pushdown fails to create plan with aggregates In-Reply-To: References: Message-ID: <8972614da36fcb0745e8060acf528cb2@postgrespro.ru> X-Sender: a.pyhalov@postgrespro.ru Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-KSMG-AntiPhishing: NotDetected, bases: 2026/09/25 06:41:00 X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 3.0.0.9059, bases: 2026/09/25 05:23:00 #28625684 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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= out>, context=0x7ffce16693c0) at var.c:699 > #2 0x00005c3e6bda543b in expression_tree_walker_impl (node= out>, walker=0x5c3e6be5f3a0 , > context=0x7ffce16693c0) at nodeFuncs.c:2544 > #3 0x00005c3e6be605d2 in pull_var_clause (node=, > 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=, > best_path=0x5c3ea98fa558, tlist=0x0, scan_clauses=0x0, outer_plan=0x0) > at postgres_fdw.c:1471 > #6 0x00005c3e6be1f268 in create_foreignscan_plan > (scan_clauses=, tlist=0x0, best_path=0x5c3ea98fa558, > root=0x5c3ea98f3e88) at createplan.c:4011 > #7 create_scan_plan (root=0x5c3ea98f3e88, best_path=0x5c3ea98fa558, > flags=) 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.jpnprd01.prod.outlook.com#54ab5e31f1c46ec6f109667f09b0407f -- Best regards, Alexander Pyhalov, Postgres Professional