From 02e7d9fd2ecab9376f564e6d41d578e755c8281e Mon Sep 17 00:00:00 2001
From: Sami Imseih <samimseih.pg@gmail.com>
Date: Wed, 14 Oct 2026 13:59:34 -0500
Subject: [PATCH v2 1/2] Disallow serialized name callbacks for fixed-numbered
 custom stats kinds

to_serialized_name and from_serialized_name are documented as being
for variable-numbered stats only, as they map an entry key to a name
written to the stats file, but pgstat_register_kind() did not enforce
that for custom stats kinds.  Fixed-numbered kinds have exactly one
entry and are never written to the stats file by name, so setting
these callbacks for them makes no sense.

This commit makes pgstat_register_kind() reject a fixed-numbered custom
kind that sets either callback, like it already does for own_hash and
track_entry_count.  This also guarantees that a stats file entry
identified by name can never be matched with a fixed-numbered kind when
reading the stats file at startup.

Reported-by: Zsolt Parragi <zsolt.parragi@percona.com>
Author: Sami Imseih <samimseih.pg@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/CAN4CZFMxuOOuYV14+i3YcPG-16q02eBtSVa82J111=ZnDwSicA@mail.gmail.com
---
 src/backend/utils/activity/pgstat.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activity/pgstat.c
index c393bf7d59e..620d4d79a46 100644
--- a/src/backend/utils/activity/pgstat.c
+++ b/src/backend/utils/activity/pgstat.c
@@ -1592,6 +1592,10 @@ pgstat_register_kind(PgStat_Kind kind, const PgStat_KindInfo *kind_info)
 			ereport(ERROR,
 					(errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
 					 errhint("Custom cumulative statistics cannot use a dedicated hash table for fixed-numbered objects.")));
+		if (kind_info->to_serialized_name || kind_info->from_serialized_name)
+			ereport(ERROR,
+					(errmsg("failed to register custom cumulative statistics \"%s\" with ID %u", kind_info->name, kind),
+					 errhint("Custom cumulative statistics cannot use serialized names for fixed-numbered objects.")));
 	}
 	else
 	{
-- 
2.54.0 (Apple Git-157)

