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.94.2) (envelope-from ) id 1tVZR9-009ePt-IX for pgsql-general@arkaria.postgresql.org; Wed, 08 Jan 2025 17:01:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tVZR8-003d26-7b for pgsql-general@arkaria.postgresql.org; Wed, 08 Jan 2025 17:01:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tVZR7-003d1x-Sz for pgsql-general@lists.postgresql.org; Wed, 08 Jan 2025 17:01:41 +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.96) (envelope-from ) id 1tVZR5-000YHu-1C for pgsql-general@postgresql.org; Wed, 08 Jan 2025 17:01:40 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 508H1aqB1665867; Wed, 8 Jan 2025 12:01:36 -0500 From: Tom Lane To: Andrey cc: pgsql-general@postgresql.org Subject: Re: Unexpected results from a query with UNION ALL In-reply-to: <638683.1736015099@sss.pgh.pa.us> References: <638683.1736015099@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sat, 04 Jan 2025 13:24:59 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1665865.1736355696.1@sss.pgh.pa.us> Date: Wed, 08 Jan 2025 12:01:36 -0500 Message-ID: <1665866.1736355696@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I wrote: > Andrey writes: >> ... but I would expect to get the same result as previously. Is it a bug >> or am I doing something wrong here? > It's a surprising result for sure, but I believe it's explained by > the algorithm for READ COMMITTED [1], specifically the bit about Actually, on further thought I believe this really is a bug, because if you change the UNION ALL to UNION it works fine. It probably used to work with UNION ALL too, but that was a few decades ago before we started adding optimizations of UNION ALL :-( I've been poking at this off and on for the last few days, and I've found three different things that will need to be changed to make it work again. At least one of them looks too invasive to consider for back-patch. So don't hold your breath for a proper fix, but perhaps you could use UNION as a workaround? regards, tom lane