From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: pgsql-bugs@lists.postgresql.org, sergey@romanovsky.org, Zhijiang Li <zl256@cornell.edu>
Subject: Re: BUG #15604: NOT IN condition incorrectly returns False
Date: Wed, 23 Jan 2019 06:57:36 +0000
Message-ID: <87fttj3m43.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <15604-22de62eff9d98199@postgresql.org>
References: <15604-22de62eff9d98199@postgresql.org>
>>>>> "PG" == PG Bug reporting form <noreply@postgresql.org> writes:
PG> The following bug has been logged on the website:
PG> Bug reference: 15604
PG> Logged by: Sergey Romanovsky
PG> Email address: sergey@romanovsky.org
PG> PostgreSQL version: 10.6
PG> Operating system: linux Red Hat 4.8.3-9
PG> Description:
PG> Zhijiang Li <zl256@cornell.edu> and I found the following bug described
PG> here: https://github.com/romanovsky/postgres/blob/master/README.md
PG> # Postgres bug: NOT IN condition incorrectly returns False
Not a bug. This is actually how NOT IN is supposed to work, and it has
nothing to do with hash lookups or instance size (the output is the same
whether a hashed or non-hashed plan is used).
Here is why:
select count(*) from postgres_not_in_bug where request_id is null;
count
-------
1
(1 row)
The condition 1 NOT IN (2,NULL) is equivalent to (1=2) OR (1=NULL),
which evaluates to (false) OR (NULL) which in turn evaluates to NULL.
Since this is not TRUE, the WHERE clause does not accept the row.
When you do NOT IN (select col ...) then the null handling is the same;
if there is _any_ null value in the selected data, then the NOT IN will
never return TRUE (only FALSE or NULL according to whether the value is
found or not).
See also https://wiki.postgresql.org/wiki/Don't_Do_This#Don.27t_use_NOT_IN
--
Andrew (irc:RhodiumToad)
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-bugs@postgresql.org
Cc: andrew@tao11.riddles.org.uk, zl256@cornell.edu
Subject: Re: BUG #15604: NOT IN condition incorrectly returns False
In-Reply-To: <87fttj3m43.fsf@news-spur.riddles.org.uk>
References: <15604-22de62eff9d98199@postgresql.org> <87fttj3m43.fsf@news-spur.riddles.org.uk>
* 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