idea.diffs

application/octet-stream

Filename: idea.diffs
Type: application/octet-stream
Part: 0
Message: RE: Newly created replication slot may be invalidated by checkpoint
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index fd91bcd68ec..48394ac1a90 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2672,6 +2672,19 @@ XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
 	SpinLockRelease(&XLogCtl->info_lck);
 }
 
+/*
+ * Similar with XLogSetReplicationSlotMinimumLSN(), but record the LSN only
+ * when it is older than the oldest protected WAL.
+ */
+void
+XLogMaybeSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
+{
+	SpinLockAcquire(&XLogCtl->info_lck);
+	if (lsn > XLogCtl->replicationSlotMinLSN ||
+		XLogCtl->replicationSlotMinLSN == InvalidXLogRecPtr)
+		XLogCtl->replicationSlotMinLSN = lsn;
+	SpinLockRelease(&XLogCtl->info_lck);
+}
 
 /*
  * Return the oldest LSN we must retain to satisfy the needs of some
diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c
index 5fa19a34bbe..98a30a2d610 100644
--- a/src/backend/replication/slot.c
+++ b/src/backend/replication/slot.c
@@ -1610,6 +1610,8 @@ ReplicationSlotReserveWal(void)
 		else
 			restart_lsn = GetXLogInsertRecPtr();
 
+		XLogMaybeSetReplicationSlotMinimumLSN(restart_lsn);
+
 		INJECTION_POINT("physical-slot-reserve-wal-get-redo", NULL);
 
 		SpinLockAcquire(&slot->mutex);
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index a12757e46e5..51f8a1f7897 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -215,6 +215,7 @@ extern XLogSegNo XLogGetLastRemovedSegno(void);
 extern XLogSegNo XLogGetOldestSegno(TimeLineID tli);
 extern void XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN);
 extern void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn);
+extern void XLogMaybeSetReplicationSlotMinimumLSN(XLogRecPtr lsn);
 
 extern void xlog_redo(struct XLogReaderState *record);
 extern void xlog_desc(StringInfo buf, struct XLogReaderState *record);