postgres.git / summary / log / commit / refs

commit    ecebc02f69a20b539513f4c253eac24221a901e5
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 a999918b45e..b2eec947e6e 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 4f440f8d0f6..7b369a767ae 100644 --- a/src/test/regress/expected/with.out +++ b/src/test/regress/expected/with.out @@ -1541,6 +1541,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 3e78823e528..00a04448770 100644 --- a/src/test/regress/sql/with.sql +++ b/src/test/regress/sql/with.sql @@ -738,6 +738,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: e22173e29b71]