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 1oiFnb-0002aT-1K for pgsql-general@arkaria.postgresql.org; Tue, 11 Oct 2022 13:59:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oiFnZ-00015N-If for pgsql-general@arkaria.postgresql.org; Tue, 11 Oct 2022 13:59:57 +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 1oiFnZ-000147-7p for pgsql-general@lists.postgresql.org; Tue, 11 Oct 2022 13:59:57 +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 1oiFnX-0001uY-42 for pgsql-general@lists.postgresql.org; Tue, 11 Oct 2022 13:59:56 +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 29BDxhvT299200; Tue, 11 Oct 2022 09:59:43 -0400 From: Tom Lane To: David Rowley cc: gzh , pgsql-general@lists.postgresql.org Subject: Re: Different execution plan between PostgreSQL 8.4 and 12.11 In-reply-to: References: <23fe7e4b.5866.183bc03584f.Coremail.gzhcoder@126.com> <3810464.1665331329@sss.pgh.pa.us> <3bb0d13f.1d81.183bfbcf7e4.Coremail.gzhcoder@126.com> <3977622.1665369954@sss.pgh.pa.us> <5cac5c06.25be.183c507f02f.Coremail.gzhcoder@126.com> Comments: In-reply-to David Rowley message dated "Tue, 11 Oct 2022 23:53:36 +1300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <299198.1665496783.1@sss.pgh.pa.us> Date: Tue, 11 Oct 2022 09:59:43 -0400 Message-ID: <299199.1665496783@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Rowley writes: > It feels like something is a bit lacking in our cost model here. I'm > just not sure what that is. The example you show is the same old problem that we've understood for decades: for cost-estimation purposes, we assume that matching rows are more or less evenly distributed in the table. Their actual location doesn't matter that much if you're scanning the whole table; but if you're hoping that a LIMIT will be able to stop after scanning just a few rows, it does matter. While it'd be pretty easy to insert some ad-hoc penalty into the LIMIT estimation to reduce the chance of being fooled this way, that would also discourage us from using fast-start plans when they *do* help. So I don't see any easy fix. regards, tom lane