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 1tO8Jf-004Kpl-VS for pgsql-general@arkaria.postgresql.org; Thu, 19 Dec 2024 04:39:16 +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 1tO8Ih-006tAJ-4q for pgsql-general@arkaria.postgresql.org; Thu, 19 Dec 2024 04:38:14 +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 1tO8Ig-006t4p-Py for pgsql-general@lists.postgresql.org; Thu, 19 Dec 2024 04:38:14 +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.94.2) (envelope-from ) id 1tO8Id-000WbD-6K for pgsql-general@postgresql.org; Thu, 19 Dec 2024 04:38:13 +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 4BJ4c8Ou3062510; Wed, 18 Dec 2024 23:38:08 -0500 From: Tom Lane To: "David G. Johnston" cc: Adrian Garcia Badaracco , "pgsql-general@postgresql.org" Subject: Re: Wrapping a where clause to preserve rows with nulls In-reply-to: References: Comments: In-reply-to "David G. Johnston" message dated "Wed, 18 Dec 2024 21:14:43 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <3062508.1734583088.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Wed, 18 Dec 2024 23:38:08 -0500 Message-ID: <3062509.1734583088@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: > On Wednesday, December 18, 2024, Adrian Garcia Badaracco < > adrian@adriangb.com> wrote: >> Is there any way to include the rows where the predicate evaluates to null >> while still using an index? > ... A btree index, which handles =, can’t be told to behave > differently and so cannot fulfill your desire to produce rows where the > stored value is null; it can only produce those equal to 5000. Not in a single scan, no. But multiple scans are possible: regression=# create table t (id int unique); CREATE TABLE regression=# explain select * from t where id = 5000 or id is null; QUERY PLAN ------------------------------------------------------------------------------ Bitmap Heap Scan on t (cost=8.42..18.98 rows=14 width=4) Recheck Cond: ((id IS NULL) OR (id = 5000)) -> BitmapOr (cost=8.42..8.42 rows=14 width=0) -> Bitmap Index Scan on t_id_key (cost=0.00..4.25 rows=13 width=0) Index Cond: (id IS NULL) -> Bitmap Index Scan on t_id_key (cost=0.00..4.16 rows=1 width=0) Index Cond: (id = 5000) (7 rows) The OP was quite unclear about what semantics he wants for multiple-variable WHERE clauses, but maybe something like this would work: WHERE (original-clause) OR x IS NULL OR y IS NULL OR ... where each variable mentioned in original-clause is allowed to also be NULL. Or perhaps what is wanted is WHERE (original-clause) OR (x IS NULL AND y IS NULL AND ...) ?? regards, tom lane