Re: Assert failure in CTE inlining with view and correlated subquery
Richard Guo <guofenglinux@gmail.com>
From: Richard Guo <guofenglinux@gmail.com>
To: Tomas Vondra <tomas.vondra@enterprisedb.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2022-04-21T07:28:32Z
Lists: pgsql-hackers
On Thu, Apr 21, 2022 at 5:33 AM Tomas Vondra <tomas.vondra@enterprisedb.com>
wrote:
>
> The difference between plans in (2) and (3) is interesting, because it
> seems the CTE got inlined, so why was the refcount not decremented?
>
The query is not actually referencing the cte. So the cte range table
entry would not appear anywhere in the query tree. That's why refcount
is not decremented after inline cte walker.
If we explicitly reference the cte in the query, say in the targetlist,
it would then work.
# explain (costs off) SELECT * FROM results_agg ORDER BY 1;
QUERY PLAN
---------------------------------------
Sort
Sort Key: r.run
-> HashAggregate
Group Key: r.run
-> Seq Scan on results r
SubPlan 1
-> Seq Scan on results
Filter: (run = r.run)
(8 rows)
IMO the culprit is that we incorrectly set cterefcount to one while
actually the cte is not referenced at all.
Thanks
Richard
Commits
-
Remove inadequate assertion check in CTE inlining.
- eade0043caaf 12.11 landed
- e6440d5007aa 13.7 landed
- da22ef388a24 14.3 landed
- 92e7a5375209 15.0 landed