Replace hardcoded identifiers of pgstats file by #defines
authorMichael Paquier <michael@paquier.xyz>
Wed, 3 Jul 2024 04:09:20 +0000 (13:09 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 3 Jul 2024 04:09:20 +0000 (13:09 +0900)
This changes pgstat.c so as the three types of entries that can exist in
a pgstats file are not hardcoded anymore, replacing them with
descriptively-named macros, when reading and writing stats files:
- 'N' for named entries, like replication slot stats.
- 'S' for entries identified by a hash.
- 'E' for the end-of-file

This has come up while working on making this area of the code more
pluggable.  The format of the stats file is unchanged, hence there is no
need to bump PGSTAT_FILE_FORMAT_ID.

Reviewed-by: Bertrand Drouvot
Discussion: https://postgr.es/m/Zmqm9j5EO0I4W8dx@paquier.xyz

src/backend/utils/activity/pgstat.c

index 27783d004a512f93fb6702ebf2cc40c33b8ef899..c37c11b2ecb0b1d2ca7e62fde1f1d8d05a1ace2e 100644 (file)
 
 #define PGSTAT_SNAPSHOT_HASH_SIZE      512
 
+/* ---------
+ * Identifiers in stats file.
+ * ---------
+ */
+#define PGSTAT_FILE_ENTRY_END  'E' /* end of file */
+#define PGSTAT_FILE_ENTRY_NAME 'N' /* stats entry identified by name */
+#define PGSTAT_FILE_ENTRY_HASH 'S' /* stats entry identified by
+                                                                        * PgStat_HashKey */
 
 /* hash table for statistics snapshots entry */
 typedef struct PgStat_SnapshotEntry
@@ -1431,7 +1439,7 @@ pgstat_write_statsfile(void)
                if (!kind_info->to_serialized_name)
                {
                        /* normal stats entry, identified by PgStat_HashKey */
-                       fputc('S', fpout);
+                       fputc(PGSTAT_FILE_ENTRY_HASH, fpout);
                        write_chunk_s(fpout, &ps->key);
                }
                else
@@ -1441,7 +1449,7 @@ pgstat_write_statsfile(void)
 
                        kind_info->to_serialized_name(&ps->key, shstats, &name);
 
-                       fputc('N', fpout);
+                       fputc(PGSTAT_FILE_ENTRY_NAME, fpout);
                        write_chunk_s(fpout, &ps->key.kind);
                        write_chunk_s(fpout, &name);
                }
@@ -1458,7 +1466,7 @@ pgstat_write_statsfile(void)
         * pgstat.stat with it.  The ferror() check replaces testing for error
         * after each individual fputc or fwrite (in write_chunk()) above.
         */
-       fputc('E', fpout);
+       fputc(PGSTAT_FILE_ENTRY_END, fpout);
 
        if (ferror(fpout))
        {
@@ -1569,8 +1577,8 @@ pgstat_read_statsfile(void)
 
                switch (t)
                {
-                       case 'S':
-                       case 'N':
+                       case PGSTAT_FILE_ENTRY_HASH:
+                       case PGSTAT_FILE_ENTRY_NAME:
                                {
                                        PgStat_HashKey key;
                                        PgStatShared_HashEntry *p;
@@ -1578,7 +1586,7 @@ pgstat_read_statsfile(void)
 
                                        CHECK_FOR_INTERRUPTS();
 
-                                       if (t == 'S')
+                                       if (t == PGSTAT_FILE_ENTRY_HASH)
                                        {
                                                /* normal stats entry, identified by PgStat_HashKey */
                                                if (!read_chunk_s(fpin, &key))
@@ -1644,8 +1652,12 @@ pgstat_read_statsfile(void)
 
                                        break;
                                }
-                       case 'E':
-                               /* check that 'E' actually signals end of file */
+                       case PGSTAT_FILE_ENTRY_END:
+
+                               /*
+                                * check that PGSTAT_FILE_ENTRY_END actually signals end of
+                                * file
+                                */
                                if (fgetc(fpin) != EOF)
                                        goto error;