Re: BUG #19106: Potential regression with CTE materialization planning in Postgres 18
Vik Fearing <vik@postgresfriends.org>
From: Vik Fearing <vik@postgresfriends.org>
To: Tom Lane <tgl@sss.pgh.pa.us>, Kamil Monicz <kamil@monicz.dev>
Cc: pgsql-bugs@lists.postgresql.org, Peter Eisentraut <peter@eisentraut.org>
Date: 2025-11-11T15:16:06Z
Lists: pgsql-bugs
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Don't allow CTEs to determine semantic levels of aggregates.
- b85397481ce3 14.21 landed
- 9f5a58aacf65 15.16 landed
- 35b5c62c3ad9 19 (unreleased) landed
- 1c8c3206f4e0 16.12 landed
- 12bc3291772e 18.2 landed
- 075a763e2d42 17.8 landed
-
Calculate agglevelsup correctly when Aggref contains a CTE.
- b0cc0a71e0a0 19 (unreleased) cited
On 10/11/2025 22:05, Tom Lane wrote: > [ cc'ing Peter and Vik for possible input on SQL-standard question ] Thanks! > WITH a AS ( > SELECT id FROM (VALUES (1), (2)) AS v(id) > ), > b AS ( > SELECT max((SELECT sum(id) FROM a)) AS agg > ) > SELECT agg FROM b; snip > I looked at the SQL standard for possible guidance and found none: > they disallow subqueries altogether within aggregate arguments, > so they need not consider such cases. I am curious though whether > Peter or Vik know if the committee ever considered relaxing that > restriction, and if so whether they stopped to think about this > particular point. I am not seeing that restriction in the standard. For this test case, we have MAX which has the lineage: <aggregate function> <general set function> <set function type> <computational operation> MAX Its argument, (SELECT SUM(id) FROM a), has this lineage: <value expression> <common value expression> <numeric value expression> <term> <factor> <numeric primary> <value expression primary> <non-parenthesized value expression primary> <scalar subquery> <subquery> Since there are no outer column references, the subquery should be independent. And if we inline it: WITH b (agg) AS ( SELECT MAX(( SELECT SUM(id) FROM (VALUES (1), (2)) AS v (id) )) -- FROM nothing ) TABLE b then the query works as expected. MATERIALIZEDing either or both CTEs has no effect, which I find strange. -- Vik Fearing