From 9d99c3840e92f94984ab742bba0d13c7aa0f3563 Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Sun, 28 Jun 2026 15:26:23 -0500
Subject: [PATCH v3 04/15] Stop using fcinfo in stats_check functions.

Change the stats_check utlity functions from taking a full
FunctionCallInfo and instead just use the NullableData array, which is
all the functions needed anyway.

Callers can now pass in fcinfo->args instead of fcinfo as an
intermediate step towards removing FunctionCallInfo entirely.
---
 src/backend/statistics/attribute_stats.c      | 26 +++++++++----------
 src/backend/statistics/extended_stats_funcs.c | 20 +++++++-------
 src/backend/statistics/relation_stats.c       |  4 +--
 src/backend/statistics/stat_utils.c           | 20 +++++++-------
 src/include/statistics/stat_utils.h           |  7 ++---
 5 files changed, 39 insertions(+), 38 deletions(-)

diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index 1e988456c78..fb64871b5b5 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -141,8 +141,8 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 	bool		inherited;
 	Oid			locked_table = InvalidOid;
 
-	stats_check_required_arg(fcinfo, attarginfo, ATTARG_ATTRELSCHEMA);
-	stats_check_required_arg(fcinfo, attarginfo, ATTARG_ATTRELNAME);
+	stats_check_required_arg(fcinfo->args, attarginfo, ATTARG_ATTRELSCHEMA);
+	stats_check_required_arg(fcinfo->args, attarginfo, ATTARG_ATTRELNAME);
 
 	nspname = TextDatumGetCString(PG_GETARG_DATUM(ATTARG_ATTRELSCHEMA));
 	relname = TextDatumGetCString(PG_GETARG_DATUM(ATTARG_ATTRELNAME));
@@ -201,7 +201,7 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 				 errmsg("cannot modify statistics on system column \"%s\"",
 						attname)));
 
