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 account for varreturningtype
Date: Fri, 10 Apr 2026 06:51:43 +0000
Message-ID: <E1wB5iR-000GBL-16@gemulon.postgresql.org> (raw)

Fix var_is_nonnullable() to account for varreturningtype

var_is_nonnullable() failed to consider varreturningtype, which meant
it could incorrectly claim a Var is non-nullable based on a column's
NOT NULL constraint even when the Var refers to a non-existent row.
Specifically, OLD.col is NULL for INSERT (no old row exists) and
NEW.col is NULL for DELETE (no new row exists), regardless of any NOT
NULL constraint on the column.

This caused the planner's constant folding in eval_const_expressions
to incorrectly simplify IS NULL / IS NOT NULL tests on such Vars.  For
example, "old.a IS NULL" in an INSERT's RETURNING clause would be
folded to false when column "a" has a NOT NULL constraint, even though
the correct result is true.

Fix by returning false from var_is_nonnullable() when varreturningtype
is not VAR_RETURNING_DEFAULT, since such Vars can be NULL regardless
of table constraints.

Author: SATYANARAYANA NARLAPURAM <satyanarlapuram@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/CAHg+QDfaAipL6YzOq2H=gAhKBbcUTYmfbAv+W1zueOfRKH43FQ@mail.gmail.com

Branch
------
master

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

Modified Files
--------------
src/backend/optimizer/util/clauses.c    |  8 ++++++++
src/test/regress/expected/returning.out | 31 +++++++++++++++++++++++++++++++
src/test/regress/sql/returning.sql      | 20 ++++++++++++++++++++
3 files changed, 59 insertions(+)



Message-ID: <E1wB5iR-000GBL-16@gemulon.postgresql.org>
Permalink:  ../E1wB5iR-000GBL-16@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1wB5iR-000GBL-16@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 account for varreturningtype
  In-Reply-To: <E1wB5iR-000GBL-16@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