diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 63481d826f..7bdd4d86b1 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -7463,8 +7463,11 @@ KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
 	{
 		XLByteToSeg(keep, segno, wal_segment_size);
 
-		/* Cap by max_slot_wal_keep_size ... */
-		if (max_slot_wal_keep_size_mb >= 0)
+		/*
+		 * Cap by max_slot_wal_keep_size. However this is not required if the
+		 * slots alredy won't protect any segments.
+		 */
+		if (max_slot_wal_keep_size_mb >= 0 && segno < currSegNo)
 		{
 			uint64		slot_keep_segs;
 
-- 
2.31.1
