From 47a131b524fea7213ff51c21efb5665308e3664c Mon Sep 17 00:00:00 2001
From: Shinya Kato <shinya11.kato@gmail.com>
Date: Sat, 18 Oct 2025 16:28:05 +0900
Subject: [PATCH v2 2/2] Expose WAL FPI byte totals in EXPLAIN

Show the new wal_fpi_bytes counter when EXPLAIN (ANALYZE, WAL) reports
per-plan WAL usage, emitting it alongside the existing counters in both
text and JSON/YAML output. Extend WalUsage aggregation helpers so the
value accumulates correctly.

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/ref/explain.sgml     | 4 +++-
 src/backend/commands/explain.c    | 8 +++++++-
 src/backend/executor/instrument.c | 2 ++
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/doc/src/sgml/ref/explain.sgml b/doc/src/sgml/ref/explain.sgml
index 6dda680aa0d..ead4017d172 100644
--- a/doc/src/sgml/ref/explain.sgml
+++ b/doc/src/sgml/ref/explain.sgml
@@ -241,7 +241,9 @@ ROLLBACK;
      <para>
       Include information on WAL record generation. Specifically, include the
       number of records, number of full page images (fpi), the amount of WAL
-      generated in bytes and the number of times the WAL buffers became full.
+      generated in bytes, the number of times the WAL buffers became full, the
+      amount of WAL fpi generated in bytes (if <varname>wal_compression</varname>
+      is enabled, this reflects the compressed size).
       In text format, only non-zero values are printed.
       This parameter may only be used when <literal>ANALYZE</literal> is also
       enabled.  It defaults to <literal>FALSE</literal>.
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index e6edae0845c..68c3d20353c 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -4283,7 +4283,8 @@ show_wal_usage(ExplainState *es, const WalUsage *usage)
 	{
 		/* Show only positive counter values. */
 		if ((usage->wal_records > 0) || (usage->wal_fpi > 0) ||
-			(usage->wal_bytes > 0) || (usage->wal_buffers_full > 0))
+			(usage->wal_bytes > 0) || (usage->wal_buffers_full > 0) ||
+			(usage->wal_fpi_bytes > 0))
 		{
 			ExplainIndentText(es);
 			appendStringInfoString(es->str, "WAL:");
@@ -4300,6 +4301,9 @@ show_wal_usage(ExplainState *es, const WalUsage *usage)
 			if (usage->wal_buffers_full > 0)
 				appendStringInfo(es->str, " buffers full=%" PRId64,
 								 usage->wal_buffers_full);
+			if (usage->wal_fpi_bytes > 0)
+				appendStringInfo(es->str, " fpi bytes=%" PRIu64,
+								 usage->wal_fpi_bytes);
 			appendStringInfoChar(es->str, '\n');
 		}
 	}
@@ -4313,6 +4317,8 @@ show_wal_usage(ExplainState *es, const WalUsage *usage)
 								usage->wal_bytes, es);
 		ExplainPropertyInteger("WAL Buffers Full", NULL,
 							   usage->wal_buffers_full, es);
+		ExplainPropertyUInteger("WAL FPI Bytes Compressed", NULL,
+								usage->wal_fpi_bytes, es);
 	}
 }
 
diff --git a/src/backend/executor/instrument.c b/src/backend/executor/instrument.c
index 56e635f4700..e5f9ec14ef6 100644
--- a/src/backend/executor/instrument.c
+++ b/src/backend/executor/instrument.c
@@ -281,6 +281,7 @@ WalUsageAdd(WalUsage *dst, WalUsage *add)
 	dst->wal_records += add->wal_records;
 	dst->wal_fpi += add->wal_fpi;
 	dst->wal_buffers_full += add->wal_buffers_full;
+	dst->wal_fpi_bytes += add->wal_fpi_bytes;
 }
 
 void
@@ -290,4 +291,5 @@ WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, const WalUsage *sub)
 	dst->wal_records += add->wal_records - sub->wal_records;
 	dst->wal_fpi += add->wal_fpi - sub->wal_fpi;
 	dst->wal_buffers_full += add->wal_buffers_full - sub->wal_buffers_full;
+	dst->wal_fpi_bytes += add->wal_fpi_bytes - sub->wal_fpi_bytes;
 }
-- 
2.47.3

