From 280c53ecfbcad442c4b5eaab4cc3e65408f486eb Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 19 Apr 2017 02:16:34 +0900
Subject: [PATCH] A collection of small fixes for logical replication.

* Be sure to reset the launcher's pid (LogicalRepCtx->launcher_pid) to 0
  even when the launcher emits an error.

* Declare ApplyLauncherWakeup() as a static function because it's called
  only in launcher.c.

* Previously IsBackendPId() was used to check whether the launcher's pid
  was valid. IsBackendPid() was necessary because there was the bug where
  the launcher's pid was not reset to 0. But now it's fixed, so IsBackendPid()
  is not necessary and this patch removes it.

Author: Masahiko Sawada
Reviewed-by: Kyotaro Horiguchi
Reported-by: Fujii Masao
Discussion: http://postgr.es/m/CAHGQGwFDWh_Qr-q_GEMpD+qH=vYPMdVqw=ZOSY3kX_Pna9R9SA@mail.gmail.com
---
 src/backend/replication/logical/launcher.c | 19 +++++++++++++++++--
 src/include/replication/logicallauncher.h  |  1 -
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c
index 0dba25962ad..f6ae61060ec 100644
--- a/src/backend/replication/logical/launcher.c
+++ b/src/backend/replication/logical/launcher.c
@@ -72,6 +72,8 @@ typedef struct LogicalRepCtxStruct
 
 LogicalRepCtxStruct *LogicalRepCtx;
 
+static void ApplyLauncherWakeup(void);
+static void logicalrep_launcher_onexit(int code, Datum arg);
 static void logicalrep_worker_onexit(int code, Datum arg);
 static void logicalrep_worker_detach(void);
 
@@ -480,6 +482,17 @@ logicalrep_worker_detach(void)
 	LWLockRelease(LogicalRepWorkerLock);
 }
 
+/*
+ * Cleanup function for logical replication launcher.
+ *
+ * Called on logical replication launcher exit.
+ */
+static void
+logicalrep_launcher_onexit(int code, Datum arg)
+{
+	LogicalRepCtx->launcher_pid = 0;
+}
+
 /*
  * Cleanup function.
  *
@@ -643,10 +656,10 @@ ApplyLauncherWakeupAtCommit(void)
 		on_commit_launcher_wakeup = true;
 }
 
-void
+static void
 ApplyLauncherWakeup(void)
 {
-	if (IsBackendPid(LogicalRepCtx->launcher_pid))
+	if (LogicalRepCtx->launcher_pid != 0)
 		kill(LogicalRepCtx->launcher_pid, SIGUSR1);
 }
 
@@ -659,6 +672,8 @@ ApplyLauncherMain(Datum main_arg)
 	ereport(DEBUG1,
 			(errmsg("logical replication launcher started")));
 
+	before_shmem_exit(logicalrep_launcher_onexit, (Datum) 0);
+
 	/* Establish signal handlers. */
 	pqsignal(SIGHUP, logicalrep_worker_sighup);
 	pqsignal(SIGTERM, logicalrep_worker_sigterm);
diff --git a/src/include/replication/logicallauncher.h b/src/include/replication/logicallauncher.h
index 060946a0964..0c2bf03a5fe 100644
--- a/src/include/replication/logicallauncher.h
+++ b/src/include/replication/logicallauncher.h
@@ -21,7 +21,6 @@ extern void ApplyLauncherMain(Datum main_arg);
 extern Size ApplyLauncherShmemSize(void);
 extern void ApplyLauncherShmemInit(void);
 
-extern void ApplyLauncherWakeup(void);
 extern void ApplyLauncherWakeupAtCommit(void);
 extern void AtCommit_ApplyLauncher(void);
 
-- 
GitLab