From eac7821f16eb19ab4b68e419e16ca86583ae84e3 Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Sun, 28 Jun 2026 16:38:56 -0500
Subject: [PATCH v3 06/15] Make attribute_statistics_update stop using fcinfo.

Change the function signature of attribute_statistics_update to use a
NullableDatum array instead of a full FunctionCallInfo.
---
 src/backend/statistics/attribute_stats.c | 72 ++++++++++++------------
 1 file changed, 36 insertions(+), 36 deletions(-)

diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index fb64871b5b5..455bce7b2cf 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -109,7 +109,7 @@ static bool attribute_statistics_update_internal(Oid reloid,
 												 const char *attname,
 												 AttrNumber attnum,
 												 bool inherited,
-												 FunctionCallInfo fcinfo);
+												 const NullableDatum *args);
 static void upsert_pg_statistic(Relation starel, HeapTuple oldtup,
 								const Datum *values, const bool *nulls, const bool *replaces);
 static bool delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit);
@@ -205,7 +205,7 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 	inherited = PG_GETARG_BOOL(ATTARG_INHERITED);
 
 	return attribute_statistics_update_internal(reloid, attname, attnum,
-												inherited, fcinfo);
+												inherited, fcinfo->args);
 }
 
 /*
@@ -214,7 +214,7 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 static bool
 attribute_statistics_update_internal(Oid reloid,
 									 const char *attname, AttrNumber attnum,
-									 bool inherited, FunctionCallInfo fcinfo)
+									 bool inherited, const NullableDatum *args)
 {
 	Relation	starel;
 	HeapTuple	statup;
@@ -231,16 +231,16 @@ attribute_statistics_update_internal(Oid reloid,
 
 	FmgrInfo	array_in_fn;
 
-	bool		do_mcv = !PG_ARGISNULL(ATTARG_MOST_COMMON_FREQS) &&
-		!PG_ARGISNULL(ATTARG_MOST_COMMON_VALS);
-	bool		do_histogram = !PG_ARGISNULL(ATTARG_HISTOGRAM_BOUNDS);
-	bool		do_correlation = !PG_ARGISNULL(ATTARG_CORRELATION);
-	bool		do_mcelem = !PG_ARGISNULL(ATTARG_MOST_COMMON_ELEMS) &&
-		!PG_ARGISNULL(ATTARG_MOST_COMMON_ELEM_FREQS);
-	bool		do_dechist = !PG_ARGISNULL(ATTARG_ELEM_COUNT_HISTOGRAM);
-	bool		do_bounds_histogram = !PG_ARGISNULL(ATTARG_RANGE_BOUNDS_HISTOGRAM);
-	bool		do_range_length_histogram = !PG_ARGISNULL(ATTARG_RANGE_LENGTH_HISTOGRAM) &&
-		!PG_ARGISNULL(ATTARG_RANGE_EMPTY_FRAC);
+	bool		do_mcv = !args[ATTARG_MOST_COMMON_FREQS].isnull &&
+		!args[ATTARG_MOST_COMMON_VALS].isnull;
+	bool		do_histogram = !args[ATTARG_HISTOGRAM_BOUNDS].isnull;
+	bool		do_correlation = !args[ATTARG_CORRELATION].isnull;
+	bool		do_mcelem = !args[ATTARG_MOST_COMMON_ELEMS].isnull &&
+		!args[ATTARG_MOST_COMMON_ELEM_FREQS].isnull;
+	bool		do_dechist = !args[ATTARG_ELEM_COUNT_HISTOGRAM].isnull;
+	bool		do_bounds_histogram = !args[ATTARG_RANGE_BOUNDS_HISTOGRAM].isnull;
+	bool		do_range_length_histogram = !args[ATTARG_RANGE_LENGTH_HISTOGRAM].isnull &&
+		!args[ATTARG_RANGE_EMPTY_FRAC].isnull;
 
 	Datum		values[Natts_pg_statistic] = {0};
 	bool		nulls[Natts_pg_statistic] = {0};
@@ -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->args, attarginfo, ATTARG_MOST_COMMON_FREQS))
+	if (!stats_check_arg_array(args, attarginfo, ATTARG_MOST_COMMON_FREQS))
 	{
 		do_mcv = false;
 		result = false;
 	}
 
-	if (!stats_check_arg_array(fcinfo->args, attarginfo, ATTARG_MOST_COMMON_ELEM_FREQS))
+	if (!stats_check_arg_array(args, attarginfo, ATTARG_MOST_COMMON_ELEM_FREQS))
 	{
 		do_mcelem = false;
 		result = false;
 	}
-	if (!stats_check_arg_array(fcinfo->args, attarginfo, ATTARG_ELEM_COUNT_HISTOGRAM))
+	if (!stats_check_arg_array(args, attarginfo, ATTARG_ELEM_COUNT_HISTOGRAM))
 	{
 		do_dechist = false;
 		result = false;
 	}
 
-	if (!stats_check_arg_pair(fcinfo->args, attarginfo,
+	if (!stats_check_arg_pair(args, attarginfo,
 							  ATTARG_MOST_COMMON_VALS, ATTARG_MOST_COMMON_FREQS))
 	{
 		do_mcv = false;
 		result = false;
 	}
 
-	if (!stats_check_arg_pair(fcinfo->args, attarginfo,
+	if (!stats_check_arg_pair(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->args, attarginfo,
+	if (!stats_check_arg_pair(args, attarginfo,
 							  ATTARG_RANGE_LENGTH_HISTOGRAM,
 							  ATTARG_RANGE_EMPTY_FRAC))
 	{
@@ -361,19 +361,19 @@ attribute_statistics_update_internal(Oid reloid,
 								 replaces);
 
 	/* if specified, set to argument values */
