From be7ba029036613df80562cc735a3040d6f760a90 Mon Sep 17 00:00:00 2001
From: tomas <tomas>
Date: Sat, 9 Nov 2024 12:09:04 +0100
Subject: [PATCH 1/5] asserts on restart_lsn backwards move

---
 src/backend/replication/logical/logical.c | 3 +++
 src/backend/replication/slot.c            | 2 ++
 2 files changed, 5 insertions(+)

diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c
index 2f7b2c85d9b..d76889664a8 100644
--- a/src/backend/replication/logical/logical.c
+++ b/src/backend/replication/logical/logical.c
@@ -376,6 +376,7 @@ CreateInitDecodingContext(const char *plugin,
 	else
 	{
 		SpinLockAcquire(&slot->mutex);
+		Assert(slot->data.restart_lsn <= restart_lsn);
 		slot->data.restart_lsn = restart_lsn;
 		SpinLockRelease(&slot->mutex);
 	}
@@ -1746,6 +1747,8 @@ LogicalConfirmReceivedLocation(XLogRecPtr lsn)
 		{
 			Assert(MyReplicationSlot->candidate_restart_lsn != InvalidXLogRecPtr);
 
+			Assert(MyReplicationSlot->data.restart_lsn <= MyReplicationSlot->candidate_restart_lsn);
+
 			MyReplicationSlot->data.restart_lsn = MyReplicationSlot->candidate_restart_lsn;
 			MyReplicationSlot->candidate_restart_lsn = InvalidXLogRecPtr;
 			MyReplicationSlot->candidate_restart_valid = InvalidXLogRecPtr;
diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c
index 037a347cba0..01e854b4486 100644
--- a/src/backend/replication/slot.c
+++ b/src/backend/replication/slot.c
@@ -1109,6 +1109,7 @@ ReplicationSlotReserveWal(void)
 			/* start at current insert position */
 			restart_lsn = GetXLogInsertRecPtr();
 			SpinLockAcquire(&slot->mutex);
+			Assert(slot->data.restart_lsn <= restart_lsn);
 			slot->data.restart_lsn = restart_lsn;
 			SpinLockRelease(&slot->mutex);
 
@@ -1122,6 +1123,7 @@ ReplicationSlotReserveWal(void)
 		{
 			restart_lsn = GetRedoRecPtr();
 			SpinLockAcquire(&slot->mutex);
+			Assert(slot->data.restart_lsn <= restart_lsn);
 			slot->data.restart_lsn = restart_lsn;
 			SpinLockRelease(&slot->mutex);
 		}
-- 
2.39.5

