From 91e98ced946d4668d005cad0294874ec5e7d27af Mon Sep 17 00:00:00 2001
From: Shinya Kato <shinya11.kato@gmail.com>
Date: Tue, 28 Oct 2025 19:14:14 +0900
Subject: [PATCH v3] 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 +++++++-
 2 files changed, 10 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);
 	}
 }
 
-- 
2.47.3

