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 1tlcnP-000f0p-Q1 for pgsql-bugs@arkaria.postgresql.org; Fri, 21 Feb 2025 23:51:04 +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 1tlcnP-001AJi-1O for pgsql-bugs@arkaria.postgresql.org; Fri, 21 Feb 2025 23:51:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tlcnO-001AJZ-PK for pgsql-bugs@lists.postgresql.org; Fri, 21 Feb 2025 23:51:02 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tlcnM-0003NA-0U for pgsql-bugs@lists.postgresql.org; Fri, 21 Feb 2025 23:51:02 +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 51LNowf93736133; Fri, 21 Feb 2025 18:50:58 -0500 From: Tom Lane To: Bruce Momjian cc: David Rowley , Ronald Cruz , pgsql-bugs@lists.postgresql.org, "'Peter Ford'" , "Aaron J. Garcia" Subject: Re: Query result differences between PostgreSQL 17 vs 16 In-reply-to: References: Comments: In-reply-to Bruce Momjian message dated "Fri, 21 Feb 2025 18:05:10 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3736131.1740181858.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 21 Feb 2025 18:50:58 -0500 Message-ID: <3736132.1740181858@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Bruce Momjian writes: > On Fri, Feb 21, 2025 at 11:13:17AM -0500, Ronald Cruz wrote: >> We have observed an issue after upgrading to PostgreSQL 17 that caused = us to >> roll back to 16. Some of our queries are returning what I believe to be >> erroneous results. > We have a known problem with composite types and NULL constraints in PG > 17 that I think we are fixing in PG 18. There's no composite type at hand here. I think the problem is an erroneous deduction from a column NOT NULL constraint. I can reproduce a faulty plan in the regression database with explain (costs off) select * from tenk1 left join int4_tbl i on (unique1 =3D f1) left join customer on (i.f1 =3D cid) left join int4_tbl j on cid is not null; v16 produces Hash Left Join Hash Cond: (tenk1.unique1 =3D i.f1) -> Seq Scan on tenk1 -> Hash -> Nested Loop Left Join Join Filter: (customer.cid IS NOT NULL) -> Hash Right Join Hash Cond: (customer.cid =3D i.f1) -> Seq Scan on customer -> Hash -> Seq Scan on int4_tbl i -> Materialize -> Seq Scan on int4_tbl j but HEAD produces Hash Left Join Hash Cond: (tenk1.unique1 =3D i.f1) -> Seq Scan on tenk1 -> Hash -> Nested Loop Left Join -> Hash Right Join Hash Cond: (customer.cid =3D i.f1) -> Seq Scan on customer -> Hash -> Seq Scan on int4_tbl i -> Materialize -> Seq Scan on int4_tbl j Note the lack of any IS NOT NULL test. I think the planner has convinced itself that the not null constraint on customer.cid makes that test redundant, despite the fact that what it is testing is a post-outer-join value that most certainly could be null. "git bisect" fingers this commit: b262ad440edecda0b1aba81d967ab560a83acb8a is the first bad commit commit b262ad440edecda0b1aba81d967ab560a83acb8a Author: David Rowley Date: Tue Jan 23 18:09:18 2024 +1300 Add better handling of redundant IS [NOT] NULL quals I've not looked at the code, but I suspect that it is failing to check varnullingrels before believing that it can trust the applicability of table constraints. regards, tom lane