]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix parse_cte.c's failure to examine sub-WITHs in DML statements.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 5 Apr 2025 19:01:33 +0000 (15:01 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 5 Apr 2025 19:01:33 +0000 (15:01 -0400)
commitede29a1e400bc9d1488ec70ec5630b62ca071fd5
tree1562cedf677cd2ef7249f8582d3010bb7d6c2ad3
parentf5069f0264eedd71ee138b872b131a2974017b6a
Fix parse_cte.c's failure to examine sub-WITHs in DML statements.

makeDependencyGraphWalker thought that only SelectStmt nodes could
contain a WithClause.  Which was true in our original implementation
of WITH, but astonishingly we missed updating this code when we added
the ability to attach WITH to INSERT/UPDATE/DELETE (and later MERGE).
Moreover, since it was coded to deliberately block recursion to a
WithClause, even updating raw_expression_tree_walker didn't save it.

The upshot of this was that we didn't see references to outer CTE
names appearing within an inner WITH, and would neither complain about
disallowed recursion nor account for such references when sorting CTEs
into a usable order.  The lack of complaints about this is perhaps not
so surprising, because typical usage of WITH wouldn't hit either case.
Still, it's pretty broken; failing to detect recursion here leads to
assert failures or worse later on.

Fix by factoring out the processing of sub-WITHs into a new function
WalkInnerWith, and invoking that for all the statement types that
can have WITH.

Bug: #18878
Reported-by: Yu Liang <luy70@psu.edu>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/18878-a26fa5ab6be2f2cf@postgresql.org
Backpatch-through: 13
src/backend/parser/parse_cte.c
src/test/regress/expected/with.out
src/test/regress/sql/with.sql