Re: Fold NOT IN / <> ALL expressions containing NULL to FALSE

From: Ilia Evdokimov <ilidock95(at)gmail(dot)com>
To: Rustam ALLAKOV <rustamallakov(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org, "darthunix(at)gmail(dot)com" <darthunix(at)gmail(dot)com>
Subject: Re: Fold NOT IN / <> ALL expressions containing NULL to FALSE
Date: 2026-10-19 13:04:54
Message-ID: CAAgWR6YQhoc=UOdfsuG608R9LYx9q6fGALk5VGf2_=WKpcB65w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Rustam,

On Mon, Oct 19, 2026 at 1:15 PM Rustam ALLAKOV <rustamallakov(at)gmail(dot)com> wrote:
> I found one new regression. It is caused by the new IS NOT TRUE
> handling: the qual now differs from a partial index predicate written
> the same way.

You're right. Folding IS NOT TRUE to TRUE removes the qual needed to
prove the partial index predicate. The OR case has the same cause: the
whole OR becomes TRUE and disappears, while the index predicate
retains its original structure.

The attached v7 leaves IS NOT TRUE expressions unchanged. IS TRUE can
still fold to FALSE when its argument can never be TRUE, but otherwise
BooleanTest arguments are left unchanged.

With this change, your first example uses a Bitmap Index Scan on the
partial index, and the OR example uses an Index Scan. Direct NOT IN
expressions and their IS TRUE forms still fold to a One-Time Filter:
false.

I've added regression tests for both partial index cases, checking
index availability with enable_seqscan disabled and checking the query
results.

There are also a few additional changes in v7:
- Tests for a non-strict operator that returns TRUE for NULL
arguments. A NULL array element must not trigger folding for such an
operator, whereas a NULL array still produces NULL for both ANY and
ALL
- A test ensuring that strict ANY with a matching element and a NULL
element retains the matching row
- A stack-depth check in simplify_qual_null_saops()
- AND/OR argument lists are now copied only when a rewrite is needed,
avoiding unnecessary list allocation for unchanged expressions

--
Best regards,
Ilia Evdokimov,
Tantor Labs LLC,
https://tantorlabs.com/

Attachment Content-Type Size
v7-0001-Fold-never-true-scalar-array-comparisons-to-false.patch text/x-patch 56.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message torikoshia 2026-10-19 13:09:23 Re: RFC: Logging plan of the running query
Previous Message Andrew Dunstan 2026-10-19 12:55:09 Re: Allow table AMs to define their own reloptions