pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: tharakan@gmail.com
Cc: pgsql-bugs@lists.postgresql.org, Peter Eisentraut <peter@eisentraut.org>
Subject: Re: BUG #19922: CYCLE/SEARCH with a window in the recursive term: could not find replacement targetlist entry
Date: Thu, 19 Nov 2026 16:47:12 -0500
Message-ID: <1250317.1795124832@sss.pgh.pa.us> (raw)
In-Reply-To: <19922-a0d338245d361a4c@postgresql.org>
References: <19922-a0d338245d361a4c@postgresql.org>

PG Bug reporting form <noreply@postgresql.org> writes:
> A recursive query with a CYCLE or SEARCH clause fails when its
> recursive term has a resjunk column, such as a window function's
> ORDER BY column.

Yeah; you can make it fail with a resjunk GROUP BY term too, such as

WITH RECURSIVE t(x) AS (
    SELECT 1
  UNION ALL
    SELECT x+1 FROM t WHERE x < 3 GROUP BY x
) SEARCH DEPTH FIRST BY x SET o
SELECT * FROM t;
ERROR:  attribute 2 of type record has wrong type
DETAIL:  Table has type integer, but query expects record[].

I think the problem is that rewriteSearchAndCycle() is appending extra
items to the right subquery's tlist without regard for the possibility
that there are resjunk items in it.  This goes against our general
expectation that resjunk entries come last, and in particular it
breaks the correspondence between the subquery's column name lists and
their corresponding tlist entries.  The names that are meant to refer
to the added extra items instead refer to resjunk entries, causing
all sorts of jolliness later.

There may be a similar problem with the left subquery too, not sure.

I think the way to fix this is to inject the added columns before any
resjunk columns (renumbering the latter).  I've got a hard stop coming
up shortly, but can look at this tomorrow if no one beats me to it.

			regards, tom lane






view thread (4+ messages)  latest in thread

Message-ID: <1250317.1795124832@sss.pgh.pa.us>
Permalink:  ../1250317.1795124832@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1250317.1795124832@sss.pgh.pa.us

 · 

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-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, tharakan@gmail.com, peter@eisentraut.org
  Subject: Re: BUG #19922: CYCLE/SEARCH with a window in the recursive term: could not find replacement targetlist entry
  In-Reply-To: <1250317.1795124832@sss.pgh.pa.us>
  References: <19922-a0d338245d361a4c@postgresql.org> <1250317.1795124832@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