From 3a6332213d144a4e520c0d78ca915fdc10f22d85 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Thu, 26 Jun 2025 12:18:31 -0400 Subject: [PATCH] move debug output routines to a new file --- contrib/pg_plan_advice/Makefile | 1 + contrib/pg_plan_advice/meson.build | 1 + contrib/pg_plan_advice/pg_plan_advice.c | 1 + contrib/pg_plan_advice/pgpa_join.c | 145 --------------------- contrib/pg_plan_advice/pgpa_join.h | 10 -- contrib/pg_plan_advice/pgpa_output.c | 161 ++++++++++++++++++++++++ 6 files changed, 164 insertions(+), 155 deletions(-) create mode 100644 contrib/pg_plan_advice/pgpa_output.c diff --git a/contrib/pg_plan_advice/Makefile b/contrib/pg_plan_advice/Makefile index 7934baed1e..f60b8023c1 100644 --- a/contrib/pg_plan_advice/Makefile +++ b/contrib/pg_plan_advice/Makefile @@ -6,6 +6,7 @@ OBJS = \ pg_plan_advice.o \ pgpa_identifier.o \ pgpa_join.o \ + pgpa_output.o \ pgpa_walker.o EXTENSION = pg_plan_advice diff --git a/contrib/pg_plan_advice/meson.build b/contrib/pg_plan_advice/meson.build index eda3fe7cc1..4bbe1e21f3 100644 --- a/contrib/pg_plan_advice/meson.build +++ b/contrib/pg_plan_advice/meson.build @@ -4,6 +4,7 @@ pg_plan_advice_sources = files( 'pg_plan_advice.c', 'pgpa_identifier.c', 'pgpa_join.c', + 'pgpa_output.c', 'pgpa_walker.c', ) diff --git a/contrib/pg_plan_advice/pg_plan_advice.c b/contrib/pg_plan_advice/pg_plan_advice.c index 0e50c63f86..6a90dfc89e 100644 --- a/contrib/pg_plan_advice/pg_plan_advice.c +++ b/contrib/pg_plan_advice/pg_plan_advice.c @@ -13,6 +13,7 @@ #include "funcapi.h" #include "pgpa_identifier.h" +#include "pgpa_output.h" #include "pgpa_walker.h" PG_MODULE_MAGIC; diff --git a/contrib/pg_plan_advice/pgpa_join.c b/contrib/pg_plan_advice/pgpa_join.c index adfa6e0279..86f6b7590e 100644 --- a/contrib/pg_plan_advice/pgpa_join.c +++ b/contrib/pg_plan_advice/pgpa_join.c @@ -48,11 +48,6 @@ static ElidedNode *pgpa_descend_any_unique(PlannedStmt *pstmt, Plan **plan); static Index pgpa_scanrelid(Plan *plan); static Bitmapset *pgpa_relids(Plan *plan); -static void pgpa_debug_out_join_member(StringInfo buf, - pgpa_join_member *member, - const char **rt_identifiers); -static char *pgpa_cstring_join_clump_strategy(pgpa_join_clump_strategy strategy); -static char *pgpa_cstring_join_strategy(pgpa_join_strategy strategy); static bool is_result_node_with_child(Plan *plan); static bool is_sorting_plan(Plan *plan); @@ -728,146 +723,6 @@ pgpa_relids(Plan *plan) return NULL; } -void -pgpa_debug_out_clumped_join(StringInfo buf, pgpa_clumped_join *clump, - const char **rt_identifiers) -{ - char *cstrategy; - int rti = -1; - bool first = true; - - cstrategy = pgpa_cstring_join_clump_strategy(clump->strategy); - appendStringInfo(buf, "%s(", cstrategy); - while ((rti = bms_next_member(clump->relids, rti)) >= 0) - { - const char *identifier = rt_identifiers[rti - 1]; - - if (identifier == NULL) - elog(ERROR, "no identifier for RTI %d", rti); - - if (first) - { - first = false; - appendStringInfoString(buf, identifier); - } - else - appendStringInfo(buf, " %s", identifier); - } - appendStringInfoChar(buf, ')'); -} - -void -pgpa_debug_out_unrolled_join(StringInfo buf, pgpa_unrolled_join *join, - const char **rt_identifiers) -{ - appendStringInfoChar(buf, '('); - - pgpa_debug_out_join_member(buf, &join->outer, rt_identifiers); - - for (int k = 0; k < join->ninner; ++k) - { - char *cstrategy; - - cstrategy = pgpa_cstring_join_strategy(join->strategy[k]); - appendStringInfo(buf, " %s ", cstrategy); - pgpa_debug_out_join_member(buf, &join->inner[k], rt_identifiers); - } - - appendStringInfoChar(buf, ')'); -} - -void -pgpa_debug_out_gathered_join(StringInfo buf, pgpa_gathered_join *gathered_join, - const char **rt_identifiers) -{ - int rti = -1; - bool first = true; - - if (gathered_join->is_merge) - appendStringInfo(buf, "GATHER_MERGE("); - else - appendStringInfo(buf, "GATHER("); - - while ((rti = bms_next_member(gathered_join->relids, rti)) >= 0) - { - const char *identifier = rt_identifiers[rti - 1]; - - if (identifier == NULL) - elog(ERROR, "no identifier for RTI %d", rti); - - if (first) - { - first = false; - appendStringInfoString(buf, identifier); - } - else - appendStringInfo(buf, " %s", identifier); - } - appendStringInfoChar(buf, ')'); -} - -static void -pgpa_debug_out_join_member(StringInfo buf, pgpa_join_member *member, - const char **rt_identifiers) -{ - if (member->clump_join != NULL) - pgpa_debug_out_clumped_join(buf, member->clump_join, - rt_identifiers); - else if (member->unrolled_join != NULL) - pgpa_debug_out_unrolled_join(buf, member->unrolled_join, - rt_identifiers); - else - { - if (rt_identifiers[member->rti - 1] == NULL) - elog(ERROR, "no identifier for RTI %d", member->rti); - appendStringInfoString(buf, rt_identifiers[member->rti - 1]); - } -} - -/* - * Get a C string that corresponds to the specified join clump strategy. - */ -static char * -pgpa_cstring_join_clump_strategy(pgpa_join_clump_strategy strategy) -{ - switch (strategy) - { - case JSTRAT_CLUMP_DEGENERATE: - return "DEGENERATE"; - case JSTRAT_CLUMP_FOREIGN: - return "FOREIGN"; - case JSTRAT_CLUMP_PARTITIONWISE: - return "PARTITIONWISE"; - } - - Assert(false); -} - -/* - * Get a C string that corresponds to the specified join strategy. - */ -static char * -pgpa_cstring_join_strategy(pgpa_join_strategy strategy) -{ - switch (strategy) - { - case JSTRAT_MERGE_JOIN_PLAIN: - return "MERGE_JOIN_PLAIN"; - case JSTRAT_MERGE_JOIN_MATERIALIZE: - return "MERGE_JOIN_MATERIALIZE"; - case JSTRAT_NESTED_LOOP_PLAIN: - return "NESTED_LOOP_PLAIN"; - case JSTRAT_NESTED_LOOP_MATERIALIZE: - return "NESTED_LOOP_MATERIALIZE"; - case JSTRAT_NESTED_LOOP_MEMOIZE: - return "NESTED_LOOP_MEMOIZE"; - case JSTRAT_HASH_JOIN: - return "HASH_JOIN"; - } - - Assert(false); -} - /* * Is this a Result node that has a child? */ diff --git a/contrib/pg_plan_advice/pgpa_join.h b/contrib/pg_plan_advice/pgpa_join.h index 0b03cc1b08..34992afc81 100644 --- a/contrib/pg_plan_advice/pgpa_join.h +++ b/contrib/pg_plan_advice/pgpa_join.h @@ -156,14 +156,4 @@ extern void pgpa_destroy_join_unroller(pgpa_join_unroller *join_unroller); extern void pgpa_add_to_gathered_join(pgpa_gathered_join *gathered_join, Plan *plan); -extern void pgpa_debug_out_clumped_join(StringInfo buf, - pgpa_clumped_join *clump, - const char **rt_identifiers); -extern void pgpa_debug_out_unrolled_join(StringInfo buf, - pgpa_unrolled_join *join, - const char **rt_identifiers); -extern void pgpa_debug_out_gathered_join(StringInfo buf, - pgpa_gathered_join *gathered_join, - const char **rt_identifiers); - #endif diff --git a/contrib/pg_plan_advice/pgpa_output.c b/contrib/pg_plan_advice/pgpa_output.c new file mode 100644 index 0000000000..907212cebb --- /dev/null +++ b/contrib/pg_plan_advice/pgpa_output.c @@ -0,0 +1,161 @@ +/*------------------------------------------------------------------------- + * + * pgpa_output.c + * produce textual output from the results of a plan tree walk + * + * Copyright (c) 2016-2025, PostgreSQL Global Development Group + * + * contrib/pg_plan_advice/pgpa_output.c + * + *------------------------------------------------------------------------- + */ + +#include "postgres.h" + +#include "pgpa_output.h" + +static void pgpa_debug_out_join_member(StringInfo buf, + pgpa_join_member *member, + const char **rt_identifiers); +static char *pgpa_cstring_join_clump_strategy(pgpa_join_clump_strategy strategy); +static char *pgpa_cstring_join_strategy(pgpa_join_strategy strategy); + +void +pgpa_debug_out_clumped_join(StringInfo buf, pgpa_clumped_join *clump, + const char **rt_identifiers) +{ + char *cstrategy; + int rti = -1; + bool first = true; + + cstrategy = pgpa_cstring_join_clump_strategy(clump->strategy); + appendStringInfo(buf, "%s(", cstrategy); + while ((rti = bms_next_member(clump->relids, rti)) >= 0) + { + const char *identifier = rt_identifiers[rti - 1]; + + if (identifier == NULL) + elog(ERROR, "no identifier for RTI %d", rti); + + if (first) + { + first = false; + appendStringInfoString(buf, identifier); + } + else + appendStringInfo(buf, " %s", identifier); + } + appendStringInfoChar(buf, ')'); +} + +void +pgpa_debug_out_unrolled_join(StringInfo buf, pgpa_unrolled_join *join, + const char **rt_identifiers) +{ + appendStringInfoChar(buf, '('); + + pgpa_debug_out_join_member(buf, &join->outer, rt_identifiers); + + for (int k = 0; k < join->ninner; ++k) + { + char *cstrategy; + + cstrategy = pgpa_cstring_join_strategy(join->strategy[k]); + appendStringInfo(buf, " %s ", cstrategy); + pgpa_debug_out_join_member(buf, &join->inner[k], rt_identifiers); + } + + appendStringInfoChar(buf, ')'); +} + +void +pgpa_debug_out_gathered_join(StringInfo buf, pgpa_gathered_join *gathered_join, + const char **rt_identifiers) +{ + int rti = -1; + bool first = true; + + if (gathered_join->is_merge) + appendStringInfo(buf, "GATHER_MERGE("); + else + appendStringInfo(buf, "GATHER("); + + while ((rti = bms_next_member(gathered_join->relids, rti)) >= 0) + { + const char *identifier = rt_identifiers[rti - 1]; + + if (identifier == NULL) + elog(ERROR, "no identifier for RTI %d", rti); + + if (first) + { + first = false; + appendStringInfoString(buf, identifier); + } + else + appendStringInfo(buf, " %s", identifier); + } + appendStringInfoChar(buf, ')'); +} + +static void +pgpa_debug_out_join_member(StringInfo buf, pgpa_join_member *member, + const char **rt_identifiers) +{ + if (member->clump_join != NULL) + pgpa_debug_out_clumped_join(buf, member->clump_join, + rt_identifiers); + else if (member->unrolled_join != NULL) + pgpa_debug_out_unrolled_join(buf, member->unrolled_join, + rt_identifiers); + else + { + if (rt_identifiers[member->rti - 1] == NULL) + elog(ERROR, "no identifier for RTI %d", member->rti); + appendStringInfoString(buf, rt_identifiers[member->rti - 1]); + } +} + +/* + * Get a C string that corresponds to the specified join clump strategy. + */ +static char * +pgpa_cstring_join_clump_strategy(pgpa_join_clump_strategy strategy) +{ + switch (strategy) + { + case JSTRAT_CLUMP_DEGENERATE: + return "DEGENERATE"; + case JSTRAT_CLUMP_FOREIGN: + return "FOREIGN"; + case JSTRAT_CLUMP_PARTITIONWISE: + return "PARTITIONWISE"; + } + + Assert(false); +} + +/* + * Get a C string that corresponds to the specified join strategy. + */ +static char * +pgpa_cstring_join_strategy(pgpa_join_strategy strategy) +{ + switch (strategy) + { + case JSTRAT_MERGE_JOIN_PLAIN: + return "MERGE_JOIN_PLAIN"; + case JSTRAT_MERGE_JOIN_MATERIALIZE: + return "MERGE_JOIN_MATERIALIZE"; + case JSTRAT_NESTED_LOOP_PLAIN: + return "NESTED_LOOP_PLAIN"; + case JSTRAT_NESTED_LOOP_MATERIALIZE: + return "NESTED_LOOP_MATERIALIZE"; + case JSTRAT_NESTED_LOOP_MEMOIZE: + return "NESTED_LOOP_MEMOIZE"; + case JSTRAT_HASH_JOIN: + return "HASH_JOIN"; + } + + Assert(false); +} -- 2.39.5