From a791f83c453e5e94d3508cde526b6d992512dc14 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Thu, 16 Jan 2025 15:06:01 +0000
Subject: [PATCH v8 2/3] Adding a new PgStat_WalCounters struct

This new struct contains only the counters related to the WAL statistics.
This will be used in a follow-up commit that uses the same structures but
for the PGSTAT_KIND_BACKEND statistics kind.
---
 src/backend/utils/activity/pgstat_wal.c |  2 +-
 src/backend/utils/adt/pgstatfuncs.c     | 20 +++++++++++---------
 src/include/pgstat.h                    |  7 ++++++-
 src/tools/pgindent/typedefs.list        |  1 +
 4 files changed, 19 insertions(+), 11 deletions(-)
   8.5% src/backend/utils/activity/
  82.0% src/backend/utils/adt/
   7.8% src/include/

diff --git a/src/backend/utils/activity/pgstat_wal.c b/src/backend/utils/activity/pgstat_wal.c
index 4dc41a4a590..6d024872701 100644
--- a/src/backend/utils/activity/pgstat_wal.c
+++ b/src/backend/utils/activity/pgstat_wal.c
@@ -115,7 +115,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);
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 620d60a0938..9de14ffd449 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -1635,10 +1635,11 @@ pg_stat_get_backend_io(PG_FUNCTION_ARGS)
  * pg_stat_wal_build_tuple
  *
  * Helper routine for pg_stat_get_wal() returning one tuple based on the contents
- * of wal_stats.
+ * of wal_counters.
  */
 static Datum
-pg_stat_wal_build_tuple(PgStat_WalStats wal_stats)
+pg_stat_wal_build_tuple(PgStat_WalCounters wal_counters,
+						TimestampTz stat_reset_timestamp)
 {
 #define PG_STAT_WAL_COLS	5
 	TupleDesc	tupdesc;
@@ -1662,20 +1663,20 @@ pg_stat_wal_build_tuple(PgStat_WalStats wal_stats)
 	BlessTupleDesc(tupdesc);
 
 	/* 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);
 
-	if (wal_stats.stat_reset_timestamp != 0)
-		values[4] = TimestampTzGetDatum(wal_stats.stat_reset_timestamp);
+	if (stat_reset_timestamp != 0)
+		values[4] = TimestampTzGetDatum(stat_reset_timestamp);
 	else
 		nulls[4] = true;
 
@@ -1694,7 +1695,8 @@ pg_stat_get_wal(PG_FUNCTION_ARGS)
 	/* Get statistics about WAL activity */
 	wal_stats = pgstat_fetch_stat_wal();
 
-	return (pg_stat_wal_build_tuple(*wal_stats));
+	return (pg_stat_wal_build_tuple(wal_stats->wal_counters,
+									wal_stats->stat_reset_timestamp));
 }
 
 /*
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index fc651d03cf9..438736870b3 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -474,12 +474,17 @@ typedef struct PgStat_StatTabEntry
 	PgStat_Counter total_autoanalyze_time;
 } PgStat_StatTabEntry;
 
-typedef struct PgStat_WalStats
+typedef struct PgStat_WalCounters
 {
 	PgStat_Counter wal_records;
 	PgStat_Counter wal_fpi;
 	uint64		wal_bytes;
 	PgStat_Counter wal_buffers_full;
+} PgStat_WalCounters;
+
+typedef struct PgStat_WalStats
+{
+	PgStat_WalCounters wal_counters;
 	TimestampTz stat_reset_timestamp;
 } PgStat_WalStats;
 
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index e3e09a2207e..19d510c9ec3 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2188,6 +2188,7 @@ PgStat_SubXactStatus
 PgStat_TableCounts
 PgStat_TableStatus
 PgStat_TableXactStatus
+PgStat_WalCounters
 PgStat_WalStats
 PgXmlErrorContext
 PgXmlStrictness
-- 
2.34.1

