From 3b6fc2b08260d437bc908378cfc050f538c6433e Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon, 4 Dec 2023 13:24:01 +0200
Subject: [PATCH v5 6/8] Refactor AuxProcess startup

Old:

    AuxProcessMain()
      Initialize a bunch of stuff
      <aux process specific Main function>()

New:

    AuxProcessMain()
      <aux process specific Main function>()
        AuxiliaryProcessInit()
          Initialize a bunch of stuff

This isn't too useful as is, but the next commits will remove the
AuxProcessMain() function and dispatch directly to the aux-process
specific Main function, like this:

    <aux process specific Main function>()
      AuxiliaryProcessInit()
        Initialize a bunch of stuff

This commit makes that next commit smaller.

XXX: We now have functions called AuxiliaryProcessInit() and
InitAuxiliaryProcess(). Confusing.
---
 src/backend/postmaster/auxprocess.c   | 86 ++++++++++++---------------
 src/backend/postmaster/bgwriter.c     |  5 ++
 src/backend/postmaster/checkpointer.c |  5 ++
 src/backend/postmaster/pgarch.c       |  5 ++
 src/backend/postmaster/startup.c      |  5 ++
 src/backend/postmaster/walwriter.c    |  5 ++
 src/backend/replication/walreceiver.c |  5 ++
 src/include/postmaster/auxprocess.h   |  3 +-
 8 files changed, 69 insertions(+), 50 deletions(-)

diff --git a/src/backend/postmaster/auxprocess.c b/src/backend/postmaster/auxprocess.c
index bae6f68c402..d62adf4c993 100644
--- a/src/backend/postmaster/auxprocess.c
+++ b/src/backend/postmaster/auxprocess.c
@@ -44,7 +44,6 @@ static void ShutdownAuxiliaryProcess(int code, Datum arg);
 
 AuxProcType MyAuxProcType = NotAnAuxProcess;	/* declared in miscadmin.h */
 
-
 /*
  *	 AuxiliaryProcessMain
  *
@@ -58,33 +57,56 @@ AuxiliaryProcessMain(AuxProcType auxtype)
 {
 	Assert(IsUnderPostmaster);
 
-	MyAuxProcType = auxtype;
-
 	switch (MyAuxProcType)
 	{
 		case StartupProcess:
-			MyBackendType = B_STARTUP;
-			break;
+			StartupProcessMain();
+			proc_exit(1);
+
 		case ArchiverProcess:
-			MyBackendType = B_ARCHIVER;
-			break;
+			PgArchiverMain();
+			proc_exit(1);
+
 		case BgWriterProcess:
-			MyBackendType = B_BG_WRITER;
-			break;
+			BackgroundWriterMain();
+			proc_exit(1);
+
 		case CheckpointerProcess:
-			MyBackendType = B_CHECKPOINTER;
-			break;
+			CheckpointerMain();
+			proc_exit(1);
+
 		case WalWriterProcess:
-			MyBackendType = B_WAL_WRITER;
-			break;
+			WalWriterMain();
+			proc_exit(1);
+
 		case WalReceiverProcess:
-			MyBackendType = B_WAL_RECEIVER;
-			break;
+			WalReceiverMain();
+			proc_exit(1);
+
 		default:
 			elog(PANIC, "unrecognized process type: %d", (int) MyAuxProcType);
-			MyBackendType = B_INVALID;
+			proc_exit(1);
+	}
+}
+
+/*
+ *	 AuxiliaryProcessInit
+ *
+ *	 Common initialization code for auxiliary processes, such as the bgwriter,
+ *	 walwriter, walreceiver, bootstrapper and the shared memory checker code.
+ */
+void
+AuxiliaryProcessInit(void)
+{
+	/* Release postmaster's working memory context */
+	if (PostmasterContext)
+	{
+		MemoryContextDelete(PostmasterContext);
+		PostmasterContext = NULL;
 	}
 
+	Assert(IsUnderPostmaster);
+
 	init_ps_display(NULL);
 
 	SetProcessingMode(BootstrapProcessing);
@@ -122,7 +144,6 @@ AuxiliaryProcessMain(AuxProcType auxtype)
 	 */
 	CreateAuxProcessResourceOwner();
 
-
 	/* Initialize backend status information */
 	pgstat_beinit();
 	pgstat_bestart();
@@ -131,37 +152,6 @@ AuxiliaryProcessMain(AuxProcType auxtype)
 	before_shmem_exit(ShutdownAuxiliaryProcess, 0);
 
 	SetProcessingMode(NormalProcessing);
-
-	switch (MyAuxProcType)
-	{
-		case StartupProcess:
-			StartupProcessMain();
-			proc_exit(1);
-
-		case ArchiverProcess:
-			PgArchiverMain();
-			proc_exit(1);
-
-		case BgWriterProcess:
-			BackgroundWriterMain();
-			proc_exit(1);
-
-		case CheckpointerProcess:
-			CheckpointerMain();
-			proc_exit(1);
-
-		case WalWriterProcess:
-			WalWriterMain();
-			proc_exit(1);
-
-		case WalReceiverProcess:
-			WalReceiverMain();
-			proc_exit(1);
-
-		default:
-			elog(PANIC, "unrecognized process type: %d", (int) MyAuxProcType);
-			proc_exit(1);
-	}
 }
 
 /*
diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c
index d02dc17b9c1..95abdd7fa6d 100644
--- a/src/backend/postmaster/bgwriter.c
+++ b/src/backend/postmaster/bgwriter.c
@@ -36,6 +36,7 @@
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "pgstat.h"
+#include "postmaster/auxprocess.h"
 #include "postmaster/bgwriter.h"
 #include "postmaster/interrupt.h"
 #include "storage/buf_internals.h"
@@ -95,6 +96,10 @@ BackgroundWriterMain(void)
 	bool		prev_hibernate;
 	WritebackContext wb_context;
 
+	MyAuxProcType = BgWriterProcess;
+	MyBackendType = B_BG_WRITER;
+	AuxiliaryProcessInit();
+
 	/*
 	 * Properly accept or ignore signals that might be sent to us.
 	 */
diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c
index dc2da5a2cd8..1871ac52921 100644
--- a/src/backend/postmaster/checkpointer.c
+++ b/src/backend/postmaster/checkpointer.c
@@ -42,6 +42,7 @@
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "pgstat.h"
+#include "postmaster/auxprocess.h"
 #include "postmaster/bgwriter.h"
 #include "postmaster/interrupt.h"
 #include "replication/syncrep.h"
@@ -174,6 +175,10 @@ CheckpointerMain(void)
 	sigjmp_buf	local_sigjmp_buf;
 	MemoryContext checkpointer_context;
 
+	MyAuxProcType = CheckpointerProcess;
+	MyBackendType = B_CHECKPOINTER;
+	AuxiliaryProcessInit();
+
 	CheckpointerShmem->checkpointer_pid = MyProcPid;
 
 	/*
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index a2555e8578c..5d5c5733340 100644
--- a/src/backend/postmaster/pgarch.c
+++ b/src/backend/postmaster/pgarch.c
@@ -36,6 +36,7 @@
 #include "lib/binaryheap.h"
 #include "libpq/pqsignal.h"
 #include "pgstat.h"
+#include "postmaster/auxprocess.h"
 #include "postmaster/interrupt.h"
 #include "postmaster/pgarch.h"
 #include "storage/fd.h"
@@ -213,6 +214,10 @@ PgArchCanRestart(void)
 void
 PgArchiverMain(void)
 {
+	MyAuxProcType = ArchiverProcess;
+	MyBackendType = B_ARCHIVER;
+	AuxiliaryProcessInit();
+
 	/*
 	 * Ignore all signals usually bound to some action in the postmaster,
 	 * except for SIGHUP, SIGTERM, SIGUSR1, SIGUSR2, and SIGQUIT.
diff --git a/src/backend/postmaster/startup.c b/src/backend/postmaster/startup.c
index 082c870e03a..0fdfa1822db 100644
--- a/src/backend/postmaster/startup.c
+++ b/src/backend/postmaster/startup.c
@@ -27,6 +27,7 @@
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "pgstat.h"
+#include "postmaster/auxprocess.h"
 #include "postmaster/startup.h"
 #include "storage/ipc.h"
 #include "storage/latch.h"
@@ -243,6 +244,10 @@ StartupProcExit(int code, Datum arg)
 void
 StartupProcessMain(void)
 {
+	MyAuxProcType = StartupProcess;
+	MyBackendType = B_STARTUP;
+	AuxiliaryProcessInit();
+
 	/* Arrange to clean up at startup process exit */
 	on_shmem_exit(StartupProcExit, 0);
 
diff --git a/src/backend/postmaster/walwriter.c b/src/backend/postmaster/walwriter.c
index 48bc92205b5..0575d2c967d 100644
--- a/src/backend/postmaster/walwriter.c
+++ b/src/backend/postmaster/walwriter.c
@@ -48,6 +48,7 @@
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "pgstat.h"
+#include "postmaster/auxprocess.h"
 #include "postmaster/interrupt.h"
 #include "postmaster/walwriter.h"
 #include "storage/bufmgr.h"
@@ -95,6 +96,10 @@ WalWriterMain(void)
 	int			left_till_hibernate;
 	bool		hibernating;
 
+	MyAuxProcType = WalWriterProcess;
+	MyBackendType = B_WAL_WRITER;
+	AuxiliaryProcessInit();
+
 	/*
 	 * Properly accept or ignore signals the postmaster might send us
 	 *
diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index 26ded928a71..51fd1de9c8b 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -65,6 +65,7 @@
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "pgstat.h"
+#include "postmaster/auxprocess.h"
 #include "postmaster/interrupt.h"
 #include "replication/walreceiver.h"
 #include "replication/walsender.h"
@@ -199,6 +200,10 @@ WalReceiverMain(void)
 	char	   *sender_host = NULL;
 	int			sender_port = 0;
 
+	MyAuxProcType = WalReceiverProcess;
+	MyBackendType = B_WAL_RECEIVER;
+	AuxiliaryProcessInit();
+
 	/*
 	 * WalRcv should be set up already (if we are a backend, we inherit this
 	 * by fork() or EXEC_BACKEND mechanism from the postmaster).
diff --git a/src/include/postmaster/auxprocess.h b/src/include/postmaster/auxprocess.h
index 5c2d6527ff6..cc75f246818 100644
--- a/src/include/postmaster/auxprocess.h
+++ b/src/include/postmaster/auxprocess.h
@@ -13,8 +13,7 @@
 #ifndef AUXPROCESS_H
 #define AUXPROCESS_H
 
-#include "miscadmin.h"
-
 extern void AuxiliaryProcessMain(AuxProcType auxtype) pg_attribute_noreturn();
+extern void AuxiliaryProcessInit(void);
 
 #endif							/* AUXPROCESS_H */
-- 
2.39.2

