From 3d2036d97b2f1232b476047a70f5870467c10aaa Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Tue, 15 Jul 2025 13:17:49 -0400 Subject: [PATCH] context->walker --- contrib/pg_plan_advice/pg_plan_advice.c | 12 +++--- contrib/pg_plan_advice/pgpa_join.c | 18 ++++----- contrib/pg_plan_advice/pgpa_join.h | 2 +- contrib/pg_plan_advice/pgpa_walker.c | 53 ++++++++++++------------- contrib/pg_plan_advice/pgpa_walker.h | 4 +- 5 files changed, 44 insertions(+), 45 deletions(-) diff --git a/contrib/pg_plan_advice/pg_plan_advice.c b/contrib/pg_plan_advice/pg_plan_advice.c index 992e711b91..03d8e584c4 100644 --- a/contrib/pg_plan_advice/pg_plan_advice.c +++ b/contrib/pg_plan_advice/pg_plan_advice.c @@ -311,7 +311,7 @@ pg_plan_advice_explain_per_plan_hook(PlannedStmt *plannedstmt, static char * pg_plan_advice_generate(PlannedStmt *pstmt) { - pgpa_plan_walker_context context; + pgpa_plan_walker_context walker; StringInfoData buf; ListCell *lc; const char **rt_identifiers; @@ -320,11 +320,11 @@ pg_plan_advice_generate(PlannedStmt *pstmt) rt_identifiers = pgpa_create_identifiers_for_planned_stmt(pstmt); /* Initialization. */ - memset(&context, 0, sizeof(pgpa_plan_walker_context)); - context.pstmt = pstmt; + memset(&walker, 0, sizeof(pgpa_plan_walker_context)); + walker.pstmt = pstmt; /* Walk the main plan tree. */ - pgpa_plan_walker(&context, pstmt->planTree, 0, NULL, NIL); + pgpa_plan_walker(&walker, pstmt->planTree, 0, NULL, NIL); /* Main plan tree walk won't reach subplans, so walk those. */ foreach(lc, pstmt->subplans) @@ -332,11 +332,11 @@ pg_plan_advice_generate(PlannedStmt *pstmt) Plan *plan = lfirst(lc); if (plan != NULL) - pgpa_plan_walker(&context, plan, 0, NULL, NIL); + pgpa_plan_walker(&walker, plan, 0, NULL, NIL); } /* Put advice into string form. */ initStringInfo(&buf); - pgpa_output_advice(&buf, &context, rt_identifiers); + pgpa_output_advice(&buf, &walker, rt_identifiers); return buf.data; } diff --git a/contrib/pg_plan_advice/pgpa_join.c b/contrib/pg_plan_advice/pgpa_join.c index 14c0a444da..64f8dfdb8a 100644 --- a/contrib/pg_plan_advice/pgpa_join.c +++ b/contrib/pg_plan_advice/pgpa_join.c @@ -34,7 +34,7 @@ struct pgpa_join_unroller pgpa_join_unroller **inner_unrollers; }; -static pgpa_join_strategy pgpa_decompose_join(pgpa_plan_walker_context *context, +static pgpa_join_strategy pgpa_decompose_join(pgpa_plan_walker_context *walker, Plan *plan, Plan **realouter, Plan **realinner, @@ -185,7 +185,7 @@ pgpa_create_join_unroller(void) * XXX. More comments. */ void -pgpa_unroll_join(pgpa_plan_walker_context *context, Plan *plan, +pgpa_unroll_join(pgpa_plan_walker_context *walker, Plan *plan, pgpa_join_unroller *join_unroller, pgpa_join_unroller **outer_join_unroller, pgpa_join_unroller **inner_join_unroller) @@ -237,7 +237,7 @@ pgpa_unroll_join(pgpa_plan_walker_context *context, Plan *plan, * this should be an unrollable join. */ Assert(pgpa_get_join_class(plan) == PGPA_UNROLLED_JOIN); - strategy = pgpa_decompose_join(context, plan, + strategy = pgpa_decompose_join(walker, plan, &realouter, &realinner, &elidedouter, &elidedinner); @@ -397,11 +397,11 @@ pgpa_destroy_join_unroller(pgpa_join_unroller *join_unroller) * *elidedrealinner to the last of any correspoding elided nodes. */ static pgpa_join_strategy -pgpa_decompose_join(pgpa_plan_walker_context *context, Plan *plan, +pgpa_decompose_join(pgpa_plan_walker_context *walker, Plan *plan, Plan **realouter, Plan **realinner, ElidedNode **elidedrealouter, ElidedNode **elidedrealinner) { - PlannedStmt *pstmt = context->pstmt; + PlannedStmt *pstmt = walker->pstmt; JoinType jointype = ((Join *) plan)->jointype; Plan *outerplan = plan->lefttree; Plan *innerplan = plan->righttree; @@ -536,13 +536,13 @@ pgpa_decompose_join(pgpa_plan_walker_context *context, Plan *plan, * to the main tree walk, but I don't currently have a better idea. */ if (uniqueouter) - pgpa_add_future_feature(context, PGPAQF_SEMIJOIN_UNIQUE, outerplan); + pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_UNIQUE, outerplan); else if (jointype == JOIN_RIGHT_SEMI) - pgpa_add_future_feature(context, PGPAQF_SEMIJOIN_NON_UNIQUE, outerplan); + pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_NON_UNIQUE, outerplan); if (uniqueinner) - pgpa_add_future_feature(context, PGPAQF_SEMIJOIN_UNIQUE, innerplan); + pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_UNIQUE, innerplan); else if (jointype == JOIN_SEMI) - pgpa_add_future_feature(context, PGPAQF_SEMIJOIN_NON_UNIQUE, innerplan); + pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_NON_UNIQUE, innerplan); /* Set output parameters. */ *realouter = outerplan; diff --git a/contrib/pg_plan_advice/pgpa_join.h b/contrib/pg_plan_advice/pgpa_join.h index a3bc287e48..3a8585c616 100644 --- a/contrib/pg_plan_advice/pgpa_join.h +++ b/contrib/pg_plan_advice/pgpa_join.h @@ -140,7 +140,7 @@ extern pgpa_clumped_join *pgpa_build_clumped_join(PlannedStmt *pstmt, ElidedNode *elided_node); extern pgpa_join_unroller *pgpa_create_join_unroller(void); -extern void pgpa_unroll_join(struct pgpa_plan_walker_context *context, +extern void pgpa_unroll_join(struct pgpa_plan_walker_context *walker, Plan *plan, pgpa_join_unroller *join_unroller, pgpa_join_unroller **outer_join_unroller, diff --git a/contrib/pg_plan_advice/pgpa_walker.c b/contrib/pg_plan_advice/pgpa_walker.c index 3d8bd752af..8973180379 100644 --- a/contrib/pg_plan_advice/pgpa_walker.c +++ b/contrib/pg_plan_advice/pgpa_walker.c @@ -5,7 +5,7 @@ #include "nodes/plannodes.h" -static pgpa_query_feature *pgpa_add_feature(pgpa_plan_walker_context *context, +static pgpa_query_feature *pgpa_add_feature(pgpa_plan_walker_context *walker, pgpa_qf_type type, Plan *plan); @@ -19,7 +19,7 @@ static void pgpa_qf_add_plan_rtis(List *active_query_features, Plan *plan); * XXX. More comments. */ void -pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, +pgpa_plan_walker(pgpa_plan_walker_context *walker, Plan *plan, int join_unroll_level, pgpa_join_unroller *join_unroller, List *active_query_features) @@ -46,26 +46,26 @@ pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, { active_query_features = lappend(active_query_features, - pgpa_add_feature(context, PGPAQF_GATHER, plan)); + pgpa_add_feature(walker, PGPAQF_GATHER, plan)); is_query_feature = true; } else if (IsA(plan, GatherMerge)) { active_query_features = lappend(active_query_features, - pgpa_add_feature(context, PGPAQF_GATHER_MERGE, plan)); + pgpa_add_feature(walker, PGPAQF_GATHER_MERGE, plan)); is_query_feature = true; } else { - foreach_ptr(pgpa_query_feature, qf, context->future_query_features) + foreach_ptr(pgpa_query_feature, qf, walker->future_query_features) { if (qf->plan == plan) { is_query_feature = true; active_query_features = lappend(active_query_features, qf); - context->future_query_features = - list_delete_ptr(context->future_query_features, plan); + walker->future_query_features = + list_delete_ptr(walker->future_query_features, plan); break; } } @@ -74,7 +74,7 @@ pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, /* * Find all elided nodes for this Plan node. */ - foreach_node(ElidedNode, n, context->pstmt->elidedNodes) + foreach_node(ElidedNode, n, walker->pstmt->elidedNodes) { if (n->plan_node_id == plan->plan_node_id) elided_nodes = lappend(elided_nodes, n); @@ -103,7 +103,7 @@ pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, /* * Every element of elided_nodes is an ElidedNode for which any * necessary pgpa_clumped_join has not yet been created. Do that here, - * and attach the resulting objects directly to the context object, + * and attach the resulting objects directly to the walker object, * since we have nowhere else to put a reference to it. */ foreach_node(ElidedNode, n, elided_nodes) @@ -112,9 +112,8 @@ pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, { pgpa_clumped_join *cjoin; - cjoin = pgpa_build_clumped_join(context->pstmt, plan, n); - context->clumped_joins = - lappend(context->clumped_joins, cjoin); + cjoin = pgpa_build_clumped_join(walker->pstmt, plan, n); + walker->clumped_joins = lappend(walker->clumped_joins, cjoin); } } @@ -156,8 +155,8 @@ pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, { pgpa_clumped_join *cjoin; - cjoin = pgpa_build_clumped_join(context->pstmt, plan, NULL); - context->clumped_joins = lappend(context->clumped_joins, cjoin); + cjoin = pgpa_build_clumped_join(walker->pstmt, plan, NULL); + walker->clumped_joins = lappend(walker->clumped_joins, cjoin); } /* @@ -166,7 +165,7 @@ pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, * outer and inner sides of the plan. */ if (join_unroller != NULL) - pgpa_unroll_join(context, plan, join_unroller, + pgpa_unroll_join(walker, plan, join_unroller, &outer_join_unroller, &inner_join_unroller); /* Add RTIs from the plan node to all active query features. */ @@ -174,10 +173,10 @@ pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, /* Recurse into the outer and inner subtrees. */ if (plan->lefttree != NULL) - pgpa_plan_walker(context, plan->lefttree, join_unroll_level, + pgpa_plan_walker(walker, plan->lefttree, join_unroll_level, outer_join_unroller, active_query_features); if (plan->righttree != NULL) - pgpa_plan_walker(context, plan->righttree, join_unroll_level, + pgpa_plan_walker(walker, plan->righttree, join_unroll_level, inner_join_unroller, active_query_features); /* @@ -188,8 +187,8 @@ pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, { pgpa_unrolled_join *ujoin; - ujoin = pgpa_build_unrolled_join(context->pstmt, join_unroller); - context->unrolled_joins = lappend(context->unrolled_joins, ujoin); + ujoin = pgpa_build_unrolled_join(walker->pstmt, join_unroller); + walker->unrolled_joins = lappend(walker->unrolled_joins, ujoin); pgpa_destroy_join_unroller(join_unroller); } @@ -227,7 +226,7 @@ pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, * We don't pass down active_query_features across here, because * those are specific to a subquery level. */ - pgpa_plan_walker(context, ((SubqueryScan *) plan)->subplan, + pgpa_plan_walker(walker, ((SubqueryScan *) plan)->subplan, 0, NULL, NIL); break; case T_CustomScan: @@ -242,7 +241,7 @@ pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, { Plan *subplan = lfirst(lc); - pgpa_plan_walker(context, subplan, 0, NULL, + pgpa_plan_walker(walker, subplan, 0, NULL, pushdown_query_features ? active_query_features : NIL); } @@ -273,13 +272,13 @@ pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, * yet! */ void -pgpa_add_future_feature(pgpa_plan_walker_context *context, +pgpa_add_future_feature(pgpa_plan_walker_context *walker, pgpa_qf_type type, Plan *plan) { - pgpa_query_feature *qf = pgpa_add_feature(context, type, plan); + pgpa_query_feature *qf = pgpa_add_feature(walker, type, plan); - context->future_query_features = - lappend(context->future_query_features, qf); + walker->future_query_features = + lappend(walker->future_query_features, qf); } /* @@ -369,7 +368,7 @@ pgpa_scanrelid(Plan *plan) * for this plan. */ static pgpa_query_feature * -pgpa_add_feature(pgpa_plan_walker_context *context, +pgpa_add_feature(pgpa_plan_walker_context *walker, pgpa_qf_type type, Plan *plan) { pgpa_query_feature *qf = palloc0_object(pgpa_query_feature); @@ -377,7 +376,7 @@ pgpa_add_feature(pgpa_plan_walker_context *context, qf->type = type; qf->plan = plan; - context->query_features = lappend(context->query_features, qf); + walker->query_features = lappend(walker->query_features, qf); return qf; } diff --git a/contrib/pg_plan_advice/pgpa_walker.h b/contrib/pg_plan_advice/pgpa_walker.h index 223d42d746..692018bfe9 100644 --- a/contrib/pg_plan_advice/pgpa_walker.h +++ b/contrib/pg_plan_advice/pgpa_walker.h @@ -68,12 +68,12 @@ typedef struct pgpa_plan_walker_context List *future_query_features; } pgpa_plan_walker_context; -extern void pgpa_plan_walker(pgpa_plan_walker_context *context, Plan *plan, +extern void pgpa_plan_walker(pgpa_plan_walker_context *walker, Plan *plan, int join_unroll_level, pgpa_join_unroller *join_unroller, List *active_query_features); -extern void pgpa_add_future_feature(pgpa_plan_walker_context *context, +extern void pgpa_add_future_feature(pgpa_plan_walker_context *walker, pgpa_qf_type type, Plan *plan); -- 2.39.5