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 1leUgu-0004Zp-My for pgsql-bugs@arkaria.postgresql.org; Thu, 06 May 2021 03:28:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1leUgr-00042M-PU for pgsql-bugs@arkaria.postgresql.org; Thu, 06 May 2021 03:28:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1leUgr-00042D-If for pgsql-bugs@lists.postgresql.org; Thu, 06 May 2021 03:28:41 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1leUgn-0004jj-Vg for pgsql-bugs@lists.postgresql.org; Thu, 06 May 2021 03:28:40 +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 1463SZRk4113745; Wed, 5 May 2021 23:28:35 -0400 From: Tom Lane To: David Rowley cc: a.schnabl@synedra.com, PostgreSQL mailing lists Subject: Re: BUG #16993: Query Planner does not use index for EXISTS-query on a large table with all equal foreign key values In-reply-to: References: <16993-1298e8dc9c41ab97@postgresql.org> <4035793.1620236861@sss.pgh.pa.us> Comments: In-reply-to David Rowley message dated "Thu, 06 May 2021 14:13:04 +1200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4113741.1620271715.1@sss.pgh.pa.us> Date: Wed, 05 May 2021 23:28:35 -0400 Message-ID: <4113744.1620271715@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Rowley writes: > On Thu, 6 May 2021 at 05:47, Tom Lane wrote: >> A narrower fix would be to hack var_eq_non_const so that it doesn't >> assume that the comparison value must be one of the entries in the >> column. But it seems like whatever change we made in that line would >> be a very unprincipled hack, because what are you going to assume >> instead? > Yeah, this is the same problem as I was mentioning in [1] > My solution was to go for that "unprincipled hack" in var_eq_non_const(). > I'm not sure I 100% agree that it's a complete hack, you don't really > have to change the n_distinct by much to get the good plan. It's a > massive risk to assume that the given value will *always* be the > single distinct value that's indexed. Yeah, I agree that it doesn't seem great to let var_eq_non_const return 1.0 when it has no idea what the comparison value is. However, that doesn't translate to having much confidence in any other value either. The actual number-of-rows-fetched, given that we know the column contents are all the same value, is either zero or the whole table. It's hard to do much with that; and biasing it towards believing the optimistic value over the pessimistic value seems dangerous. In any case, I think the real issue here is that we know that *in use*, the indexscan will fetch either zero or one row, and be pretty quick either way. The problem is that that knowledge is being applied at the join level, which is too late to save the path from losing. How could we move that knowledge down to the scan path costs? regards, tom lane