From 5789b35c1eca77ad63066e6671921e1e8a656372 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Tue, 27 Sep 2022 14:28:56 +0900
Subject: [PATCH v1] Do not reset slot name of replication slot stats

pg_stat_reset_replication_slot() clears counters involving slot name,
which is not intended and causes crash.  Move slot name out of
PgStat_StatReplSlotEntry to PgStatShared_Database so that the name
won't be wiped out by a counter reset.
---
 src/backend/utils/activity/pgstat_replslot.c | 12 ++++++------
 src/include/pgstat.h                         |  1 -
 src/include/utils/pgstat_internal.h          |  1 +
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/backend/utils/activity/pgstat_replslot.c b/src/backend/utils/activity/pgstat_replslot.c
index 2039ac3147..0a383f9f32 100644
--- a/src/backend/utils/activity/pgstat_replslot.c
+++ b/src/backend/utils/activity/pgstat_replslot.c
@@ -86,7 +86,7 @@ pgstat_report_replslot(ReplicationSlot *slot, const PgStat_StatReplSlotEntry *re
 	 * Any mismatch should have been fixed in pgstat_create_replslot() or
 	 * pgstat_acquire_replslot().
 	 */
-	Assert(namestrcmp(&statent->slotname, NameStr(slot->data.name)) == 0);
+	Assert(namestrcmp(&shstatent->slotname, NameStr(slot->data.name)) == 0);
 
 	/* Update the replication slot statistics */
 #define REPLSLOT_ACC(fld) statent->fld += repSlotStat->fld
@@ -124,7 +124,7 @@ pgstat_create_replslot(ReplicationSlot *slot)
 	 * if we previously crashed after dropping a slot.
 	 */
 	memset(&shstatent->stats, 0, sizeof(shstatent->stats));
-	namestrcpy(&shstatent->stats.slotname, NameStr(slot->data.name));
+	namestrcpy(&shstatent->slotname, NameStr(slot->data.name));
 
 	pgstat_unlock_entry(entry_ref);
 }
@@ -148,11 +148,11 @@ pgstat_acquire_replslot(ReplicationSlot *slot)
 	 * NB: need to accept that there might be stats from an older slot, e.g.
 	 * if we previously crashed after dropping a slot.
 	 */
-	if (NameStr(statent->slotname)[0] == 0 ||
-		namestrcmp(&statent->slotname, NameStr(slot->data.name)) != 0)
+	if (NameStr(shstatent->slotname)[0] == 0 ||
+		namestrcmp(&shstatent->slotname, NameStr(slot->data.name)) != 0)
 	{
 		memset(statent, 0, sizeof(*statent));
-		namestrcpy(&statent->slotname, NameStr(slot->data.name));
+		namestrcpy(&shstatent->slotname, NameStr(slot->data.name));
 	}
 
 	pgstat_unlock_entry(entry_ref);
@@ -187,7 +187,7 @@ pgstat_fetch_replslot(NameData slotname)
 void
 pgstat_replslot_to_serialized_name_cb(const PgStatShared_Common *header, NameData *name)
 {
-	namestrcpy(name, NameStr(((PgStatShared_ReplSlot *) header)->stats.slotname));
+	namestrcpy(name, NameStr(((PgStatShared_ReplSlot *) header)->slotname));
 }
 
 bool
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index ad7334a0d2..530d49e5c3 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -321,7 +321,6 @@ typedef struct PgStat_StatFuncEntry
 
 typedef struct PgStat_StatReplSlotEntry
 {
-	NameData	slotname;
 	PgStat_Counter spill_txns;
 	PgStat_Counter spill_count;
 	PgStat_Counter spill_bytes;
diff --git a/src/include/utils/pgstat_internal.h b/src/include/utils/pgstat_internal.h
index 40a3602855..9bdcc08ed3 100644
--- a/src/include/utils/pgstat_internal.h
+++ b/src/include/utils/pgstat_internal.h
@@ -380,6 +380,7 @@ typedef struct PgStatShared_Subscription
 typedef struct PgStatShared_ReplSlot
 {
 	PgStatShared_Common header;
+	NameData	slotname;
 	PgStat_StatReplSlotEntry stats;
 } PgStatShared_ReplSlot;
 
-- 
2.31.1

