Adding new PgStat_WalCounters structure in pgstat.h
authorMichael Paquier <michael@paquier.xyz>
Wed, 26 Feb 2025 07:48:54 +0000 (16:48 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 26 Feb 2025 07:48:54 +0000 (16:48 +0900)
This new structure contains the counters and the data related to the WAL
activity statistics gathered from WalUsage, separated into its own
structure so as it can be shared across more than one Stats structure in
pg_stat.h.

This refactoring will be used by an upcoming patch introducing
backend-level WAL statistics.

Bump PGSTAT_FILE_FORMAT_ID.

Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Discussion: https://postgr.es/m/Z3zqc4o09dM/Ezyz@ip-10-97-1-34.eu-west-3.compute.internal

src/backend/utils/activity/pgstat_wal.c
src/backend/utils/adt/pgstatfuncs.c
src/include/pgstat.h
src/tools/pgindent/typedefs.list

index 7beee2095a859a7f0695a31a1ad8bbdd4ece8ee2..943be0cbeefd85a1e59ad3459d5dbf6b9bab316e 100644 (file)
@@ -106,7 +106,7 @@ pgstat_wal_flush_cb(bool nowait)
        return true;
 
 #define WALSTAT_ACC(fld, var_to_add) \
-   (stats_shmem->stats.fld += var_to_add.fld)
+   (stats_shmem->stats.wal_counters.fld += var_to_add.fld)
    WALSTAT_ACC(wal_records, wal_usage_diff);
    WALSTAT_ACC(wal_fpi, wal_usage_diff);
    WALSTAT_ACC(wal_bytes, wal_usage_diff);
index 68e16e52ab6dab2d0db19d56f85db5c73e33354e..0ea41299e07966896d07614e30b5752fae9f309f 100644 (file)
@@ -1643,6 +1643,7 @@ pg_stat_get_wal(PG_FUNCTION_ARGS)
    bool        nulls[PG_STAT_GET_WAL_COLS] = {0};
    char        buf[256];
    PgStat_WalStats *wal_stats;
+   PgStat_WalCounters wal_counters;
 
    /* Initialise attributes information in the tuple descriptor */
    tupdesc = CreateTemplateTupleDesc(PG_STAT_GET_WAL_COLS);
@@ -1661,19 +1662,20 @@ pg_stat_get_wal(PG_FUNCTION_ARGS)
 
    /* Get statistics about WAL activity */
    wal_stats = pgstat_fetch_stat_wal();
+   wal_counters = wal_stats->wal_counters;
 
    /* Fill values and NULLs */
-   values[0] = Int64GetDatum(wal_stats->wal_records);
-   values[1] = Int64GetDatum(wal_stats->wal_fpi);
+   values[0] = Int64GetDatum(wal_counters.wal_records);
+   values[1] = Int64GetDatum(wal_counters.wal_fpi);
 
    /* Convert to numeric. */
-   snprintf(buf, sizeof buf, UINT64_FORMAT, wal_stats->wal_bytes);
+   snprintf(buf, sizeof buf, UINT64_FORMAT, wal_counters.wal_bytes);
    values[2] = DirectFunctionCall3(numeric_in,
                                    CStringGetDatum(buf),
                                    ObjectIdGetDatum(0),
                                    Int32GetDatum(-1));
 
-   values[3] = Int64GetDatum(wal_stats->wal_buffers_full);
+   values[3] = Int64GetDatum(wal_counters.wal_buffers_full);
 
    values[4] = TimestampTzGetDatum(wal_stats->stat_reset_timestamp);
 
index e64c527265b9f7884782ab4d1e424f4129217467..67656264b62472cd5435cbd76da0dee60c2f7287 100644 (file)
@@ -212,7 +212,7 @@ typedef struct PgStat_TableXactStatus
  * ------------------------------------------------------------
  */
 
-#define PGSTAT_FILE_FORMAT_ID  0x01A5BCB4
+#define PGSTAT_FILE_FORMAT_ID  0x01A5BCB5
 
 typedef struct PgStat_ArchiverStats
 {
@@ -474,12 +474,29 @@ typedef struct PgStat_StatTabEntry
    PgStat_Counter total_autoanalyze_time;
 } PgStat_StatTabEntry;
 
-typedef struct PgStat_WalStats
+/* ------
+ * PgStat_WalCounters  WAL activity data gathered from WalUsage
+ *
+ * This stores all the counters and data gathered from WalUsage for WAL
+ * activity statistics, separated into its own structure so as this can be
+ * shared across multiple Stats structures.
+ * ------
+ */
+typedef struct PgStat_WalCounters
 {
    PgStat_Counter wal_records;
    PgStat_Counter wal_fpi;
    uint64      wal_bytes;
    PgStat_Counter wal_buffers_full;
+} PgStat_WalCounters;
+
+/* -------
+ * PgStat_WalStats     WAL statistics
+ * -------
+ */
+typedef struct PgStat_WalStats
+{
+   PgStat_WalCounters wal_counters;
    TimestampTz stat_reset_timestamp;
 } PgStat_WalStats;
 
index 1649f5e1011533f378871ed25952869ba3a61ee4..cfbab589d613e73b26af006148538ca52463462f 100644 (file)
@@ -2189,6 +2189,7 @@ PgStat_SubXactStatus
 PgStat_TableCounts
 PgStat_TableStatus
 PgStat_TableXactStatus
+PgStat_WalCounters
 PgStat_WalStats
 PgXmlErrorContext
 PgXmlStrictness