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 1qffi0-00Ceh7-Ni for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Sep 2023 12:08:05 +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 1qffhz-000Kjj-HZ for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Sep 2023 12:08:03 +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 1qfcqJ-00GGjQ-Nj for pgsql-bugs@lists.postgresql.org; Mon, 11 Sep 2023 09:04:28 +0000 Received: from new4-smtp.messagingengine.com ([66.111.4.230]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qfcqE-004Q06-CA for pgsql-bugs@lists.postgresql.org; Mon, 11 Sep 2023 09:04:26 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailnew.nyi.internal (Postfix) with ESMTP id 843D5580A4B; Mon, 11 Sep 2023 05:04:19 -0400 (EDT) Received: from imap53 ([10.202.2.103]) by compute1.internal (MEProxy); Mon, 11 Sep 2023 05:04:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastmail.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm1; t= 1694423059; x=1694423659; bh=bP4DCi1404pjTcj4tDkpLiafxFg1g5CBEVP 8Gq6ROtY=; b=oK9s0wPqovva3uwT51+dWG0N1yhq5WHqLgO4VSPSNm7j45otAvX UL/6z6amRSoOfNb0AJPV1todxqw8jXpNiIB4Tz/Y1TUSl8HhifHdZbbmSn7R5sK7 nI4cC8XPsZH414+886ltm4RMWqa9ZcBwgNiz7RVvKzATVUEvCPCLi1EUHK6cuWxz oPnys78sOAqKcc7E0TSjqQEAQ3guZ3hIz/sANfXX6iWYIWJEu+yFv/u1xO8uBWYu xf2BlSWK/JRCuBBTtBg8yH7ebNEJ9jKhV8qFY92zfga2Mizo0aWkrMToQWFea68U Y2iFQyvbCfDPKrPjJdFkqkWt+AI8LPYSAwg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=i80a949e3.fm1; t=1694423059; x=1694423659; bh=bP4DCi1404pjTcj4tDkpLiafxFg1g5CB EVP8Gq6ROtY=; b=ihVKI1I8lISPFxb0DfSwpO0mHDNAOpQwZPKrcrBEIZkMg/d7 qoAXnFvNvgqA5ct2pfe/m6zhOj6IFGP9vqzLEw8EQnL0We8KZoW2jHMJh3MGPyRt XsDLb8ML8WJJ6HGnT9WDqjFLbYsLSIwqWuyH6kL76rMAncPtPv0642Vv6NL+UVQ2 aSFLolbMmCOmY4WH5K5WuGHTbZiYG6/TYERvLm/B1v3wxjZXjWy1ObLmQcQRybv3 QNziKCgfierkSsoY6w20tsV2MpUGeTptybD4jz8jnrZz8Dz7UU+JEG1xkMXQstmJ II/guNVNNE2kDdYyCEzJ9PIBgXKSIyhpBZfimA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrudeigedguddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvvefutgfgse htqhertderreejnecuhfhrohhmpedfnfgvphhikhhhohhvucetnhgurhgvihdfuceolhgv phhikhhhohhvsehfrghsthhmrghilhdrtghomheqnecuggftrfgrthhtvghrnhepkeekfe etieejteelgeevgfeihfeihfevtdehtdfhgfevuedtjeduffelgeevffefnecuvehluhhs thgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgvphhikhhhohhvse hfrghsthhmrghilhdrtghomh X-ME-Proxy: Feedback-ID: i80a949e3:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id BEDA388005E; Mon, 11 Sep 2023 05:04:18 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-711-g440737448e-fm-20230828.001-g44073744 Mime-Version: 1.0 Message-Id: In-Reply-To: References: Date: Mon, 11 Sep 2023 16:03:58 +0700 From: "Lepikhov Andrei" To: "Richard Guo" Cc: pgsql-bugs@lists.postgresql.org, tgl@sss.pgh.pa.us, =?UTF-8?Q?=D0=90=D0=BB=D0=B5=D0=BD=D0=B0_=D0=A0=D1=8B=D0=B1=D0=B0=D0=BA?= =?UTF-8?Q?=D0=B8=D0=BD=D0=B0?= , =?UTF-8?Q?=D0=94=D0=B0=D0=BC=D0=B8=D1=80_=D0=91=D0=B5=D0=BB=D1=8F=D0=BB?= =?UTF-8?Q?=D0=BE=D0=B2?= Subject: Re: EXPLAIN Verbose issue - custom_scan_tlist can directly refer CTE and Subquery Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Sep 11, 2023, at 1:28 PM, Richard Guo wrote: > On Tue, Sep 5, 2023 at 3:23=E2=80=AFPM Lepikhov Andrei wrote: >> Hi, >>=20 >> While designing a CustomScan node, I got stuck into two errors: >> 1. "failed to find plan for CTE." >> 2. "failed to find plan for subquery." >> After a short research, I found commit 3f50b82, which shows the probl= em's origins - setrefs don't change the varno of custom_scan_tlist and c= an directly reference CTE or Subquery entry. In the "EXPLAIN VERBOSE" ca= se, the deparsing routine can't find dpns->inner_plan for such an entry. > > I was able to reproduce both errors with the help of the query in [1] > and the extension provided in [2]. It seems that the assumption in the > case of RTE_SUBQUERY and RTE_CTE in get_name_for_var_field() does not > always hold: > > * the only place we'd see a Var directly referencing a > * SUBQUERY RTE is in a SubqueryScan plan node > > * the only places we'd see a Var directly > * referencing a CTE RTE are in CteScan or WorkTableScan > * plan nodes. I have written the letter with second thoughts, because the logic of bui= lding CustomScan target list isn't clear for me. Maybe I just have made = a mistake in my code? =20 > > But this issue shows that in a CustomScan node we can also see a Var > directly referencing a SUBQUERY RTE or CTE RTE. (I suspect that it al= so > happens with ForeignScan node.) Maybe, but I couldn't imagine such a situation. > > So it seems that we need to assign a proper INNER referent for > CustomScan node in set_deparse_plan(). I tried 'trick.diff' in [1] > which uses linitial(dpns->subplans), it fixes the query there but would > crash the query below. I see here two different cases. Direct link to a Subquery need rte->subq= uery, which was nullified due to optimization. The case with reference t= o CTE is more complex. which subplan of the statement subplans should we= refer here? But it is my first glance into this code, maybe someone und= erstand it better. --=20 Regards, Andrei Lepikhov