From 490add168b7cf63ec584e75f6a7f79efc08ae200 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 3 Feb 2017 10:31:01 +0900
Subject: [PATCH] Refactor the lock section for subscription worker termination

---
 src/backend/commands/subscriptioncmds.c    | 3 ---
 src/backend/replication/logical/launcher.c | 5 +++++
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index 3b70807..67c587c 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -508,9 +508,6 @@ DropSubscription(DropSubscriptionStmt *stmt)
 	/* Clean up dependencies */
 	deleteSharedDependencyRecordsFor(SubscriptionRelationId, subid, 0);
 
-	/* Protect against launcher restarting the worker. */
-	LWLockAcquire(LogicalRepLauncherLock, LW_EXCLUSIVE);
-
 	/* Kill the apply worker so that the slot becomes accessible. */
 	logicalrep_worker_stop(subid);
 
diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c
index d222cff..233be06 100644
--- a/src/backend/replication/logical/launcher.c
+++ b/src/backend/replication/logical/launcher.c
@@ -368,6 +368,9 @@ logicalrep_worker_stop(Oid subid)
 			break;
 	}
 
+	/* Block the lauchner not to restart this worker */
+	LWLockAcquire(LogicalRepLauncherLock);
+
 	/* Now terminate the worker ... */
 	kill(worker->proc->pid, SIGTERM);
 	LWLockRelease(LogicalRepWorkerLock);
@@ -398,6 +401,8 @@ logicalrep_worker_stop(Oid subid)
 
 		ResetLatch(&MyProc->procLatch);
 	}
+
+	LWLockRelease(LogicalRepLauncherLock);
 }
 
 /*
-- 
2.9.2

