1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

Change EXPLAIN output so that subplans and initplans (particularly CTEs)

are individually labeled, rather than just grouped under an "InitPlan"
or "SubPlan" heading.  This in turn makes it possible for decompilation of
a subplan reference to usefully identify which subplan it's referencing.
I also made InitPlans identify which parameter symbol(s) they compute,
so that references to those parameters elsewhere in the plan tree can
be connected to the initplan that will be executed.  Per a gripe from
Robert Haas about EXPLAIN output of a WITH query being inadequate,
plus some longstanding pet peeves of my own.
This commit is contained in:
Tom Lane
2009-04-05 19:59:40 +00:00
parent 329a5322e9
commit fbcce08046
7 changed files with 82 additions and 22 deletions

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.147 2009/03/10 22:09:26 tgl Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.148 2009/04/05 19:59:40 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -412,8 +412,8 @@ build_subplan(PlannerInfo *root, Plan *plan, List *rtable,
int paramid;
/*
* Initialize the SubPlan node. Note plan_id isn't set till further down,
* likewise the cost fields.
* Initialize the SubPlan node. Note plan_id, plan_name, and cost fields
* are set further down.
*/
splan = makeNode(SubPlan);
splan->subLinkType = subLinkType;
@ -606,6 +606,30 @@ build_subplan(PlannerInfo *root, Plan *plan, List *rtable,
root->glob->rewindPlanIDs = bms_add_member(root->glob->rewindPlanIDs,
splan->plan_id);
/* Label the subplan for EXPLAIN purposes */
if (isInitPlan)
{
ListCell *lc;
int offset;
splan->plan_name = palloc(32 + 12 * list_length(splan->setParam));
sprintf(splan->plan_name, "InitPlan %d (returns ", splan->plan_id);
offset = strlen(splan->plan_name);
foreach(lc, splan->setParam)
{
sprintf(splan->plan_name + offset, "$%d%s",
lfirst_int(lc),
lnext(lc) ? "," : "");
offset += strlen(splan->plan_name + offset);
}
sprintf(splan->plan_name + offset, ")");
}
else
{
splan->plan_name = palloc(32);
sprintf(splan->plan_name, "SubPlan %d", splan->plan_id);
}
/* Lastly, fill in the cost estimates for use later */
cost_subplan(root, splan, plan);
@ -875,7 +899,7 @@ SS_process_ctes(PlannerInfo *root)
* Make a SubPlan node for it. This is just enough unlike
* build_subplan that we can't share code.
*
* Note plan_id isn't set till further down, likewise the cost fields.
* Note plan_id, plan_name, and cost fields are set further down.
*/
splan = makeNode(SubPlan);
splan->subLinkType = CTE_SUBLINK;
@ -931,6 +955,10 @@ SS_process_ctes(PlannerInfo *root)
root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
/* Label the subplan for EXPLAIN purposes */
splan->plan_name = palloc(4 + strlen(cte->ctename) + 1);
sprintf(splan->plan_name, "CTE %s", cte->ctename);
/* Lastly, fill in the cost estimates for use later */
cost_subplan(root, splan, plan);
}
@ -2134,5 +2162,10 @@ SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
prm = generate_new_param(root, resulttype, resulttypmod);
node->setParam = list_make1_int(prm->paramid);
/* Label the subplan for EXPLAIN purposes */
node->plan_name = palloc(64);
sprintf(node->plan_name, "InitPlan %d (returns $%d)",
node->plan_id, prm->paramid);
return prm;
}