Re: Incorrect NOT IN results with implicit casts returning NULL after anti-join optimization

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: BHUVAN CHILUKUTI <c(dot)bhuvan(at)iitg(dot)ac(dot)in>
Cc: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: Incorrect NOT IN results with implicit casts returning NULL after anti-join optimization
Date: 2026-11-04 07:47:57
Message-ID: CAMbWs49vruDbw4u6Ymu-JXccoPx92tTP191++BqiV35hvh4UGw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Sat, Oct 24, 2026 at 3:44 PM BHUVAN CHILUKUTI <c(dot)bhuvan(at)iitg(dot)ac(dot)in> wrote:
> I am reporting a query correctness issue in PostgreSQL involving the optimization of NOT IN subqueries into anti-joins when an implicit cast on the inner comparison operand can return SQL NULL for a non-NULL input.

I've pushed a fix for this bug.

As for keeping the conversion for built-in cast functions, checking
that the function is built-in is not enough, because a user can create
an implicit cast using a built-in function that returns NULL for some
non-null input.

One could argue that we should instead check whether the coercion
comes from a built-in pg_cast entry, but I'm not inclined to add that
much code for such a rare and AI-artificial case. I'd rather just
return false, in the spirit of the comment there:

* It is acceptable for this check not to be exhaustive. We can err on the
* side of conservatism: if we're not sure, it's okay to return FALSE.

- Richard

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Aleksander Alekseev 2026-11-04 09:05:27 Re: BUG #19603: Vuln47: distance_taxicab and distance_chebyshev silently return 0 instead of NaN when a cube coordin
Previous Message Tatsuo Ishii 2026-11-04 07:44:15 Re: first_value()/nth_value() with IGNORE NULLS loop forever (EXCLUDE GROUP/TIES, frame past partition end, no ORDER BY)