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 1odzfc-0000hw-AF for pgsql-bugs@arkaria.postgresql.org; Thu, 29 Sep 2022 19:58:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1odzfb-0001t7-4o for pgsql-bugs@arkaria.postgresql.org; Thu, 29 Sep 2022 19:58:07 +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 1odzfa-0001sy-TC for pgsql-bugs@lists.postgresql.org; Thu, 29 Sep 2022 19:58:06 +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 1odzfY-00073s-T0 for pgsql-bugs@lists.postgresql.org; Thu, 29 Sep 2022 19:58:06 +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 28TJw2Ym347705; Thu, 29 Sep 2022 15:58:02 -0400 From: Tom Lane To: Malay Keshav cc: pgsql-bugs@lists.postgresql.org, "malay.keshav@gmail.com" Subject: Re: [Bug][Ver 11]: Generic query plan selected is worse than custom query plan In-reply-to: References: Comments: In-reply-to Malay Keshav message dated "Thu, 29 Sep 2022 11:33:54 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <347703.1664481482.1@sss.pgh.pa.us> Date: Thu, 29 Sep 2022 15:58:02 -0400 Message-ID: <347704.1664481482@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Malay Keshav writes: > In our particular case, the Postgres engine decided to pick the generic > query plan and cache it for all further calls with that query. My > understanding was that the generic query plan would only be selected if it > had a better execution time than the custom query plan. Which in our case > is not true. > We were able to reproduce this deterministically using the same query > parameters to trigger the engine to pick the bad generic query plan on the > 6th run (first 5 runs shows the engine used the efficient query plan). Why > does the engine pick the generic query plan when its execution time is > worse than the custom query plan? Is this a bug? Since you've provided precisely zero detail, it's impossible to answer in any detail, but this is unlikely to be anything we'd call a bug. What's presumably happening is that the planner's estimate of the cost of the generic plan comes out less than its estimate of the cost of a custom plan, but that estimate is faulty. Sure, we'd love to improve all such situations, but it's not an easy task --- and any changes in that area would not be something we'd consider back-patching into long-stable branches. regards, tom lane