-	stats_check_required_arg(fcinfo, attarginfo, ATTARG_INHERITED);
+	stats_check_required_arg(fcinfo->args, attarginfo, ATTARG_INHERITED);
 	inherited = PG_GETARG_BOOL(ATTARG_INHERITED);
 
 	return attribute_statistics_update_internal(reloid, attname, attnum,
@@ -253,31 +253,31 @@ attribute_statistics_update_internal(Oid reloid,
 	 * and set the corresponding argument to NULL in fcinfo.
 	 */
 
-	if (!stats_check_arg_array(fcinfo, attarginfo, ATTARG_MOST_COMMON_FREQS))
+	if (!stats_check_arg_array(fcinfo->args, attarginfo, ATTARG_MOST_COMMON_FREQS))
 	{
 		do_mcv = false;
 		result = false;
 	}
 
-	if (!stats_check_arg_array(fcinfo, attarginfo, ATTARG_MOST_COMMON_ELEM_FREQS))
+	if (!stats_check_arg_array(fcinfo->args, attarginfo, ATTARG_MOST_COMMON_ELEM_FREQS))
 	{
 		do_mcelem = false;
 		result = false;
 	}
-	if (!stats_check_arg_array(fcinfo, attarginfo, ATTARG_ELEM_COUNT_HISTOGRAM))
+	if (!stats_check_arg_array(fcinfo->args, attarginfo, ATTARG_ELEM_COUNT_HISTOGRAM))
 	{
 		do_dechist = false;
 		result = false;
 	}
 
-	if (!stats_check_arg_pair(fcinfo, attarginfo,
+	if (!stats_check_arg_pair(fcinfo->args, attarginfo,
 							  ATTARG_MOST_COMMON_VALS, ATTARG_MOST_COMMON_FREQS))
 	{
 		do_mcv = false;
 		result = false;
 	}
 
-	if (!stats_check_arg_pair(fcinfo, attarginfo,
+	if (!stats_check_arg_pair(fcinfo->args, attarginfo,
 							  ATTARG_MOST_COMMON_ELEMS,
 							  ATTARG_MOST_COMMON_ELEM_FREQS))
 	{
@@ -285,7 +285,7 @@ attribute_statistics_update_internal(Oid reloid,
 		result = false;
 	}
 
-	if (!stats_check_arg_pair(fcinfo, attarginfo,
+	if (!stats_check_arg_pair(fcinfo->args, attarginfo,
 							  ATTARG_RANGE_LENGTH_HISTOGRAM,
 							  ATTARG_RANGE_EMPTY_FRAC))
 	{
@@ -623,10 +623,10 @@ pg_clear_attribute_stats(PG_FUNCTION_ARGS)
 	bool		inherited;
 	Oid			locked_table = InvalidOid;
 
-	stats_check_required_arg(fcinfo, cleararginfo, C_ATTARG_ATTRELSCHEMA);
-	stats_check_required_arg(fcinfo, cleararginfo, C_ATTARG_ATTRELNAME);
-	stats_check_required_arg(fcinfo, cleararginfo, C_ATTARG_ATTNAME);
-	stats_check_required_arg(fcinfo, cleararginfo, C_ATTARG_INHERITED);
+	stats_check_required_arg(fcinfo->args, cleararginfo, C_ATTARG_ATTRELSCHEMA);
+	stats_check_required_arg(fcinfo->args, cleararginfo, C_ATTARG_ATTRELNAME);
+	stats_check_required_arg(fcinfo->args, cleararginfo, C_ATTARG_ATTNAME);
+	stats_check_required_arg(fcinfo->args, cleararginfo, C_ATTARG_INHERITED);
 
 	nspname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTARG_ATTRELSCHEMA));
 	relname = TextDatumGetCString(PG_GETARG_DATUM(C_ATTARG_ATTRELNAME));
diff --git a/src/backend/statistics/extended_stats_funcs.c b/src/backend/statistics/extended_stats_funcs.c
index 4f42cb1298e..9b11ecd6356 100644
--- a/src/backend/statistics/extended_stats_funcs.c
+++ b/src/backend/statistics/extended_stats_funcs.c
@@ -373,17 +373,17 @@ extended_statistics_update(FunctionCallInfo fcinfo)
 	}
 
 	/* relation arguments */
-	stats_check_required_arg(fcinfo, extarginfo, EXTARG_RELSCHEMA);
+	stats_check_required_arg(fcinfo->args, extarginfo, EXTARG_RELSCHEMA);
 	relnspname = TextDatumGetCString(PG_GETARG_DATUM(EXTARG_RELSCHEMA));
-	stats_check_required_arg(fcinfo, extarginfo, EXTARG_RELNAME);
+	stats_check_required_arg(fcinfo->args, extarginfo, EXTARG_RELNAME);
 	relname = TextDatumGetCString(PG_GETARG_DATUM(EXTARG_RELNAME));
 
 	/* extended statistics arguments */
-	stats_check_required_arg(fcinfo, extarginfo, EXTARG_STATSCHEMA);
+	stats_check_required_arg(fcinfo->args, extarginfo, EXTARG_STATSCHEMA);
 	nspname = TextDatumGetCString(PG_GETARG_DATUM(EXTARG_STATSCHEMA));
-	stats_check_required_arg(fcinfo, extarginfo, EXTARG_STATNAME);
+	stats_check_required_arg(fcinfo->args, extarginfo, EXTARG_STATNAME);
 	stxname = TextDatumGetCString(PG_GETARG_DATUM(EXTARG_STATNAME));
-	stats_check_required_arg(fcinfo, extarginfo, EXTARG_INHERITED);
+	stats_check_required_arg(fcinfo->args, extarginfo, EXTARG_INHERITED);
 	inherited = PG_GETARG_BOOL(EXTARG_INHERITED);
 
 	/*
@@ -1758,17 +1758,17 @@ pg_clear_extended_stats(PG_FUNCTION_ARGS)
 	Oid			locked_table = InvalidOid;
 
 	/* relation arguments */
-	stats_check_required_arg(fcinfo, extarginfo, EXTARG_RELSCHEMA);
+	stats_check_required_arg(fcinfo->args, extarginfo, EXTARG_RELSCHEMA);
 	relnspname = TextDatumGetCString(PG_GETARG_DATUM(EXTARG_RELSCHEMA));
-	stats_check_required_arg(fcinfo, extarginfo, EXTARG_RELNAME);
+	stats_check_required_arg(fcinfo->args, extarginfo, EXTARG_RELNAME);
 	relname = TextDatumGetCString(PG_GETARG_DATUM(EXTARG_RELNAME));
 
 	/* extended statistics arguments */
-	stats_check_required_arg(fcinfo, extarginfo, EXTARG_STATSCHEMA);
+	stats_check_required_arg(fcinfo->args, extarginfo, EXTARG_STATSCHEMA);
 	nspname = TextDatumGetCString(PG_GETARG_DATUM(EXTARG_STATSCHEMA));
-	stats_check_required_arg(fcinfo, extarginfo, EXTARG_STATNAME);
+	stats_check_required_arg(fcinfo->args, extarginfo, EXTARG_STATNAME);
 	stxname = TextDatumGetCString(PG_GETARG_DATUM(EXTARG_STATNAME));
-	stats_check_required_arg(fcinfo, extarginfo, EXTARG_INHERITED);
+	stats_check_required_arg(fcinfo->args, extarginfo, EXTARG_INHERITED);
 	inherited = PG_GETARG_BOOL(EXTARG_INHERITED);
 
 	if (RecoveryInProgress())
diff --git a/src/backend/statistics/relation_stats.c b/src/backend/statistics/relation_stats.c
index 37b4ab71655..deac37cbab0 100644
--- a/src/backend/statistics/relation_stats.c
+++ b/src/backend/statistics/relation_stats.c
@@ -74,8 +74,8 @@ relation_statistics_update(FunctionCallInfo fcinfo)
 	Oid			reloid;
 	Oid			locked_table = InvalidOid;
 
-	stats_check_required_arg(fcinfo, relarginfo, RELARG_SCHEMA);
-	stats_check_required_arg(fcinfo, relarginfo, RELARG_RELNAME);
+	stats_check_required_arg(fcinfo->args, relarginfo, RELARG_SCHEMA);
+	stats_check_required_arg(fcinfo->args, relarginfo, RELARG_RELNAME);
 
 	nspname = TextDatumGetCString(PG_GETARG_DATUM(RELARG_SCHEMA));
 	relname = TextDatumGetCString(PG_GETARG_DATUM(RELARG_RELNAME));
diff --git a/src/backend/statistics/stat_utils.c b/src/backend/statistics/stat_utils.c
index 5fb6a91d95b..f248db97959 100644
--- a/src/backend/statistics/stat_utils.c
+++ b/src/backend/statistics/stat_utils.c
@@ -51,11 +51,11 @@ static Node *statatt_get_index_expr(Relation rel, int attnum);
  * Ensure that a given argument is not null.
  */
 void
-stats_check_required_arg(FunctionCallInfo fcinfo,
+stats_check_required_arg(const NullableDatum *args,
 						 struct StatsArgInfo *arginfo,
 						 int argnum)
 {
-	if (PG_ARGISNULL(argnum))
+	if (args[argnum].isnull)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 				 errmsg("argument \"%s\" must not be null",
@@ -70,16 +70,16 @@ stats_check_required_arg(FunctionCallInfo fcinfo,
  * true.
  */
 bool
-stats_check_arg_array(FunctionCallInfo fcinfo,
+stats_check_arg_array(const NullableDatum *args,
 					  struct StatsArgInfo *arginfo,
 					  int argnum)
 {
 	ArrayType  *arr;
 
-	if (PG_ARGISNULL(argnum))
+	if (args[argnum].isnull)
 		return true;
 
-	arr = DatumGetArrayTypeP(PG_GETARG_DATUM(argnum));
+	arr = DatumGetArrayTypeP(args[argnum].value);
 
 	if (ARR_NDIM(arr) != 1)
 	{
@@ -111,17 +111,17 @@ stats_check_arg_array(FunctionCallInfo fcinfo,
  * true.
  */
 bool
-stats_check_arg_pair(FunctionCallInfo fcinfo,
+stats_check_arg_pair(const NullableDatum *args,
 					 struct StatsArgInfo *arginfo,
 					 int argnum1, int argnum2)
 {
-	if (PG_ARGISNULL(argnum1) && PG_ARGISNULL(argnum2))
+	if (args[argnum1].isnull && args[argnum2].isnull)
 		return true;
 
-	if (PG_ARGISNULL(argnum1) || PG_ARGISNULL(argnum2))
+	if (args[argnum1].isnull || args[argnum2].isnull)
 	{
-		int			nullarg = PG_ARGISNULL(argnum1) ? argnum1 : argnum2;
-		int			otherarg = PG_ARGISNULL(argnum1) ? argnum2 : argnum1;
+		int			nullarg = args[argnum1].isnull ? argnum1 : argnum2;
+		int			otherarg = args[argnum1].isnull ? argnum2 : argnum1;
 
 		ereport(WARNING,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
diff --git a/src/include/statistics/stat_utils.h b/src/include/statistics/stat_utils.h
index 15e962dbb7c..db7b770bd32 100644
--- a/src/include/statistics/stat_utils.h
+++ b/src/include/statistics/stat_utils.h
@@ -15,6 +15,7 @@
 
 #include "access/attnum.h"
 #include "fmgr.h"
+#include "postgres.h"
 
 /* avoid including primnodes.h here */
 typedef struct RangeVar RangeVar;
@@ -25,12 +26,12 @@ struct StatsArgInfo
 	Oid			argtype;
 };
 
-extern void stats_check_required_arg(FunctionCallInfo fcinfo,
+extern void stats_check_required_arg(const NullableDatum *args,
 									 struct StatsArgInfo *arginfo,
 									 int argnum);
-extern bool stats_check_arg_array(FunctionCallInfo fcinfo,
+extern bool stats_check_arg_array(const NullableDatum *args,
 								  struct StatsArgInfo *arginfo, int argnum);
-extern bool stats_check_arg_pair(FunctionCallInfo fcinfo,
+extern bool stats_check_arg_pair(const NullableDatum *args,
 								 struct StatsArgInfo *arginfo,
 								 int argnum1, int argnum2);
 
-- 
2.50.1 (Apple Git-155)

