pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Calculate agglevelsup correctly when Aggref contains a CTE.
Date: Wed, 17 Sep 2025 20:33:10 +0000
Message-ID: <E1uyypy-001Duk-12@gemulon.postgresql.org> (raw)

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

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/afd18f27615b0e452373acbc8de2e5282dfeb31b

Modified Files
--------------
src/backend/parser/parse_agg.c     | 28 +++++++++++++++++++++++++++-
src/test/regress/expected/with.out | 34 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/with.sql      | 14 ++++++++++++++
3 files changed, 75 insertions(+), 1 deletion(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1uyypy-001Duk-12@gemulon.postgresql.org>
Permalink:  ../E1uyypy-001Duk-12@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1uyypy-001Duk-12@gemulon.postgresql.org

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Calculate agglevelsup correctly when Aggref contains a CTE.
  In-Reply-To: <E1uyypy-001Duk-12@gemulon.postgresql.org>

* 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