Re: Optimize IS DISTINCT FROM with non-nullable inputs

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Tender Wang <tndrwang(at)gmail(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Optimize IS DISTINCT FROM with non-nullable inputs
Date: 2026-01-27 07:10:07
Message-ID: CAMbWs49iW_yCvYaDoU_RYJiZ89qtrhr62DFCECne6--+tWwC8w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Jan 27, 2026 at 11:32 AM Tender Wang <tndrwang(at)gmail(dot)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)".

Another case is custom plans for prepared statements (e.g., WHERE
not_null_col IS DISTINCT FROM $1), which will inject NULL constants
directly into the DistinctExpr during planning, and the parser does
not have a chance to transform it into a NullTest.

> The test case can't cover the following codes:
> + /*
> + * If one input is an explicit
> NULL constant, and the
> + * other is a non-nullable
> expression, the result is
> + * always TRUE.
> + */
> + if (has_null_input)
> + return
> makeBoolConst(true, false);

Right. I'll need to adjust the test cases to cover this.

- Richard

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message ocean_li_996 2026-01-27 07:18:58 Re: [BUG] Incorrect historic snapshot may be serialized to disk during fast-forwarding
Previous Message Michael Paquier 2026-01-27 07:09:44 Re: Remove PG_MMAP_FLAGS