From ff10d7684ead9efd0b7f6ff03b68d598917c7c5c Mon Sep 17 00:00:00 2001
From: Shinya Kato <shinya11.kato@gmail.com>
Date: Sat, 18 Oct 2025 16:27:32 +0900
Subject: [PATCH v2 1/2] Add wal_fpi_bytes to pg_stat_wal

Track the total size of full-page images in WAL statistics so that the
effect of wal_compression can be monitored. XLogRecordAssemble now
increments a new wal_fpi_bytes counter in WalUsage, and the backend and
shared WAL stats paths flush that value alongside the existing counters.

Expose the counter through pg_stat_get_wal(), pg_stat_get_backend_wal(),
pg_stat_wal.

Author: Shinya Kato <shinya11.kato@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discusssion: https://postgr.es/m/CAOzEurQtZEAfg6P0kU3Wa-f9BWQOi0RzJEMPN56wNTOmJLmfaQ@mail.gmail.com
---
 doc/src/sgml/monitoring.sgml                | 11 +++++++++++
 src/backend/access/transam/xloginsert.c     |  6 ++++++
 src/backend/catalog/system_views.sql        |  3 ++-
 src/backend/utils/activity/pgstat_backend.c |  1 +
 src/backend/utils/activity/pgstat_wal.c     |  1 +
 src/backend/utils/adt/pgstatfuncs.c         | 10 +++++++++-
 src/include/catalog/pg_proc.dat             | 12 ++++++------
 src/include/executor/instrument.h           |  1 +
 src/include/pgstat.h                        |  1 +
 src/test/regress/expected/rules.out         |  5 +++--
 10 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index d5f0fb7ba7c..0db378ee99e 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -3340,6 +3340,17 @@ description | Waiting for a newly initialized WAL file to reach durable storage
        Time at which these statistics were last reset
       </para></entry>
      </row>
+
+    <row>
+     <entry role="catalog_table_entry"><para role="column_definition">
+      <structfield>wal_fpi_bytes</structfield> <type>numeric</type>
+     </para>
+     <para>
+      Total amount of WAL full page images in bytes; if
+      <varname>wal_compression</varname> is enabled, this reflects the
+      compressed size
+     </para></entry>
+    </row>
      </tbody>
    </tgroup>
   </table>
diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c
index 496e0fa4ac6..b3abf386f80 100644
--- a/src/backend/access/transam/xloginsert.c
+++ b/src/backend/access/transam/xloginsert.c
@@ -33,12 +33,14 @@
 #include "access/xloginsert.h"
 #include "catalog/pg_control.h"
 #include "common/pg_lzcompress.h"
+#include "executor/instrument.h"
 #include "miscadmin.h"
 #include "pg_trace.h"
 #include "replication/origin.h"
 #include "storage/bufmgr.h"
 #include "storage/proc.h"
 #include "utils/memutils.h"
+#include "utils/pgstat_internal.h"
 
 /*
  * Guess the maximum buffer size required to store a compressed version of
@@ -796,6 +798,10 @@ XLogRecordAssemble(RmgrId rmid, uint8 info,
 			}
 
 			total_len += bimg.length;
+
+			/* Track the WAL full page images in bytes */
+			pgWalUsage.wal_fpi_bytes += bimg.length;
+			pgstat_report_fixed = true;
 		}
 
 		if (needs_data)
diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql
index 823776c1498..d7f149f9328 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -1222,7 +1222,8 @@ CREATE VIEW pg_stat_wal AS
         w.wal_fpi,
         w.wal_bytes,
         w.wal_buffers_full,
-        w.stats_reset
+        w.stats_reset,
+        w.wal_fpi_bytes
     FROM pg_stat_get_wal() w;
 
 CREATE VIEW pg_stat_progress_analyze AS
diff --git a/src/backend/utils/activity/pgstat_backend.c b/src/backend/utils/activity/pgstat_backend.c
index a864ae8e6a6..199ba2cc17a 100644
--- a/src/backend/utils/activity/pgstat_backend.c
+++ b/src/backend/utils/activity/pgstat_backend.c
@@ -252,6 +252,7 @@ pgstat_flush_backend_entry_wal(PgStat_EntryRef *entry_ref)
 	WALSTAT_ACC(wal_records, wal_usage_diff);
 	WALSTAT_ACC(wal_fpi, wal_usage_diff);
 	WALSTAT_ACC(wal_bytes, wal_usage_diff);
