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 1hK8Lz-0003ms-FP for pgsql-general@arkaria.postgresql.org; Fri, 26 Apr 2019 21:25:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hK8Ly-00016d-3w for pgsql-general@arkaria.postgresql.org; Fri, 26 Apr 2019 21:25:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hK8Lx-0000xu-Kr for pgsql-general@lists.postgresql.org; Fri, 26 Apr 2019 21:25:53 +0000 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hK8Lv-0002QJ-Gd for pgsql-general@lists.postgresql.org; Fri, 26 Apr 2019 21:25:53 +0000 Received: by mail-pf1-x442.google.com with SMTP id b3so2316455pfd.1 for ; Fri, 26 Apr 2019 14:25:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:content-transfer-encoding:mime-version:subject:message-id:date :to; bh=qeKSQ7+oogOXKyY69zW3qUAb1Lxoras138FDuAn75bs=; b=i0G+Paet1o+KjuLhCwXvduz48EfLRnlg8alMrGCfAtJuXwNf5n/lvKrCZGjvNLM+i6 kvO+us4obAh+HBicX6CQs5AKe7JAern+FIc304GoaGkjP5V41tBjYMnuCADc1TURJkU4 HMpuowJCEpxGfozdoMCRGfEWKy5oWaxMfbgWgqTymYZWqn2QBMPgbCiClry4ZOfkw/dI aI1FTX9odzf6zP+Idx3/sG6ep8a3HnCl2cSFp1tc34jailjAj3Y+juCNG2sLWsHPdgnx jq2RdSStB6dDpMuKZIPydM7n3h/9RAI6ZP5Fj+6DCEI0UTW0CX/Mbz0WIMZuWssscqtd xI7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:content-transfer-encoding:mime-version :subject:message-id:date:to; bh=qeKSQ7+oogOXKyY69zW3qUAb1Lxoras138FDuAn75bs=; b=U2AglXhcdfqXgxekadIJN6xHCzQjgBZnDg2xhWTmIzoJtSsv9N0hbKZzmrdf7c1pos kyd8fJdkpsjEuDOct/vCwLrV8YdkNagy3lZbycLD4XJWvQ1WNs9NTLrjHAV2ABjClHs0 jraBc1nf8vhaE6GOpNDM0UGtY+pvTWQFlO9whVStstO9XoPoHvQ71TzDMg8Raq2PY6zg gbttBOwjaWpqAJGTKCkBQxhVEwtqT1AxW6Xvce3lBgBludMaT/pDzRWCmMopKB9ak3Jf eVIfhsVTO4TzuusdhOPicxL7IXzv/PfLQy9N0Kk3SfUIQbCJMi+Q/6C8R3Cs20nHJdom 5Pgg== X-Gm-Message-State: APjAAAUyQ2RNiWxjOWBXN2AT4Df9jiuL6yUZdT+GVx1dhcv2EqlQQG2i bU5sPyx6pf5MNi9B5dO+Uc5b3+XAcMw= X-Google-Smtp-Source: APXvYqy6L1E7mI872UAiHy5vGYN7DP6Kh87CKHh2pspLunpu5XUpBHAsn4qNPGcsFMfUDTjwxaIMeg== X-Received: by 2002:aa7:8b8b:: with SMTP id r11mr18836692pfd.130.1556313949233; Fri, 26 Apr 2019 14:25:49 -0700 (PDT) Received: from [10.7.108.6] ([65.200.105.250]) by smtp.gmail.com with ESMTPSA id a6sm40326673pfn.181.2019.04.26.14.25.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Apr 2019 14:25:48 -0700 (PDT) From: Steven Schlansker Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 12.2 \(3445.102.3\)) Subject: Computed index on transformation of jsonb key set Message-Id: <8A0F49D1-CA16-445B-BC4E-F53E9925809C@gmail.com> Date: Fri, 26 Apr 2019 14:25:47 -0700 To: pgsql-general@lists.postgresql.org X-Mailer: Apple Mail (2.3445.102.3) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi Postgres fans, We store a Java Map in Postgres as a jsonb column. As json does not have a UUID type, it is of course stored as text. A simple value might be: {"04e623c0-6940-542f-a0de-4c999c626dfe": 50000000, = "6d3e24b6-9e8c-5eb1-9e4c-f32cc40864c9": 208250000} I am trying to implement an efficient set-intersection test. "Find all rows where the map has a key in (uuid1, uuid2, uuid3)" The most straightforward approach is to create a GIN over the whole = column. This has the documented disadvantage that all keys and values are stored = in the GIN repeatedly, with the kicker that the UUID will be stored as = text. I figured I'd end up with significantly better storage and performance = characteristics if I first compute a uuid[] value and build the GIN over = that, and use the array operator class instead. Additionally, this = eliminates possible confusion about uuid casing (text is case sensitive, = uuid is not) and this has already caused at least one bug in our = application. I attempted to optimize a query like: select * from tbl where array(select jsonb_object_keys(mapData)::uuid) = && array['320982a7-cfaa-572a-b5ea-2074d7f3b014'::uuid]; with: create index my_idx on tbl using gin((array(select = jsonb_object_keys(mapData)::uuid))); Of course, this fails, ERROR: cannot use subquery in index expression Clever me says, "well, I'll just use array_agg instead!" create index my_idx on tbl using = gin((array_agg(jsonb_object_keys(mapData)::uuid))); Postgres 9.6 says: ERROR: aggregate functions are not allowed in index expressions Just to see what happened, knowing that GIN supports multi-valued data, = I tried: create index my_idx on tbl using = gin((jsonb_object_keys(mapData)::uuid)); ERROR: index expression cannot return a set How can I efficiently implement the feature I've described? It seems = difficult to use computed indexing with GIN. Thank you for any advice and have a great weekend, Steven