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 1xU04Q-00000000fEP-0fXz for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Nov 2026 11:12:51 +0000 Received: from localhost ([127.0.0.1]:53868 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU04P-00000000PsE-0aRu for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Nov 2026 11:12:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:58996) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xTsoh-0000000Fdjw-14Dp for pgsql-bugs@lists.postgresql.org; Thu, 19 Nov 2026 03:28:07 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xTsof-00000000QNE-0JeH for pgsql-bugs@lists.postgresql.org; Thu, 19 Nov 2026 03:28:06 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=0t6hJHEkFpTOlRrwwCNiFPsOw6/bLXxUiLy+c99YfWM=; b=ezsPigpBdpO//970wYqtsW2nSx lS++ORyLZf1GlDZvXMN3EUANlgmmjt3Uc2wbmGzT7IwHp681mb2oMqiGF1Vir1wOdc8VkttjgSH5Q wTupnjs82ZZZ4I0VhWyhH7Yb69na1TZn2Ub8nuRGjweBYLldZbGp2ld9kq1qSefnm3MAdDHg2zVHT ZHidFXfuOArH5xr9FvTDPatBA5UdgNOmJtKT6Q5dZA5SAxHSHN/KA4YNCgPp3yuO462iO5CpdSGCJ CsXsJT9lsAobvWNAXu/qeJE3xNHuWveDsmU9vdypmGoNUCwCb0opoDvmDsaBFOpiJA0HrnFWpynN+ N8ledEXg==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xTsoe-00000001CxV-2aen for pgsql-bugs@lists.postgresql.org; Thu, 19 Nov 2026 03:28:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xTsoc-0000000302n-3x56 for pgsql-bugs@lists.postgresql.org; Thu, 19 Nov 2026 03:28:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19922: CYCLE/SEARCH with a window in the recursive term: could not find replacement targetlist entry To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: tharakan@gmail.com Reply-To: tharakan@gmail.com, pgsql-bugs@lists.postgresql.org Date: Thu, 19 Nov 2026 03:27:49 +0000 Message-ID: <19922-a0d338245d361a4c@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19922 Logged by: Robins Tharakan Email address: tharakan@gmail.com PostgreSQL version: Unsupported/Unknown Operating system: Unsupported/Unknown Description: =20 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. With CYCLE, builds without assertions, and assert-enabled builds of 17 and older, raise ERROR: XX000: could not find replacement targetlist entry for attno 2 LOCATION: ReplaceVarFromTargetList, rewriteManip.c:1903 and assert-enabled builds of 18 and later fail an assertion before that: TRAP: failed Assert("OidIsValid(eqop)"), File: "allpaths.c", Line: 4602 With SEARCH in place of CYCLE, the same recursive term fails on every branch, with or without assertions: ERROR: 42804: attribute 2 of type record has wrong type DETAIL: Table has type integer, but query expects record[]. LOCATION: CheckVarSlotCompatibility, execExprInterp.c:2429 Commit =3D=3D=3D=3D=3D=3D Master: 8303f2e13c5 (2026-11-18) affected Also affects: REL_14_STABLE through REL_19_STABLE Built with --enable-cassert --enable-debug at -O0, and master also without --enable-cassert at -O2 (gcc 14.2.0, glibc 2.41, Debian 13, x86_64). Error Log =3D=3D=3D=3D=3D=3D=3D=3D master (8303f2e13c5), query 1: TRAP: failed Assert("OidIsValid(eqop)"), File: "allpaths.c", Line: 4602, PID: 1176274 LOG: client backend (PID 1176274) was terminated by signal 6: Aborted master without assertions, and REL_17_STABLE (6c8717359a4), query 1: ERROR: could not find replacement targetlist entry for attno 2 Backtrace =3D=3D=3D=3D=3D=3D=3D=3D The assertion on master, query 1: #0 ExceptionalCondition (conditionName=3D"OidIsValid(eqop)", ...) #1 pushdown_var_grouping_eqop (...) at allpaths.c:4602 #2 grouping_conflict_walker (...) at clauses.c:6576 #5 grouping_conflict_walker (...) at clauses.c:6710 #6 expression_has_grouping_conflict (...) at clauses.c:6533 #7 qual_is_pushdown_safe (...) at allpaths.c:4569 #8 set_subquery_pathlist (...) at allpaths.c:2774 #9 set_rel_size (...) at allpaths.c:472 #14 subquery_planner (...) at planner.c:1368 #15 recurse_set_operations (...) at prepunion.c:252 #16 generate_recursion_path (...) at prepunion.c:405 SQL =3D=3D=3D -- 1. CYCLE, with the window's ORDER BY column in the recursive term. -- Builds without assertions, and 17 and older: -- ERROR: could not find replacement targetlist entry for attno 2 -- Assert-enabled builds of 18 and later end the session at -- TRAP: failed Assert("OidIsValid(eqop)"), File: "allpaths.c" -- It should return (1, f, {(1)}) and (1, t, {(1),(1)}). WITH RECURSIVE t(x) AS ( SELECT 1 UNION ALL SELECT max(x) OVER (ORDER BY x) FROM t ) CYCLE x SET m USING p SELECT * FROM t; -- 2. The same recursive term with SEARCH, on every branch: -- ERROR: attribute 2 of type record has wrong type -- It should return what the term without the window does: -- 1 | {(1)} -- 2 | {(1),(2)} -- 3 | {(1),(2),(3)} WITH RECURSIVE t(x) AS ( SELECT 1 UNION ALL SELECT max(x) OVER (ORDER BY x) + 1 FROM t WHERE x < 3 ) SEARCH DEPTH FIRST BY x SET o SELECT * FROM t; -- 3. Both clauses, on every branch, with or without assertions: -- ERROR: could not find replacement targetlist entry for attno 4 -- It should return one empty row. WITH RECURSIVE a AS ( SELECT NULL b, NULL ~ NULL c UNION SELECT NULL, NULL ~ max(NULL) OVER (PARTITION BY 0 ORDER BY 1) FROM a ) SEARCH BREADTH FIRST BY b SET d CYCLE c SET e USING f, g AS (SELECT FROM a) SELECT; All three queries fail on master and every supported branch (and on 14.25). A release build of master (no --enable-cassert, -O2) raises the XX000 and 42804 errors. A recursive term with DISTINCT fails the same assertion on 18 and later, for a different reason: a regression of 44fb59fc605. I'm reporting that separately. Found using SQLsmith, SQLReduce and Claude. - Robins Tharakan Databricks