Thread: pgsql: Fix PG 17 [NOT] NULL optimization bug for domains

pgsql: Fix PG 17 [NOT] NULL optimization bug for domains

From
Bruce Momjian
Date:
Fix PG 17 [NOT] NULL optimization bug for domains

A PG 17 optimization allowed columns with NOT NULL constraints to skip
table scans for IS NULL queries, and to skip IS NOT NULL checks for IS
NOT NULL queries.  This didn't work for domain types, since domain types
don't follow the IS NULL/IS NOT NULL constraint logic.  To fix, disable
this optimization for domains for PG 17+.

Reported-by: Jan Behrens

Diagnosed-by: Tom Lane

Discussion: https://postgr.es/m/Z37p0paENWWUarj-@momjian.us

Backpatch-through: 17

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/b8b1e87b70aab380a4270926f49c06a07f00e14a

Modified Files
--------------
doc/src/sgml/ref/create_domain.sgml    |  3 ++-
src/backend/optimizer/plan/initsplan.c | 14 ++++++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)


Re: pgsql: Fix PG 17 [NOT] NULL optimization bug for domains

From
Bruce Momjian
Date:
On Tue, Apr  8, 2025 at 01:34:07AM +0000, Bruce Momjian wrote:
> Fix PG 17 [NOT] NULL optimization bug for domains
> 
> A PG 17 optimization allowed columns with NOT NULL constraints to skip
> table scans for IS NULL queries, and to skip IS NOT NULL checks for IS
> NOT NULL queries.  This didn't work for domain types, since domain types
> don't follow the IS NULL/IS NOT NULL constraint logic.  To fix, disable
> this optimization for domains for PG 17+.
>
> Discussion:  https://postgr.es/m/Z37p0paENWWUarj-@momjian.us

Ugh, I knew there was something odd about the commit message I wrote
above, but I couldn't put my figure on it until this morning.  The
problem above is not "domains" but row types;  the bug report showed it
as an issue with row types used for domains, but it can happen with
columns which are row types too.  The doc adjustment was for domains,
and that confused me.

Tom, can you remember this in the 17.5 release notes?  Thanks.  The
commit should have been:

    Fix PG 17 [NOT] NULL optimization bug for row types

    A PG 17 optimization allowed columns with NOT NULL constraints
    to skip table scans for IS NULL queries, and to skip IS NOT NULL
    checks for IS NOT NULL queries.  This didn't work for row types,
    since row types don't follow the IS NULL/IS NOT NULL constraint
    logic.    To fix, disable this optimization for row types for PG 17+.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  Do not let urgent matters crowd out time for investment in the future.