Add write_to_file to PgStat_KindInfo for pgstats kinds
authorMichael Paquier <[email protected]>
Fri, 22 Nov 2024 01:12:26 +0000 (10:12 +0900)
committerMichael Paquier <[email protected]>
Fri, 22 Nov 2024 01:12:26 +0000 (10:12 +0900)
This new field controls if entries of a stats kind should be written or
not to the on-disk pgstats file when shutting down an instance.  This
affects both fixed and variable-numbered kinds.

This is useful for custom statistics by itself, and a  is under
discussion to add a new builtin stats kind where the write of the stats
is not necessary.  All the built-in stats kinds, as well as the two
custom stats kinds in the test module injection_points, set this flag to
"true" for now, so as stats entries are written to the on-disk pgstats
file.

Author: Bertrand Drouvot
Reviewed-by: Nazir Bilal Yavuz
Discussion: https://postgr.es/m/[email protected]

src/backend/utils/activity/pgstat.c
src/include/utils/pgstat_internal.h
src/test/modules/injection_points/injection_stats.c
src/test/modules/injection_points/injection_stats_fixed.c

index ea8c5691e87501704db6e04d389f9cb6c4be8887..6f8d237826603e64e0c0a34228c873b8cc5e081b 100644 (file)
@@ -12,7 +12,8 @@
  * Statistics are loaded from the filesystem during startup (by the startup
  * process), unless preceded by a crash, in which case all stats are
  * discarded. They are written out by the checkpointer process just before
- * shutting down, except when shutting down in immediate mode.
+ * shutting down (if the stats kind allows it), except when shutting down in
+ * immediate mode.
  *
  * Fixed-numbered stats are stored in plain (non-dynamic) shared memory.
  *
@@ -281,6 +282,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
        .name = "database",
 
        .fixed_amount = false,
+       .write_to_file = true,
        /* so pg_stat_database entries can be seen in all databases */
        .accessed_across_databases = true,
 
@@ -297,6 +299,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
        .name = "relation",
 
        .fixed_amount = false,
+       .write_to_file = true,
 
        .shared_size = sizeof(PgStatShared_Relation),
        .shared_data_off = offsetof(PgStatShared_Relation, stats),
@@ -311,6 +314,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
        .name = "function",
 
        .fixed_amount = false,
+       .write_to_file = true,
 
        .shared_size = sizeof(PgStatShared_Function),
        .shared_data_off = offsetof(PgStatShared_Function, stats),
@@ -324,6 +328,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
        .name = "replslot",
 
        .fixed_amount = false,
+       .write_to_file = true,
 
        .accessed_across_databases = true,
 
@@ -340,6 +345,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
        .name = "subscription",
 
        .fixed_amount = false,
+       .write_to_file = true,
        /* so pg_stat_subscription_stats entries can be seen in all databases */
        .accessed_across_databases = true,
 
@@ -359,6 +365,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
        .name = "archiver",
 
        .fixed_amount = true,
+       .write_to_file = true,
 
        .snapshot_ctl_off = offsetof(PgStat_Snapshot, archiver),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, archiver),
@@ -374,6 +381,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
        .name = "bgwriter",
 
        .fixed_amount = true,
+       .write_to_file = true,
 
        .snapshot_ctl_off = offsetof(PgStat_Snapshot, bgwriter),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, bgwriter),
@@ -389,6 +397,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
        .name = "checkpointer",
 
        .fixed_amount = true,
+       .write_to_file = true,
 
        .snapshot_ctl_off = offsetof(PgStat_Snapshot, checkpointer),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, checkpointer),
@@ -404,6 +413,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
        .name = "io",
 
        .fixed_amount = true,
+       .write_to_file = true,
 
        .snapshot_ctl_off = offsetof(PgStat_Snapshot, io),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, io),
@@ -421,6 +431,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
        .name = "slru",
 
        .fixed_amount = true,
+       .write_to_file = true,
 
        .snapshot_ctl_off = offsetof(PgStat_Snapshot, slru),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, slru),
@@ -438,6 +449,7 @@ static const PgStat_KindInfo pgstat_kind_builtin_infos[PGSTAT_KIND_BUILTIN_SIZE]
        .name = "wal",
 
        .fixed_amount = true,
+       .write_to_file = true,
 
        .snapshot_ctl_off = offsetof(PgStat_Snapshot, wal),
        .shared_ctl_off = offsetof(PgStat_ShmemControl, wal),
@@ -1617,6 +1629,10 @@ pgstat_write_statsfile(XLogRecPtr redo)
        if (pgstat_is_kind_builtin(kind))
            Assert(info->snapshot_ctl_off != 0);
 
+       /* skip if no need to write to file */
+       if (!info->write_to_file)
+           continue;
+
        pgstat_build_snapshot_fixed(kind);
        if (pgstat_is_kind_builtin(kind))
            ptr = ((char *) &pgStatLocal.snapshot) + info->snapshot_ctl_off;
@@ -1663,6 +1679,10 @@ pgstat_write_statsfile(XLogRecPtr redo)
        /* if not dropped the valid-entry refcount should exist */
        Assert(pg_atomic_read_u32(&ps->refcount) > 0);
 
+       /* skip if no need to write to file */
+       if (!kind_info->write_to_file)
+           continue;
+
        if (!kind_info->to_serialized_name)
        {
            /* normal stats entry, identified by PgStat_HashKey */
index 437db06910b4199d6db2f4684332a33864977da9..7338bc1e28ec4f770669f155460264b771b41eaf 100644 (file)
@@ -213,6 +213,9 @@ typedef struct PgStat_KindInfo
     */
    bool        accessed_across_databases:1;
 
+   /* Should stats be written to the on-disk stats file? */
+   bool        write_to_file:1;
+
    /*
     * The size of an entry in the shared stats hash table (pointed to by
     * PgStatShared_HashEntry->body).  For fixed-numbered statistics, this is
index d89d0559134fab259a29248060ab6c71b417326a..e16b9db284519201f1f982e30452fd5329c6db27 100644 (file)
@@ -39,6 +39,7 @@ static bool injection_stats_flush_cb(PgStat_EntryRef *entry_ref, bool nowait);
 static const PgStat_KindInfo injection_stats = {
    .name = "injection_points",
    .fixed_amount = false,      /* Bounded by the number of points */
+   .write_to_file = true,
 
    /* Injection points are system-wide */
    .accessed_across_databases = true,
index 2fed178b7a65696e3f0954d7440ee98551513cec..c5b0bb8cf041629eac82e3852a8f2c6d0439343e 100644 (file)
@@ -50,6 +50,7 @@ static void injection_stats_fixed_snapshot_cb(void);
 static const PgStat_KindInfo injection_stats_fixed = {
    .name = "injection_points_fixed",
    .fixed_amount = true,
+   .write_to_file = true,
 
    .shared_size = sizeof(PgStat_StatInjFixedEntry),
    .shared_data_off = offsetof(PgStatShared_InjectionPointFixed, stats),