postgres.git / summary / log / commit / refs
commit 0f8e93fab91ea2efe1410b52e31edbefefb074c8
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri Nov 20 16:52:14 2026 +0000
Fix SEARCH/CYCLE when the recursive term has resjunk columns.
rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist. If that targetlist had
resjunk entries, for example a window function's ORDER BY column or a
GROUP BY column that is not selected, the new columns landed after
them. That breaks the expectation that resjunk entries come last, and
the column names added to the wrapping subquery then pointed at the
wrong entries. Depending on the query, this caused errors such as
"could not find replacement targetlist entry" or "attribute N of type
record has wrong type", an assertion failure in assert-enabled builds,
or silently wrong results.
Fix by detaching any resjunk entries before adding the new columns and
re-attaching them afterwards with renumbered resnos. Sort/group
clauses reference targetlist entries via ressortgroupref, not resno,
so renumbering them is safe.
The non-recursive term is not affected, since it is wrapped in a new
subquery without modifying its targetlist.
Bug: #19922
Reported-by: Robins Tharakan <tharakan@gmail.com>
Diagnosed-by: Tom Lane <tgl@sss.pgh.pa.us>
Author: Nitin Motiani <nitinmotiani@google.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19922-a0d338245d361a4c@postgresql.org
Backpatch-through: 15
src/backend/rewrite/rewriteSearchCycle.c | 42 ++++++++++++++++++++++++++++++--
src/test/regress/expected/with.out | 40 ++++++++++++++++++++++++++++++
src/test/regress/sql/with.sql | 23 +++++++++++++++++
3 files changed, 103 insertions(+), 2 deletions(-)
diff --git a/src/backend/rewrite/rewriteSearchCycle.c b/src/backend/rewrite/rewriteSearchCycle.c
index 58f684cd52e..5b2d8cca3ce 100644
--- a/src/backend/rewrite/rewriteSearchCycle.c
+++ b/src/backend/rewrite/rewriteSearchCycle.c
@@ -220,11 +220,17 @@ rewriteSearchAndCycle(CommonTableExpr *cte)
TargetEntry *tle;
RowExpr *cycle_col_rowexpr = NULL;
RowExpr *search_col_rowexpr = NULL;
- List *ewcl;
+ List *ewcl,
+ *junk_tles,
+ *nonjunk_tles;
+ ListCell *lc;
int cte_rtindex = -1;
Assert(cte->search_clause || cte->cycle_clause);
+ /*
+ * Copy the whole CTE structure, so we can scribble on it freely.
+ */
cte = copyObject(cte);
ctequery = castNode(Query, cte->ctequery);
@@ -410,12 +416,36 @@ rewriteSearchAndCycle(CommonTableExpr *cte)
errmsg("with a SEARCH or CYCLE clause, the recursive reference to WITH query \"%s\" must be at the top level of its right-hand SELECT",
cte->ctename)));
+ /*
+ * Make a copy of the original subquery, which will become a subquery of
+ * newq2, so it needs to be bumped down in level.
+ */
newsubquery = copyObject(rte2->subquery);
IncrementVarSublevelsUp((Node *) newsubquery, 1, 1);
/*
- * Add extra columns to target list of subquery of right subquery
+ * Add extra columns to target list of subquery of right subquery.
+ *
+ * The subquery's targetlist might contain resjunk entries, such as sort
+ * or grouping columns, which are not part of its output. Those must stay
+ * after all the non-resjunk entries, both by convention and because the
+ * column names we assigned above to the wrapping subquery assume that the
+ * added columns immediately follow the original output columns. So
+ * detach any resjunk entries before adding the new columns, and re-attach
+ * them afterwards with new resnos.
*/
+ nonjunk_tles = junk_tles = NIL;
+ foreach(lc, newsubquery->targetList)
+ {
+ tle = lfirst_node(TargetEntry, lc);
+ if (tle->resjunk)
+ junk_tles = lappend(junk_tles, tle);
+ else
+ nonjunk_tles = lappend(nonjunk_tles, tle);
+ }
+ Assert(list_length(nonjunk_tles) == list_length(cte->ctecolnames));
+ newsubquery->targetList = nonjunk_tles;
+
if (cte->search_clause)
{
Var *var;
@@ -454,6 +484,14 @@ rewriteSearchAndCycle(CommonTableExpr *cte)
newsubquery->targetList = lappend(newsubquery->targetList, tle);
}
+ /* Re-attach the resjunk entries, if any, after the new columns */
+ foreach(lc, junk_tles)
+ {
+ tle = lfirst_node(TargetEntry, lc);
+ tle->resno = list_length(newsubquery->targetList) + 1;
+ newsubquery->targetList = lappend(newsubquery->targetList, tle);
+ }
+
newrte->subquery = newsubquery;
newrte->inFromCl = true;
newq2->rtable = list_make1(newrte);
diff --git a/src/test/regress/expected/with.out b/src/test/regress/expected/with.out
index be23e932654..eb9c1b33a7c 100644
--- a/src/test/regress/expected/with.out
+++ b/src/test/regress/expected/with.out
@@ -1427,6 +1427,46 @@ select * from search_graph;
2 | 3 | arc 2 -> 3 | (4,2,3) | f | {"(1,4)","(4,5)","(5,1)","(1,2)","(2,3)"}
(25 rows)
+-- resjunk columns in the recursive term (from window functions or GROUP BY)
+with recursive t(x) as (
+ select 1
+ union all
+ select max(x) over (order by x) from t
+) cycle x set m using p
+select * from t;
+ x | m | p
+---+---+-----------
+ 1 | f | {(1)}
+ 1 | t | {(1),(1)}
+(2 rows)
+
+with recursive t(x) as (
+ select 1
+ union all
+ select max(x) over (order by x) + 1 from t where x < 3
+) search depth first by x set o
+select * from t;
+ x | o
+---+---------------
+ 1 | {(1)}
+ 2 | {(1),(2)}
+ 3 | {(1),(2),(3)}
+(3 rows)
+
+with recursive t(x) as (
+ select 1
+ union all
+ select x + 1 from t where x < 3 group by x
+) search breadth first by x set o
+ cycle x set m using p
+select * from t;
+ x | o | m | p
+---+-------+---+---------------
+ 1 | (0,1) | f | {(1)}
+ 2 | (1,2) | f | {(1),(2)}
+ 3 | (2,3) | f | {(1),(2),(3)}
+(3 rows)
+
-- various syntax errors
with recursive search_graph(f, t, label) as (
select * from graph g
diff --git a/src/test/regress/sql/with.sql b/src/test/regress/sql/with.sql
index c33dd695e38..1bf70517a93 100644
--- a/src/test/regress/sql/with.sql
+++ b/src/test/regress/sql/with.sql
@@ -682,6 +682,29 @@ with recursive search_graph(f, t, label) as (
cycle f, t set is_cycle using path
select * from search_graph;
+-- resjunk columns in the recursive term (from window functions or GROUP BY)
+with recursive t(x) as (
+ select 1
+ union all
+ select max(x) over (order by x) from t
+) cycle x set m using p
+select * from t;
+
+with recursive t(x) as (
+ select 1
+ union all
+ select max(x) over (order by x) + 1 from t where x < 3
+) search depth first by x set o
+select * from t;
+
+with recursive t(x) as (
+ select 1
+ union all
+ select x + 1 from t where x < 3 group by x
+) search breadth first by x set o
+ cycle x set m using p
+select * from t;
+
-- various syntax errors
with recursive search_graph(f, t, label) as (
select * from graph g
[parent: a51ba655d2f2]