From f143c8d7475e1808274c19020a2f66706e895260 Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Thu, 23 Nov 2023 04:32:57 +0000
Subject: [PATCH v1] Introduce lockless exit path for
 ReplicationOriginExitCleanup

---
 src/backend/replication/logical/origin.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/backend/replication/logical/origin.c b/src/backend/replication/logical/origin.c
index 460e3dcc38..7b879d1861 100644
--- a/src/backend/replication/logical/origin.c
+++ b/src/backend/replication/logical/origin.c
@@ -172,9 +172,9 @@ static ReplicationState *replication_states;
 static ReplicationStateCtl *replication_states_ctl;
 
 /*
- * Backend-local, cached element from ReplicationState for use in a backend
- * replaying remote commits, so we don't have to search ReplicationState for
- * the backends current RepOriginId.
+ * A backend-local cached pointer to backend's ReplicationState in the shared
+ * memory array of replication states so that the backend while replaying
+ * remote commits doesn't have to search the array with current RepOriginId.
  */
 static ReplicationState *session_replication_state = NULL;
 
@@ -1056,10 +1056,12 @@ ReplicationOriginExitCleanup(int code, Datum arg)
 {
 	ConditionVariable *cv = NULL;
 
+	if (session_replication_state == NULL)
+		return;
+
 	LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
 
-	if (session_replication_state != NULL &&
-		session_replication_state->acquired_by == MyProcPid)
+	if (session_replication_state->acquired_by == MyProcPid)
 	{
 		cv = &session_replication_state->origin_cv;
 
-- 
2.34.1