+	WALSTAT_ACC(wal_fpi_bytes, wal_usage_diff);
 #undef WALSTAT_ACC
 
 	/*
diff --git a/src/backend/utils/activity/pgstat_wal.c b/src/backend/utils/activity/pgstat_wal.c
index 0d04480d2f6..4d796c5e790 100644
--- a/src/backend/utils/activity/pgstat_wal.c
+++ b/src/backend/utils/activity/pgstat_wal.c
@@ -122,6 +122,7 @@ pgstat_wal_flush_cb(bool nowait)
 	WALSTAT_ACC(wal_fpi, wal_usage_diff);
 	WALSTAT_ACC(wal_bytes, wal_usage_diff);
 	WALSTAT_ACC(wal_buffers_full, wal_usage_diff);
+	WALSTAT_ACC(wal_fpi_bytes, wal_usage_diff);
 #undef WALSTAT_ACC
 
 	LWLockRelease(&stats_shmem->lock);
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 1fe33df2756..21efc010334 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -1637,7 +1637,7 @@ static Datum
 pg_stat_wal_build_tuple(PgStat_WalCounters wal_counters,
 						TimestampTz stat_reset_timestamp)
 {
-#define PG_STAT_WAL_COLS	5
+#define PG_STAT_WAL_COLS	6
 	TupleDesc	tupdesc;
 	Datum		values[PG_STAT_WAL_COLS] = {0};
 	bool		nulls[PG_STAT_WAL_COLS] = {0};
@@ -1655,6 +1655,8 @@ pg_stat_wal_build_tuple(PgStat_WalCounters wal_counters,
 					   INT8OID, -1, 0);
 	TupleDescInitEntry(tupdesc, (AttrNumber) 5, "stats_reset",
 					   TIMESTAMPTZOID, -1, 0);
+	TupleDescInitEntry(tupdesc, (AttrNumber) 6, "wal_fpi_bytes",
+					   NUMERICOID, -1, 0);
 
 	BlessTupleDesc(tupdesc);
 
@@ -1676,6 +1678,12 @@ pg_stat_wal_build_tuple(PgStat_WalCounters wal_counters,
 	else
 		nulls[4] = true;
 
+	snprintf(buf, sizeof buf, UINT64_FORMAT, wal_counters.wal_fpi_bytes);
+	values[5] = DirectFunctionCall3(numeric_in,
+									CStringGetDatum(buf),
+									ObjectIdGetDatum(0),
+									Int32GetDatum(-1));
+
 	/* Returns the record as Datum */
 	PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
 }
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index eecb43ec6f0..952a51969b3 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -6029,16 +6029,16 @@
 { oid => '1136', descr => 'statistics: information about WAL activity',
   proname => 'pg_stat_get_wal', proisstrict => 'f', provolatile => 's',
   proparallel => 'r', prorettype => 'record', proargtypes => '',
-  proallargtypes => '{int8,int8,numeric,int8,timestamptz}',
-  proargmodes => '{o,o,o,o,o}',
-  proargnames => '{wal_records,wal_fpi,wal_bytes,wal_buffers_full,stats_reset}',
+  proallargtypes => '{int8,int8,numeric,int8,timestamptz,numeric}',
+  proargmodes => '{o,o,o,o,o,o}',
+  proargnames => '{wal_records,wal_fpi,wal_bytes,wal_buffers_full,stats_reset,wal_fpi_bytes}',
   prosrc => 'pg_stat_get_wal' },
 { oid => '6313', descr => 'statistics: backend WAL activity',
   proname => 'pg_stat_get_backend_wal', provolatile => 'v', proparallel => 'r',
   prorettype => 'record', proargtypes => 'int4',
-  proallargtypes => '{int4,int8,int8,numeric,int8,timestamptz}',
-  proargmodes => '{i,o,o,o,o,o}',
-  proargnames => '{backend_pid,wal_records,wal_fpi,wal_bytes,wal_buffers_full,stats_reset}',
+  proallargtypes => '{int4,int8,int8,numeric,int8,timestamptz,numeric}',
+  proargmodes => '{i,o,o,o,o,o,o}',
+  proargnames => '{backend_pid,wal_records,wal_fpi,wal_bytes,wal_buffers_full,stats_reset,wal_fpi_bytes}',
   prosrc => 'pg_stat_get_backend_wal' },
 { oid => '6248', descr => 'statistics: information about WAL prefetching',
   proname => 'pg_stat_get_recovery_prefetch', prorows => '1', proretset => 't',
diff --git a/src/include/executor/instrument.h b/src/include/executor/instrument.h
index 03653ab6c6c..7eb02833fad 100644
--- a/src/include/executor/instrument.h
+++ b/src/include/executor/instrument.h
@@ -54,6 +54,7 @@ typedef struct WalUsage
 	int64		wal_fpi;		/* # of WAL full page images produced */
 	uint64		wal_bytes;		/* size of WAL records produced */
 	int64		wal_buffers_full;	/* # of times the WAL buffers became full */
+	uint64		wal_fpi_bytes;	/* size of FPIs after compression */
 } WalUsage;
 
 /* Flag bits included in InstrAlloc's instrument_options bitmask */
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index bc8077cbae6..9c2d44bab89 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -473,6 +473,7 @@ typedef struct PgStat_WalCounters
 	PgStat_Counter wal_records;
 	PgStat_Counter wal_fpi;
 	uint64		wal_bytes;
+	uint64		wal_fpi_bytes;
 	PgStat_Counter wal_buffers_full;
 } PgStat_WalCounters;
 
diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out
index 16753b2e4c0..6c3d78e96aa 100644
--- a/src/test/regress/expected/rules.out
+++ b/src/test/regress/expected/rules.out
@@ -2307,8 +2307,9 @@ pg_stat_wal| SELECT wal_records,
     wal_fpi,
     wal_bytes,
     wal_buffers_full,
-    stats_reset
-   FROM pg_stat_get_wal() w(wal_records, wal_fpi, wal_bytes, wal_buffers_full, stats_reset);
+    stats_reset,
+    wal_fpi_bytes
+   FROM pg_stat_get_wal() w(wal_records, wal_fpi, wal_bytes, wal_buffers_full, stats_reset, wal_fpi_bytes);
 pg_stat_wal_receiver| SELECT pid,
     status,
     receive_start_lsn,
-- 
2.47.3

