Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nwlqw-00024b-7V for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jun 2022 14:31:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nwlqu-00072q-Uq for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Jun 2022 14:31:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nwlqu-00072h-MG for pgsql-bugs@lists.postgresql.org; Thu, 02 Jun 2022 14:31:08 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nwlqo-0005TW-R7 for pgsql-bugs@lists.postgresql.org; Thu, 02 Jun 2022 14:31:07 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 252EV0Zl967135; Thu, 2 Jun 2022 10:31:00 -0400 From: Tom Lane To: Shaheed Haque cc: pgsql-bugs@lists.postgresql.org Subject: Re: Unable to make use of "deep" JSONB index In-reply-to: References: Comments: In-reply-to Shaheed Haque message dated "Thu, 02 Jun 2022 11:27:26 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <967133.1654180260.1@sss.pgh.pa.us> Date: Thu, 02 Jun 2022 10:31:00 -0400 Message-ID: <967134.1654180260@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Shaheed Haque writes: > -- Create index designed to match the query. > -- > create index idx1 on payrun using gin ((snapshot->'$.employee.* ? > (@.pay_graph <> 0 || @.last_run_of_employment == true || > @.state.employment[last][2] == 0)')); But that doesn't match the query; it's not even the same topmost operator: > explain analyse SELECT id,snapshot #>'{employee,999,state,employment}' FROM > "payrun" WHERE (snapshot @? '$.employee."999" ? (@.pay_graph <> 0 || > @.last_run_of_employment > == true || @.state.employment[last][2] == 0)'); In general you seem to have much too high an opinion of what PG's index machinery can cope with. The general pattern is that it can use a query WHERE clause with an index if the clause is of the form "indexed-column indexable-operator constant". There's a small number of special cases where it can transform things that don't initially look like that into the right form, but AFAIR we don't have any such special cases for any json-related operators. The one saving grace is that "indexed-column" can be an expression appearing in an index, so in some cases you can finesse things that way. But you won't find any deep knowledge of jsonpath expressions in there. Having said that, @? is reported as an indexable operator in v14: regression=# \dAo gin jsonb* List of operators of operator families AM | Operator family | Operator | Strategy | Purpose -----+-----------------+--------------------+----------+--------- gin | jsonb_ops | @>(jsonb,jsonb) | 7 | search gin | jsonb_ops | @?(jsonb,jsonpath) | 15 | search gin | jsonb_ops | @@(jsonb,jsonpath) | 16 | search gin | jsonb_ops | ?(jsonb,text) | 9 | search gin | jsonb_ops | ?|(jsonb,text[]) | 10 | search gin | jsonb_ops | ?&(jsonb,text[]) | 11 | search gin | jsonb_path_ops | @>(jsonb,jsonb) | 7 | search gin | jsonb_path_ops | @?(jsonb,jsonpath) | 15 | search gin | jsonb_path_ops | @@(jsonb,jsonpath) | 16 | search (9 rows) so it seems like you ought to get some benefit for this query from just a plain GIN index on "snapshot". regards, tom lane