X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7135352805 for ; Tue, 31 May 2005 00:18:07 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 02904-06 for ; Tue, 31 May 2005 03:18:03 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id 8DA3D52806 for ; Tue, 31 May 2005 00:18:01 -0300 (ADT) Received: (qmail 19522 invoked by uid 500); 31 May 2005 03:16:54 -0000 Date: Mon, 30 May 2005 22:16:53 -0500 From: Bruno Wolff III To: Tobias Brox Cc: pgsql-performance@postgresql.org Subject: Re: Index on a NULL-value Message-ID: <20050531031653.GA17723@wolff.to> Mail-Followup-To: Bruno Wolff III , Tobias Brox , pgsql-performance@postgresql.org References: <20050531030207.GD26100@tobias.exoweb.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050531030207.GD26100@tobias.exoweb.net> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.009 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200505/455 X-Sequence-Number: 12619 On Tue, May 31, 2005 at 11:02:07 +0800, Tobias Brox wrote: > I read in the manual today: > > Indexes are not used for IS NULL clauses by default. The best way to use > indexes in such cases is to create a partial index using an IS NULL > predicate. > > This is from the documentation for PostgreSQL 8. I did not find anything > equivalent in the 7.4.8-documentation. > > I wasn't aware of this until it became an issue :-) Well, so I follow the > tip but in vain. Reduced and reproduced like this in PostgreSQL 7.4.7: > > test=# create table mock(a int, b int); > CREATE TABLE > test=# create index b_is_null on mock((b IS NULL)); > CREATE INDEX > test=# insert into mock values (10,20); > INSERT 70385040 1 > test=# insert into mock values (20,30); > INSERT 70385041 1 > test=# insert into mock values (30, NULL); > INSERT 70385042 1 > test=# set enable_seqscan=off; > SET > test=# explain select * from mock where b is NULL; > QUERY PLAN > -------------------------------------------------------------------- > Seq Scan on mock (cost=100000000.00..100000020.00 rows=6 width=8) > Filter: (b IS NULL) > (2 rows) > > vacuum analyze also didn't help to recognize the index ;-) It isn't surprising that an index wasn't used since a sequential scan is going to be faster in your test case. If you want to test this out, you to want use realistically sized tables.