Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r44jB-007FU8-0Z for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Nov 2023 19:42:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1r44j9-00FViR-Cj for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Nov 2023 19:42:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r44j9-00FViF-3R for pgsql-hackers@lists.postgresql.org; Fri, 17 Nov 2023 19:42:07 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r44j6-006x1D-SK for pgsql-hackers@lists.postgresql.org; Fri, 17 Nov 2023 19:42: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 3AHJg0XL754094; Fri, 17 Nov 2023 14:42:00 -0500 From: Tom Lane To: Richard Guo cc: Jian Guo , Tomas Vondra , Hans Buschmann , "pgsql-hackers@lists.postgresql.org" , Zhenghua Lyu Subject: Re: Wrong rows estimations with joins of CTEs slows queries by more than factor 500 In-reply-to: References: <4504e67078d648cdac3651b2960da6e7@nidsa.net> <46a7d950-7ca7-8ab9-76dd-f0bc7611c609@enterprisedb.com> <891440.1699483495@sss.pgh.pa.us> <414317.1700158568@sss.pgh.pa.us> <482957.1700192299@sss.pgh.pa.us> Comments: In-reply-to Richard Guo message dated "Fri, 17 Nov 2023 14:41:12 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <754092.1700250120.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Fri, 17 Nov 2023 14:42:00 -0500 Message-ID: <754093.1700250120@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Richard Guo writes: > On Fri, Nov 17, 2023 at 11:38 AM Tom Lane wrote: >> That line of argument also leads to the conclusion that it'd be >> okay to expose info about the ordering of the CTE result to the >> upper planner. This patch doesn't do that, and I'm not sufficiently >> excited about the issue to go write some code. But if someone else >> does, I think we shouldn't exclude doing it on the grounds of wanting >> to preserve an optimization fence. The fence is sort of one-way >> in this line of thinking: information can propagate up to the outer >> planner level, but not down into the CTE plan. > Exactly! Thanks for the detailed explanation. OK. I pushed the patch after a bit more review: we can simplify things some more by using the subroot->parse querytree for all tests. After the previous refactoring, it wasn't buying us anything to do some initial tests with the raw querytree. (The original idea of that, I believe, was to avoid doing find_base_rel if we could; but now that's not helpful.) regards, tom lane