X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 3F2BED1BABE for ; Fri, 22 Aug 2003 11:35:24 -0300 (ADT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 56089-03 for ; Fri, 22 Aug 2003 11:34:57 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id BD24FD1BA13 for ; Fri, 22 Aug 2003 11:34:52 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.9/8.12.9) with ESMTP id h7MEYd5H010843; Fri, 22 Aug 2003 10:34:39 -0400 (EDT) To: "scott.marlowe" Cc: Sam Barnett-Cormack , PostgreSQL Administrators Mailing List Subject: Re: Partial indexes (was: Re: Indexing a boolean) In-reply-to: References: Comments: In-reply-to "scott.marlowe" message dated "Fri, 22 Aug 2003 07:48:56 -0600" Date: Fri, 22 Aug 2003 10:34:39 -0400 Message-ID: <10842.1061562879@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/263 X-Sequence-Number: 9984 "scott.marlowe" writes: > Basically, for partial / functional indexes, the create index where clause > needs to exactly (or nearly so) match the select query's where clause: Another consideration is that an index declaration like this: create index fooi on foo (flag) where flag; is really rather redundant, since the entries will only be made for rows where flag is true, and so storage of the column value in the index is useless. You may be able to get more mileage out of the index by making the index on another column that you often test in conjunction with the flag. For example: regression=# create table foo (flag bool, ts timestamp); CREATE TABLE regression=# create index fooi on foo(ts) where flag; CREATE INDEX regression=# explain select * from foo where ts > '2003-10-11' and flag; QUERY PLAN -------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..22.50 rows=167 width=9) Filter: ((ts > '2003-10-11 00:00:00'::timestamp without time zone) AND flag) (2 rows) regression=# set enable_seqscan TO 0; SET regression=# explain select * from foo where ts > '2003-10-11' and flag; QUERY PLAN ------------------------------------------------------------------------- Index Scan using fooi on foo (cost=0.00..43.25 rows=167 width=9) Index Cond: (ts > '2003-10-11 00:00:00'::timestamp without time zone) Filter: flag (3 rows) (In a more realistic situation, the planner would probably have chosen the indexscan without any prompting.) regards, tom lane