diff options
| author | Tom Lane | 2025-04-05 19:01:33 +0000 |
|---|---|---|
| committer | Tom Lane | 2025-04-05 19:01:33 +0000 |
| commit | 5e7be43f4b8bfe77570e566b89f23fc73ea625dc (patch) | |
| tree | e7ae12265120978d4696ca2935983559c9e73a47 /src/test | |
| parent | 717e8a1e5256fd01ed5b4cd60f2aac97dceb00bd (diff) | |
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
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/regress/expected/with.out | 8 | ||||
| -rw-r--r-- | src/test/regress/sql/with.sql | 7 |
2 files changed, 15 insertions, 0 deletions
diff --git a/src/test/regress/expected/with.out b/src/test/regress/expected/with.out index 460d0ea2b1c..0a8b48d5e93 100644 --- a/src/test/regress/expected/with.out +++ b/src/test/regress/expected/with.out @@ -2084,6 +2084,14 @@ WITH RECURSIVE x(n) AS ( ERROR: ORDER BY in a recursive query is not implemented LINE 3: ORDER BY (SELECT n FROM x)) ^ +-- and this +WITH RECURSIVE x(n) AS ( + WITH sub_cte AS (SELECT * FROM x) + DELETE FROM graph RETURNING f) + SELECT * FROM x; +ERROR: recursive query "x" must not contain data-modifying statements +LINE 1: WITH RECURSIVE x(n) AS ( + ^ CREATE TEMPORARY TABLE y (a INTEGER); INSERT INTO y SELECT generate_series(1, 10); -- LEFT JOIN diff --git a/src/test/regress/sql/with.sql b/src/test/regress/sql/with.sql index bcf0242fa40..6d55c7731a9 100644 --- a/src/test/regress/sql/with.sql +++ b/src/test/regress/sql/with.sql @@ -949,6 +949,13 @@ WITH RECURSIVE x(n) AS ( ORDER BY (SELECT n FROM x)) SELECT * FROM x; +-- and this +WITH RECURSIVE x(n) AS ( + WITH sub_cte AS (SELECT * FROM x) + DELETE FROM graph RETURNING f) + SELECT * FROM x; + + CREATE TEMPORARY TABLE y (a INTEGER); INSERT INTO y SELECT generate_series(1, 10); |
