Re: Optimize IS DISTINCT FROM with non-nullable inputs

Tender Wang <tndrwang@gmail.com>

From: Tender Wang <tndrwang@gmail.com>
To: Richard Guo <guofenglinux@gmail.com>
Cc: Pg Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2026-01-28T01:59:00Z
Lists: pgsql-hackers
Richard Guo <guofenglinux@gmail.com> 于2026年1月27日周二 15:10写道:
>
> On Tue, Jan 27, 2026 at 11:32 AM Tender Wang <tndrwang@gmail.com> wrote:
> > Interesting optimization. I look through the v2-0001 patch. In the
> > commit message, it says:
> > ...
> >  This patch extends the optimization to cases where inputs are non-constant but
> >  proven to be non-nullable.  Specifically, "x IS DISTINCT FROM NULL"
> >  folds to constant TRUE if "x" is known to be non-nullable.
> > ...
> >
> > But I found that the case "x IS DISTINCT FROM NULL"  is converted to
> > NullTest in transformAExprDistinct().
> > It will be optimized in the "case T_NullTest:" not by this patch.
>
> Well, while it's true that the parser would do this transformation for
> "literal" NULLs, here we are talking more about "calculated" NULLs.
> Consider "not_null_col IS DISTINCT FROM (1 + NULL)".

Got it.

I looked through the v2-0002 patch. LGTM.


-- 
Thanks,
Tender Wang



Commits

  1. Teach planner to transform "x IS [NOT] DISTINCT FROM NULL" to a NullTest

  2. Optimize BooleanTest with non-nullable input

  3. Optimize IS DISTINCT FROM with non-nullable inputs