Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hKIBr-0007dE-Mz for pgsql-general@arkaria.postgresql.org; Sat, 27 Apr 2019 07:56:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hKIBp-0007gm-HB for pgsql-general@arkaria.postgresql.org; Sat, 27 Apr 2019 07:56:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hKIBp-0007gf-3u for pgsql-general@lists.postgresql.org; Sat, 27 Apr 2019 07:56:05 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hKIBh-0004qp-VS for pgsql-general@lists.postgresql.org; Sat, 27 Apr 2019 07:56:03 +0000 Received: from [192.168.127.1] (port=47027 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1hKIBd-0006iA-VW; Sat, 27 Apr 2019 07:55:53 +0000 Received: from [127.0.0.1] (port=37458 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1hKIBc-0007X6-VP; Sat, 27 Apr 2019 07:55:53 +0000 From: Andrew Gierth To: Steven Schlansker Cc: pgsql-general@lists.postgresql.org Subject: Re: Computed index on transformation of jsonb key set In-Reply-To: <8A0F49D1-CA16-445B-BC4E-F53E9925809C@gmail.com> (Steven Schlansker's message of "Fri, 26 Apr 2019 14:25:47 -0700") Message-ID: <87h8ajx4y4.fsf@news-spur.riddles.org.uk> References: <8A0F49D1-CA16-445B-BC4E-F53E9925809C@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Sat, 27 Apr 2019 08:55:52 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Steven" == Steven Schlansker 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)