From 44571c9b2ec6427bdab703b22050feca718220c5 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 v1 5/5] 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      |  6 ++++--
 src/backend/statistics/extended_stats_funcs.c |  4 +++-
 src/backend/statistics/relation_stats.c       |  8 ++++++--
 src/backend/statistics/stat_utils.c           | 10 ----------
 4 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index e7ee0b3443f..2414b19c82c 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -37,7 +37,8 @@
 
 enum attribute_stats_argnum
 {
-	ATTRELSCHEMA_ARG = 0,
+	VERSION_ARG = 0,
+	ATTRELSCHEMA_ARG,
 	ATTRELNAME_ARG,
 	ATTNAME_ARG,
 	ATTNUM_ARG,
@@ -60,6 +61,7 @@ enum attribute_stats_argnum
 
 static struct StatsArgInfo attarginfo[] =
 {
+	[VERSION_ARG] = {"version", INT4OID},
 	[ATTRELSCHEMA_ARG] = {"schemaname", TEXTOID},
 	[ATTRELNAME_ARG] = {"relname", TEXTOID},
 	[ATTNAME_ARG] = {"attname", TEXTOID},
@@ -763,12 +765,12 @@ import_attribute_statistics(Relation rel, AttrNumber attnum, bool inherited,
 				 errmsg("column %d of relation \"%s\" does not exist",
 						attnum, relname)));
 
+	args[VERSION_ARG] = (version) ? *version : unused;
 	args[ATTRELSCHEMA_ARG] = unused;
 	args[ATTRELNAME_ARG] = unused;
 	args[ATTNAME_ARG] = unused;
 	args[ATTNUM_ARG] = unused;
 	args[INHERITED_ARG] = unused;
-
 	args[NULL_FRAC_ARG] = *null_frac;
 	args[AVG_WIDTH_ARG] = *avg_width;
 	args[N_DISTINCT_ARG] = *n_distinct;
diff --git a/src/backend/statistics/extended_stats_funcs.c b/src/backend/statistics/extended_stats_funcs.c
index 9fa448cc18b..5fadd3efa3d 100644
--- a/src/backend/statistics/extended_stats_funcs.c
+++ b/src/backend/statistics/extended_stats_funcs.c
@@ -45,7 +45,8 @@
  */
 enum extended_stats_argnum
 {
-	RELSCHEMA_ARG = 0,
+	VERSION_ARG = 0,
+	RELSCHEMA_ARG,
 	RELNAME_ARG,
 	STATSCHEMA_ARG,
 	STATNAME_ARG,
@@ -65,6 +66,7 @@ enum extended_stats_argnum
  */
 static struct StatsArgInfo extarginfo[] =
 {
+	[VERSION_ARG] = {"version", INT4OID},
 	[RELSCHEMA_ARG] = {"schemaname", TEXTOID},
 	[RELNAME_ARG] = {"relname", TEXTOID},
 	[STATSCHEMA_ARG] = {"statistics_schemaname", TEXTOID},
diff --git a/src/backend/statistics/relation_stats.c b/src/backend/statistics/relation_stats.c
index 392aeed5d25..47d2da29bca 100644
--- a/src/backend/statistics/relation_stats.c
+++ b/src/backend/statistics/relation_stats.c
@@ -39,7 +39,8 @@
 
 enum relation_stats_argnum
 {
-	RELSCHEMA_ARG = 0,
+	VERSION_ARG = 0,
+	RELSCHEMA_ARG,
 	RELNAME_ARG,
 	RELPAGES_ARG,
 	RELTUPLES_ARG,
@@ -50,6 +51,7 @@ enum relation_stats_argnum
 
 static struct StatsArgInfo relarginfo[] =
 {
+	[VERSION_ARG] = {"version", INT4OID},
 	[RELSCHEMA_ARG] = {"schemaname", TEXTOID},
 	[RELNAME_ARG] = {"relname", TEXTOID},
 	[RELPAGES_ARG] = {"relpages", INT4OID},
@@ -225,6 +227,8 @@ pg_clear_relation_stats(PG_FUNCTION_ARGS)
 {
 	NullableDatum args[NUM_RELATION_STATS_ARGS];
 
+	args[VERSION_ARG].value = (Datum) 0;
+	args[VERSION_ARG].isnull = true;
 	args[RELSCHEMA_ARG].value = PG_GETARG_DATUM(0);
 	args[RELSCHEMA_ARG].isnull = PG_ARGISNULL(0);
 	args[RELNAME_ARG].value = PG_GETARG_DATUM(1);
@@ -281,9 +285,9 @@ import_relation_statistics(Relation rel,
 	Assert(relallvisible);
 	Assert(relallfrozen);
 
+	args[VERSION_ARG] = (version) ? *version : unused;
 	args[RELSCHEMA_ARG] = unused;
 	args[RELNAME_ARG] = unused;
-
 	args[RELPAGES_ARG] = *relpages;
 	args[RELTUPLES_ARG] = *reltuples;
 	args[RELALLVISIBLE_ARG] = *relallvisible;
diff --git a/src/backend/statistics/stat_utils.c b/src/backend/statistics/stat_utils.c
index 4befc887cd2..0642b66c06d 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)

