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 1mOhF7-0005oc-3l for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Sep 2021 14:11:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mOhF3-0005IC-Tk for pgsql-hackers@arkaria.postgresql.org; Fri, 10 Sep 2021 14:10:57 +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 1mOhF3-0005I4-Ld for pgsql-hackers@lists.postgresql.org; Fri, 10 Sep 2021 14:10:57 +0000 Received: from oss.nttdata.com ([49.212.34.109]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mOhEv-0006nx-OB for pgsql-hackers@postgresql.org; Fri, 10 Sep 2021 14:10:57 +0000 Received: from oss.nttdata.com (localhost [127.0.0.1]) by oss.nttdata.com (Postfix) with ESMTPA id CA4366067A; Fri, 10 Sep 2021 23:10:43 +0900 (JST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.3 at oss.nttdata.com MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=_235512faf3b5f7f5d43b34d2102e73c2" Content-Transfer-Encoding: 7bit Date: Fri, 10 Sep 2021 23:10:43 +0900 From: torikoshia To: Peter Eisentraut Cc: Tom Lane , Pgsql Hackers Subject: Re: EXPLAIN(VERBOSE) to CTE with SEARCH BREADTH FIRST fails In-Reply-To: References: <5bafa66ad529e11860339565c9e7c166@oss.nttdata.com> <3208199.1631039493@sss.pgh.pa.us> User-Agent: Roundcube Webmail/1.4.11 Message-ID: X-Sender: torikoshia@oss.nttdata.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=_235512faf3b5f7f5d43b34d2102e73c2 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed On 2021-09-09 19:03, Peter Eisentraut wrote: > On 07.09.21 20:31, Tom Lane wrote: >> torikoshia writes: >>> While working on [1], we found that EXPLAIN(VERBOSE) to CTE with >>> SEARCH >>> BREADTH FIRST ends up ERROR. >> >> Yeah. It's failing here: >> >> * We're deparsing a Plan tree so we don't have a >> CTE >> * list. But the only place we'd see a Var >> directly >> * referencing a CTE RTE is in a CteScan plan >> node, and we >> * can look into the subplan's tlist instead. >> >> if (!dpns->inner_plan) >> elog(ERROR, "failed to find plan for CTE %s", >> rte->eref->aliasname); >> >> The problematic Var is *not* in a CteScan plan node; it's in a >> WorkTableScan node. It's not clear to me whether this is a bug >> in the planner's handling of SEARCH BREADTH FIRST, or if the plan >> is as-intended and ruleutils.c is failing to cope. > > The search clause is resolved by the rewriter, so it's unlikely that > the planner is doing something wrong. Either the rewriting produces > something incorrect (but then one might expect that the query results > would be wrong), or the structures constructed by rewriting are not > easily handled by ruleutils.c. > > If we start from the example in the documentation > : > > """ > WITH RECURSIVE search_tree(id, link, data, depth) AS ( > SELECT t.id, t.link, t.data, 0 > FROM tree t > UNION ALL > SELECT t.id, t.link, t.data, depth + 1 > FROM tree t, search_tree st > WHERE t.id = st.link > ) > SELECT * FROM search_tree ORDER BY depth; > > To get a stable sort, add data columns as secondary sorting columns. > """ > > In order to handle that part about the stable sort, the query > constructed internally is something like > > WITH RECURSIVE search_tree(id, link, data, seq) AS ( > SELECT t.id, t.link, t.data, ROW(0, id, link) > FROM tree t > UNION ALL > SELECT t.id, t.link, t.data, ROW(seq.depth + 1, id, link) > FROM tree t, search_tree st > WHERE t.id = st.link > ) > SELECT * FROM search_tree ORDER BY seq; > > The bit "seq.depth" isn't really valid when typed in like that, I > think, but of course internally this is all wired together with > numbers rather than identifiers. I suspect that that is what > ruleutils.c trips over. Thanks for your advice, it seems right. EXPLAIN VERBOSE can be output without error when I assigned testing purpose CoercionForm to 'seq.depth + 1'. I've attached the patch for the changes made for this test for your reference, but I'm not sure it's appropriate for creating a new CoercionForm to fix the issue.. -- Regards, -- Atsushi Torikoshi NTT DATA CORPORATION --=_235512faf3b5f7f5d43b34d2102e73c2 Content-Transfer-Encoding: base64 Content-Type: text/x-diff; name=v1-0001-fix_err_explain_verbose_cte_breadth.patch Content-Disposition: attachment; filename=v1-0001-fix_err_explain_verbose_cte_breadth.patch; size=1736 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL3Jld3JpdGUvcmV3cml0ZVNlYXJjaEN5Y2xlLmMgYi9z cmMvYmFja2VuZC9yZXdyaXRlL3Jld3JpdGVTZWFyY2hDeWNsZS5jCmluZGV4IGM1MGViZGJhMjQu LjY1OWE1YzQ0MGYgMTAwNjQ0Ci0tLSBhL3NyYy9iYWNrZW5kL3Jld3JpdGUvcmV3cml0ZVNlYXJj aEN5Y2xlLmMKKysrIGIvc3JjL2JhY2tlbmQvcmV3cml0ZS9yZXdyaXRlU2VhcmNoQ3ljbGUuYwpA QCAtNTA4LDcgKzUwOCw3IEBAIHJld3JpdGVTZWFyY2hBbmRDeWNsZShDb21tb25UYWJsZUV4cHIg KmN0ZSkKIAkJCWZzLT5yZXN1bHR0eXBlID0gSU5UOE9JRDsKIAkJCWZzLT5yZXN1bHR0eXBtb2Qg PSAtMTsKIAotCQkJZmV4cHIgPSBtYWtlRnVuY0V4cHIoRl9JTlQ4SU5DLCBJTlQ4T0lELCBsaXN0 X21ha2UxKGZzKSwgSW52YWxpZE9pZCwgSW52YWxpZE9pZCwgQ09FUkNFX0VYUExJQ0lUX0NBTEwp OworCQkJZmV4cHIgPSBtYWtlRnVuY0V4cHIoRl9JTlQ4SU5DLCBJTlQ4T0lELCBsaXN0X21ha2Ux KGZzKSwgSW52YWxpZE9pZCwgSW52YWxpZE9pZCwgQ09FUkNFX05PTUlOQUxfQ0FMTCk7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