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 1mWJ2O-0002yP-Dm for pgsql-bugs@arkaria.postgresql.org; Fri, 01 Oct 2021 13:57: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 1mWJ2N-0005M4-Ci for pgsql-bugs@arkaria.postgresql.org; Fri, 01 Oct 2021 13:57: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 1mWJ1t-0001Dg-9M for pgsql-bugs@lists.postgresql.org; Fri, 01 Oct 2021 13:56:49 +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 1mWJ1r-0008RX-11 for pgsql-bugs@lists.postgresql.org; Fri, 01 Oct 2021 13:56:48 +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 191DujvM3312648; Fri, 1 Oct 2021 09:56:45 -0400 From: Tom Lane To: sema@sema.in cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17207: Bad cost estimate of Merge Join despite correct row estimate In-reply-to: <17207-5265aefa79e333b4@postgresql.org> References: <17207-5265aefa79e333b4@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Thu, 30 Sep 2021 23:29:48 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3312646.1633096605.1@sss.pgh.pa.us> Date: Fri, 01 Oct 2021 09:56:45 -0400 Message-ID: <3312647.1633096605@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > [ seriously awful mergejoin cost estimate ] > While it is possible for Merge Join to also finish quickly - if user_id are > all low numbers - I'm not sure if that's a realistic expectation for the > default plan. Yeah, it looks like it's incorrectly adjusting based on a belief that it won't have to scan all of the join input. I found that the problem only appears in v11 and later; earlier versions come out with a more realistic estimate of the cost of the merge. "git bisect" says it changed at 7d08ce286cd5854d58152e428c28636a616bdc42 is the first bad commit commit 7d08ce286cd5854d58152e428c28636a616bdc42 Author: Tom Lane Date: Wed Sep 13 11:12:39 2017 -0400 Distinguish selectivity of < from <= and > from >=. I've not yet figured out what the connection is, but a reasonable bet is that there's an edge case associated with the ranges of the two join keys being exactly the same. Anyway, I'll find it and fix it. Thanks for the report! regards, tom lane