Skip to content

Commit 9fd0252

Browse files
committed
Replace hardcoded identifiers of pgstats file by #defines
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
1 parent dd56921 commit 9fd0252

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/backend/utils/activity/pgstat.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@
127127

128128
#define PGSTAT_SNAPSHOT_HASH_SIZE 512
129129

130+
/* ---------
131+
* Identifiers in stats file.
132+
* ---------
133+
*/
134+
#define PGSTAT_FILE_ENTRY_END 'E' /* end of file */
135+
#define PGSTAT_FILE_ENTRY_NAME 'N' /* stats entry identified by name */
136+
#define PGSTAT_FILE_ENTRY_HASH 'S' /* stats entry identified by
137+
* PgStat_HashKey */
130138

131139
/* hash table for statistics snapshots entry */
132140
typedef struct PgStat_SnapshotEntry
@@ -1431,7 +1439,7 @@ pgstat_write_statsfile(void)
14311439
if (!kind_info->to_serialized_name)
14321440
{
14331441
/* normal stats entry, identified by PgStat_HashKey */
1434-
fputc('S', fpout);
1442+
fputc(PGSTAT_FILE_ENTRY_HASH, fpout);
14351443
write_chunk_s(fpout, &ps->key);
14361444
}
14371445
else
@@ -1441,7 +1449,7 @@ pgstat_write_statsfile(void)
14411449

14421450
kind_info->to_serialized_name(&ps->key, shstats, &name);
14431451

1444-
fputc('N', fpout);
1452+
fputc(PGSTAT_FILE_ENTRY_NAME, fpout);
14451453
write_chunk_s(fpout, &ps->key.kind);
14461454
write_chunk_s(fpout, &name);
14471455
}
@@ -1458,7 +1466,7 @@ pgstat_write_statsfile(void)
14581466
* pgstat.stat with it. The ferror() check replaces testing for error
14591467
* after each individual fputc or fwrite (in write_chunk()) above.
14601468
*/
1461-
fputc('E', fpout);
1469+
fputc(PGSTAT_FILE_ENTRY_END, fpout);
14621470

14631471
if (ferror(fpout))
14641472
{
@@ -1569,16 +1577,16 @@ pgstat_read_statsfile(void)
15691577

15701578
switch (t)
15711579
{
1572-
case 'S':
1573-
case 'N':
1580+
case PGSTAT_FILE_ENTRY_HASH:
1581+
case PGSTAT_FILE_ENTRY_NAME:
15741582
{
15751583
PgStat_HashKey key;
15761584
PgStatShared_HashEntry *p;
15771585
PgStatShared_Common *header;
15781586

15791587
CHECK_FOR_INTERRUPTS();
15801588

1581-
if (t == 'S')
1589+
if (t == PGSTAT_FILE_ENTRY_HASH)
15821590
{
15831591
/* normal stats entry, identified by PgStat_HashKey */
15841592
if (!read_chunk_s(fpin, &key))
@@ -1644,8 +1652,12 @@ pgstat_read_statsfile(void)
16441652

16451653
break;
16461654
}
1647-
case 'E':
1648-
/* check that 'E' actually signals end of file */
1655+
case PGSTAT_FILE_ENTRY_END:
1656+
1657+
/*
1658+
* check that PGSTAT_FILE_ENTRY_END actually signals end of
1659+
* file
1660+
*/
16491661
if (fgetc(fpin) != EOF)
16501662
goto error;
16511663

0 commit comments

Comments
 (0)