]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Rename routines for write/read of pgstats file
authorMichael Paquier <michael@paquier.xyz>
Tue, 25 Nov 2025 01:55:40 +0000 (10:55 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 25 Nov 2025 01:55:40 +0000 (10:55 +0900)
This commit renames write_chunk and read_chunk to respectively
pgstat_write_chunk() and pgstat_read_chunk(), along with the *_s
convenience macros.

These are made available for plug-ins, so as any code that decides to
write and/or read stats data can rely on a single code path for this
work.

Extracted from a larger patch by the same author.

Author: Sami Imseih <samimseih@gmail.com>
Discussion: https://postgr.es/m/CAA5RZ0s9SDOu+Z6veoJCHWk+kDeTktAtC-KY9fQ9Z6BJdDUirQ@mail.gmail.com

src/backend/utils/activity/pgstat.c
src/include/utils/pgstat_internal.h

index 7ef06150df7fba3e9150f407f1d7f494df9441a6..8713c7a048340f03945af2733d3d80f7874094e9 100644 (file)
@@ -1551,20 +1551,18 @@ pgstat_assert_is_up(void)
  * ------------------------------------------------------------
  */
 
-/* helpers for pgstat_write_statsfile() */
-static void
-write_chunk(FILE *fpout, void *ptr, size_t len)
+/* helper for pgstat_write_statsfile() */
+void
+pgstat_write_chunk(FILE *fpout, void *ptr, size_t len)
 {
        int                     rc;
 
        rc = fwrite(ptr, len, 1, fpout);
 
-       /* we'll check for errors with ferror once at the end */
+       /* We check for errors with ferror() when done writing the stats. */
        (void) rc;
 }
 
-#define write_chunk_s(fpout, ptr) write_chunk(fpout, ptr, sizeof(*ptr))
-
 /*
  * This function is called in the last process that is accessing the shared
  * stats so locking is not required.
@@ -1606,7 +1604,7 @@ pgstat_write_statsfile(void)
         * Write the file header --- currently just a format ID.
         */
        format_id = PGSTAT_FILE_FORMAT_ID;
-       write_chunk_s(fpout, &format_id);
+       pgstat_write_chunk_s(fpout, &format_id);
 
        /* Write various stats structs for fixed number of objects */
        for (PgStat_Kind kind = PGSTAT_KIND_MIN; kind <= PGSTAT_KIND_MAX; kind++)
@@ -1631,8 +1629,8 @@ pgstat_write_statsfile(void)
                        ptr = pgStatLocal.snapshot.custom_data[kind - PGSTAT_KIND_CUSTOM_MIN];
 
                fputc(PGSTAT_FILE_ENTRY_FIXED, fpout);
-               write_chunk_s(fpout, &kind);
-               write_chunk(fpout, ptr, info->shared_data_len);
+               pgstat_write_chunk_s(fpout, &kind);
+               pgstat_write_chunk(fpout, ptr, info->shared_data_len);
        }
 
        /*
@@ -1686,7 +1684,7 @@ pgstat_write_statsfile(void)
                {
                        /* normal stats entry, identified by PgStat_HashKey */
                        fputc(PGSTAT_FILE_ENTRY_HASH, fpout);
-                       write_chunk_s(fpout, &ps->key);
+                       pgstat_write_chunk_s(fpout, &ps->key);
                }
                else
                {
@@ -1696,21 +1694,21 @@ pgstat_write_statsfile(void)
                        kind_info->to_serialized_name(&ps->key, shstats, &name);
 
                        fputc(PGSTAT_FILE_ENTRY_NAME, fpout);
-                       write_chunk_s(fpout, &ps->key.kind);
-                       write_chunk_s(fpout, &name);
+                       pgstat_write_chunk_s(fpout, &ps->key.kind);
+                       pgstat_write_chunk_s(fpout, &name);
                }
 
                /* Write except the header part of the entry */
-               write_chunk(fpout,
-                                       pgstat_get_entry_data(ps->key.kind, shstats),
-                                       pgstat_get_entry_len(ps->key.kind));
+               pgstat_write_chunk(fpout,
+                                                  pgstat_get_entry_data(ps->key.kind, shstats),
+                                                  pgstat_get_entry_len(ps->key.kind));
        }
        dshash_seq_term(&hstat);
 
        /*
         * No more output to be done. Close the temp file and replace the old
         * pgstat.stat with it.  The ferror() check replaces testing for error
-        * after each individual fputc or fwrite (in write_chunk()) above.
+        * after each individual fputc or fwrite (in pgstat_write_chunk()) above.
         */
        fputc(PGSTAT_FILE_ENTRY_END, fpout);
 
