From f9153f1e7cf0a1fbe9b09b60562873508bff61a2 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 v1 1/3] Add wal_fpi_bytes_[un]compressed to pg_stat_wal

---
 doc/src/sgml/monitoring.sgml                | 18 +++++++++++++++++
 src/backend/access/transam/xlog.c           |  4 ++++
 src/backend/access/transam/xloginsert.c     | 22 ++++++++++++++++++---
 src/backend/catalog/system_views.sql        |  4 +++-
 src/backend/utils/activity/pgstat_backend.c |  2 ++
 src/backend/utils/activity/pgstat_wal.c     |  2 ++
 src/backend/utils/adt/pgstatfuncs.c         | 20 ++++++++++++++++++-
 src/include/access/xlog.h                   |  2 ++
 src/include/catalog/pg_proc.dat             | 12 +++++------
 src/include/pgstat.h                        |  2 ++
 src/test/regress/expected/rules.out         |  6 ++++--
 11 files changed, 81 insertions(+), 13 deletions(-)

diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index d5f0fb7ba7c..4cbdf0889d7 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -3340,6 +3340,24 @@ 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_uncompressed</structfield> <type>numeric</type>
+     </para>
+     <para>
+      Total amount of WAL full page images in bytes before compression
+     </para></entry>
+    </row>
+
+    <row>
+     <entry role="catalog_table_entry"><para role="column_definition">
+      <structfield>wal_fpi_bytes_compressed</structfield> <type>numeric</type>
+     </para>
+     <para>
+      Total amount of WAL full page images in bytes after compression
+     </para></entry>
+    </row>
      </tbody>
    </tgroup>
   </table>
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index eceab341255..8b22b9a1d46 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -749,6 +749,8 @@ XLogInsertRecord(XLogRecData *rdata,
 				 XLogRecPtr fpw_lsn,
 				 uint8 flags,
 				 int num_fpi,
+				 uint64 fpi_bytes_uncompressed,
+				 uint64 fpi_bytes_compressed,
 				 bool topxid_included)
 {
 	XLogCtlInsert *Insert = &XLogCtl->Insert;
@@ -1081,6 +1083,8 @@ XLogInsertRecord(XLogRecData *rdata,
 		pgWalUsage.wal_bytes += rechdr->xl_tot_len;
 		pgWalUsage.wal_records++;
 		pgWalUsage.wal_fpi += num_fpi;
+		pgWalUsage.wal_fpi_bytes_uncompressed += fpi_bytes_uncompressed;
+		pgWalUsage.wal_fpi_bytes_compressed += fpi_bytes_compressed;
 
 		/* Required for the flush of pending stats WAL data */
 		pgstat_report_fixed = true;
diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c
index 496e0fa4ac6..47e9025993d 100644
--- a/src/backend/access/transam/xloginsert.c
+++ b/src/backend/access/transam/xloginsert.c
@@ -137,7 +137,9 @@ static MemoryContext xloginsert_cxt;
 static XLogRecData *XLogRecordAssemble(RmgrId rmid, uint8 info,
 									   XLogRecPtr RedoRecPtr, bool doPageWrites,
 									   XLogRecPtr *fpw_lsn, int *num_fpi,
-									   bool *topxid_included);
+									   bool *topxid_included,
+									   uint64 *fpi_bytes_uncompressed,
+									   uint64 *fpi_bytes_compressed);
 static bool XLogCompressBackupBlock(const PageData *page, uint16 hole_offset,
 									uint16 hole_length, void *dest, uint16 *dlen);
 
@@ -510,6 +512,8 @@ XLogInsert(RmgrId rmid, uint8 info)
 		XLogRecPtr	fpw_lsn;
 		XLogRecData *rdt;
 		int			num_fpi = 0;
+		uint64		fpi_bytes_uncompressed = 0;
+		uint64		fpi_bytes_compressed = 0;
 
 		/*
 		 * Get values needed to decide whether to do full-page writes. Since
@@ -519,9 +523,13 @@ XLogInsert(RmgrId rmid, uint8 info)
 		GetFullPageWriteInfo(&RedoRecPtr, &doPageWrites);
 
 		rdt = XLogRecordAssemble(rmid, info, RedoRecPtr, doPageWrites,
-								 &fpw_lsn, &num_fpi, &topxid_included);
+								 &fpw_lsn, &num_fpi, &topxid_included,
+								 &fpi_bytes_uncompressed,
+								 &fpi_bytes_compressed);
 
 		EndPos = XLogInsertRecord(rdt, fpw_lsn, curinsert_flags, num_fpi,
+								  fpi_bytes_uncompressed,
+								  fpi_bytes_compressed,
 								  topxid_included);
 	} while (EndPos == InvalidXLogRecPtr);
 
@@ -560,7 +568,9 @@ XLogSimpleInsertInt64(RmgrId rmid, uint8 info, int64 value)
 static XLogRecData *
 XLogRecordAssemble(RmgrId rmid, uint8 info,
 				   XLogRecPtr RedoRecPtr, bool doPageWrites,
-				   XLogRecPtr *fpw_lsn, int *num_fpi, bool *topxid_included)
+				   XLogRecPtr *fpw_lsn, int *num_fpi, bool *topxid_included,
+				   uint64 *fpi_bytes_uncompressed,
+				   uint64 *fpi_bytes_compressed)
 {
 	XLogRecData *rdt;
 	uint64		total_len = 0;
@@ -571,6 +581,9 @@ XLogRecordAssemble(RmgrId rmid, uint8 info,
 	XLogRecord *rechdr;
 	char	   *scratch = hdr_scratch;
 
+	*fpi_bytes_uncompressed = 0;
+	*fpi_bytes_compressed = 0;
+
 	/*
 	 * Note: this function can be called multiple times for the same record.
 	 * All the modifications we do to the rdata chains below must handle that.
@@ -796,6 +809,9 @@ XLogRecordAssemble(RmgrId rmid, uint8 info,
 			}
 
 			total_len += bimg.length;
+
+			*fpi_bytes_uncompressed += BLCKSZ - cbimg.hole_length;
+			*fpi_bytes_compressed += bimg.length;
 		}
 
 		if (needs_data)
diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql
index 823776c1498..9cbf3723e73 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -1222,7 +1222,9 @@ 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_uncompressed,
+        w.wal_fpi_bytes_compressed
     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..b31c7205e9d 100644
--- a/src/backend/utils/activity/pgstat_backend.c
+++ b/src/backend/utils/activity/pgstat_backend.c
@@ -252,6 +252,8 @@ 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_uncompressed, wal_usage_diff);
+	WALSTAT_ACC(wal_fpi_bytes_compressed, 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..c1fd0758c19 100644
--- a/src/backend/utils/activity/pgstat_wal.c
+++ b/src/backend/utils/activity/pgstat_wal.c
@@ -121,6 +121,8 @@ pgstat_wal_flush_cb(bool nowait)
 	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_uncompressed, wal_usage_diff);
+	WALSTAT_ACC(wal_fpi_bytes_compressed, wal_usage_diff);
 	WALSTAT_ACC(wal_buffers_full, wal_usage_diff);
 #undef WALSTAT_ACC
 
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 1fe33df2756..69acfb863f8 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	7
 	TupleDesc	tupdesc;
 	Datum		values[PG_STAT_WAL_COLS] = {0};
 	bool		nulls[PG_STAT_WAL_COLS] = {0};
@@ -1655,6 +1655,10 @@ 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_uncompressed",
+					   NUMERICOID, -1, 0);
+	TupleDescInitEntry(tupdesc, (AttrNumber) 7, "wal_fpi_bytes_compressed",
+					   NUMERICOID, -1, 0);
 
 	BlessTupleDesc(tupdesc);
 
@@ -1676,6 +1680,20 @@ pg_stat_wal_build_tuple(PgStat_WalCounters wal_counters,
 	else
 		nulls[4] = true;
 
+	snprintf(buf, sizeof buf, UINT64_FORMAT,
+			 wal_counters.wal_fpi_bytes_uncompressed);
+	values[5] = DirectFunctionCall3(numeric_in,
+									CStringGetDatum(buf),
+									ObjectIdGetDatum(0),
+									Int32GetDatum(-1));
+
+	snprintf(buf, sizeof buf, UINT64_FORMAT,
+			 wal_counters.wal_fpi_bytes_compressed);
+	values[6] = 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/access/xlog.h b/src/include/access/xlog.h
index d12798be3d8..1505b6842a4 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -202,6 +202,8 @@ extern XLogRecPtr XLogInsertRecord(struct XLogRecData *rdata,
 								   XLogRecPtr fpw_lsn,
 								   uint8 flags,
 								   int num_fpi,
+								   uint64 fpi_bytes_uncompressed,
+								   uint64 fpi_bytes_compressed,
 								   bool topxid_included);
 extern void XLogFlush(XLogRecPtr record);
 extern bool XLogBackgroundFlush(void);
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index eecb43ec6f0..f6115f3aa4e 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,numeric}',
+  proargmodes => '{o,o,o,o,o,o,o}',
+  proargnames => '{wal_records,wal_fpi,wal_bytes,wal_buffers_full,stats_reset,wal_fpi_bytes_uncompressed,wal_fpi_bytes_compressed}',
   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,numeric}',
+  proargmodes => '{i,o,o,o,o,o,o,o}',
+  proargnames => '{backend_pid,wal_records,wal_fpi,wal_bytes,wal_buffers_full,stats_reset,wal_fpi_bytes_uncompressed,wal_fpi_bytes_compressed}',
   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/pgstat.h b/src/include/pgstat.h
index bc8077cbae6..c6349f3a43e 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -473,6 +473,8 @@ typedef struct PgStat_WalCounters
 	PgStat_Counter wal_records;
 	PgStat_Counter wal_fpi;
 	uint64		wal_bytes;
+	uint64		wal_fpi_bytes_uncompressed;
+	uint64		wal_fpi_bytes_compressed;
 	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..3690272435e 100644
--- a/src/test/regress/expected/rules.out
+++ b/src/test/regress/expected/rules.out
@@ -2307,8 +2307,10 @@ 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_uncompressed,
+    wal_fpi_bytes_compressed
+   FROM pg_stat_get_wal() w(wal_records, wal_fpi, wal_bytes, wal_buffers_full, stats_reset, wal_fpi_bytes_uncompressed, wal_fpi_bytes_compressed);
 pg_stat_wal_receiver| SELECT pid,
     status,
     receive_start_lsn,
-- 
2.47.3

