Re: Computed index on transformation of jsonb key set

From: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
To: Steven Schlansker <stevenschlansker(at)gmail(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Computed index on transformation of jsonb key set
Date: 2019-04-27 07:55:52
Message-ID: 87h8ajx4y4.fsf@news-spur.riddles.org.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>>>>> "Steven" == Steven Schlansker <stevenschlansker(at)gmail(dot)com> writes:

Steven> I figured I'd end up with significantly better storage and
Steven> performance characteristics if I first compute a uuid[] value
Steven> and build the GIN over that, and use the array operator class
Steven> instead. Additionally, this eliminates possible confusion about
Steven> uuid casing (text is case sensitive, uuid is not) and this has
Steven> already caused at least one bug in our application.

Steven> I attempted to optimize a query like:
Steven> select * from tbl where array(select jsonb_object_keys(mapData)::uuid) && array['320982a7-cfaa-572a-b5ea-2074d7f3b014'::uuid];

Obvious solution:

create function uuid_keys(mapData jsonb) returns uuid[]
language plpgsql immutable strict
as $$
begin
return array(select jsonb_object_keys(mapData)::uuid);
end;
$$;

create index on tbl using gin (uuid_keys(mapData));

select * from tbl where uuid_keys(mapData) && array[...];

--
Andrew (irc:RhodiumToad)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Matthias Apitz 2019-04-27 07:58:49 Missing pg_config on SuSE SLES 12 for PostgreSQL 10
Previous Message Laurenz Albe 2019-04-26 22:20:59 Re: 9.6.9 Default configuration for a default installation but different with-krb-srvnam