From 45314b7e7cc6d9e01caa18188a6e21aee08b6dc7 Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Fri, 31 Jul 2026 04:33:54 -0400
Subject: [PATCH v3 09/15] Remove check-and-ignore of version parameter in
 statistics import.

Remove the check for "version" in stats_fill_fcinfo_from_arg_pairs() and
move the responsibility for defining/not defining a "version" parameter
to the calling function. This has the added benefit and burden of
allowing the caller to establish the datatype of the version parameter.

Previously, stats_fill_fcinfo_from_arg_pairs() would specifically check
for an arg pair with the keyword "version" and then discard it without
first checking the datatype of the value. While this was a convenience
that saved the argument lists one parameter that has yet to have a use,
it was at best a minor savings and made things more difficult for future
work processing explicit NullableDatum parameters.
---
 src/backend/statistics/attribute_stats.c      |  4 +++
 src/backend/statistics/extended_stats_funcs.c |  2 ++
 src/backend/statistics/relation_stats.c       | 32 +++++++++++--------
 src/backend/statistics/stat_utils.c           | 10 ------
 4 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index f19a0d1e373..4ee14b3ebdf 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -39,6 +39,7 @@ enum attribute_stats_argnum
 {
 	ATTARG_ATTRELSCHEMA = 0,
 	ATTARG_ATTRELNAME,
+	ATTARG_VERSION,
 	ATTARG_ATTNAME,
 	ATTARG_ATTNUM,
 	ATTARG_INHERITED,
@@ -62,6 +63,7 @@ static struct StatsArgInfo attarginfo[] =
 {
 	[ATTARG_ATTRELSCHEMA] = {"schemaname", TEXTOID},
 	[ATTARG_ATTRELNAME] = {"relname", TEXTOID},
+	[ATTARG_VERSION] = {"version", INT4OID},
 	[ATTARG_ATTNAME] = {"attname", TEXTOID},
 	[ATTARG_ATTNUM] = {"attnum", INT2OID},
 	[ATTARG_INHERITED] = {"inherited", BOOLOID},
@@ -770,6 +772,8 @@ import_attribute_statistics(Relation rel, AttrNumber attnum, bool inherited,
 	newfcinfo->args[ATTARG_INHERITED].value = BoolGetDatum(inherited);
 	newfcinfo->args[ATTARG_INHERITED].isnull = false;
 
+	newfcinfo->args[ATTARG_VERSION].value = (version) ? version->value : (Datum) 0;
+	newfcinfo->args[ATTARG_VERSION].isnull = (version) ? version->isnull : true;
 	newfcinfo->args[ATTARG_NULL_FRAC] = *null_frac;
 	newfcinfo->args[ATTARG_AVG_WIDTH] = *avg_width;
 	newfcinfo->args[ATTARG_N_DISTINCT] = *n_distinct;
diff --git a/src/backend/statistics/extended_stats_funcs.c b/src/backend/statistics/extended_stats_funcs.c
index be31ac67fba..1b730b26c44 100644
--- a/src/backend/statistics/extended_stats_funcs.c
+++ b/src/backend/statistics/extended_stats_funcs.c
@@ -50,6 +50,7 @@ enum extended_stats_argnum
 	EXTARG_STATSCHEMA,
 	EXTARG_STATNAME,
 	EXTARG_INHERITED,
+	EXTARG_VERSION,
 	EXTARG_NDISTINCT,
 	EXTARG_DEPENDENCIES,
 	EXTARG_MOST_COMMON_VALS,
@@ -70,6 +71,7 @@ static struct StatsArgInfo extarginfo[] =
 	[EXTARG_STATSCHEMA] = {"statistics_schemaname", TEXTOID},
 	[EXTARG_STATNAME] = {"statistics_name", TEXTOID},
 	[EXTARG_INHERITED] = {"inherited", BOOLOID},
+	[EXTARG_VERSION] = {"version", INT4OID},
 	[EXTARG_NDISTINCT] = {"n_distinct", PG_NDISTINCTOID},
 	[EXTARG_DEPENDENCIES] = {"dependencies", PG_DEPENDENCIESOID},
 	[EXTARG_MOST_COMMON_VALS] = {"most_common_vals", TEXTARRAYOID},
diff --git a/src/backend/statistics/relation_stats.c b/src/backend/statistics/relation_stats.c
index 03312985bc3..eaf7a1e326b 100644
--- a/src/backend/statistics/relation_stats.c
+++ b/src/backend/statistics/relation_stats.c
@@ -41,6 +41,7 @@ enum relation_stats_argnum
 {
 	RELARG_SCHEMA = 0,
 	RELARG_RELNAME,
+	RELARG_VERSION,
 	RELARG_RELPAGES,
 	RELARG_RELTUPLES,
 	RELARG_RELALLVISIBLE,
@@ -52,6 +53,7 @@ static struct StatsArgInfo relarginfo[] =
 {
 	[RELARG_SCHEMA] = {"schemaname", TEXTOID},
 	[RELARG_RELNAME] = {"relname", TEXTOID},
+	[RELARG_VERSION] = {"version", INT4OID},
 	[RELARG_RELPAGES] = {"relpages", INT4OID},
 	[RELARG_RELTUPLES] = {"reltuples", FLOAT4OID},
 	[RELARG_RELALLVISIBLE] = {"relallvisible", INT4OID},
@@ -223,22 +225,24 @@ relation_statistics_update_internal(Oid reloid, const NullableDatum *args)
 Datum
 pg_clear_relation_stats(PG_FUNCTION_ARGS)
 {
-	LOCAL_FCINFO(newfcinfo, 6);
+	LOCAL_FCINFO(newfcinfo, RELARG_NUM_RELARGS);
 
 	InitFunctionCallInfoData(*newfcinfo, NULL, 6, InvalidOid, NULL, NULL);
 
-	newfcinfo->args[0].value = PG_GETARG_DATUM(0);
-	newfcinfo->args[0].isnull = PG_ARGISNULL(0);
-	newfcinfo->args[1].value = PG_GETARG_DATUM(1);
-	newfcinfo->args[1].isnull = PG_ARGISNULL(1);
-	newfcinfo->args[2].value = UInt32GetDatum(0);
-	newfcinfo->args[2].isnull = false;
-	newfcinfo->args[3].value = Float4GetDatum(-1.0);
-	newfcinfo->args[3].isnull = false;
-	newfcinfo->args[4].value = UInt32GetDatum(0);
-	newfcinfo->args[4].isnull = false;
-	newfcinfo->args[5].value = UInt32GetDatum(0);
-	newfcinfo->args[5].isnull = false;
+	newfcinfo->args[RELARG_SCHEMA].value = PG_GETARG_DATUM(RELARG_SCHEMA);
+	newfcinfo->args[RELARG_SCHEMA].isnull = PG_ARGISNULL(RELARG_SCHEMA);
+	newfcinfo->args[RELARG_RELNAME].value = PG_GETARG_DATUM(RELARG_RELNAME);
+	newfcinfo->args[RELARG_RELNAME].isnull = PG_ARGISNULL(RELARG_RELNAME);
+	newfcinfo->args[RELARG_VERSION].value = (Datum) 0;
+	newfcinfo->args[RELARG_VERSION].isnull = true;
+	newfcinfo->args[RELARG_RELPAGES].value = UInt32GetDatum(0);
+	newfcinfo->args[RELARG_RELPAGES].isnull = false;
+	newfcinfo->args[RELARG_RELTUPLES].value = Float4GetDatum(-1.0);
+	newfcinfo->args[RELARG_RELTUPLES].isnull = false;
+	newfcinfo->args[RELARG_RELALLVISIBLE].value = UInt32GetDatum(0);
+	newfcinfo->args[RELARG_RELALLVISIBLE].isnull = false;
+	newfcinfo->args[RELARG_RELALLFROZEN].value = UInt32GetDatum(0);
+	newfcinfo->args[RELARG_RELALLFROZEN].isnull = false;
 
 	relation_statistics_update(newfcinfo->args);
 	PG_RETURN_VOID();
@@ -291,6 +295,8 @@ import_relation_statistics(Relation rel,
 	newfcinfo->args[RELARG_RELNAME].value =
 		CStringGetTextDatum(RelationGetRelationName(rel));
 	newfcinfo->args[RELARG_RELNAME].isnull = false;
+	newfcinfo->args[RELARG_VERSION].value = (version) ? version->value : (Datum) 0;
+	newfcinfo->args[RELARG_VERSION].isnull = (version) ? version->isnull : true;
 
 	newfcinfo->args[RELARG_RELPAGES] = *relpages;
 	newfcinfo->args[RELARG_RELTUPLES] = *reltuples;
diff --git a/src/backend/statistics/stat_utils.c b/src/backend/statistics/stat_utils.c
index df3bf3d6914..29655d55339 100644
--- a/src/backend/statistics/stat_utils.c
+++ b/src/backend/statistics/stat_utils.c
@@ -398,16 +398,6 @@ stats_fill_args_from_arg_pairs(FunctionCallInfo pairs_fcinfo,
 
 		argname = TextDatumGetCString(args[i]);
 
-		/*
-		 * The 'version' argument is a special case, not handled by arginfo
-		 * because it's not a valid positional argument.
-		 *
-		 * For now, 'version' is accepted but ignored. In the future it can be
-		 * used to interpret older statistics properly.
-		 */
-		if (pg_strcasecmp(argname, "version") == 0)
-			continue;
-
 		argnum = get_arg_by_name(argname, arginfo);
 
 		if (argnum < 0 || !stats_check_arg_type(argname, types[i + 1],
-- 
2.50.1 (Apple Git-155)

