Switch PgStat_Kind from an enum to a uint32 type
authorMichael Paquier <[email protected]>
Thu, 1 Aug 2024 19:49:34 +0000 (04:49 +0900)
committerMichael Paquier <[email protected]>
Thu, 1 Aug 2024 19:49:34 +0000 (04:49 +0900)
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/[email protected]

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

index 81484222cffdb6c941185d259cb3e072883ac3f5..143478aca0f0cff558bea96ad27321f281aa36b8 100644 (file)
@@ -183,7 +183,7 @@ static void pgstat_prep_snapshot(void);
 static void pgstat_build_snapshot(void);
 static void pgstat_build_snapshot_fixed(PgStat_Kind kind);
 
-static inline bool pgstat_is_kind_valid(int ikind);
+static inline bool pgstat_is_kind_valid(PgStat_Kind kind);
 
 
 /* ----------
@@ -1089,7 +1089,7 @@ pgstat_build_snapshot(void)
    /*
     * Build snapshot of all fixed-numbered stats.
     */
-   for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
+   for (PgStat_Kind kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
    {
        const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
 
@@ -1286,7 +1286,7 @@ pgstat_flush_pending_entries(bool nowait)
 PgStat_Kind
 pgstat_get_kind_from_str(char *kind_str)
 {
-   for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
+   for (PgStat_Kind kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
    {
        if (pg_strcasecmp(kind_str, pgstat_kind_infos[kind].name) == 0)
            return kind;
@@ -1299,9 +1299,9 @@ pgstat_get_kind_from_str(char *kind_str)
 }
 
 static inline bool
-pgstat_is_kind_valid(int ikind)
+pgstat_is_kind_valid(PgStat_Kind kind)
 {
-   return ikind >= PGSTAT_KIND_FIRST_VALID && ikind <= PGSTAT_KIND_LAST;
+   return kind >= PGSTAT_KIND_FIRST_VALID && kind <= PGSTAT_KIND_LAST;
 }
 
 const PgStat_KindInfo *
@@ -1393,7 +1393,7 @@ pgstat_write_statsfile(XLogRecPtr redo)
    write_chunk_s(fpout, &redo);
 
    /* Write various stats structs for fixed number of objects */
-   for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
+   for (PgStat_Kind kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
    {
        char       *ptr;
        const PgStat_KindInfo *info = pgstat_get_kind_info(kind);
@@ -1777,7 +1777,7 @@ pgstat_reset_after_failure(void)
    TimestampTz ts = GetCurrentTimestamp();
 
    /* reset fixed-numbered stats */
-   for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
+   for (PgStat_Kind kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
    {
        const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
 
index 1c2b69d5634690de7e0a32a16489d8413d44bedd..2d5f7d46de76cae5e24990bcbb4f539c1474859a 100644 (file)
@@ -197,7 +197,7 @@ StatsShmemInit(void)
        pg_atomic_init_u64(&ctl->gc_request_count, 1);
 
        /* initialize fixed-numbered stats */
-       for (int kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
+       for (PgStat_Kind kind = PGSTAT_KIND_FIRST_VALID; kind <= PGSTAT_KIND_LAST; kind++)
        {
            const PgStat_KindInfo *kind_info = pgstat_get_kind_info(kind);
            char       *ptr;
index 043d39a5651d795ba898dbc4a0667a180e4c17bb..f84e9fdeca5972778033fee4bf20352fdd7800d5 100644 (file)
 #define PG_STAT_TMP_DIR        "pg_stat_tmp"
 
 /* The types of statistics entries */
-typedef enum PgStat_Kind
-{
-   /* use 0 for INVALID, to catch zero-initialized data */
-   PGSTAT_KIND_INVALID = 0,
-
-   /* stats for variable-numbered objects */
-   PGSTAT_KIND_DATABASE,       /* database-wide statistics */
-   PGSTAT_KIND_RELATION,       /* per-table statistics */
-   PGSTAT_KIND_FUNCTION,       /* per-function statistics */
-   PGSTAT_KIND_REPLSLOT,       /* per-slot statistics */
-   PGSTAT_KIND_SUBSCRIPTION,   /* per-subscription statistics */
-
-   /* stats for fixed-numbered objects */
-   PGSTAT_KIND_ARCHIVER,
-   PGSTAT_KIND_BGWRITER,
-   PGSTAT_KIND_CHECKPOINTER,
-   PGSTAT_KIND_IO,
-   PGSTAT_KIND_SLRU,
-   PGSTAT_KIND_WAL,
-} PgStat_Kind;
+#define PgStat_Kind uint32
+
+/* use 0 for INVALID, to catch zero-initialized data */
+#define PGSTAT_KIND_INVALID 0
+
+/* stats for variable-numbered objects */
+#define PGSTAT_KIND_DATABASE   1   /* database-wide statistics */
+#define PGSTAT_KIND_RELATION   2   /* per-table statistics */
+#define PGSTAT_KIND_FUNCTION   3   /* per-function statistics */
+#define PGSTAT_KIND_REPLSLOT   4   /* per-slot statistics */
+#define PGSTAT_KIND_SUBSCRIPTION   5   /* per-subscription statistics */
+
+/* stats for fixed-numbered objects */
+#define PGSTAT_KIND_ARCHIVER   6
+#define PGSTAT_KIND_BGWRITER   7
+#define PGSTAT_KIND_CHECKPOINTER   8
+#define PGSTAT_KIND_IO 9
+#define PGSTAT_KIND_SLRU   10
+#define PGSTAT_KIND_WAL    11
 
 #define PGSTAT_KIND_FIRST_VALID PGSTAT_KIND_DATABASE
 #define PGSTAT_KIND_LAST PGSTAT_KIND_WAL