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 1xUU6V-00000000zrP-0G6R for pgsql-bugs@arkaria.postgresql.org; Fri, 20 Nov 2026 19:17:00 +0000 Received: from localhost ([127.0.0.1]:38604 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xUU6U-0000000B27M-1hIP for pgsql-bugs@arkaria.postgresql.org; Fri, 20 Nov 2026 19:16:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:44400) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xUU6U-0000000B277-0Zjc for pgsql-bugs@lists.postgresql.org; Fri, 20 Nov 2026 19:16:58 +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 1xUU6R-00000000iQB-3gGa for pgsql-bugs@lists.postgresql.org; Fri, 20 Nov 2026 19:16:57 +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 6AKJGofc1597505; Fri, 20 Nov 2026 14:16:50 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 sss.pgh.pa.us 6AKJGofc1597505 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sss.pgh.pa.us; s=default; t=1795202210; bh=9Vcd7RMwfZIy2SbAoca8iWK14SnaOFMESR3sWOTT7hs=; h=From:To:cc:Subject:In-reply-to:References:Date:From; b=OofouZ1lD4/jYog91sZD0XIDfo2GdtTSpK4tF2yupD5z6wDSbv8+CQCURdBYBhBl8 Ccb22f8C0YDH2v/nA+7nyPl+tU2ZTwdFSlFpx5XcYWvCavBhlDc33XSuQ86XOnbR7+ HU/9zIRBLCzdG3766qYynu8BTnV6M4NAv6RfD3+g= From: Tom Lane To: Peter Eisentraut cc: tharakan@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19923: SEARCH/CYCLE with DISTINCT in the recursive term: rows depend on input order In-reply-to: <1537677.1795198637@sss.pgh.pa.us> References: <19923-24cef1e958fa2370@postgresql.org> <1535856.1795196546@sss.pgh.pa.us> <1536293.1795197174@sss.pgh.pa.us> <1537677.1795198637@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Fri, 20 Nov 2026 13:17:17 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <1597443.1795202143.0@sss.pgh.pa.us> Date: Fri, 20 Nov 2026 14:16:50 -0500 Message-ID: <1597504.1795202210@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1597443.1795202143.1@sss.pgh.pa.us> I wrote: > Oh, I have it: the right fix here is to leave the right subquery's > tlist entirely alone, and instead compute the extra columns in its > parent query. Ah, that doesn't work: there's no way to get the sequence column out of the subquery. What does work is to make the DISTINCT structure valid again by converting it to DISTINCT ON, as in the trivial patch attached. Despite my worries an hour ago, it doesn't seem like this will cause a problem for any remotely-sane behavior of the mark column. Also, if I'm not mistaken, it reverts the behavior to what we did pre-v18. I've not dug to see exactly where the planner goes wrong now with the inconsistent hasDistinctOn setting, but it doesn't matter: rewriteSearchAndCycle is clearly at fault for generating an inconsistent query. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="bug-19923-draft-fix.patch"; charset="us-ascii" Content-ID: <1597443.1795202143.2@sss.pgh.pa.us> Content-Description: bug-19923-draft-fix.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/rewrite/rewriteSearchCycle.c b/src/backend/rewrit= e/rewriteSearchCycle.c index a999918b45e..b2eec947e6e 100644 --- a/src/backend/rewrite/rewriteSearchCycle.c +++ b/src/backend/rewrite/rewriteSearchCycle.c @@ -492,6 +492,15 @@ rewriteSearchAndCycle(CommonTableExpr *cte) newsubquery->targetList =3D lappend(newsubquery->targetList, tle); } = + /* + * If the subquery had SELECT DISTINCT, rather than adding the new colum= ns + * to the DISTINCT semantics, let's just mark it DISTINCT ON. (The new + * columns should be immutable functions of the regular columns, or at + * worst stable functions, so this doesn't break any semantics.) + */ + if (newsubquery->distinctClause) + newsubquery->hasDistinctOn =3D true; + newrte->subquery =3D newsubquery; newrte->inFromCl =3D true; newq2->rtable =3D list_make1(newrte); ------- =_aaaaaaaaaa0--