Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k5d2Z-0007O9-8G for pgsql-performance@arkaria.postgresql.org; Tue, 11 Aug 2020 22:46:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k5d2W-000284-SF for pgsql-performance@arkaria.postgresql.org; Tue, 11 Aug 2020 22:46:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k5d2W-00027x-K7 for pgsql-performance@lists.postgresql.org; Tue, 11 Aug 2020 22:46:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k5d2T-0003Dm-ON for pgsql-performance@postgresql.org; Tue, 11 Aug 2020 22:46:38 +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 07BMkW2J069329; Tue, 11 Aug 2020 18:46:33 -0400 From: Tom Lane To: Michael Lewis cc: Burhan Akbulut , postgres performance list Subject: Re: Hstore index for full text search In-reply-to: References: Comments: In-reply-to Michael Lewis message dated "Tue, 11 Aug 2020 15:55:16 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <69327.1597185992.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 11 Aug 2020 18:46:32 -0400 Message-ID: <69328.1597185992@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Michael Lewis writes: > Hash Cond: (o.courier_id =3D cc.id) > Filter: (((o.tracker_code)::text ~~* '%1654323%'::text) OR > ((table_cus.name)::text > ~~* '%1654323%'::text) OR ((au.username)::text ~~ '%1654323%'::text) OR > ((o.source)::text ~~* '%1654323%'::text) OR ((o.ops -> 'shop'::text) ~~* > '%1654323%'::text) OR ((o.ops -> 'camp_code'::text) ~~* '%1654323%'::tex= t) > OR ((city.name)::text ~~* '%1654323%'::text) OR ((co.name)::text ~~* > '%1654323%'::text) OR ((o.tr_code)::text ~~* '%1654323%'::text) OR ((o.o= ps > ? 'shipping_company'::text) AND ((o.ops -> 'shipping_company'::text) ~~* > '%1654323%'::text)) OR ((cc.name)::text ~~* '%1654323%'::text)) > All those OR conditions on different tables and fields seems like it wil= l > be unlikely that the planner will do anything with the index you are try= ing > to create (for this query). A GIN index on an hstore column only provides the ability to search for exact matches to hstore key strings. There are a few bells and whistles, like the ability to AND or OR such conditions. But basically it's just an exact-match engine, and it doesn't index the hstore's data values at all (which is why the implementors weren't too concerned about having a length limit on the index entries). There is 0 chance of this index type being useful for what the OP wants to do. Given these examples, I'd think about setting up a collection of pg_trgm indexes on the specific hstore keys you care about, ie something like CREATE INDEX ON mytable USING GIST ((ops -> 'camp_code') gist_trgm_ops); CREATE INDEX ON mytable USING GIST ((ops -> 'shipping_company') gist_trgm_= ops); ... which'd allow indexing queries like ... WHERE (ops -> 'camp_code') LIKE '%1654323%' OR (ops -> 'shipping_company') LIKE '%1654323%' I'm not sure how far this will get you, though; if there's a whole lot of different keys of interest, maintaining a separate index for each one is probably too much overhead. Another point is that you will only get an indexscan if *every* OR'd clause matches some index. The example query looks sufficiently unstructured that that might be hard to ensure. I kind of wonder whether this data design is actually a good idea. It doesn't seem to match your querying style terribly well. regards, tom lane