Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xU9yT-00000000oKy-0o30 for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Nov 2026 21:47:21 +0000 Received: from localhost ([127.0.0.1]:46522 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU9yS-00000003pZh-2bc6 for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Nov 2026 21:47:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:43444) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xU9yS-00000003pZZ-1XYt for pgsql-bugs@lists.postgresql.org; Thu, 19 Nov 2026 21:47:20 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xU9yP-00000000Yod-2CEU for pgsql-bugs@lists.postgresql.org; Thu, 19 Nov 2026 21:47:19 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 6AJLlCst1250318; Thu, 19 Nov 2026 16:47:12 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 sss.pgh.pa.us 6AJLlCst1250318 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sss.pgh.pa.us; s=default; t=1795124832; bh=6em6ZZlbP7OYqnaOZsOthsJD0WPTYVLgFzMot2gpXyI=; h=From:To:cc:Subject:In-reply-to:References:Date:From; b=O2L3cp1cHdITXK5DP9Nmy6HUj9Bzv39kQ4m8++e216ub8PLMvWjBGTrUK3queje9M UqIl6zuhLonPb+h9IDv/h6hm4eO2EjvXNgVPxVwhQGcw75jGuR8Ksu3eLLBBj04ZWv Wt6ETQG52OxN0OBhAjClfghNUoIg/g386p6FUgbw= From: Tom Lane To: tharakan@gmail.com cc: pgsql-bugs@lists.postgresql.org, Peter Eisentraut Subject: Re: BUG #19922: CYCLE/SEARCH with a window in the recursive term: could not find replacement targetlist entry In-reply-to: <19922-a0d338245d361a4c@postgresql.org> References: <19922-a0d338245d361a4c@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Thu, 19 Nov 2026 03:27:49 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1250316.1795124832.1@sss.pgh.pa.us> Date: Thu, 19 Nov 2026 16:47:12 -0500 Message-ID: <1250317.1795124832@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form 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