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.96) (envelope-from ) id 1wuBcx-000WNf-2E for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Aug 2026 16:16:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wuBcu-008wNY-1l for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Aug 2026 16:16:25 +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.96) (envelope-from ) id 1wuBcu-008wNQ-0q for pgsql-hackers@lists.postgresql.org; Wed, 12 Aug 2026 16:16:25 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wuBcs-00000000Ntu-3Dmn for pgsql-hackers@postgresql.org; Wed, 12 Aug 2026 16:16:25 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 67CGGKaa2732077; Wed, 12 Aug 2026 12:16:20 -0400 From: Tom Lane To: Sami Imseih cc: pgsql-hackers , Ewan Young , Ashutosh Bapat Subject: Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE In-reply-to: References: Comments: In-reply-to Sami Imseih message dated "Wed, 12 Aug 2026 10:34:51 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2732075.1786551380.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 12 Aug 2026 12:16:20 -0400 Message-ID: <2732076.1786551380@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Sami Imseih writes: > Commit f585671055d1 [1] disallowed aggregates, window functions, and SRF= s in a > GRAPH_TABLE COLUMNS list. For the aggregate case it tests pstate->p_hasA= ggs > after transforming the columns. That is not enough. An aggregate that > references an outer query is attributed to a parent query level, so > check_agglevels_and_constraints sets p_hasAggs on that parent ParseState= and > not on the GRAPH_TABLE's own, so the aggregate is not caught. It kind of seems like that should work, since an outer-level Agg is effectively a constant within any execution of the lower query. However, I'm content to say that making it work is out of scope for v19. > The attached patch closes both gaps by walking the transformed COLUMNS l= ist and > the graph pattern for Aggref and GroupingFunc nodes. Unlike p_hasAggs, t= he walk > detects an aggregate by its presence in those the trees. Window function= s > and SRFs only mark the local ParseState, so their checks introduced in > f585671055d1 remain in place. I do not like this fix approach, and for that matter I don't like f58567105. This is expensive thanks to the extra tree traversal, f58567105 is user-unfriendly because it fails to say exactly what or where is the construct it's rejecting, and neither patch is following the perfectly good structure that the parser already has for this kind of check. IMO the correct way to handle this restriction is to check it in check_agglevels_and_constraints() based on the ParseExprKind of the surrounding expression. It looks like the graph transformation steps in the parser think they can get away with using EXPR_KIND_WHERE and EXPR_KIND_SELECT_TARGET, but that's just wrong if the constraints on these subexpressions are any different from what they are for regular WHERE and SELECT targets. The right way to go about this is to back-fill new ParseExprKind(s) as needed and then make the necessary checks in a way similar to existing code. Doing that might help you find other comparable oversights, too. regards, tom lane