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 1xUSdB-00000000z0u-0qhr for pgsql-bugs@arkaria.postgresql.org; Fri, 20 Nov 2026 17:42:37 +0000 Received: from localhost ([127.0.0.1]:34756 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xUSd8-0000000Adaz-1kr8 for pgsql-bugs@arkaria.postgresql.org; Fri, 20 Nov 2026 17:42:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:36492) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xUSd8-0000000Adap-0dj4 for pgsql-bugs@lists.postgresql.org; Fri, 20 Nov 2026 17:42:34 +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 1xUSd5-00000000hkp-3ogq for pgsql-bugs@lists.postgresql.org; Fri, 20 Nov 2026 17:42:33 +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 6AKHgQ6f1535857; Fri, 20 Nov 2026 12:42:26 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 sss.pgh.pa.us 6AKHgQ6f1535857 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sss.pgh.pa.us; s=default; t=1795196547; bh=MWwlFzos34CuK7pYXGITfqpELrYzkKWeSQXj1/O2YmI=; h=From:To:cc:Subject:In-reply-to:References:Date:From; b=CS0Ub8GL/ES7mYdClO35N1v82SWTT+ylFGJBMyfHmLeV1VQXLnhd4YbJixlYtWnwd NAoTjY6zgw5btZnkh9S/mv161rtLu0792+0n41nHkEieVTBNXoARZucNL07/ExtQS/ KhtI86A/Uw5EuWL8V/7c6JmB+cZUi6uACYvwpA5M= From: Tom Lane To: tharakan@gmail.com cc: pgsql-bugs@lists.postgresql.org, Peter Eisentraut Subject: Re: BUG #19923: SEARCH/CYCLE with DISTINCT in the recursive term: rows depend on input order In-reply-to: <19923-24cef1e958fa2370@postgresql.org> References: <19923-24cef1e958fa2370@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Thu, 19 Nov 2026 06:44:17 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1535855.1795196546.1@sss.pgh.pa.us> Date: Fri, 20 Nov 2026 12:42:26 -0500 Message-ID: <1535856.1795196546@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 SEARCH or CYCLE clause returns rows that > depend on the order of its input when its recursive term uses DISTINCT > or GROUP BY. Hm, I see no GROUP BY in your examples? The proximate cause of the problem seems to be that we add extra columns to the righthand subquery without any consideration for the fact that if it has a DISTINCT clause then that clause suggests that we need to include the new columns in the DISTINCT-ing. But we don't actually do so. Pre-v18 planner versions seem to tolerate the inconsistency, at least to the point of not crashing, while currently we don't. But I think this is definitely the rewriter's error not the planner's. Before we invest a lot of effort in fixing that, I think we ought to push back a little on whether this is actually a sensible thing to do. I see nothing in the SQL spec forbidding it, but including the extra columns in the DISTINCT list seems to carry a risk of splitting what the user expected to be a single output row into multiple rows. OTOH, if we avoided that by converting DISTINCT to DISTINCT ON, we'd have uncertainty about which extra-column values get emitted. The only way that this is stable is if the extra columns are absolutely, positively guaranteed to be immutable functions of the regular columns. Do we want to assume that? The main hazard I see is that we allow a much wider range of and than the spec contemplates, eg we will happily take "timestamptz 'now'" as a mark value. I'm not entirely convinced that mark values of non-built-in datatypes couldn't cause a problem. The SQL spec's definition of SEARCH/CYCLE via rewrite rule definitely results in including the added columns in any DISTINCT behavior. If the committee thought about this issue, they must have decided that they believe in the immutability of the added columns --- which may well be the reason that they restrict the mark values to be literals. Thoughts? regards, tom lane