From 6cd3a9ad9a31e9d1f8d57815b556642f0a7f2b7d Mon Sep 17 00:00:00 2001
From: Ubuntu <ubuntu@ip-172-31-46-230.ec2.internal>
Date: Wed, 17 Dec 2025 16:23:13 +0000
Subject: [PATCH v1 1/1] Fix const correctness in pgstat serialization
 callbacks

4ba012a8ed9c defined the header in from_serialized_data as const, even
though the callback may modify it when reconstructing entry state.
Also update the to_serialized_data callback in test_custom_stats to
make the header parameter const since it should not be modified.

This eliminates unsafe const casts and clarifies the API contract.

Reported-By: Peter Eisentraut <peter@eisentraut.org>
Author: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Sami Imseih <samimseih@gmail.com>
Discussion: https://postgr.es/m/d87a93b0-19c7-4db6-b9c0-d6827e7b2da1%40eisentraut.org
---
 src/include/utils/pgstat_internal.h                        | 5 +++--
 src/test/modules/test_custom_stats/test_custom_var_stats.c | 6 +++---
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/include/utils/pgstat_internal.h b/src/include/utils/pgstat_internal.h
index 5c1ce4d3d6a..67f7071fbfd 100644
--- a/src/include/utils/pgstat_internal.h
+++ b/src/include/utils/pgstat_internal.h
@@ -329,13 +329,14 @@ typedef struct PgStat_KindInfo
 	 *
 	 * "statfile" is a pointer to the on-disk stats file, named
 	 * PGSTAT_STAT_PERMANENT_FILENAME.  "key" is the hash key of the entry
-	 * just written or read.  "header" is a pointer to the stats data.
+	 * just written or read.  "header" is a pointer to the stats data; it may
+	 * be modified only in from_serialized_data to reconstruct entry state.
 	 */
 	void		(*to_serialized_data) (const PgStat_HashKey *key,
 									   const PgStatShared_Common *header,
 									   FILE *statfile);
 	bool		(*from_serialized_data) (const PgStat_HashKey *key,
-										 const PgStatShared_Common *header,
+										 PgStatShared_Common *header,
 										 FILE *statfile);
 
 	/*
diff --git a/src/test/modules/test_custom_stats/test_custom_var_stats.c b/src/test/modules/test_custom_stats/test_custom_var_stats.c
index c71922dc4a8..294085d6866 100644
--- a/src/test/modules/test_custom_stats/test_custom_var_stats.c
+++ b/src/test/modules/test_custom_stats/test_custom_var_stats.c
@@ -92,7 +92,7 @@ static void test_custom_stats_var_to_serialized_data(const PgStat_HashKey *key,
 
 /* Deserialization callback: read auxiliary entry data */
 static bool test_custom_stats_var_from_serialized_data(const PgStat_HashKey *key,
-													   const PgStatShared_Common *header,
+													   PgStatShared_Common *header,
 													   FILE *statfile);
 
 /* Finish callback: end of statistics file operations */
@@ -196,7 +196,7 @@ test_custom_stats_var_to_serialized_data(const PgStat_HashKey *key,
 {
 	char	   *description;
 	size_t		len;
-	PgStatShared_CustomVarEntry *entry = (PgStatShared_CustomVarEntry *) header;
+	const PgStatShared_CustomVarEntry *entry = (const PgStatShared_CustomVarEntry *) header;
 	bool		found;
 	uint32		magic_number = TEST_CUSTOM_VAR_MAGIC_NUMBER;
 
@@ -276,7 +276,7 @@ test_custom_stats_var_to_serialized_data(const PgStat_HashKey *key,
  */
 static bool
 test_custom_stats_var_from_serialized_data(const PgStat_HashKey *key,
-										   const PgStatShared_Common *header,
+										   PgStatShared_Common *header,
 										   FILE *statfile)
 {
 	PgStatShared_CustomVarEntry *entry;
-- 
2.43.0

