| From: | Richard Guo <guofenglinux(at)gmail(dot)com> |
|---|---|
| To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
| Cc: | Vik Fearing <vik(at)postgresfriends(dot)org>, Kamil Monicz <kamil(at)monicz(dot)dev>, pgsql-bugs(at)lists(dot)postgresql(dot)org, Peter Eisentraut <peter(at)eisentraut(dot)org> |
| Subject: | Re: BUG #19106: Potential regression with CTE materialization planning in Postgres 18 |
| Date: | 2025-11-13 03:49:17 |
| Message-ID: | CAMbWs498fBh_p+kXRv=-3d8C7TXanjLP_d6RTP3dOXKp+59-rA@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
On Thu, Nov 13, 2025 at 4:32 AM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> The sticky point here is that a CTE reference isn't quite as absolute
> as a physical-table reference: the CTE name only has meaning within
> a portion of the query. So the problem that b0cc0a71e tried to solve
> is "what do we do if the SQL-standard rules about semantic level of
> an aggregate would result in putting the aggregate outside of the
> scope of a CTE it references?"
So, IIUC, the confusion arises in cases where an aggregation is to be
assigned to the outer side of its syntactic level. With the current
patch, if the aggregation does not reference any CTEs, it would be
evaluated at the outer query level. If the aggregation references any
CTEs, it'd be evaluated at its syntactic query level.
However, I still find this behavior somewhat confusing. For example,
one might expect that an inlined CTE should be semantically equivalent
to a subquery, yet the following two queries can produce different
results.
create table t (a int);
insert into t values (1), (2);
with ss as not materialized (select * from t)
select (select sum((select a from ss where a = t.a limit 1))) from t;
sum
-----
1
2
(2 rows)
select (select sum((select a from (select * from t) ss where a = t.a
limit 1))) from t;
sum
-----
3
(1 row)
I don't have much experience reading the SQL spec, but from the
discussions, it seems that the spec does not provide guidance on this
case. So the current behavior may be acceptable. I think it might be
helpful to explicitly document this behavior somewhere.
- Richard
| From | Date | Subject | |
|---|---|---|---|
| Next Message | PG Bug reporting form | 2025-11-13 10:04:08 | BUG #19111: Using EXPLAIN ANALYZE with MERGE causes failed assert |
| Previous Message | Tom Lane | 2025-11-12 19:32:53 | Re: BUG #19106: Potential regression with CTE materialization planning in Postgres 18 |