From: Andrei Lepikhov <a.lepikhov@postgrespro.ru>
To: Richard Guo <guofenglinux@gmail.com>
To: Lepikhov Andrei <lepikhov@fastmail.com>
Cc: pgsql-bugs@lists.postgresql.org, Алена Рыбакина <a.rybakina@postgrespro.ru>
Cc: Дамир Белялов <d.belyalov@postgrespro.ru>
Subject: Re: EXPLAIN Verbose issue - custom_scan_tlist can directly refer CTE and Subquery
Date: Mon, 9 Oct 2023 15:41:09 +0700
Message-ID: <0cbbd87e-1b57-4b7e-9825-19a6fb2f8670@postgrespro.ru> (raw)
In-Reply-To: <ca3f78bc-9775-4c12-97b5-5e03282df3ea@postgrespro.ru>
References: <dfe47e78-f9c3-4f59-a345-591f7fd3ceec@app.fastmail.com>
<CAMbWs49A1VegrUX08on3WdH7b7t7FUE15JT4qk_vsQ_qfHMGhQ@mail.gmail.com>
<ca3f78bc-9775-4c12-97b5-5e03282df3ea@postgrespro.ru>
After some attempts to resolve this issue, I must admit that we should
add a non-trivial method in the CustomScan node interface, which would
be called in the ruleutils code.
I guess it is too much for such a rare situation. So maybe just add a
documentation entry on this thing and set a couple of assertions on
early detection of problems?.
In the attachment - is an offer on how to change the code.
--
regards,
Andrey Lepikhov
Postgres Professional
Attachments:0001-Be-careful-allowing-custom-nodes-to-use-CTE-and-Subp.patch (text/plain, 3.0K) download
From d3b4761a53d8817922b0a6a7aae0fc19c70f28df Mon Sep 17 00:00:00 2001
From: "Andrey V. Lepikhov" <a.lepikhov@postgrespro.ru>
Date: Mon, 9 Oct 2023 15:31:04 +0700
Subject: [PATCH] Be careful allowing custom nodes to use CTE and Subplan links
in the targetlist. Add a paragraph in documentation, comment in the place of
the error triggers and assert to quickly detect problem at the developing
stage.
---
doc/src/sgml/custom-scan.sgml | 4 ++++
src/backend/utils/adt/ruleutils.c | 20 ++++++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/doc/src/sgml/custom-scan.sgml b/doc/src/sgml/custom-scan.sgmlindex a200d502cd..0bc65cbd06 100644--- a/doc/src/sgml/custom-scan.sgml+++ b/doc/src/sgml/custom-scan.sgml@@ -206,6 +206,10 @@ typedef struct CustomScan
the actual scan tuples. <structfield>custom_scan_tlist</structfield> must be
provided for joins, and could be provided for scans if the custom scan
provider can compute some non-Var expressions.
+ Don't include into <structfield>custom_scan_tlist</structfield> references to CTE+ or SubPlan entries because it directly points to the list of RangeTableEntries.+ In most cases it doesn't cause any issues but during the EXPLAIN ANALYZE it can fail+ to find proper subplan during preparing textual representation of targetlist.
<structfield>custom_relids</structfield> is set by the core code to the set of
relations (range table indexes) that this scan node handles; except when
this scan is replacing a join, it will have only one member.
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.cindex 442205382e..a60268dcac 100644--- a/src/backend/utils/adt/ruleutils.c+++ b/src/backend/utils/adt/ruleutils.c@@ -7860,6 +7860,16 @@ get_name_for_var_field(Var *var, int fieldno,
deparse_namespace save_dpns;
const char *result;
+ /*+ * Limitation, related to CustomScan nodes.+ * custom_scan_tlist points to the range table entries+ * directly. In the case of CTEs and subqueries we have some+ * sort of uncertainity here: only the developer of the+ * custom node knows from which subtree they get the subplan+ * link.+ */+ Assert(!IsA(dpns->plan, CustomScan));+
if (!dpns->inner_plan)
elog(ERROR, "failed to find plan for subquery %s",
rte->eref->aliasname);
@@ -7982,6 +7992,16 @@ get_name_for_var_field(Var *var, int fieldno,
deparse_namespace save_dpns;
const char *result;
+ /*+ * Limitation, related to CustomScan nodes.+ * custom_scan_tlist points to the range table entries+ * directly. In the case of CTEs and subqueries we have some+ * sort of uncertainity here: only the developer of the+ * custom node knows from which subtree they get the subplan+ * link.+ */+ Assert(!IsA(dpns->plan, CustomScan));+
if (!dpns->inner_plan)
elog(ERROR, "failed to find plan for CTE %s",
rte->eref->aliasname);
--
2.42.0
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: a.lepikhov@postgrespro.ru, guofenglinux@gmail.com, lepikhov@fastmail.com, a.rybakina@postgrespro.ru, d.belyalov@postgrespro.ru
Subject: Re: EXPLAIN Verbose issue - custom_scan_tlist can directly refer CTE and Subquery
In-Reply-To: <0cbbd87e-1b57-4b7e-9825-19a6fb2f8670@postgrespro.ru>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox