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 1xU06t-00000000fFI-03Jn for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Nov 2026 11:15:23 +0000 Received: from localhost ([127.0.0.1]:42582 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xU06s-00000000SKa-1wIf for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Nov 2026 11:15:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]:51260) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xTvtN-0000000GZ3q-4AdV for pgsql-bugs@lists.postgresql.org; Thu, 19 Nov 2026 06:45:10 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xTvtL-00000000Trz-0Ivg for pgsql-bugs@lists.postgresql.org; Thu, 19 Nov 2026 06:45:09 +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=tdI0B3K9417JWkKNd1/PuAvcJlE9rudVnGutScXFxwg=; b=kEtbyOYaMrzYTZzVhW/8T5pJ6X QXVuySYFyOFVqwDj+GhzGEfxEd2fIbmW0G2LpkCJnfVFkkzGAJ520X+TGDsdOvAhqKCS/SuARagaP XhFo2aXkRq/PCGwEQFS6mk/3amynRwqcHPXCQWNKt6p6h7s7lnbXWduU2Dm1KCW6ScLdme4G/WUVH 6KJAuhQadfjXhXbjuTuz208u2nzmcelgTC5KM/o3nAiNjvrqn+GAfWBoIh/YMFksnXUGiVo+SHgP/ hKb/TWCLBkFiFuBQULRX9yAGpWB3cRnHxKcbQwZrvWpGt4HfxGOcm47S6Mh0sNzfjyJmxJ1RZhIOK y4rDtXPA==; 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 1xTvtJ-00000001Gzm-0lEC for pgsql-bugs@lists.postgresql.org; Thu, 19 Nov 2026 06:45: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 1xTvtH-00000003CRe-3fiC for pgsql-bugs@lists.postgresql.org; Thu, 19 Nov 2026 06:45:04 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19923: SEARCH/CYCLE with DISTINCT in the recursive term: rows depend on input order 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 06:44:17 +0000 Message-ID: <19923-24cef1e958fa2370@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: 19923 Logged by: Robins Tharakan Email address: tharakan@gmail.com PostgreSQL version: Unsupported/Unknown Operating system: Debian 13 (x86_64) Description: =20 A recursive query with a SEARCH or CYCLE clause returns rows that depend on the order of its input when its recursive term uses DISTINCT or GROUP BY. On master, REL_19_STABLE and REL_18_STABLE, the CYCLE form also fails this assertion in assert-enabled builds: TRAP: failed Assert("OidIsValid(eqop)"), File: "allpaths.c", Line: 4602 This appears to be separate from BUG #19922, and thus filed separately. Commit =3D=3D=3D=3D=3D=3D Affects: REL_14_STABLE 853b35f35b1 (2026-11-10) REL_15_STABLE 6ee74fe7389 (2026-11-18) REL_16_STABLE f8bd8d39783 (2026-11-18) REL_17_STABLE 6c8717359a4 (2026-11-18) REL_18_STABLE 9107706245e (2026-11-18), with the assertion REL_19_STABLE f66932c8dd4 (2026-11-19), with the assertion Master: ba11c483900 (2026-11-19), the newest commit tested Built with --enable-cassert --enable-debug at -O0, and master also without --enable-cassert at -O2 (gcc 14.2.0, Debian 13, x86_64). Error Log =3D=3D=3D=3D=3D=3D=3D=3D=3D TRAP: failed Assert("OidIsValid(eqop)"), File: "allpaths.c", Line: 4602, PID: 1405985 LOG: client backend (PID 1405985) was terminated by signal 6: Aborted Backtrace (v18+) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #0 ExceptionalCondition (...) at assert.c:35 #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. On 18 and later, assert-enabled builds end the session with the -- TRAP above. 14 to 17 return (1, f, {(1)}) and (1, t, {(1),(1)}). WITH RECURSIVE t(x) AS ( SELECT 1 UNION ALL SELECT DISTINCT x FROM t ) CYCLE x SET m USING p SELECT * FROM t; -- 2. Edges 1->3, 2->3, 3->1, starting from 1 and 2. On 14 to 17, and -- in builds without assertions, 4 rows: -- (1,f,{(1)}) (3,f,{(1),(3)}) (1,t,{(1),(3),(1)}) (2,f,{(2)}) WITH RECURSIVE e(src, dst) AS (VALUES (1, 3), (2, 3), (3, 1)), t(x) AS ( SELECT * FROM (VALUES (1), (2)) v(x) UNION ALL SELECT DISTINCT e.dst FROM t JOIN e ON e.src =3D t.x ) CYCLE x SET m USING p SELECT * FROM t ORDER BY p::text; -- 3. The same, starting from 2 and 1: 5 rows. -- (1,f,{(1)}) (2,f,{(2)}) (3,f,{(2),(3)}) (1,f,{(2),(3),(1)}) -- (3,t,{(2),(3),(1),(3)}) WITH RECURSIVE e(src, dst) AS (VALUES (1, 3), (2, 3), (3, 1)), t(x) AS ( SELECT * FROM (VALUES (2), (1)) v(x) UNION ALL SELECT DISTINCT e.dst FROM t JOIN e ON e.src =3D t.x ) CYCLE x SET m USING p SELECT * FROM t ORDER BY p::text; -- 4. Query 2 with UNION in place of DISTINCT: 7 rows on every branch, -- those of queries 2 and 3 together. WITH RECURSIVE e(src, dst) AS (VALUES (1, 3), (2, 3), (3, 1)), t(x) AS ( SELECT * FROM (VALUES (1), (2)) v(x) UNION SELECT e.dst FROM t JOIN e ON e.src =3D t.x ) CYCLE x SET m USING p SELECT * FROM t ORDER BY p::text; -- 5. SEARCH, no cycles: edges 1->3, 2->3, 3->4, starting from 1 and 2. -- Every branch: (1,{(1)}) (3,{(1),(3)}) (4,{(1),(3),(4)}) (2,{(2)}) WITH RECURSIVE e(src, dst) AS (VALUES (1, 3), (2, 3), (3, 4)), t(x) AS ( SELECT * FROM (VALUES (1), (2)) v(x) UNION ALL SELECT DISTINCT e.dst FROM t JOIN e ON e.src =3D t.x ) SEARCH DEPTH FIRST BY x SET s SELECT * FROM t ORDER BY s; -- 6. The same, starting from 2 and 1: -- (1,{(1)}) (2,{(2)}) (3,{(2),(3)}) (4,{(2),(3),(4)}) WITH RECURSIVE e(src, dst) AS (VALUES (1, 3), (2, 3), (3, 4)), t(x) AS ( SELECT * FROM (VALUES (2), (1)) v(x) UNION ALL SELECT DISTINCT e.dst FROM t JOIN e ON e.src =3D t.x ) SEARCH DEPTH FIRST BY x SET s SELECT * FROM t ORDER BY s; Queries 2 and 3 differ only in the order of their two starting rows, but query 2 returns 4 rows, query 3 returns 5. SEARCH DEPTH FIRST (queries 5 and 6) returns the same nodes in either order, but nodes 3 and 4 carry the path from whichever start came first. - The assertion fails on master through REL_18_STABLE - REL_17_STABLE through REL_14_STABLE + release builds run every query and return the rows given in its comments Found using SQLsmith, SQLReduce and Claude. - Robins Tharakan Databricks