Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1phF2p-0002sS-Ne for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Mar 2023 19:31:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1phF2o-0003Fv-BP for pgsql-bugs@arkaria.postgresql.org; Tue, 28 Mar 2023 19:31:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1phF2o-0003Ba-2H for pgsql-bugs@lists.postgresql.org; Tue, 28 Mar 2023 19:31:46 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1phF2l-00060o-Nf for pgsql-bugs@lists.postgresql.org; Tue, 28 Mar 2023 19:31:45 +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 32SJVgQN3655677; Tue, 28 Mar 2023 15:31:42 -0400 From: Tom Lane To: Dave Bothwell cc: pgsql-bugs@lists.postgresql.org Subject: Re: CTE subquery referencing phantom records In-reply-to: References: Comments: In-reply-to Dave Bothwell message dated "Tue, 28 Mar 2023 13:14:10 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3655675.1680031902.1@sss.pgh.pa.us> Date: Tue, 28 Mar 2023 15:31:42 -0400 Message-ID: <3655676.1680031902@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Dave Bothwell writes: > Common Table Expression issue in PostgreSQL 15 > The final WHERE statement in our CTE appears to be referencing more > records, then the first subquery of our CTE is returning. It works when the > first subquery is filtered by the primary key (We left a commented out > example in the WHERE statement). It fails when the first subquery is > filtered by a string. We have provided a complete example of the problem > below (The following example works in PostgreSQL 11): I don't think this is a Postgres bug: your query is making unwarranted assumptions about the order in which different WHERE clauses will be evaluated. You could return to the PG 11 behavior by marking the CTE as "materialized": with phase_in_date as materialized ( ... but it'd be better to make the data structure more normalized so that you don't have hazards like applying date() to fields for which it would fail on some rows. regards, tom lane