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 1o0VYo-0002JS-Qk for pgsql-bugs@arkaria.postgresql.org; Sun, 12 Jun 2022 21:55:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o0VYn-0005FY-0d for pgsql-bugs@arkaria.postgresql.org; Sun, 12 Jun 2022 21:55:53 +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 1o0VYm-0005CA-Fz for pgsql-bugs@lists.postgresql.org; Sun, 12 Jun 2022 21:55:52 +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 1o0VYj-0003pz-T8 for pgsql-bugs@lists.postgresql.org; Sun, 12 Jun 2022 21:55:51 +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 25CLtlti3543019; Sun, 12 Jun 2022 17:55:47 -0400 From: Tom Lane To: Jeff Janes cc: Shaheed Haque , pgsql-bugs Subject: Re: Unable to make use of "deep" JSONB index In-reply-to: References: <967134.1654180260@sss.pgh.pa.us> Comments: In-reply-to Jeff Janes message dated "Sun, 12 Jun 2022 17:24:48 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3543017.1655070947.1@sss.pgh.pa.us> Date: Sun, 12 Jun 2022 17:55:47 -0400 Message-ID: <3543018.1655070947@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Jeff Janes writes: > On Sun, Jun 12, 2022 at 5:34 AM Shaheed Haque > wrote: >> OK, I have corrected and simplified the test case (including switching >> to a btree index). The WHERE clause and the inex now look like this: >> >> ...WHERE ((snapshot -> 'employee' -> '999' ->> >> 'pay_graph')::integer != 0); >> ...USING btree (((snapshot -> 'employee' -> '$.*' ->> >> 'pay_graph')::integer != 0)); > But, this is not a correction. You are still trying to use -> as if it > were @?, and that is still not going to work. In hopes of clarifying some more: all that index does is to record the boolean result of (snapshot -> 'employee' -> '$.*' ->> 'pay_graph')::integer != 0 at each row. We could use it for a query that contains *exactly* that condition as a WHERE clause. We cannot use it for a query that contains some other condition, even if that other condition looks related to you. > You are indexing the part of snapshot which has the employee number of > '$.*', which is a weird employee number for anyone to have. You might want > to represent a wildcard but that is not what -> does. Yeah, there's also the problem that the semantics of this particular expression aren't really useful. But even if they were, PG's index machinery is not smart enough to pick apart the contents of an index expression. If the index expression *exactly* matches some sub-expression of a WHERE clause, and what's above that sub-expression is an operator that's indexable according to the index opclass, then we have a chance of using it. This example is not that. regards, tom lane