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 1tdUaX-007bSz-Ll for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Jan 2025 13:28:10 +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 1tdUaW-00Bino-Ru for pgsql-hackers@arkaria.postgresql.org; Thu, 30 Jan 2025 13:28:08 +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 1tdUZR-00Bfy2-5C for pgsql-hackers@lists.postgresql.org; Thu, 30 Jan 2025 13:27:01 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tdUZO-002Ln2-01 for pgsql-hackers@lists.postgresql.org; Thu, 30 Jan 2025 13:27:00 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=momjian.us; s=2025010100; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description; bh=dRlcTKSs1gmBwVGZGRpF+NioKXRYq7pwACbP/aX+oZA=; b=n/BSF EP0a7N+9PjpCAZkUPLoOTE6TJ9cscYXfTYOoxcjai2s2SVlo7jhrpmUs3bCQPRO8zWrFgtdSNZaZn YMPqrQr3evdgzk4VvSjOpDw8zSfG6cwDGRu9JSCSHZnKpIX1O12WuLpwvma+5UlbCfgBExzeZRoui +HeoQW5/zHl5FUV9Ay/YtxW1c4Zm0ThDtmU77noU2D1toJEHl8dB0g0uEnhsa5YZDEMzVMOUB6913 3vzzuh6lQ8RTLjXDt16FWN9RCJkCyWEvZif0rWgjrR6tonZhCRANh6PCGSaMZ8adVzlBOqWDNEO8t ZPoFv03sU+p2qUsNG3qY808r5MHVA==; Received: from bruce by momjian.us with local (Exim 4.96) (envelope-from ) id 1tdUZK-00AUc5-1R; Thu, 30 Jan 2025 08:26:54 -0500 Date: Thu, 30 Jan 2025 08:26:54 -0500 From: Bruce Momjian To: Tom Lane Cc: jbe-mlist@magnetkern.de, PostgreSQL-development Subject: Re: Parameter NOT NULL to CREATE DOMAIN not the same as CHECK (VALUE IS NOT NULL) Message-ID: References: <173591158454.714.7664064332419606037@wrigleys.postgresql.org> <1694911.1736371474@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="kWMRNxtW5m9DMfJ2" Content-Disposition: inline In-Reply-To: <1694911.1736371474@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --kWMRNxtW5m9DMfJ2 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Jan 8, 2025 at 04:24:34PM -0500, Tom Lane wrote: > Bruce Momjian writes: > > I think this needs some serious research. > > We've discussed this topic before. The spec's definition of IS [NOT] > NULL for composite values is bizarre to say the least. I think > there's been an intentional choice to keep most NOT NULL checks > "simple", that is we look at the overall value's isnull bit and > don't probe any deeper than that. > > If the optimizations added in v17 changed existing behavior, > I agree that's bad. We should probably fix it so that those > are only applied when argisrow is false. I have developed the attached patch using your argisrow suggestion which fixes the test I posted. Is this something we should backpatch? -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Do not let urgent matters crowd out time for investment in the future. --kWMRNxtW5m9DMfJ2 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="domain.diff" diff --git a/doc/src/sgml/ref/create_domain.sgml b/doc/src/sgml/ref/create_domain.sgml index ce555203486..c111285a69c 100644 --- a/doc/src/sgml/ref/create_domain.sgml +++ b/doc/src/sgml/ref/create_domain.sgml @@ -283,7 +283,8 @@ CREATE TABLE us_snail_addy ( The syntax NOT NULL in this command is a PostgreSQL extension. (A standard-conforming - way to write the same would be CHECK (VALUE IS NOT + way to write the same for non-composite data types would be + CHECK (VALUE IS NOT NULL). However, per , such constraints are best avoided in practice anyway.) The NULL constraint is a diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c index 2cb0ae6d659..e291547112d 100644 --- a/src/backend/optimizer/plan/initsplan.c +++ b/src/backend/optimizer/plan/initsplan.c @@ -3100,6 +3100,13 @@ restriction_is_always_true(PlannerInfo *root, if (nulltest->nulltesttype != IS_NOT_NULL) return false; + /* + * Empty rows can appear NULL in some contexts and NOT NULL in others, + * so avoid this optimization for row expressions. + */ + if (nulltest->argisrow) + return false; + return expr_is_nonnullable(root, nulltest->arg); } @@ -3149,6 +3156,13 @@ restriction_is_always_false(PlannerInfo *root, if (nulltest->nulltesttype != IS_NULL) return false; + /* + * Empty rows can appear NULL in some contexts and NOT NULL in others, + * so avoid this optimization for row expressions. + */ + if (nulltest->argisrow) + return false; + return expr_is_nonnullable(root, nulltest->arg); } --kWMRNxtW5m9DMfJ2--