Skip to content

Commit 9e4664d

Browse files
committed
Add a new 'F' entry type for fixed-numbered stats in pgstats file
This new entry type is used for all the fixed-numbered statistics, making possible support for custom pluggable stats. In short, we need to be able to detect more easily if a stats kind exists or not when reading back its data from the pgstats file without a dependency on the order of the entries read. The kind ID of the stats is added to the data written. The data is written in the same fashion as previously, with the fixed-numbered stats first and the dshash entries next. The read part becomes more flexible, loading fixed-numbered stats into shared memory based on the new entry type found. Bump PGSTAT_FILE_FORMAT_ID. Reviewed-by: Bertrand Drouvot Discussion: https://postgr.es/m/Zot5bxoPYdS7yaoy@paquier.xyz
1 parent 21471f1 commit 9e4664d

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/backend/utils/activity/pgstat.c

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
* ---------
133133
*/
134134
#define PGSTAT_FILE_ENTRY_END 'E' /* end of file */
135+
#define PGSTAT_FILE_ENTRY_FIXED 'F' /* fixed-numbered stats entry */
135136
#define PGSTAT_FILE_ENTRY_NAME 'N' /* stats entry identified by name */
136137
#define PGSTAT_FILE_ENTRY_HASH 'S' /* stats entry identified by
137138
* PgStat_HashKey */
@@ -1396,6 +1397,9 @@ pgstat_write_statsfile(void)
13961397

13971398
pgstat_build_snapshot_fixed(kind);
13981399
ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
1400+
1401+
fputc(PGSTAT_FILE_ENTRY_FIXED, fpout);
1402+
write_chunk_s(fpout, &kind);
13991403
write_chunk(fpout, ptr, info->shared_data_len);
14001404
}
14011405

@@ -1537,32 +1541,43 @@ pgstat_read_statsfile(void)
15371541
format_id != PGSTAT_FILE_FORMAT_ID)
15381542
goto error;
15391543

1540-
/* Read various stats structs with fixed number of objects */
1541-
for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1542-
{
1543-
char *ptr;
1544-
const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
1545-
1546-
if (!info->fixed_amount)
1547-
continue;
1548-
1549-
Assert(info->shared_ctl_off != 0);
1550-
1551-
ptr = ((char *) shmem) + info->shared_ctl_off + info->shared_data_off;
1552-
if (!read_chunk(fpin, ptr, info->shared_data_len))
1553-
goto error;
1554-
}
1555-
15561544
/*
1557-
* We found an existing statistics file. Read it and put all the hash
1558-
* table entries into place.
1545+
* We found an existing statistics file. Read it and put all the stats
1546+
* data into place.
15591547
*/
15601548
for (;;)
15611549
{
15621550
int t = fgetc(fpin);
15631551

15641552
switch (t)
15651553
{
1554+
case PGSTAT_FILE_ENTRY_FIXED:
1555+
{
1556+
PgStat_Kind kind;
1557+
const PgStat_KindInfo *info;
1558+
char *ptr;
1559+
1560+
/* entry for fixed-numbered stats */
1561+
if (!read_chunk_s(fpin, &kind))
1562+
goto error;
1563+
1564+
if (!pgstat_is_kind_valid(kind))
1565+
goto error;
1566+
1567+
info = pgstat_get_kind_info(kind);
1568+
1569+
if (!info->fixed_amount)
1570+
goto error;
1571+
1572+
/* Load back stats into shared memory */
1573+
ptr = ((char *) shmem) + info->shared_ctl_off +
1574+
info->shared_data_off;
1575+
1576+
if (!read_chunk(fpin, ptr, info->shared_data_len))
1577+
goto error;
1578+
1579+
break;
1580+
}
15661581
case PGSTAT_FILE_ENTRY_HASH:
15671582
case PGSTAT_FILE_ENTRY_NAME:
15681583
{

src/include/pgstat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ typedef struct PgStat_TableXactStatus
235235
* ------------------------------------------------------------
236236
*/
237237

238-
#define PGSTAT_FILE_FORMAT_ID 0x01A5BCAC
238+
#define PGSTAT_FILE_FORMAT_ID 0x01A5BCAD
239239

240240
typedef struct PgStat_ArchiverStats
241241
{

0 commit comments

Comments
 (0)