]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Simplify some stats restore code with InputFunctionCallSafe()
authorMichael Paquier <michael@paquier.xyz>
Mon, 29 Jun 2026 23:30:08 +0000 (08:30 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 29 Jun 2026 23:30:08 +0000 (08:30 +0900)
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

src/backend/statistics/extended_stats_funcs.c
src/backend/statistics/stat_utils.c

index 2cb3942056f9ae7b94818e4d5c0fe896cb78a369..a5dce8a220641f3c2151fecf6983ff64e2614511 100644 (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;
 
index a673e3c704b25726d50a370753d43a5dabb426ad..0b190e8823703e8116f7e54b52552c436f791ebc 100644 (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,