@@ -1738,15 +1736,13 @@ pgstat_write_statsfile(void)
        }
 }
 
-/* helpers for pgstat_read_statsfile() */
-static bool
-read_chunk(FILE *fpin, void *ptr, size_t len)
+/* helper for pgstat_read_statsfile() */
+bool
+pgstat_read_chunk(FILE *fpin, void *ptr, size_t len)
 {
        return fread(ptr, 1, len, fpin) == len;
 }
 
-#define read_chunk_s(fpin, ptr) read_chunk(fpin, ptr, sizeof(*ptr))
-
 /*
  * Reads in existing statistics file into memory.
  *
@@ -1790,7 +1786,7 @@ pgstat_read_statsfile(void)
        /*
         * Verify it's of the expected format.
         */
-       if (!read_chunk_s(fpin, &format_id))
+       if (!pgstat_read_chunk_s(fpin, &format_id))
        {
                elog(WARNING, "could not read format ID");
                goto error;
@@ -1820,7 +1816,7 @@ pgstat_read_statsfile(void)
                                        char       *ptr;
 
                                        /* entry for fixed-numbered stats */
-                                       if (!read_chunk_s(fpin, &kind))
+                                       if (!pgstat_read_chunk_s(fpin, &kind))
                                        {
                                                elog(WARNING, "could not read stats kind for entry of type %c", t);
                                                goto error;
@@ -1860,7 +1856,7 @@ pgstat_read_statsfile(void)
                                                        info->shared_data_off;
                                        }
 
-                                       if (!read_chunk(fpin, ptr, info->shared_data_len))
+                                       if (!pgstat_read_chunk(fpin, ptr, info->shared_data_len))
                                        {
                                                elog(WARNING, "could not read data of stats kind %u for entry of type %c with size %u",
                                                         kind, t, info->shared_data_len);
@@ -1881,7 +1877,7 @@ pgstat_read_statsfile(void)
                                        if (t == PGSTAT_FILE_ENTRY_HASH)
                                        {
                                                /* normal stats entry, identified by PgStat_HashKey */
-                                               if (!read_chunk_s(fpin, &key))
+                                               if (!pgstat_read_chunk_s(fpin, &key))
                                                {
                                                        elog(WARNING, "could not read key for entry of type %c", t);
                                                        goto error;
@@ -1910,12 +1906,12 @@ pgstat_read_statsfile(void)
                                                PgStat_Kind kind;
                                                NameData        name;
 
-                                               if (!read_chunk_s(fpin, &kind))
+                                               if (!pgstat_read_chunk_s(fpin, &kind))
                                                {
                                                        elog(WARNING, "could not read stats kind for entry of type %c", t);
                                                        goto error;
                                                }
-                                               if (!read_chunk_s(fpin, &name))
+                                               if (!pgstat_read_chunk_s(fpin, &name))
                                                {
                                                        elog(WARNING, "could not read name of stats kind %u for entry of type %c",
                                                                 kind, t);
@@ -1990,9 +1986,9 @@ pgstat_read_statsfile(void)
                                                         key.objid, t);
                                        }
 
-                                       if (!read_chunk(fpin,
-                                                                       pgstat_get_entry_data(key.kind, header),
-                                                                       pgstat_get_entry_len(key.kind)))
+                                       if (!pgstat_read_chunk(fpin,
+                                                                                  pgstat_get_entry_data(key.kind, header),
+                                                                                  pgstat_get_entry_len(key.kind)))
                                        {
                                                elog(WARNING, "could not read data for entry %u/%u/%" PRIu64 " of type %c",
                                                         key.kind, key.dboid,
index 4d2b8aa6081a5b133d1a457972991c6f12e99b5b..ca1ba6420ca19d1ba8f3a3b33bf9ffb838f2aeae 100644 (file)
@@ -818,6 +818,12 @@ extern PGDLLIMPORT bool pgstat_report_fixed;
 /* Backend-local stats state */
 extern PGDLLIMPORT PgStat_LocalState pgStatLocal;
 
+/* Helper functions for reading and writing of on-disk stats file */
+extern void pgstat_write_chunk(FILE *fpout, void *ptr, size_t len);
+extern bool pgstat_read_chunk(FILE *fpin, void *ptr, size_t len);
+#define pgstat_read_chunk_s(fpin, ptr) pgstat_read_chunk(fpin, ptr, sizeof(*ptr))
+#define pgstat_write_chunk_s(fpout, ptr) pgstat_write_chunk(fpout, ptr, sizeof(*ptr))
+
 /*
  * Implementation of inline functions declared above.
  */