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 1lcU8x-00044A-2P for pgsql-general@arkaria.postgresql.org; Fri, 30 Apr 2021 14:29:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lcU8v-0003Ye-BU for pgsql-general@arkaria.postgresql.org; Fri, 30 Apr 2021 14:29:21 +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 1lcU8v-0003YP-3b for pgsql-general@lists.postgresql.org; Fri, 30 Apr 2021 14:29:21 +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 1lcU8o-0004d2-Tk for pgsql-general@lists.postgresql.org; Fri, 30 Apr 2021 14:29:20 +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 13UETC7w3257727; Fri, 30 Apr 2021 10:29:12 -0400 From: Tom Lane To: Maarten Mortier cc: pgsql-general@lists.postgresql.org Subject: Re: "Skipping" BitmapHeapScan for simple counts In-reply-to: References: Comments: In-reply-to Maarten Mortier message dated "Fri, 30 Apr 2021 09:38:28 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3257725.1619792952.1@sss.pgh.pa.us> Date: Fri, 30 Apr 2021 10:29:12 -0400 Message-ID: <3257726.1619792952@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Maarten Mortier writes: > We have a jsonb_ops GIN index on this field, because we query the following > "freeform" match: > SELECT COUNT(id) > FROM records > WHERE data @@ '$.**.label == "person"'; > However, in the case of jsonb queries above, there is no need to do the > BitmapHeapScan really: the BitmapIndexScan has the correct row count, > always. This claim is wrong (in general) because of the need to verify visibility of the heap row. You might be able to look aside at the visibility map and find out that a particular heap page is all-visible, but unless the table is mostly static that can easily be a net waste of cycles. The reason we make a distinction between plain indexscans and index-only scans is to encode in the plan whether the planner thought such checks were likely to be a win. To transpose the idea into bitmapscans, you'd need similar infrastructure, which is not going to be a hack-it-in-one- place project. More to the point: I doubt this will help at all for the query above, because I doubt that the index is lossless for this query. From what I recall of GIN for jsonb, what it will return is TIDs for tuples that contain both "label" and "person". Whether they have the relationship of "person" being the value of a "label" tag can't be told without looking at the actual JSON value. regards, tom lane