pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.
Date: Fri, 20 Nov 2026 19:53:16 +0000
Message-ID: <E1xUUfb-00000000YNh-1sWt@gemulon.postgresql.org> (raw)

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

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/ecebc02f69a20b539513f4c253eac24221a901e5

Modified Files
--------------
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(+)



view thread (12+ messages)  latest in thread

Message-ID: <E1xUUfb-00000000YNh-1sWt@gemulon.postgresql.org>
Permalink:  ../E1xUUfb-00000000YNh-1sWt@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xUUfb-00000000YNh-1sWt@gemulon.postgresql.org

 · 

reply

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-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.
  In-Reply-To: <E1xUUfb-00000000YNh-1sWt@gemulon.postgresql.org>
  References: <E1xUUfb-00000000YNh-1sWt@gemulon.postgresql.org>

* 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