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 1rMsqZ-007P4R-9r for pgsql-hackers@arkaria.postgresql.org; Mon, 08 Jan 2024 16:51:31 +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 1rMsqY-006My9-4l for pgsql-hackers@arkaria.postgresql.org; Mon, 08 Jan 2024 16:51:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rMsqX-006My0-Rp for pgsql-hackers@lists.postgresql.org; Mon, 08 Jan 2024 16:51:29 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rMsqV-000Vrm-Es for pgsql-hackers@lists.postgresql.org; Mon, 08 Jan 2024 16:51:28 +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 408GpM2e1941516; Mon, 8 Jan 2024 11:51:22 -0500 From: Tom Lane To: Richard Guo cc: Alexander Lakhin , 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> <754093.1700250120@sss.pgh.pa.us> <608a4886-6c60-0f9e-97d5-591256bd4150@gmail.com> <1450797.1704580874@sss.pgh.pa.us> Comments: In-reply-to Richard Guo message dated "Mon, 08 Jan 2024 19:14:11 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1941514.1704732682.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Mon, 08 Jan 2024 11:51:22 -0500 Message-ID: <1941515.1704732682@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Richard Guo writes: > On Sun, Jan 7, 2024 at 6:41 AM Tom Lane wrote: >> Thanks for the report! I guess we need something like the attached. > +1. Pushed, thanks for looking at it. >> I'm surprised that this hasn't been noticed before; was the case >> really unreachable before? > It seems that this case is only reachable with Vars of an INSERT target > relation, and it seems that there is no other way to reference such a > Var other than using CTE. I'm a little uncomfortable with that conclusion, but for the moment I refrained from back-patching. We can always add the patch to v16 later if we find it's not so unreachable. (Before v16, there was no find_base_rel here at all.) regards, tom lane