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 1hK8Th-000452-Fj for pgsql-general@arkaria.postgresql.org; Fri, 26 Apr 2019 21:33:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hK8Tg-0006ZX-6S for pgsql-general@arkaria.postgresql.org; Fri, 26 Apr 2019 21:33:52 +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 1hK8Tf-0006ZQ-SE for pgsql-general@lists.postgresql.org; Fri, 26 Apr 2019 21:33:51 +0000 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hK8TZ-0002Yi-K5 for pgsql-general@lists.postgresql.org; Fri, 26 Apr 2019 21:33:51 +0000 Received: by mail-pl1-x644.google.com with SMTP id o7so2125091pll.13 for ; Fri, 26 Apr 2019 14:33:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=Zc+cSBLVWqAyh2aOaoMiGYzqTD2NZUDAOVFEDsM6YZk=; b=I69jFvcrmFBF6YAJ7/OePjG3uzMseKFIThOX/MVlyMLZFqELftbsjZuPpkS6AlzHAE Q/Hp4dUzpzPklfTFD57W8UEziFqob9BOUfDRjGpKTS1Ijw563p84YT4XJFHavyNyznHf lqZbRUZuBR631xGursjeXv9AXyFYFMyv8usS1HW25p0I4cOOLRqVvpS2MJm8JdKoTEhu R4UnziatxM9LcJjDa1/XXHlQU+jWdHKl/yw8PwbNyDx0Z66jk+mHFzQuH+KbGXcaaiX7 UurE3yFbP4KykVY92mtuY9UJH7pdXll3E3tdMT3/3ZsL2FXoMiO0LQ+l19Vckh/OXHQg uYwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=Zc+cSBLVWqAyh2aOaoMiGYzqTD2NZUDAOVFEDsM6YZk=; b=YOIV6+7pqiYkmdude5N5T3lOWXlWVLO8OiGzihbrGlzim5iPuu+T8E7Jz4cwS8emO9 vfF7MVQLivpo+33DOcwbdQk5KLHEze/KEn0BTOcyTI1DW8+KVmaTK8kNk2shLIFCLDW3 HwZOHER0gmlCsCzImtMPShNo8rtuVCP9xDa0H+il0nx2fVAakFVSDW9zRfvOcM/66QEm ByJjp4r0pWGJsQH2hfvhTLizaG7QZlf+CyzWgTE62TN82C4e74QyNf/iskJPw9HHtb+M 6wiPT92ILJMqE3XYLuEAVSeXxx23vwYPKGw3LAZOkNEa3yS102x48kw+IY27wWJFsiYV ZCXg== X-Gm-Message-State: APjAAAV7EQhxQMoCPuqPztlwNW+M6DZsCX8tXmblr9jpHhLfjuiwQktW RyXvyFpwij+SXNy69s7oXgTDnCAH X-Google-Smtp-Source: APXvYqzdiVDASQXsdU5Z0WEKRdIBPPx39qB0twQlTRALtTDx826i9COy9EkVVQqyqhBmZsBLsK9Adg== X-Received: by 2002:a17:902:1e2:: with SMTP id b89mr46551997plb.278.1556314423110; Fri, 26 Apr 2019 14:33:43 -0700 (PDT) Received: from [10.104.134.41] ([155.100.47.1]) by smtp.gmail.com with ESMTPSA id s9sm35104388pfe.183.2019.04.26.14.33.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Apr 2019 14:33:42 -0700 (PDT) Subject: Re: Computed index on transformation of jsonb key set To: pgsql-general@lists.postgresql.org References: <8A0F49D1-CA16-445B-BC4E-F53E9925809C@gmail.com> From: Rob Sargent Message-ID: <996b3096-a521-521e-5d10-0978de5f5052@gmail.com> Date: Fri, 26 Apr 2019 15:33:41 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <8A0F49D1-CA16-445B-BC4E-F53E9925809C@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 4/26/19 3:25 PM, Steven Schlansker wrote: > 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 > > > Storing the map in a child table (parentId, UUID, int) is out of the question?