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 1jnPsm-0005uk-Mb for pgsql-general@arkaria.postgresql.org; Mon, 22 Jun 2020 17:05:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jnPsl-0000GP-JO for pgsql-general@arkaria.postgresql.org; Mon, 22 Jun 2020 17:05:19 +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 1jnPsl-0000E4-Bz for pgsql-general@lists.postgresql.org; Mon, 22 Jun 2020 17:05:19 +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 1jnPsj-0001Iz-6v for pgsql-general@lists.postgresql.org; Mon, 22 Jun 2020 17:05:18 +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 05MH5EPk2081357; Mon, 22 Jun 2020 13:05:14 -0400 From: Tom Lane To: Michael Lewis cc: Sankar P , PostgreSQL General Subject: Re: DISTINCT on jsonb fields and Indexes In-reply-to: References: Comments: In-reply-to Michael Lewis message dated "Mon, 22 Jun 2020 10:45:44 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2081355.1592845514.1@sss.pgh.pa.us> Date: Mon, 22 Jun 2020 13:05:14 -0400 Message-ID: <2081356.1592845514@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Michael Lewis writes: > On Sun, Jun 21, 2020 at 10:43 PM Sankar P > wrote: >> 2) CREATE INDEX idx_btree_records ON fluent USING BTREE ((record ->> >> 'destinationServiceName')); > If it is rarely used, create a partial index perhaps. I am a little > surprised that the plain btree index wasn't used from my naive point of > view. It's not terribly surprising unfortunately. The planner will seldom recognize that an expression index is good for anything except searches, that is "WHERE indexed_expression indexable_operator constant". There's some mention of this at https://www.postgresql.org/docs/current/indexes-index-only-scans.html The core of the problem is not wanting to expend cycles on trying to match every subexpression in the query to every index expression; so at least in early planning stages, only potentially-indexable subexpressions of WHERE clauses get matched to indexes. In the example of "select distinct expression", the planner will never notice that that expression has anything to do with an index. regards, tom lane