-	if (!PG_ARGISNULL(ATTARG_NULL_FRAC))
+	if (!args[ATTARG_NULL_FRAC].isnull)
 	{
-		values[Anum_pg_statistic_stanullfrac - 1] = PG_GETARG_DATUM(ATTARG_NULL_FRAC);
+		values[Anum_pg_statistic_stanullfrac - 1] = args[ATTARG_NULL_FRAC].value;
 		replaces[Anum_pg_statistic_stanullfrac - 1] = true;
 	}
-	if (!PG_ARGISNULL(ATTARG_AVG_WIDTH))
+	if (!args[ATTARG_AVG_WIDTH].isnull)
 	{
-		values[Anum_pg_statistic_stawidth - 1] = PG_GETARG_DATUM(ATTARG_AVG_WIDTH);
+		values[Anum_pg_statistic_stawidth - 1] = args[ATTARG_AVG_WIDTH].value;
 		replaces[Anum_pg_statistic_stawidth - 1] = true;
 	}
-	if (!PG_ARGISNULL(ATTARG_N_DISTINCT))
+	if (!args[ATTARG_N_DISTINCT].isnull)
 	{
-		values[Anum_pg_statistic_stadistinct - 1] = PG_GETARG_DATUM(ATTARG_N_DISTINCT);
+		values[Anum_pg_statistic_stadistinct - 1] = args[ATTARG_N_DISTINCT].value;
 		replaces[Anum_pg_statistic_stadistinct - 1] = true;
 	}
 
