Re: Convert NOT IN sublinks to anti-joins when safe

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Convert NOT IN sublinks to anti-joins when safe
Date: 2026-02-05 06:51:20
Message-ID: CAMbWs4-YfL4pnSxg87=6YDdhZD-DYtu1Oa9OvyXaR79q4Xk_mQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Feb 4, 2026 at 6:47 PM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
> Is there a convenient way to verify that an operator never returns
> NULL on non-null inputs? Would it be sufficient to insist that the
> operator belongs to btree opclass (assuming that the strict ordering
> requirements of btree imply this safety)?

I think we can insist that the operator be a member of a btree or hash
opfamily. Btree operators must adhere to strict total order, and hash
operators must adhere to strict equality; if they return NULL for
non-null inputs, the indexes themselves would be corrupt.

I'm less confident about other access methods like gist or gin. Their
semantics can be more flexible, and using such operators in a NOT IN
clause is quite rare.

Attached is the updated patch, which adds the check requiring the
operator to be a member of a btree or hash opfamily.

> And, is it worth checking if an operator never returns NULL even on
> NULL inputs? If we can identify such operators, we should be able to
> remove the requirement that both sides of NOT IN must be non-nullable.
> Is there a convenient way to check for such operators?

I don't know how to check for such operators, so I didn't do it in the
patch.

- Richard

Attachment Content-Type Size
v2-0001-Convert-NOT-IN-sublinks-to-anti-joins-when-safe.patch application/octet-stream 57.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexandre Felipe 2026-02-05 06:59:25 Re: New access method for b-tree.
Previous Message zengman 2026-02-05 06:51:05 Small fixes for incorrect error messages