Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmCTo-0001yk-Vd for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Jan 2019 06:57:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gmCTn-0002MC-JE for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Jan 2019 06:57:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmCTn-0002LV-Cq for pgsql-bugs@lists.postgresql.org; Wed, 23 Jan 2019 06:57:43 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gmCTk-00047X-UJ for pgsql-bugs@lists.postgresql.org; Wed, 23 Jan 2019 06:57:42 +0000 Received: from [192.168.127.1] (port=59972 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gmCTg-000Hhn-Nn; Wed, 23 Jan 2019 06:57:36 +0000 Received: from [127.0.0.1] (port=51159 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gmCTg-0004Sw-17; Wed, 23 Jan 2019 06:57:36 +0000 From: Andrew Gierth To: pgsql-bugs@lists.postgresql.org, sergey@romanovsky.org, Zhijiang Li Subject: Re: BUG #15604: NOT IN condition incorrectly returns False In-Reply-To: <15604-22de62eff9d98199@postgresql.org> (PG Bug reporting form's message of "Wed, 23 Jan 2019 00:44:15 +0000") Message-ID: <87fttj3m43.fsf@news-spur.riddles.org.uk> References: <15604-22de62eff9d98199@postgresql.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Wed, 23 Jan 2019 06:57:36 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "PG" == PG Bug reporting form 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 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)