@@ -381,10 +381,10 @@ attribute_statistics_update_internal(Oid reloid,
 	if (do_mcv)
 	{
 		bool		converted;
-		Datum		stanumbers = PG_GETARG_DATUM(ATTARG_MOST_COMMON_FREQS);
+		Datum		stanumbers = args[ATTARG_MOST_COMMON_FREQS].value;
 		Datum		stavalues = statatt_build_stavalues("most_common_vals",
 														&array_in_fn,
-														PG_GETARG_DATUM(ATTARG_MOST_COMMON_VALS),
+														args[ATTARG_MOST_COMMON_VALS].value,
 														atttypid, atttypmod,
 														&converted);
 
@@ -424,7 +424,7 @@ attribute_statistics_update_internal(Oid reloid,
 
 		stavalues = statatt_build_stavalues("histogram_bounds",
 											&array_in_fn,
-											PG_GETARG_DATUM(ATTARG_HISTOGRAM_BOUNDS),
+											args[ATTARG_HISTOGRAM_BOUNDS].value,
 											atttypid, atttypmod,
 											&converted);
 
@@ -442,7 +442,7 @@ attribute_statistics_update_internal(Oid reloid,
 	/* STATISTIC_KIND_CORRELATION */
 	if (do_correlation)
 	{
-		Datum		elems[] = {PG_GETARG_DATUM(ATTARG_CORRELATION)};
+		Datum		elems[] = {args[ATTARG_CORRELATION].value};
 		ArrayType  *arry = construct_array_builtin(elems, 1, FLOAT4OID);
 		Datum		stanumbers = PointerGetDatum(arry);
 
@@ -455,13 +455,13 @@ attribute_statistics_update_internal(Oid reloid,
 	/* STATISTIC_KIND_MCELEM */
 	if (do_mcelem)
 	{
-		Datum		stanumbers = PG_GETARG_DATUM(ATTARG_MOST_COMMON_ELEM_FREQS);
+		Datum		stanumbers = args[ATTARG_MOST_COMMON_ELEM_FREQS].value;
 		bool		converted = false;
 		Datum		stavalues;
 
 		stavalues = statatt_build_stavalues("most_common_elems",
 											&array_in_fn,
-											PG_GETARG_DATUM(ATTARG_MOST_COMMON_ELEMS),
+											args[ATTARG_MOST_COMMON_ELEMS].value,
 											elemtypid, atttypmod,
 											&converted);
 
@@ -479,7 +479,7 @@ attribute_statistics_update_internal(Oid reloid,
 	/* STATISTIC_KIND_DECHIST */
 	if (do_dechist)
 	{
-		Datum		stanumbers = PG_GETARG_DATUM(ATTARG_ELEM_COUNT_HISTOGRAM);
+		Datum		stanumbers = args[ATTARG_ELEM_COUNT_HISTOGRAM].value;
 
 		statatt_set_slot(values, nulls, replaces,
 						 STATISTIC_KIND_DECHIST,
@@ -501,7 +501,7 @@ attribute_statistics_update_internal(Oid reloid,
 
 		stavalues = statatt_build_stavalues("range_bounds_histogram",
 											&array_in_fn,
-											PG_GETARG_DATUM(ATTARG_RANGE_BOUNDS_HISTOGRAM),
+											args[ATTARG_RANGE_BOUNDS_HISTOGRAM].value,
 											atttypid, atttypmod,
 											&converted);
 
@@ -521,7 +521,7 @@ attribute_statistics_update_internal(Oid reloid,
 	if (do_range_length_histogram)
 	{
 		/* The anyarray is always a float8[] for this stakind */
-		Datum		elems[] = {PG_GETARG_DATUM(ATTARG_RANGE_EMPTY_FRAC)};
+		Datum		elems[] = {args[ATTARG_RANGE_EMPTY_FRAC].value};
 		ArrayType  *arry = construct_array_builtin(elems, 1, FLOAT4OID);
 		Datum		stanumbers = PointerGetDatum(arry);
 
@@ -530,7 +530,7 @@ attribute_statistics_update_internal(Oid reloid,
 
 		stavalues = statatt_build_stavalues("range_length_histogram",
 											&array_in_fn,
-											PG_GETARG_DATUM(ATTARG_RANGE_LENGTH_HISTOGRAM),
+											args[ATTARG_RANGE_LENGTH_HISTOGRAM].value,
 											FLOAT8OID, 0, &converted);
 
 		if (converted)
@@ -788,7 +788,7 @@ import_attribute_statistics(Relation rel, AttrNumber attnum, bool inherited,
 	newfcinfo->args[ATTARG_RANGE_BOUNDS_HISTOGRAM] = *range_bounds_histogram;
 
 	return attribute_statistics_update_internal(reloid, attname, attnum,
-												inherited, newfcinfo);
+												inherited, newfcinfo->args);
 }
 
 /*
-- 
2.50.1 (Apple Git-155)

