From 489778d7056a5fd3fe1609dbe74533a44a91eab4 Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Wed, 19 Aug 2026 23:23:22 +0900 Subject: [PATCH v1] Report specific SQLSTATEs for stats restore errors The pg_restore_*_stats() functions could report SQLSTATE XX000 for invalid variadic arguments, such as an unmatched name/value pair, a NULL argument name, or a non-text argument name. Attribute and extended statistics restores could also report XX000 when the supplied statistics exceed the number of slots PostgreSQL can store. These are not internal errors. They result from invalid caller input or a PostgreSQL implementation limit, but the lack of specific SQLSTATEs made clients treat them as internal errors. Assign appropriate SQLSTATEs to these errors so that applications and tests can classify them correctly. Backpatch to v18, where pg_restore_relation_stats() and pg_restore_attribute_stats() were introduced. --- src/backend/statistics/stat_utils.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/backend/statistics/stat_utils.c b/src/backend/statistics/stat_utils.c index 5ff37ef4cf8..f4ff9ab9b20 100644 --- a/src/backend/statistics/stat_utils.c +++ b/src/backend/statistics/stat_utils.c @@ -370,8 +370,9 @@ stats_fill_fcinfo_from_arg_pairs(FunctionCallInfo pairs_fcinfo, if (nargs % 2 != 0) ereport(ERROR, - errmsg("variadic arguments must be name/value pairs"), - errhint("Provide an even number of variadic arguments that can be divided into pairs.")); + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("variadic arguments must be name/value pairs"), + errhint("Provide an even number of variadic arguments that can be divided into pairs."))); /* * For each argument name/value pair, find corresponding positional @@ -385,11 +386,13 @@ stats_fill_fcinfo_from_arg_pairs(FunctionCallInfo pairs_fcinfo, if (argnulls[i]) ereport(ERROR, - (errmsg("name at variadic position %d is null", i + 1))); + (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED), + errmsg("name at variadic position %d is null", i + 1))); if (types[i] != TEXTOID) ereport(ERROR, - (errmsg("name at variadic position %d has type %s, expected type %s", + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("name at variadic position %d has type %s, expected type %s", i + 1, format_type_be(types[i]), format_type_be(TEXTOID)))); @@ -654,7 +657,8 @@ statatt_set_slot(Datum *values, bool *nulls, bool *replaces, if (slotidx >= STATISTIC_NUM_SLOTS) ereport(ERROR, - (errmsg("maximum number of statistics slots exceeded: %d", + (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("maximum number of statistics slots exceeded: %d", slotidx + 1))); stakind_attnum = Anum_pg_statistic_stakind1 - 1 + slotidx; -- 2.55.0