From: Tom Lane <tgl@sss.pgh.pa.us>
To: Peter Eisentraut <peter@eisentraut.org>
Cc: tharakan@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #19923: SEARCH/CYCLE with DISTINCT in the recursive term: rows depend on input order
Date: Fri, 20 Nov 2026 14:16:50 -0500
Message-ID: <1597504.1795202210@sss.pgh.pa.us> (raw)
In-Reply-To: <1537677.1795198637@sss.pgh.pa.us>
References: <19923-24cef1e958fa2370@postgresql.org>
<1535856.1795196546@sss.pgh.pa.us>
<1536293.1795197174@sss.pgh.pa.us>
<1537677.1795198637@sss.pgh.pa.us>
I wrote:
> Oh, I have it: the right fix here is to leave the right subquery's
> tlist entirely alone, and instead compute the extra columns in its
> parent query.
Ah, that doesn't work: there's no way to get the sequence column
out of the subquery.
What does work is to make the DISTINCT structure valid again by
converting it to DISTINCT ON, as in the trivial patch attached.
Despite my worries an hour ago, it doesn't seem like this will
cause a problem for any remotely-sane behavior of the mark column.
Also, if I'm not mistaken, it reverts the behavior to what we did
pre-v18. I've not dug to see exactly where the planner goes wrong
now with the inconsistent hasDistinctOn setting, but it doesn't
matter: rewriteSearchAndCycle is clearly at fault for generating
an inconsistent query.
regards, tom lane
Attachments:bug-19923-draft-fix.patch (text/x-diff, 843B) downloaddiff --git a/src/backend/rewrite/rewriteSearchCycle.c b/src/backend/rewrite/rewriteSearchCycle.cindex 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);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, peter@eisentraut.org, tharakan@gmail.com
Subject: Re: BUG #19923: SEARCH/CYCLE with DISTINCT in the recursive term: rows depend on input order
In-Reply-To: <1597504.1795202210@sss.pgh.pa.us>
References: <19923-24cef1e958fa2370@postgresql.org> <1535856.1795196546@sss.pgh.pa.us> <1536293.1795197174@sss.pgh.pa.us> <1537677.1795198637@sss.pgh.pa.us> <1597504.1795202210@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox