From 68d38d2ae36aa6c9293680c7af7c9539d60a45a8 Mon Sep 17 00:00:00 2001 From: Yurii Rashkovskii Date: Tue, 16 Jul 2024 09:56:29 -0700 Subject: [PATCH] Store the total number of plan nodes in `PlannedStmt` Extensions that use planner/executor hooks can't precisely pre-allocate memory for data structures mirroring the plan as the number of plan nodes is unknown. This information is effectively collected in the standard planner in the node ID counter but was discarded at the end. This change preserves this information. --- src/backend/optimizer/plan/planner.c | 1 + src/include/nodes/plannodes.h | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 4711f912390f6..0c4afbe272fe3 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -555,6 +555,7 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions, result->relationOids = glob->relationOids; result->invalItems = glob->invalItems; result->paramExecTypes = glob->paramExecTypes; + result->plan_nodes_count = glob->lastPlanNodeId + 1; /* utilityStmt should be null, but we might as well copy it */ result->utilityStmt = parse->utilityStmt; result->stmt_location = parse->stmt_location; diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h index 1aeeaec95e14c..0c405040bd2a0 100644 --- a/src/include/nodes/plannodes.h +++ b/src/include/nodes/plannodes.h @@ -97,6 +97,8 @@ typedef struct PlannedStmt /* statement location in source string (copied from Query) */ ParseLoc stmt_location; /* start location, or -1 if unknown */ ParseLoc stmt_len; /* length in bytes; 0 means "rest of string" */ + + int plan_nodes_count; /* indicates the number of plan nodes in the statement */ } PlannedStmt; /* macro for fetching the Plan associated with a SubPlan node */