From 8c49a484e8ebb0199fba4bd68eaaedaf49b48ed0 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Tue, 7 Oct 2025 09:18:54 -0400 Subject: Assign each subquery a unique name prior to planning it. Previously, subqueries were given names only after they were planned, which makes it difficult to use information from a previous execution of the query to guide future planning. If, for example, you knew something about how you want "InitPlan 2" to be planned, you won't know whether the subquery you're currently planning will end up being "InitPlan 2" until after you've finished planning it, by which point it's too late to use the information that you had. To fix this, assign each subplan a unique name before we begin planning it. To improve consistency, use textual names for all subplans, rather than, as we did previously, a mix of numbers (such as "InitPlan 1") and names (such as "CTE foo"), and make sure that the same name is never assigned more than once. We adopt the somewhat arbitrary convention of using the type of sublink to set the plan name; for example, a query that previously had two expression sublinks shown as InitPlan 2 and InitPlan 1 will now end up named expr_1 and expr_2. Because names are assigned before rather than after planning, some of the regression test outputs show the numerical part of the name switching positions: what was previously SubPlan 2 was actually the first one encountered, but we finished planning it later. We assign names even to subqueries that aren't shown as such within the EXPLAIN output. These include subqueries that are a FROM clause item or a branch of a set operation, rather than something that will be turned into an InitPlan or SubPlan. The purpose of this is to make sure that, below the topmost query level, there's always a name for each subquery that is stable from one planning cycle to the next (assuming no changes to the query or the database schema). Author: Robert Haas Co-authored-by: Tom Lane Reviewed-by: Alexandra Wang Reviewed-by: Richard Guo Reviewed-by: Tom Lane Reviewed-by: Junwang Zhao Discussion: http://postgr.es/m/3641043.1758751399@sss.pgh.pa.us --- src/backend/commands/explain.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/backend/commands/explain.c') diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index 207f86f1d39..06191cd8a85 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -4901,6 +4901,7 @@ ExplainSubPlans(List *plans, List *ancestors, { SubPlanState *sps = (SubPlanState *) lfirst(lst); SubPlan *sp = sps->subplan; + char *cooked_plan_name; /* * There can be multiple SubPlan nodes referencing the same physical @@ -4924,8 +4925,20 @@ ExplainSubPlans(List *plans, List *ancestors, */ ancestors = lcons(sp, ancestors); + /* + * The plan has a name like exists_1 or rowcompare_2, but here we want + * to prefix that with CTE, InitPlan, or SubPlan, as appropriate, for + * display purposes. + */ + if (sp->subLinkType == CTE_SUBLINK) + cooked_plan_name = psprintf("CTE %s", sp->plan_name); + else if (sp->isInitPlan) + cooked_plan_name = psprintf("InitPlan %s", sp->plan_name); + else + cooked_plan_name = psprintf("SubPlan %s", sp->plan_name); + ExplainNode(sps->planstate, ancestors, - relationship, sp->plan_name, es); + relationship, cooked_plan_name, es); ancestors = list_delete_first(ancestors); } -- cgit v1.2.3