Re: Is it possible to create an index without keeping the indexed data in a column? - Mailing list pgsql-general

From Amit Langote
Subject Re: Is it possible to create an index without keeping the indexed data in a column?
Date
Msg-id CA+HiwqHMgmp2HnxPzzhtbm+guj2Nih-Hw8MtJUJHL7Gw-2RfWQ@mail.gmail.com
Whole thread Raw
In response to Re: Is it possible to create an index without keeping the indexed data in a column?  (Michael Paquier <michael.paquier@gmail.com>)
Responses Re: Is it possible to create an index without keeping the indexed data in a column?  (Amit Langote <amitlangote09@gmail.com>)
List pgsql-general
On Fri, Aug 1, 2014 at 10:48 AM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> On Fri, Aug 1, 2014 at 4:47 AM, Larry White <ljw1001@gmail.com> wrote:
>> Is there a way to get Postgres to index the table as if the JSON were there,
>> but not actually put the data in the table?
>> I could either store the docs
>> elsewhere and keep a reference, or compress them and put them in the table
>> in compressed form as a blob.
> No. This is equivalent to the creation of an index on a foreign table.

Not sure exactly if it applies here; but I seem to recall reading
somewhere that you can index "generated" columns. Something like
following (this example is similar what I recall seeing there)

postgres=# CREATE TABLE test(a, b) AS SELECT md5(g::text)::char(10),
md5(g::text)::char(5) FROM generate_series(1, 100000) g;
SELECT 100000

postgres=# CREATE OR REPLACE FUNCTION ab(rec test) RETURNS text AS $$
SELECT rec.a || rec.b; $$ STABLE LANGUAGE SQL;
CREATE FUNCTION

postgres=# CREATE EXTENSION pg_trgm;
CREATE EXTENSION

postgres=# CREATE INDEX test_idx ON test USING GIN (ab(test) gin_trgm_ops);
CREATE INDEX

postgres=# EXPLAIN SELECT * FROM test WHERE ab(test) LIKE '%c4c%';
                               QUERY PLAN
-------------------------------------------------------------------------
 Bitmap Heap Scan on test  (cost=16.09..52.53 rows=10 width=17)
   Recheck Cond: (((a)::text || (b)::text) ~~ '%c4c%'::text)
   ->  Bitmap Index Scan on test_idx  (cost=0.00..16.08 rows=10 width=0)
         Index Cond: (((a)::text || (b)::text) ~~ '%c4c%'::text)
 Planning time: 0.361 ms
(5 rows)

--
Amit


pgsql-general by date:

Previous
From: Larry White
Date:
Subject: Re: Very Limited Toast Compression on JSONB (9.4 beta 2)
Next
From: Amit Langote
Date:
Subject: Re: Is it possible to create an index without keeping the indexed data in a column?