Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ufITR-0059ns-Pi for pgsql-bugs@arkaria.postgresql.org; Fri, 25 Jul 2025 13:28:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1ufITP-000qV4-S6 for pgsql-bugs@arkaria.postgresql.org; Fri, 25 Jul 2025 13:28:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ufITP-000qUw-JJ for pgsql-bugs@lists.postgresql.org; Fri, 25 Jul 2025 13:28:31 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1ufITM-000mcl-0P for pgsql-bugs@lists.postgresql.org; Fri, 25 Jul 2025 13:28:31 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 56PDSJkj1856869; Fri, 25 Jul 2025 09:28:20 -0400 From: Tom Lane To: jinhui-lai@foxmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18999: Equivalent queries processing WHERE IS NULL & WHERE IS NOT NULL produce mutually exclusive results In-reply-to: <18999-4dbdbaeafb1c1023@postgresql.org> References: <18999-4dbdbaeafb1c1023@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Fri, 25 Jul 2025 04:43:03 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1856867.1753450099.1@sss.pgh.pa.us> Date: Fri, 25 Jul 2025 09:28:19 -0400 Message-ID: <1856868.1753450099@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > I think "SELECT COUNT(c0) FROM t0" = "SELECT COUNT(c0) FROM t0 WHERE c0 IS > NOT NULL" + "SELECT COUNT(c0) FROM t0 WHERE c0 IS NULL". This is not so when c0 is composite. Per [1]: If the expression is row-valued, then IS NULL is true when the row expression itself is null or when all the row's fields are null, while IS NOT NULL is true when the row expression itself is non-null and all the row's fields are non-null. Because of this behavior, IS NULL and IS NOT NULL do not always return inverse results for row-valued expressions; in particular, a row-valued expression that contains both null and non-null fields will return false for both tests. Pretty weird, I agree, but that's what the SQL standard says to do. regards, tom lane [1] https://www.postgresql.org/docs/current/functions-comparison.html