From ea90a3ed0c0d346caedcb52f942d662e7c4a90f2 Mon Sep 17 00:00:00 2001
From: Mike Palmiotto <mike.palmiotto@crunchydata.com>
Date: Wed, 4 Mar 2020 02:59:23 +0000
Subject: [PATCH 06/11] Fix up subprocess init logic and docs

---
 src/backend/bootstrap/bootstrap.c   | 9 +++++----
 src/backend/postmaster/postmaster.c | 9 ++++++---
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index 76a84a15f5..bd3d80b996 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -314,11 +314,12 @@ AuxiliaryProcessMain(int argc, char *argv[])
 	}
 
 	/*
-	 * At this point, we should know what kind of process we are. If the
-	 * MySubprocess global has not been initialized, just cast and pass along the
-	 * MyAuxProcType enum
+	 * We have two callers of this function: 1. StartSubprocess(), which calls
+	 * this for auxiliary processes after calling InitPostmasterChild() and 2.
+	 * main(), which calls this for bootstrapping postgres. In the latter case,
+	 * we need to initialize the process struct.
 	 */
-	if (MySubprocess == NULL)
+	if (!IsUnderPostmaster)
 		InitializeMySubprocess((SubprocessType)MyAuxProcType);
 
 	/*
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 55f4067f79..0589fd27f9 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -4918,8 +4918,9 @@ SubPostmasterMain(int argc, char *argv[])
 				 errmsg("out of memory")));
 #endif
 
-	if (!MySubprocess)
-		InitializeMySubprocess(MySubprocessType);
+	/* We should only be here once per fork */
+	Assert(!MySubprocess);
+	InitializeMySubprocess(MySubprocessType);
 
 	/*
 	 * If appropriate, physically re-attach to shared memory segment. We want
@@ -5391,7 +5392,9 @@ StartSubprocess(SubprocessType type)
 	char		forkname[32];
 #endif
 
-	/* This should be our first time in this function */
+	/*
+	 * Get new subprocess data every time we start a new subprocess.
+	 */
 	InitializeMySubprocess(type);
 
 	argv[argc++] = "postgres";
-- 
2.21.0

