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 1jnUUu-0000AQ-GL for pgsql-general@arkaria.postgresql.org; Mon, 22 Jun 2020 22:01:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jnUUt-0002uU-Al for pgsql-general@arkaria.postgresql.org; Mon, 22 Jun 2020 22:00:59 +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 1jnUUt-0002e6-00 for pgsql-general@lists.postgresql.org; Mon, 22 Jun 2020 22:00:59 +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 1jnUUq-0003s6-ST for pgsql-general@lists.postgresql.org; Mon, 22 Jun 2020 22:00:57 +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 05MM0sOs2093792; Mon, 22 Jun 2020 18:00:54 -0400 From: Tom Lane To: Michael Lewis cc: Sankar P , PostgreSQL General Subject: Re: DISTINCT on jsonb fields and Indexes In-reply-to: References: <2081356.1592845514@sss.pgh.pa.us> Comments: In-reply-to Michael Lewis message dated "Mon, 22 Jun 2020 14:10:19 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2093790.1592863254.1@sss.pgh.pa.us> Date: Mon, 22 Jun 2020 18:00:54 -0400 Message-ID: <2093791.1592863254@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Michael Lewis writes: >> In the example of "select distinct expression", the planner will never >> notice that that expression has anything to do with an index. > Thanks for that explanation. I assume re-writing as a 'group by' would have > no bearing on that planner decision. Hmm ... actually, now that you mention it, it might. Using GROUP BY moves the expression into the category of "things we might want to sort on", and we do pay attention to indexes for that. Trying it just now, I find that I can get a index scan but not an index-only scan for something like "select expr from tab group by expr". Not sure if that will move the needle enough to help for the original problem. regards, tom lane