Re: Optimize IS DISTINCT FROM with non-nullable inputs

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Optimize IS DISTINCT FROM with non-nullable inputs
Date: 2026-01-26 08:00:08
Message-ID: CAMbWs4_vQDaJ3fGQt+RD3+CeasJHB0j9pCjUpS00=dgYe=bW_w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Jan 23, 2026 at 10:40 PM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
> I'm kind of concerned about whether there are edge cases where this
> transformation is not safe, specifically regarding "rowtype" inputs.

After a second thought, I think this should be safe even for "rowtype"
inputs. The executor (ExecInterpExpr) evaluates a DistinctExpr with
non-null inputs by simply applying the underlying equality function
and then negating the result. Also, record_eq explicitly treats two
NULLs as equal. Therefore, in this case, converting IS DISTINCT FROM
to the inequality operator in the planner produces the exact same
behavior as the executor.

Also, I think we can play this same trick with BooleanTest (IS [NOT]
TRUE/FALSE/UNKNOWN). A BooleanTest treats a NULL input as the logical
value "unknown". If we know that the input can not be NULL, we can
simplify it directly to a boolean expression or a constant. Please
see 0002.

- Richard

Attachment Content-Type Size
v2-0001-Optimize-IS-DISTINCT-FROM-with-non-nullable-input.patch application/octet-stream 14.4 KB
v2-0002-Optimize-BooleanTest-with-non-nullable-input.patch application/octet-stream 7.3 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message jian he 2026-01-26 07:52:16 Re: support fast default for domain with constraints