Simplify some stats restore code with InputFunctionCallSafe()

statatt_build_stavalues() and array_in_safe() have been relying on
InitFunctionCallInfoData() with a locally-filled state to call a data
type input function.  InputFunctionCallSafe() can be used to achieve the
same job, simplifying some code.

This fixes an over-allocation of FunctionCallInfoBaseData done in
statatt_build_stavalues(), where there was space for 8 elements but only
3 were needed.  The over-allocation exists since REL_18_STABLE, and was
harmless in practice.

While on it, fix some comments for both routines, where elemtypid was
mentioned.

Backpatch down to v19.  This code has been reworked during the last
development cycle while working on the restore of extended statistics,
so this keeps the code consistent across all branches.

Author: Jian He <jian.universality@gmail.com>
Author: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CACJufxEGah9PaiTQ=cG14GMMBsUQ3ohGct9tdSwbMQPQ0-nbbQ@mail.gmail.com
Backpatch-through: 19
This commit is contained in:
Michael Paquier 2026-06-30 08:30:52 +09:00
parent 1a7fa06dbc
commit ac536a4061
2 changed files with 9 additions and 31 deletions

View file

@ -1033,7 +1033,7 @@ jbv_to_infunc_datum(JsonbValue *jval, PGFunction func, AttrNumber exprnum,
}
/*
* Build an array datum with element type elemtypid from a text datum, used as
* Build an array datum with element type typid from a text datum, used as
* value of an attribute in a pg_statistic tuple.
*
* If an error is encountered, capture it, and reduce the elevel to WARNING.
@ -1044,7 +1044,6 @@ static Datum
array_in_safe(FmgrInfo *array_in, const char *s, Oid typid, int32 typmod,
AttrNumber exprnum, const char *element_name, bool *ok)
{
LOCAL_FCINFO(fcinfo, 3);
Datum result;
ErrorSaveContext escontext = {
@ -1053,17 +1052,6 @@ array_in_safe(FmgrInfo *array_in, const char *s, Oid typid, int32 typmod,
};
*ok = false;
InitFunctionCallInfoData(*fcinfo, array_in, 3, InvalidOid,
(Node *) &escontext, NULL);
fcinfo->args[0].value = CStringGetDatum(s);
fcinfo->args[0].isnull = false;
fcinfo->args[1].value = ObjectIdGetDatum(typid);
fcinfo->args[1].isnull = false;
fcinfo->args[2].value = Int32GetDatum(typmod);
fcinfo->args[2].isnull = false;
result = FunctionCallInvoke(fcinfo);
/*
* If the array_in function returned an error, we will want to report that
@ -1071,7 +1059,8 @@ array_in_safe(FmgrInfo *array_in, const char *s, Oid typid, int32 typmod,
* Overwriting the existing hint (if any) is not ideal, and an error
* context would only work for level >= ERROR.
*/
if (escontext.error_occurred)
if (!InputFunctionCallSafe(array_in, (char *) s, typid, typmod,
(Node *) &escontext, &result))
{
StringInfoData hint_str;

View file

@ -555,7 +555,7 @@ statatt_get_elem_type(Oid atttypid, char atttyptype,
}
/*
* Build an array with element type elemtypid from a text datum, used as
* Build an array with element type typid from a text datum, used as
* value of an attribute in a tuple to-be-inserted into pg_statistic.
*
* The typid and typmod should be derived from a previous call to
@ -569,7 +569,6 @@ Datum
statatt_build_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid typid,
int32 typmod, bool *ok)
{
LOCAL_FCINFO(fcinfo, 8);
char *s;
Datum result;
ErrorSaveContext escontext = {T_ErrorSaveContext};
@ -578,28 +577,18 @@ statatt_build_stavalues(const char *staname, FmgrInfo *array_in, Datum d, Oid ty
s = TextDatumGetCString(d);
InitFunctionCallInfoData(*fcinfo, array_in, 3, InvalidOid,
(Node *) &escontext, NULL);
fcinfo->args[0].value = CStringGetDatum(s);
fcinfo->args[0].isnull = false;
fcinfo->args[1].value = ObjectIdGetDatum(typid);
fcinfo->args[1].isnull = false;
fcinfo->args[2].value = Int32GetDatum(typmod);
fcinfo->args[2].isnull = false;
result = FunctionCallInvoke(fcinfo);
pfree(s);
if (escontext.error_occurred)
if (!InputFunctionCallSafe(array_in, s, typid, typmod,
(Node *) &escontext, &result))
{
pfree(s);
escontext.error_data->elevel = WARNING;
ThrowErrorData(escontext.error_data);
*ok = false;
return (Datum) 0;
}
pfree(s);
if (ARR_NDIM(DatumGetArrayTypeP(result)) != 1)
{
ereport(WARNING,