]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Calculate agglevelsup correctly when Aggref contains a CTE.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 17 Sep 2025 20:32:57 +0000 (16:32 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 17 Sep 2025 20:32:57 +0000 (16:32 -0400)
commitafd18f27615b0e452373acbc8de2e5282dfeb31b
tree1edead6ed8215cbb065460315e3de237c3ea4a8d
parentf00ad440a5b6ce430dda31b03788e4d4b6659f60
Calculate agglevelsup correctly when Aggref contains a CTE.

If an aggregate function call contains a sub-select that has
an RTE referencing a CTE outside the aggregate, we must treat
that reference like a Var referencing the CTE's query level
for purposes of determining the aggregate's level.  Otherwise
we might reach the nonsensical conclusion that the aggregate
should be evaluated at some query level higher than the CTE,
ending in a planner error or a broken plan tree that causes
executor failures.

Bug: #19055
Reported-by: BugForge <dllggyx@outlook.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19055-6970cfa8556a394d@postgresql.org
Backpatch-through: 13
src/backend/parser/parse_agg.c
src/test/regress/expected/with.out
src/test/regress/sql/with.sql