Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XwhlU-00009g-R8 for pgsql-performance@arkaria.postgresql.org; Fri, 05 Dec 2014 01:33:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XwhlT-0002fE-O0 for pgsql-performance@arkaria.postgresql.org; Fri, 05 Dec 2014 01:32:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XwhlR-0002f5-K0 for pgsql-performance@postgresql.org; Fri, 05 Dec 2014 01:32:57 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XwhlN-0008LH-Oy for pgsql-performance@postgresql.org; Fri, 05 Dec 2014 01:32:56 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id sB51Wo0d020533; Thu, 4 Dec 2014 20:32:50 -0500 From: Tom Lane To: Michael Barker cc: pgsql-performance Subject: Re: Query doesn't use index on hstore column In-reply-to: References: Comments: In-reply-to Michael Barker message dated "Fri, 05 Dec 2014 09:42:20 +1300" Date: Thu, 04 Dec 2014 20:32:50 -0500 Message-ID: <20532.1417743170@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Michael Barker writes: > I'm currently experimenting with hstore on Posgtres 9.4rc1. I've created a > table with an hstore column, with and index on that column (tried both gin > and btree indexes) and the explain plan says that the index is never used > for the lookup and falls to a sequential scan every time (table has 1 000 > 000 rows). The query plans and execution time for btree index, gin index > and unindexed are the same. Is there something I'm doing wrong or missing > in order to get indexes to work on hstore columns? Well, first off, a btree index is fairly useless for this query, because btree has no concept that the hstore has any sub-structure. A GIN index or GIST index could work though. Secondly, you have to remember that indexable WHERE conditions in Postgres are *always* of the form "WHERE indexed_column indexable_operator some_comparison_value". So the trick is to recast the condition you have into something that looks like that. Instead of WHERE attributes->'accountId' = '1879355460' you could do WHERE attributes @> 'accountId=>1879355460' (@> being the hstore containment operator, ie "does attributes contain a pair that looks like this?") or equivalently but possibly easier to generate, WHERE attributes @> hstore('accountId', '1879355460') Another possibility if you're only concerned about indexing searches for one or a few specific keys is to use expression indexes: CREATE INDEX ON audit ((attributes->'accountId')); whereupon your original query works, since the left-hand side of the '=' operator is now the indexed expression. (Here, since you are testing plain equality on the indexed value, a btree works fine.) You might care to read http://www.postgresql.org/docs/9.4/static/indexes.html to get a better handle on what Postgres indexes can and can't do. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance