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 1o9HxG-00029s-RR for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Jul 2022 03:13:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o9HxF-0002mT-Nx for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Jul 2022 03:13:25 +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 1o9HxF-0002mK-G4 for pgsql-bugs@lists.postgresql.org; Thu, 07 Jul 2022 03:13:25 +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 1o9HxD-0000iZ-Fn for pgsql-bugs@lists.postgresql.org; Thu, 07 Jul 2022 03:13:25 +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 2673DIjI2733256; Wed, 6 Jul 2022 23:13:18 -0400 From: Tom Lane To: Andres Freund cc: "David G. Johnston" , william.duclot@gmail.com, PostgreSQL mailing lists Subject: Re: BUG #17540: Prepared statement: PG switches to a generic query plan which is consistently much slower In-reply-to: <20220707004646.jaowhhj4g5okwu4j@alap3.anarazel.de> References: <17540-7aa1855ad5ec18b4@postgresql.org> <20220707004646.jaowhhj4g5okwu4j@alap3.anarazel.de> Comments: In-reply-to Andres Freund message dated "Wed, 06 Jul 2022 17:46:46 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2733254.1657163598.1@sss.pgh.pa.us> Date: Wed, 06 Jul 2022 23:13:18 -0400 Message-ID: <2733255.1657163598@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Andres Freund writes: > I think the cost for the slow plan being so much cheaper can almost be > qualified as bug. > The slow plan seems pretty nonsensical to me. ISTM that something in the > costing there is at least almost broken. I think this is probably an instance of the known problem that a generic plan is made without knowledge of the actual parameter values, and that can lead us to make statistical assumptions that are not valid for the actual values, but nonetheless make one plan look cheaper than another even though the opposite is true given the actual values. In essence, comparing the cost estimate for the generic plan to the cost estimate for a custom plan is not really logically valid, because those estimates are founded on different statistics. I don't know how to fix that :-(. regards, tom lane