v2-0006-Make-attribute_statistics_update-stop-using-fcinf.patch

application/octet-stream

Filename: v2-0006-Make-attribute_statistics_update-stop-using-fcinf.patch
Type: application/octet-stream
Part: 5
Message: Re: use of SPI by postgresImportForeignStatistics

Patch

Format: format-patch
Series: patch v2-0006
Subject: Make attribute_statistics_update stop using fcinfo.
File+
src/backend/statistics/attribute_stats.c 46 46
From f8fd160b831c765f1364398e93c722f1edaeddb1 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 v2 06/13] 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 | 92 ++++++++++++------------
 1 file changed, 46 insertions(+), 46 deletions(-)

diff --git a/src/backend/statistics/attribute_stats.c b/src/backend/statistics/attribute_stats.c
index fbb52131ed0..a847d053ee4 100644
--- a/src/backend/statistics/attribute_stats.c
+++ b/src/backend/statistics/attribute_stats.c
@@ -104,7 +104,7 @@ static struct StatsArgInfo cleararginfo[] =
 	[C_ATTARG_NUM_ATTARGS] = {0}
 };
 
-static bool attribute_statistics_update(FunctionCallInfo fcinfo);
+static bool attribute_statistics_update(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);
@@ -126,7 +126,7 @@ static bool delete_pg_statistic(Oid reloid, AttrNumber attnum, bool stainherit);
  * and other statistic kinds may still be updated.
  */
 static bool
-attribute_statistics_update(FunctionCallInfo fcinfo)
+attribute_statistics_update(const NullableDatum *args)
 {
 	char	   *nspname;
 	char	   *relname;
@@ -151,16 +151,16 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 
 	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};
@@ -168,11 +168,11 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 
 	bool		result = true;
 
-	stats_check_required_arg(fcinfo->args, attarginfo, ATTARG_ATTRELSCHEMA);
-	stats_check_required_arg(fcinfo->args, attarginfo, ATTARG_ATTRELNAME);
+	stats_check_required_arg(args, attarginfo, ATTARG_ATTRELSCHEMA);
+	stats_check_required_arg(args, attarginfo, ATTARG_ATTRELNAME);
 
-	nspname = TextDatumGetCString(PG_GETARG_DATUM(ATTARG_ATTRELSCHEMA));
-	relname = TextDatumGetCString(PG_GETARG_DATUM(ATTARG_ATTRELNAME));
+	nspname = TextDatumGetCString(args[ATTARG_ATTRELSCHEMA].value);
+	relname = TextDatumGetCString(args[ATTARG_ATTRELNAME].value);
 
 	if (RecoveryInProgress())
 		ereport(ERROR,
@@ -186,13 +186,13 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 									  RangeVarCallbackForStats, &locked_table);
 
 	/* user can specify either attname or attnum, but not both */
-	if (!PG_ARGISNULL(ATTARG_ATTNAME))
+	if (!args[ATTARG_ATTNAME].isnull)
 	{
-		if (!PG_ARGISNULL(ATTARG_ATTNUM))
+		if (!args[ATTARG_ATTNUM].isnull)
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 					 errmsg("cannot specify both \"%s\" and \"%s\"", "attname", "attnum")));
-		attname = TextDatumGetCString(PG_GETARG_DATUM(ATTARG_ATTNAME));
+		attname = TextDatumGetCString(args[ATTARG_ATTNAME].value);
 		attnum = get_attnum(reloid, attname);
 		/* note that this test covers attisdropped cases too: */
 		if (attnum == InvalidAttrNumber)
@@ -201,9 +201,9 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 					 errmsg("column \"%s\" of relation \"%s\" does not exist",
 							attname, relname)));
 	}
-	else if (!PG_ARGISNULL(ATTARG_ATTNUM))
+	else if (!args[ATTARG_ATTNUM].isnull)
 	{
-		attnum = PG_GETARG_INT16(ATTARG_ATTNUM);
+		attnum = DatumGetInt16(args[ATTARG_ATTNUM].value);
 		attname = get_attname(reloid, attnum, true);
 		/* annoyingly, get_attname doesn't check attisdropped */
 		if (attname == NULL ||
@@ -228,39 +228,39 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 				 errmsg("cannot modify statistics on system column \"%s\"",
 						attname)));
 
-	stats_check_required_arg(fcinfo->args, attarginfo, ATTARG_INHERITED);
-	inherited = PG_GETARG_BOOL(ATTARG_INHERITED);
+	stats_check_required_arg(args, attarginfo, ATTARG_INHERITED);
+	inherited = DatumGetBool(args[ATTARG_INHERITED].value);
 
 	/*
 	 * Check argument sanity. If some arguments are unusable, emit a WARNING
 	 * 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))
 	{
@@ -268,7 +268,7 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 		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))
 	{
@@ -344,19 +344,19 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 								 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;
 	}
 
@@ -364,10 +364,10 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 	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);
 
@@ -407,7 +407,7 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 
 		stavalues = statatt_build_stavalues("histogram_bounds",
 											&array_in_fn,
-											PG_GETARG_DATUM(ATTARG_HISTOGRAM_BOUNDS),
+											args[ATTARG_HISTOGRAM_BOUNDS].value,
 											atttypid, atttypmod,
 											&converted);
 
@@ -425,7 +425,7 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 	/* 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);
 
@@ -438,13 +438,13 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 	/* 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);
 
@@ -462,7 +462,7 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 	/* 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,
@@ -484,7 +484,7 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 
 		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);
 
@@ -503,7 +503,7 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 	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);
 
@@ -512,7 +512,7 @@ attribute_statistics_update(FunctionCallInfo fcinfo)
 
 		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)
@@ -683,7 +683,7 @@ pg_restore_attribute_stats(PG_FUNCTION_ARGS)
 										  attarginfo))
 		result = false;
 
-	if (!attribute_statistics_update(positional_fcinfo))
+	if (!attribute_statistics_update(positional_fcinfo->args))
 		result = false;
 
 	PG_RETURN_BOOL(result);
-- 
2.50.1 (Apple Git-155)