From 114c91d8dd8b9070222b1a59abfa61a9daece4f0 Mon Sep 17 00:00:00 2001
From: Vitaly Davydov <v.davydov@postgrespro.ru>
Date: Tue, 20 May 2025 18:07:19 +0300
Subject: [PATCH] Alternative solution with 019_replslot_limit.pl fix

---
 src/backend/access/transam/xlog.c         | 16 ++++------------
 src/test/recovery/t/019_replslot_limit.pl | 16 ++++++++++++++++
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 624be87a609..4c3b68a94dc 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -7520,16 +7520,12 @@ CreateCheckPoint(int flags)
 										   _logSegNo, InvalidOid,
 										   InvalidTransactionId))
 	{
-		ReplicationSlotsComputeRequiredLSN();
-		slotsMinReqLSN = XLogGetReplicationSlotMinimumLSN();
-		CheckPointReplicationSlots(shutdown);
-
 		/*
 		 * Some slots have been invalidated; recalculate the old-segment
 		 * horizon, starting again from RedoRecPtr.
 		 */
-		XLByteToSeg(RedoRecPtr, _logSegNo, wal_segment_size);
-		KeepLogSeg(recptr, slotsMinReqLSN, &_logSegNo);
+		/* XLByteToSeg(RedoRecPtr, _logSegNo, wal_segment_size); */
+		/* KeepLogSeg(recptr, slotsMinReqLSN, &_logSegNo); */
 	}
 	_logSegNo--;
 	RemoveOldXlogFiles(_logSegNo, RedoRecPtr, recptr,
@@ -7986,16 +7982,12 @@ CreateRestartPoint(int flags)
 										   _logSegNo, InvalidOid,
 										   InvalidTransactionId))
 	{
-		ReplicationSlotsComputeRequiredLSN();
-		slotsMinReqLSN = XLogGetReplicationSlotMinimumLSN();
-		CheckPointReplicationSlots(flags & CHECKPOINT_IS_SHUTDOWN);
-
 		/*
 		 * Some slots have been invalidated; recalculate the old-segment
 		 * horizon, starting again from RedoRecPtr.
 		 */
-		XLByteToSeg(RedoRecPtr, _logSegNo, wal_segment_size);
-		KeepLogSeg(endptr, slotsMinReqLSN, &_logSegNo);
+		/* XLByteToSeg(RedoRecPtr, _logSegNo, wal_segment_size); */
+		/* KeepLogSeg(endptr, slotsMinReqLSN, &_logSegNo); */
 	}
 	_logSegNo--;
 
diff --git a/src/test/recovery/t/019_replslot_limit.pl b/src/test/recovery/t/019_replslot_limit.pl
index 6468784b83d..2ce1882cc59 100644
--- a/src/test/recovery/t/019_replslot_limit.pl
+++ b/src/test/recovery/t/019_replslot_limit.pl
@@ -212,6 +212,22 @@ for (my $i = 0; $i < 10 * $PostgreSQL::Test::Utils::timeout_default; $i++)
 }
 ok($checkpoint_ended, 'waited for checkpoint to end');
 
+# Execute one more checkpoint to advance the old-segment horizon after slot
+# invalidation. Slots are invalidated in the checkpoint after the old segment
+# horizon is calculated.
+$node_primary->safe_psql('postgres', "CHECKPOINT;");
+$checkpoint_ended = 0;
+for (my $i = 0; $i < 10 * $PostgreSQL::Test::Utils::timeout_default; $i++)
+{
+	if ($node_primary->log_contains("checkpoint complete: ", $logstart))
+	{
+		$checkpoint_ended = 1;
+		last;
+	}
+	usleep(100_000);
+}
+ok($checkpoint_ended, 'waited for checkpoint to end');
+
 # The invalidated slot shouldn't keep the old-segment horizon back;
 # see bug #17103: https://postgr.es/m/17103-004130e8f27782c9@postgresql.org
 # Test for this by creating a new slot and comparing its restart LSN
-- 
2.34.1

