From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix NOT IN to anti-join conversion with coerced sub-select outpu
Date: Wed, 04 Nov 2026 07:25:15 +0000
Message-ID: <E1xOVMx-000000008Gu-2qK8@gemulon.postgresql.org> (raw)
Fix NOT IN to anti-join conversion with coerced sub-select outputs
Commit 383eb21eb allowed converting "x NOT IN (SELECT y ...)" to an
anti-join when neither the outer expressions nor the sub-select's
output columns can be NULL, and the operator cannot return NULL for
non-null inputs. However, the operator is not necessarily applied to
the sub-select's output itself: the parser may wrap the output's Param
in a coercion to the operator's input type. If that coercion is a
cast function that returns NULL for a non-null input, the comparison
yields NULL, so NOT IN must not return true, yet the anti-join keeps
the outer row, producing wrong results.
To fix, require each inner operand of the comparison to be the
sub-select's output Param, possibly under a RelabelType, which cannot
introduce NULLs. This gives up the conversion for some coercions that
cannot actually return NULL, such as the built-in cast from integer to
numeric, but proving that for a cast function is not straightforward.
Back-patch to v19, where the conversion was introduced.
Author: BHUVAN CHILUKUTI <c.bhuvan@iitg.ac.in>
Reviewed-by: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/PN0P287MB090375874C6D5433282EC9DFA7642@PN0P287MB0903.INDP287.PROD.OUTLOOK.COM
Backpatch-through: 19
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/b7eee09eab738e085d6b62d35788f542dd233877
Modified Files
--------------
src/backend/optimizer/plan/subselect.c | 21 ++++++++++++++++++++-
src/test/regress/expected/subselect.out | 18 ++++++++++++++++++
src/test/regress/sql/subselect.sql | 12 ++++++++++++
3 files changed, 50 insertions(+), 1 deletion(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: rguo@postgresql.org
Subject: Re: pgsql: Fix NOT IN to anti-join conversion with coerced sub-select outpu
In-Reply-To: <E1xOVMx-000000008Gu-2qK8@gemulon.postgresql.org>
References: <E1xOVMx-000000008Gu-2qK8@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox