Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 696F01337B47 for ; Thu, 7 Oct 2010 21:49:38 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 36889-07 for ; Fri, 8 Oct 2010 00:49:32 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from glacier.frostconsultingllc.com (glacier.frostconsultingllc.com [69.36.227.170]) by mail.postgresql.org (Postfix) with ESMTP id 6502D1336DB6 for ; Thu, 7 Oct 2010 21:49:30 -0300 (ADT) Received: from dsl081-245-111.sfo1.dsl.speakeasy.net ([64.81.245.111] helo=Sidney-Stratton.local) by glacier.frostconsultingllc.com with esmtpsa (TLSv1:CAMELLIA256-SHA:256) (Exim 4.69) (envelope-from ) id 1P419G-0000Ex-DX; Thu, 07 Oct 2010 17:49:28 -0700 Message-ID: <4CAE6A92.4060703@agliodbs.com> Date: Thu, 07 Oct 2010 17:49:22 -0700 From: Josh Berkus User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20090223 Thunderbird/3.0b2 MIME-Version: 1.0 To: postgres hackers CC: Oleg Bartunov Subject: GIN vs. Partial Indexes Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201010/522 X-Sequence-Number: 171562 All, I thought we fixed this in 8.4.4, but apparently not. In the event that you have a GIN index containing a WHERE clause which is sufficiently restrictive, PostgreSQL will attempt to use the index even though it can't. Since this is completely out of the control of the user, it effectively prohibits using partial GIN indexes: Setup: postgres=# select version(); version -------------------------------------------------------------------------------------------------------------------------------------- PostgreSQL 9.0.0 on i386-apple-darwin9.8.0, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465), 32-bit postgres=# create table gin_test ( id serial not null primary key, type CREATE TABLE DO $$ DECLARE i INT := 1; r INT; qts tsvector; del BOOLEAN; BEGIN qts := to_tsvector('The most anticipated PostgreSQL version in five years has been released. With built-in binary replication and over a dozen new major features, PostgreSQL 9.0 has compelling reasons to upgrade or migrate for every database user and developer.'); WHILE i < 1000 LOOP r := ( random() * 20 )::INT; INSERT INTO gin_test ( "type", deleted, some_ts ) VALUES ( r, ( r % 2 ) = 0, qts ); i := i + 1; END LOOP; END;$$; create index gin_test_type ON gin_test("type"); create index gin_test_text ON gin_test USING GIN ( some_ts) WHERE deleted = FALSE AND "type" = 1; postgres=# SELECT COUNT(*) from gin_test WHERE deleted = FALSE and "type" = 1; ERROR: GIN indexes do not support whole-index scans postgres-# EXPLAIN SELECT COUNT(*) from gin_test WHERE deleted = FALSE and "type" = 1; QUERY PLAN ------------------------------------------------------------------------------------ Aggregate (cost=54.01..54.02 rows=1 width=0) -> Bitmap Heap Scan on gin_test (cost=12.38..53.95 rows=24 width=0) Recheck Cond: ((NOT deleted) AND (type = 1)) -> Bitmap Index Scan on gin_test_text (cost=0.00..12.37 rows=24 width=0) (4 rows) I find the above error interesting, because: (a) I didn't actually select the some_ts column, and (b) I can do an actual TS search which hits the whole index with no problem: postgres=# SELECT COUNT(*) from gin_test WHERE some_ts @@ to_tsquery('replication'); count ------- 999 Note that if I add the perfect index for that query, it works: postgres=# create index gin_test_type_undeleted on gin_test("type") where not deleted; CREATE INDEX postgres=# SELECT COUNT(*) from gin_test WHERE deleted = FALSE and "type" = 1; count ------- 46 (1 row) postgres=# EXPLAIN SELECT COUNT(*) from gin_test WHERE deleted = FALSE and "type" = 1; QUERY PLAN --------------------------------------------------------------------------------------------- Aggregate (cost=46.23..46.24 rows=1 width=0) -> Bitmap Heap Scan on gin_test (cost=4.60..46.17 rows=24 width=0) Recheck Cond: ((type = 1) AND (NOT deleted)) -> Bitmap Index Scan on gin_test_type_undeleted (cost=0.00..4.60 rows=24 width=0) Index Cond: (type = 1) (5 rows) Clearly the answer here seems to be that our planner should not pick GIN indexes for any query in which the indexed column is not referenced. Is that practical to implement? -- -- Josh Berkus PostgreSQL Experts Inc. http://www.pgexperts.com