Skip to content

Commit 3188a45

Browse files
committed
Switch PgStat_Kind from an enum to a uint32 type
A follow-up patch is planned to make cumulative statistics pluggable, and using a type is useful in the internal routines used by pgstats as PgStat_Kind may have a value that was not originally in the enum removed here, once made pluggable. While on it, this commit switches pgstat_is_kind_valid() to use PgStat_Kind rather than an int, to be more consistent with its existing callers. Some loops based on the stats kind IDs are switched to use PgStat_Kind rather than int, for consistency with the new time. Author: Michael Paquier Reviewed-by: Dmitry Dolgov, Bertrand Drouvot Discussion: https://postgr.es/m/Zmqm9j5EO0I4W8dx@paquier.xyz
1 parent b860848 commit 3188a45

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

src/backend/utils/activity/pgstat.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static void pgstat_prep_snapshot(void);
183183
static void pgstat_build_snapshot(void);
184184
static void pgstat_build_snapshot_fixed(PgStat_Kind kind);
185185

186-
static inline bool pgstat_is_kind_valid(int ikind);
186+
static inline bool pgstat_is_kind_valid(PgStat_Kind kind);
187187

188188

189189
/* ----------
@@ -1089,7 +1089,7 @@ pgstat_build_snapshot(void)
10891089
/*
10901090
* Build snapshot of all fixed-numbered stats.
10911091
*/
1092-
for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1092+
for (PgStat_Kind kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
10931093
{
10941094
const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
10951095

@@ -1286,7 +1286,7 @@ pgstat_flush_pending_entries(bool nowait)
12861286
PgStat_Kind
12871287
pgstat_get_kind_from_str(char *kind_str)
12881288
{
1289-
for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1289+
for (PgStat_Kind kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
12901290
{
12911291
if (pg_strcasecmp(kind_str, pgstat_kind_infos[kind].name) == 0)
12921292
return kind;
@@ -1299,9 +1299,9 @@ pgstat_get_kind_from_str(char *kind_str)
12991299
}
13001300

13011301
static inline bool
1302-
pgstat_is_kind_valid(int ikind)
1302+
pgstat_is_kind_valid(PgStat_Kind kind)
13031303
{
1304-
return ikind >= PGSTAT_KIND_FIRST_VALID && ikind <= PGSTAT_KIND_LAST;
1304+
return kind >= PGSTAT_KIND_FIRST_VALID && kind <= PGSTAT_KIND_LAST;
13051305
}
13061306

13071307
const PgStat_KindInfo *
@@ -1393,7 +1393,7 @@ pgstat_write_statsfile(XLogRecPtr redo)
13931393
write_chunk_s(fpout, &redo);
13941394

13951395
/* Write various stats structs for fixed number of objects */
1396-
for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1396+
for (PgStat_Kind kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
13971397
{
13981398
char *ptr;
13991399
const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
@@ -1777,7 +1777,7 @@ pgstat_reset_after_failure(void)
17771777
TimestampTz ts = GetCurrentTimestamp();
17781778

17791779
/* reset fixed-numbered stats */
1780-
for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
1780+
for (PgStat_Kind kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
17811781
{
17821782
const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
17831783

src/backend/utils/activity/pgstat_shmem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ StatsShmemInit(void)
197197
pg_atomic_init_u64(&ctl->gc_request_count, 1);
198198

199199
/* initialize fixed-numbered stats */
200-
for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
200+
for (PgStat_Kind kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
201201
{
202202
const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
203203
char *ptr;

src/include/pgstat.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,25 @@
3333
#define PG_STAT_TMP_DIR "pg_stat_tmp"
3434

3535
/* The types of statistics entries */
36-
typedef enum PgStat_Kind
37-
{
38-
/* use 0 for INVALID, to catch zero-initialized data */
39-
PGSTAT_KIND_INVALID = 0,
40-
41-
/* stats for variable-numbered objects */
42-
PGSTAT_KIND_DATABASE, /* database-wide statistics */
43-
PGSTAT_KIND_RELATION, /* per-table statistics */
44-
PGSTAT_KIND_FUNCTION, /* per-function statistics */
45-
PGSTAT_KIND_REPLSLOT, /* per-slot statistics */
46-
PGSTAT_KIND_SUBSCRIPTION, /* per-subscription statistics */
47-
48-
/* stats for fixed-numbered objects */
49-
PGSTAT_KIND_ARCHIVER,
50-
PGSTAT_KIND_BGWRITER,
51-
PGSTAT_KIND_CHECKPOINTER,
52-
PGSTAT_KIND_IO,
53-
PGSTAT_KIND_SLRU,
54-
PGSTAT_KIND_WAL,
55-
} PgStat_Kind;
36+
#define PgStat_Kind uint32
37+
38+
/* use 0 for INVALID, to catch zero-initialized data */
39+
#define PGSTAT_KIND_INVALID 0
40+
41+
/* stats for variable-numbered objects */
42+
#define PGSTAT_KIND_DATABASE 1 /* database-wide statistics */
43+
#define PGSTAT_KIND_RELATION 2 /* per-table statistics */
44+
#define PGSTAT_KIND_FUNCTION 3 /* per-function statistics */
45+
#define PGSTAT_KIND_REPLSLOT 4 /* per-slot statistics */
46+
#define PGSTAT_KIND_SUBSCRIPTION 5 /* per-subscription statistics */
47+
48+
/* stats for fixed-numbered objects */
49+
#define PGSTAT_KIND_ARCHIVER 6
50+
#define PGSTAT_KIND_BGWRITER 7
51+
#define PGSTAT_KIND_CHECKPOINTER 8
52+
#define PGSTAT_KIND_IO 9
53+
#define PGSTAT_KIND_SLRU 10
54+
#define PGSTAT_KIND_WAL 11
5655

5756
#define PGSTAT_KIND_FIRST_VALID PGSTAT_KIND_DATABASE
5857
#define PGSTAT_KIND_LAST PGSTAT_KIND_WAL

0 commit comments

Comments
 (0)