From 6d3d963712af9d8db91605cdb99bde598994d199 Mon Sep 17 00:00:00 2001 From: Corey Huinker Date: Mon, 3 Aug 2026 15:07:11 -0400 Subject: [PATCH v3 13/15] Refactor relation_stats update functions. Move all functionality from relation_statistics_update() to pg_restore_relation_stats() and pg_clear_relation_stats(), and then rename relation_statistics_update_internal() to relation_statistics_update(). --- src/backend/statistics/relation_stats.c | 122 +++++++++++------------- 1 file changed, 58 insertions(+), 64 deletions(-) diff --git a/src/backend/statistics/relation_stats.c b/src/backend/statistics/relation_stats.c index f8a357ba97d..aac7ab2e996 100644 --- a/src/backend/statistics/relation_stats.c +++ b/src/backend/statistics/relation_stats.c @@ -61,52 +61,23 @@ static struct StatsArgInfo relarginfo[] = [RELARG_NUM_RELARGS] = {0} }; -static bool relation_statistics_update(const NullableDatum *args); -static bool relation_statistics_update_internal(Oid reloid, - const NullableDatum *version, - const NullableDatum *relpages, - const NullableDatum *reltuples, - const NullableDatum *relallvisible, - const NullableDatum *relallfrozen); +static bool relation_statistics_update(Oid reloid, + const NullableDatum *version, + const NullableDatum *relpages, + const NullableDatum *reltuples, + const NullableDatum *relallvisible, + const NullableDatum *relallfrozen); /* * Internal function for modifying statistics for a relation. */ static bool -relation_statistics_update(const NullableDatum *args) -{ - char *nspname; - char *relname; - Oid reloid; - - stats_check_required_arg(&args[RELARG_SCHEMA], relarginfo[RELARG_SCHEMA].argname); - stats_check_required_arg(&args[RELARG_RELNAME], relarginfo[RELARG_RELNAME].argname); - - nspname = TextDatumGetCString(args[RELARG_SCHEMA].value); - relname = TextDatumGetCString(args[RELARG_RELNAME].value); - - stats_check_recovery(ERROR); - - reloid = stats_check_lock_relation(nspname, relname); - - return relation_statistics_update_internal(reloid, - &args[RELARG_VERSION], - &args[RELARG_RELPAGES], - &args[RELARG_RELTUPLES], - &args[RELARG_RELALLVISIBLE], - &args[RELARG_RELALLFROZEN]); -} - -/* - * Workhorse function for relation_statistics_update. - */ -static bool -relation_statistics_update_internal(Oid reloid, - const NullableDatum *version, - const NullableDatum *relpages, - const NullableDatum *reltuples, - const NullableDatum *relallvisible, - const NullableDatum *relallfrozen) +relation_statistics_update(Oid reloid, + const NullableDatum *version, + const NullableDatum *relpages, + const NullableDatum *reltuples, + const NullableDatum *relallvisible, + const NullableDatum *relallfrozen) { BlockNumber num_relpages = 0; bool update_relpages = false; @@ -238,38 +209,62 @@ relation_statistics_update_internal(Oid reloid, Datum pg_clear_relation_stats(PG_FUNCTION_ARGS) { - NullableDatum args[RELARG_NUM_RELARGS]; + const NullableDatum *schema_arg = &fcinfo->args[RELARG_SCHEMA]; + const NullableDatum *relation_arg = &fcinfo->args[RELARG_RELNAME]; + const char *schema_arg_name = relarginfo[RELARG_SCHEMA].argname; + const char *relation_arg_name = relarginfo[RELARG_RELNAME].argname; + + const NullableDatum version = {.value = (Datum) 0, .isnull = true}; + const NullableDatum relpages = {.value = UInt32GetDatum(0), .isnull = false}; + const NullableDatum reltuples = {.value = Float4GetDatum(-1.0), .isnull = false}; + const NullableDatum relallvisible = {.value = UInt32GetDatum(0), .isnull = false}; + const NullableDatum relallfrozen = {.value = UInt32GetDatum(0), .isnull = false}; + + Oid reloid; + + stats_check_required_arg(schema_arg, schema_arg_name); + stats_check_required_arg(relation_arg, relation_arg_name); + + stats_check_recovery(ERROR); + + reloid = stats_check_lock_relation(TextDatumGetCString(schema_arg->value), + TextDatumGetCString(relation_arg->value)); + + relation_statistics_update(reloid, &version, &relpages, &reltuples, + &relallvisible, &relallfrozen); - args[RELARG_SCHEMA].value = PG_GETARG_DATUM(RELARG_SCHEMA); - args[RELARG_SCHEMA].isnull = PG_ARGISNULL(RELARG_SCHEMA); - args[RELARG_RELNAME].value = PG_GETARG_DATUM(RELARG_RELNAME); - args[RELARG_RELNAME].isnull = PG_ARGISNULL(RELARG_RELNAME); - args[RELARG_VERSION].value = (Datum) 0; - args[RELARG_VERSION].isnull = true; - args[RELARG_RELPAGES].value = UInt32GetDatum(0); - args[RELARG_RELPAGES].isnull = false; - args[RELARG_RELTUPLES].value = Float4GetDatum(-1.0); - args[RELARG_RELTUPLES].isnull = false; - args[RELARG_RELALLVISIBLE].value = UInt32GetDatum(0); - args[RELARG_RELALLVISIBLE].isnull = false; - args[RELARG_RELALLFROZEN].value = UInt32GetDatum(0); - args[RELARG_RELALLFROZEN].isnull = false; - - relation_statistics_update(args); PG_RETURN_VOID(); } Datum pg_restore_relation_stats(PG_FUNCTION_ARGS) { - NullableDatum positional_args[RELARG_NUM_RELARGS]; + NullableDatum args[RELARG_NUM_RELARGS]; bool result = true; + char *nspname; + char *relname; + Oid reloid; - if (!stats_fill_args_from_arg_pairs(fcinfo, positional_args, + if (!stats_fill_args_from_arg_pairs(fcinfo, args, relarginfo)) result = false; - if (!relation_statistics_update(positional_args)) + stats_check_required_arg(&args[RELARG_SCHEMA], relarginfo[RELARG_SCHEMA].argname); + stats_check_required_arg(&args[RELARG_RELNAME], relarginfo[RELARG_RELNAME].argname); + + nspname = TextDatumGetCString(args[RELARG_SCHEMA].value); + relname = TextDatumGetCString(args[RELARG_RELNAME].value); + + stats_check_recovery(ERROR); + + reloid = stats_check_lock_relation(nspname, relname); + + if (!relation_statistics_update(reloid, + &args[RELARG_VERSION], + &args[RELARG_RELPAGES], + &args[RELARG_RELTUPLES], + &args[RELARG_RELALLVISIBLE], + &args[RELARG_RELALLFROZEN])) result = false; PG_RETURN_BOOL(result); @@ -301,7 +296,6 @@ import_relation_statistics(Relation rel, if (!version) version = &null_datum; - return relation_statistics_update_internal(RelationGetRelid(rel), version, - relpages, reltuples, - relallvisible, relallfrozen); + return relation_statistics_update(RelationGetRelid(rel), version, relpages, + reltuples, relallvisible, relallfrozen); } -- 2.50.1 (Apple Git-155)