From 4d3e61b362b794a99ca72cc8107d2ed72020fa04 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: Thu, 25 Sep 2025 10:15:43 +0530
Subject: [PATCH 2/2] Address review comments from Bertrand

When the stats are not enabled, we should reset the plugin stats to 0, rather
than carrying over the stale stats from the time when the plugin was supporting
the stats. This does not matter if the plugin continues not to support
statistics forever. But in case it was supporting the stats once, discontinued
doing so at some point in time and then starts supporting the stats later,
accumulating the new stats based on the earlier accumulated stats could be
misleading.

Author: Ashutosh Bapat
---
 doc/src/sgml/monitoring.sgml                 | 10 +++++-----
 src/backend/utils/activity/pgstat_replslot.c |  7 +++++++
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml
index fbe03ffd670..4d414d71742 100644
--- a/doc/src/sgml/monitoring.sgml
+++ b/doc/src/sgml/monitoring.sgml
@@ -1663,7 +1663,7 @@ description | Waiting for a newly initialized WAL file to reach durable storage
         Amount of changes, from <structfield>total_wal_bytes</structfield>, filtered
         out by the output plugin and not sent downstream. Please note that it
         does not include the changes filtered before a change is sent to
-        the output plugin, e.g. the changes filtered by origin. The count is
+        the output plugin, e.g. the changes filtered by origin. The counter is
         maintained by the output plugin mentioned in
         <structfield>plugin</structfield>. It is NULL when statistics is not
         initialized or immediately after a reset or when not maintained by the
@@ -1678,9 +1678,9 @@ description | Waiting for a newly initialized WAL file to reach durable storage
        <para>
         Number of decoded transactions sent downstream for this slot. This
         counts top-level transactions only, and is not incremented for
-        subtransactions. These transactions are subset of transctions sent to
-        the decoding plugin. Hence this count is expected to be lesser than or
-        equal to <structfield>total_wal_txns</structfield>.  The count is maintained
+        subtransactions. These transactions are subset of transactions sent to
+        the decoding plugin. Hence this count is expected to be less than or
+        equal to <structfield>total_wal_txns</structfield>.  The counter is maintained
         by the output plugin mentioned in <structfield>plugin</structfield>.  It
         is NULL when statistics is not initialized or immediately after a reset or
         when not maintained by the output plugin.
@@ -1694,7 +1694,7 @@ description | Waiting for a newly initialized WAL file to reach durable storage
        <para>
         Amount of transaction changes sent downstream for this slot by the
         output plugin after applying filtering and converting into its output
-        format. The count is maintained by the output plugin mentioned in
+        format. The counter is maintained by the output plugin mentioned in
         <structfield>plugin</structfield>.  It is NULL when statistics is not
         initialized or immediately after a reset or when not maintained by the
         output plugin.
diff --git a/src/backend/utils/activity/pgstat_replslot.c b/src/backend/utils/activity/pgstat_replslot.c
index 895940f4eb9..3e486fd28e4 100644
--- a/src/backend/utils/activity/pgstat_replslot.c
+++ b/src/backend/utils/activity/pgstat_replslot.c
@@ -88,6 +88,7 @@ pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *re
 
 	/* Update the replication slot statistics */
 #define REPLSLOT_ACC(fld) statent->fld += repSlotStat->fld
+#define REPLSLOT_SET_TO_ZERO(fld) statent->fld = 0
 	REPLSLOT_ACC(spill_txns);
 	REPLSLOT_ACC(spill_count);
 	REPLSLOT_ACC(spill_bytes);
@@ -103,6 +104,12 @@ pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *re
 		REPLSLOT_ACC(plugin_sent_bytes);
 		REPLSLOT_ACC(plugin_filtered_bytes);
 	}
+	else
+	{
+		REPLSLOT_SET_TO_ZERO(plugin_sent_txns);
+		REPLSLOT_SET_TO_ZERO(plugin_sent_bytes);
+		REPLSLOT_SET_TO_ZERO(plugin_filtered_bytes);
+	}
 #undef REPLSLOT_ACC
 
 	pgstat_unlock_entry(entry_ref);
-- 
2.34.1

