Re: Bug: var_is_nonnullable() gives wrong results for old/new in RETURNING

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Tender Wang <tndrwang(at)gmail(dot)com>
Cc: SATYANARAYANA NARLAPURAM <satyanarlapuram(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Bug: var_is_nonnullable() gives wrong results for old/new in RETURNING
Date: 2026-04-10 05:48:34
Message-ID: CAMbWs48swa+F+HmsxnVqYrSvDFgp9v7ShSjac5FsG4oFFKvs2w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Apr 10, 2026 at 10:30 AM Tender Wang <tndrwang(at)gmail(dot)com> wrote:
> SATYANARAYANA NARLAPURAM <satyanarlapuram(at)gmail(dot)com> 于2026年4月10日周五 02:43写道:
> > It appears the optimizer incorrectly simplifies old.<col> IS NULL to FALSE in RETURNING clauses when the underlying column has a NOT NULL constraint.
> >
> > The issue is that var_is_nonnullable() in clauses.c doesn't check Var.varreturningtype. It sees a NOT NULL column and concludes the Var can never be NULL.
> > But this assumption is wrong for old.* and new.* references. Because the old tuple doesn't exist on INSERT, and the new tuple doesn't exist on DELETE

Nice catch.

> Yes, the current var_is_nonnullable() ignores this case. The
> attached patch seems ok to me.

The patch also LGTM. I also checked if has_notnull_forced_var() has
the same issue, but it doesn't: Vars with non-default returning type
only appear in the RETURNING clause, so they never show up in WHERE/ON
clauses.

- Richard

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-04-10 05:53:15 Fix pgstat_database.c to honor passed database OIDs
Previous Message Michael Paquier 2026-04-10 04:37:30 Re: Fix typo in import_pg_statistic() comment