Fix const correctness in pgstat data serialization callbacks master github/master
authorMichael Paquier <[email protected]>
Wed, 17 Dec 2025 22:33:40 +0000 (07:33 +0900)
committerMichael Paquier <[email protected]>
Wed, 17 Dec 2025 22:33:40 +0000 (07:33 +0900)
4ba012a8ed9c defined the "header" (pointer to the stats data) of
from_serialized_data() as a const, even though it is fine (and
expected!) for the callback to modify the shared memory entry when
loading the stats at startup.

While on it, this commit updates the callback to_serialized_data() in
the test module test_custom_stats to make the data extracted from the
"header" parameter a const since it should never be modified: the stats
are written to disk and no modifications are expected in the shared
memory entry.

This clarifies the API contract of these new callbacks.

Reported-By: Peter Eisentraut <[email protected]>
Author: Michael Paquier <[email protected]>
Co-authored-by: Sami Imseih <[email protected]>
Discussion: https://postgr.es/m/d87a93b0-19c7-4db6-b9c0-d6827e7b2da1@eisentraut.org

src/include/utils/pgstat_internal.h
src/test/modules/test_custom_stats/test_custom_var_stats.c

index 5c1ce4d3d6af00176af4f5848934cf26ee2f0296..7dffab8dbdd8446198591e50c34511e9131ddee8 100644 (file)
@@ -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
     *
     * "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 an entry.
     */
    void        (*to_serialized_data) (const PgStat_HashKey *key,
                                       const PgStatShared_Common *header,
                                       FILE *statfile);
    bool        (*from_serialized_data) (const PgStat_HashKey *key,
     */
    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);
 
    /*
                                         FILE *statfile);
 
    /*
index c71922dc4a8f15f2858a9127dd35633eba8c6ce6..294085d68667d056a5f1126d4d7a47c70b17a9a7 100644 (file)
@@ -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,
 
 /* 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 */
                                                       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;
 {
    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;
 
    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,
  */
 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;
                                           FILE *statfile)
 {
    PgStatShared_CustomVarEntry *entry;