From 01e7ecc91b729ae5c89b048ed343710c398be110 Mon Sep 17 00:00:00 2001
From: Shveta Malik <shveta.malik@gmail.com>
Date: Tue, 29 Sep 2026 11:00:01 +0530
Subject: [PATCH] patch optimization.

---
 src/backend/replication/logical/slotsync.c |  3 +-
 src/backend/replication/slot.c             | 42 +++++++++++++---------
 src/include/replication/slot.h             |  3 +-
 3 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c
index 8191a3c4892..055f643d62b 100644
--- a/src/backend/replication/logical/slotsync.c
+++ b/src/backend/replication/logical/slotsync.c
@@ -830,8 +830,7 @@ synchronize_one_slot(RemoteSlot *remote_slot, Oid remote_dbid,
 			remote_slot->invalidated != RS_INVAL_NONE)
 		{
 			LWLockAcquire(&slot->io_in_progress_lock, LW_EXCLUSIVE);
-			ReplicationSlotPersistInvalidation(remote_slot->invalidated, false,
-											   true);
+			ReplicationSlotPersistInvalidation(remote_slot->invalidated, false);
 			LWLockRelease(&slot->io_in_progress_lock);
 			ReplicationSlotsComputeRequiredXmin(false);
 			ReplicationSlotsComputeRequiredLSN();
diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c
index 0c121b153dd..cd0f72c09e5 100644
--- a/src/backend/replication/slot.c
+++ b/src/backend/replication/slot.c
@@ -184,8 +184,9 @@ static SyncStandbySlotsConfigData *synchronized_standby_slots_config;
  */
 static XLogRecPtr ss_oldest_flush_lsn = InvalidXLogRecPtr;
 
+static bool invalidation_persist_failed = false;
+
 static void ReplicationSlotShmemExit(int code, Datum arg);
-static void ReplicationSlotReleaseInternal(bool update_inactive_since);
 static bool IsSlotForConflictCheck(const char *name);
 static void ReplicationSlotDropPtr(ReplicationSlot *slot);
 
@@ -772,20 +773,18 @@ retry:
  */
 void
 ReplicationSlotRelease(void)
-{
-	ReplicationSlotReleaseInternal(true);
-}
-
-/*
- * Release the replication slot, optionally preserving inactive_since.
- */
-static void
-ReplicationSlotReleaseInternal(bool update_inactive_since)
 {
 	ReplicationSlot *slot = MyReplicationSlot;
 	char	   *slotname = NULL;	/* keep compiler quiet */
 	bool		is_logical;
 	TimestampTz now = 0;
+	bool		update_inactive_since = !invalidation_persist_failed;
+
+	/*
+	 * Reset this as early as possible, since an error later in this function
+	 * could prevent it from being reset.
+	 */
+	invalidation_persist_failed = false;
 
 	Assert(slot != NULL && slot->active_proc != INVALID_PROC_NUMBER);
 
@@ -793,7 +792,12 @@ ReplicationSlotReleaseInternal(bool update_inactive_since)
 	 * Skipping the inactive_since update is only needed when undoing the
 	 * internal acquisition of an inactive persistent slot after an ERROR.
 	 */
-	Assert(update_inactive_since || slot->data.persistency == RS_PERSISTENT);
+	/* Perhaps this Assert is not needed now, need to think more */
+
+	/*
+	 * Assert(update_inactive_since || slot->data.persistency ==
+	 * RS_PERSISTENT);
+	 */
 
 	is_logical = SlotIsLogical(slot);
 
@@ -1201,17 +1205,21 @@ ReplicationSlotSave(void)
  * update inactive_since only if requested.
  */
 void
-ReplicationSlotPersistInvalidation(ReplicationSlotInvalidationCause cause,
-								   bool clear_restart_lsn,
-								   bool update_inactive_since)
+			ReplicationSlotPersistInvalidation(ReplicationSlotInvalidationCause cause,
+											   bool clear_restart_lsn)
+
 {
 	char		path[MAXPGPATH];
 	ReplicationSlot *slot = MyReplicationSlot;
 
 	Assert(slot != NULL);
+
+/* Need to think on this Assert again */
+#if 0
 	Assert(slot->data.persistency == RS_PERSISTENT ||
 		   (slot->data.persistency == RS_TEMPORARY &&
 			update_inactive_since));
+#endif
 	Assert(slot->data.invalidated == RS_INVAL_NONE);
 	Assert(cause != RS_INVAL_NONE);
 	Assert(!clear_restart_lsn || cause == RS_INVAL_WAL_REMOVED);
@@ -1226,7 +1234,8 @@ ReplicationSlotPersistInvalidation(ReplicationSlotInvalidationCause cause,
 	PG_CATCH();
 	{
 		HOLD_INTERRUPTS();		/* match the upcoming RESUME_INTERRUPTS */
-		ReplicationSlotReleaseInternal(update_inactive_since);
+		invalidation_persist_failed = true;
+		ReplicationSlotRelease();
 		LWLockRelease(&slot->io_in_progress_lock);
 		PG_RE_THROW();
 	}
@@ -2261,8 +2270,7 @@ InvalidatePossiblyObsoleteSlot(uint32 possible_causes,
 			released_lock = true;
 
 			ReplicationSlotPersistInvalidation(invalidation_cause,
-											   invalidation_cause == RS_INVAL_WAL_REMOVED,
-											   false);
+											   invalidation_cause == RS_INVAL_WAL_REMOVED);
 
 			/* Let caller know */
 			invalidated = true;
diff --git a/src/include/replication/slot.h b/src/include/replication/slot.h
index 885d3af236c..80d48020a87 100644
--- a/src/include/replication/slot.h
+++ b/src/include/replication/slot.h
@@ -345,8 +345,7 @@ extern void ReplicationSlotRelease(void);
 extern void ReplicationSlotCleanup(bool synced_only);
 extern void ReplicationSlotSave(void);
 extern void ReplicationSlotPersistInvalidation(ReplicationSlotInvalidationCause cause,
-											   bool clear_restart_lsn,
-											   bool update_inactive_since);
+											   bool clear_restart_lsn);
 extern void ReplicationSlotMarkDirty(void);
 
 /* misc stuff */
-- 
2.34.1

