postgres.git / summary / log / commit / refs

commit    e9d3d12c79c968b3357bfbf88d2f95212bb2cf20
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Date:     Fri Nov 20 19:52:54 2026 +0000

    Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.
    
    rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
    the end of the recursive term's targetlist, but did not consider
    the interaction of that with SELECT DISTINCT.  Since we don't (and
    shouldn't) mark them resjunk, they should logically be included in
    the unique-ification action of SELECT DISTINCT, ie get added to the
    distinctClause.  We didn't do that, which confuses recent planner
    versions into assert'ing or building an incorrect plan.
    
    The simplest fix is to mark the query as hasDistinctOn, so that
    it looks like it was originally written with a DISTINCT ON list
    containing just the original columns.  Aside from being a trivial
    change, this restores the behavior as it was pre-v18.
    
    In principle, it should not matter whether we add the extra columns
    to the distinctClause or not, as they should be immutable functions
    of the original columns and thus couldn't add extra rows to the
    de-duplicated output.  Per the discussion thread, it's possible to
    construct artificial cases where the <cycle mark column> is only
    a stable function, and maybe even a volatile function if weirdly-
    behaving casts are involved.  But even then it seems better if the
    implementation arbitrarily selects one mark value rather than
    duplicating original rows.
    
    Although no bad behavior currently manifests pre-v18, that seems
    like luck rather than something to count on: we might back-patch some
    fix that requires consistency of distinctClause and hasDistinctOn.
    So back-patch to all supported branches.
    
    Bug: #19923
    Reported-by: Robins Tharakan <tharakan@gmail.com>
    Author: Tom Lane <tgl@sss.pgh.pa.us>
    Discussion: https://postgr.es/m/19923-24cef1e958fa2370@postgresql.org
    Backpatch-through: 15


src/backend/rewrite/rewriteSearchCycle.c | 9 +++++++++ src/test/regress/expected/with.out | 13 +++++++++++++ src/test/regress/sql/with.sql | 8 ++++++++ 3 files changed, 30 insertions(+) diff --git a/src/backend/rewrite/rewriteSearchCycle.c b/src/backend/rewrite/rewriteSearchCycle.c index 5b2d8cca3ce..7d7c8c9401b 100644 --- a/src/backend/rewrite/rewriteSearchCycle.c +++ b/src/backend/rewrite/rewriteSearchCycle.c @@ -492,6 +492,15 @@ rewriteSearchAndCycle(CommonTableExpr *cte) newsubquery->targetList = lappend(newsubquery->targetList, tle); } + /* + * If the subquery had SELECT DISTINCT, rather than adding the new columns + * to the DISTINCT semantics, let's just mark it DISTINCT ON. (The new + * columns should be immutable functions of the regular columns, or at + * worst stable functions, so this doesn't break any semantics.) + */ + if (newsubquery->distinctClause) + newsubquery->hasDistinctOn = true; + 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 eb9c1b33a7c..6d44ddec2bc 100644 --- a/src/test/regress/expected/with.out +++ b/src/test/regress/expected/with.out @@ -1467,6 +1467,19 @@ select * from t; 3 | (2,3) | f | {(1),(2),(3)} (3 rows) +-- DISTINCT in the recursive term +with recursive t(x) as ( + select 1 + union all + select distinct x from t +) cycle x set m using p +select * from t; + x | m | p +---+---+----------- + 1 | f | {(1)} + 1 | t | {(1),(1)} +(2 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 1bf70517a93..f3df495e2ad 100644 --- a/src/test/regress/sql/with.sql +++ b/src/test/regress/sql/with.sql @@ -705,6 +705,14 @@ with recursive t(x) as ( cycle x set m using p select * from t; +-- DISTINCT in the recursive term +with recursive t(x) as ( + select 1 + union all + select distinct x from t +) 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: 0f8e93fab91e]