]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix var_is_nonnullable() to handle invalid NOT NULL constraints
authorRichard Guo <rguo@postgresql.org>
Wed, 15 Apr 2026 00:38:56 +0000 (09:38 +0900)
committerRichard Guo <rguo@postgresql.org>
Wed, 15 Apr 2026 00:38:56 +0000 (09:38 +0900)
commit363af93bdd24c37064d94bd4e637827442594d53
treef54a2b23e788239ec8e7bf94f8dcf35f39b06ae6
parent1f108fc02ece09da5773ece74e25812cb952ebfc
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
src/backend/optimizer/util/clauses.c
src/backend/utils/cache/lsyscache.c
src/include/optimizer/optimizer.h
src/include/utils/lsyscache.h
src/test/regress/expected/subselect.out
src/test/regress/sql/subselect.sql