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 1p5WRg-0006iX-Ez for pgsql-bugs@arkaria.postgresql.org; Wed, 14 Dec 2022 18:25:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p5WRf-00031P-Bp for pgsql-bugs@arkaria.postgresql.org; Wed, 14 Dec 2022 18:25:31 +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 1p5WRf-00031G-3w for pgsql-bugs@lists.postgresql.org; Wed, 14 Dec 2022 18:25:31 +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 1p5WRc-0007gT-U3 for pgsql-bugs@lists.postgresql.org; Wed, 14 Dec 2022 18:25:30 +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 2BEIPSH22784037; Wed, 14 Dec 2022 13:25:28 -0500 From: Tom Lane To: nh.dev@sturdy.ai cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17721: A completely unused CTE negatively affect Query Plan In-reply-to: <17721-a660b93948b165a7@postgresql.org> References: <17721-a660b93948b165a7@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Wed, 14 Dec 2022 17:41:37 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2784035.1671042328.1@sss.pgh.pa.us> Date: Wed, 14 Dec 2022 13:25:28 -0500 Message-ID: <2784036.1671042328@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: > I've boiled down an issue we have in production to a simple query that can > demonstrate it. I've run this on 13,14 and 15 locally in a docker container > with the same results. If a CTE that is completely unexecuted exists in a > subquery (or view in our production case) it affects the query plan VERY > negatively. The first explain shows a sequential scan, where the second > explain shows an index scan, just by the existence of the (obviously) > unexecuted CTE. The presence of the CTE prevents flattening of the subquery. If you don't like it, don't attach the CTE right there. regards, tom lane