agora inbox for pgsql-committers@postgresql.org  
help / color / mirror / Atom feed
From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix var_is_nonnullable() to handle invalid NOT NULL constraints
Date: Wed, 15 Apr 2026 00:39:30 +0000
Message-ID: <E1wCoHy-0010pl-0m@gemulon.postgresql.org> (raw)

Fix var_is_nonnullable() to handle invalid NOT NULL constraints

The NOTNULL_SOURCE_SYSCACHE code path in var_is_nonnullable() used
get_attnotnull() to check pg_attribute.attnotnull, which is true for
both valid and invalid (NOT VALID) NOT NULL constraints.  An invalid
constraint does not guarantee the absence of NULLs, so this could lead
to incorrect results.  For example, query_outputs_are_not_nullable()
could wrongly conclude that a subquery's output is non-nullable,
causing NOT IN to be incorrectly converted to an anti-join.

Fix by checking the attnullability field in the relation's tuple
descriptor instead, which correctly distinguishes valid from invalid
constraints, consistent with what the NOTNULL_SOURCE_HASHTABLE code
path already does.

While at it, rename NOTNULL_SOURCE_SYSCACHE to NOTNULL_SOURCE_CATALOG
to reflect that this code path no longer uses a syscache lookup, and
remove the now-unused get_attnotnull() function.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: SATYANARAYANA NARLAPURAM <satyanarlapuram@gmail.com>
Discussion: https://postgr.es/m/CAMbWs48ALW=mR0ydQ62dGS-Q+3D7WdDSh=EWDezcKp19xi=TUA@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/363af93bdd24c37064d94bd4e637827442594d53

Modified Files
--------------
src/backend/optimizer/util/clauses.c    | 31 +++++++++++++++++++++++--------
src/backend/utils/cache/lsyscache.c     | 27 ---------------------------
src/include/optimizer/optimizer.h       |  2 +-
src/include/utils/lsyscache.h           |  1 -
src/test/regress/expected/subselect.out | 23 +++++++++++++++++++++++
src/test/regress/sql/subselect.sql      | 14 ++++++++++++++
6 files changed, 61 insertions(+), 37 deletions(-)



Message-ID: <E1wCoHy-0010pl-0m@gemulon.postgresql.org>
Permalink:  ../E1wCoHy-0010pl-0m@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1wCoHy-0010pl-0m@gemulon.postgresql.org

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: rguo@postgresql.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Fix var_is_nonnullable() to handle invalid NOT NULL constraints
  In-Reply-To: <E1wCoHy-0010pl-0m@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox