v12-0006-Refactor-AuxProcess-startup.patch

text/x-patch

Filename: v12-0006-Refactor-AuxProcess-startup.patch
Type: text/x-patch
Part: 5
Message: Re: Refactoring backend fork+exec code

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v12-0006
Subject: Refactor AuxProcess startup
File+
src/backend/postmaster/auxprocess.c 53 36
src/backend/postmaster/bgwriter.c 3 0
src/backend/postmaster/checkpointer.c 3 0
src/backend/postmaster/pgarch.c 3 0
src/backend/postmaster/startup.c 3 0
src/backend/postmaster/walsummarizer.c 3 1
src/backend/postmaster/walwriter.c 3 0
src/backend/replication/walreceiver.c 3 0
src/include/postmaster/auxprocess.h 1 2
From fb18820bc3938d685ef1ad40e3b392f4804c74f2 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 v12 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    | 89 +++++++++++++++-----------
 src/backend/postmaster/bgwriter.c      |  3 +
 src/backend/postmaster/checkpointer.c  |  3 +
 src/backend/postmaster/pgarch.c        |  3 +
 src/backend/postmaster/startup.c       |  3 +
 src/backend/postmaster/walsummarizer.c |  4 +-
 src/backend/postmaster/walwriter.c     |  3 +
 src/backend/replication/walreceiver.c  |  3 +
 src/include/postmaster/auxprocess.h    |  3 +-
 9 files changed, 75 insertions(+), 39 deletions(-)

diff --git a/src/backend/postmaster/auxprocess.c b/src/backend/postmaster/auxprocess.c
index fc13cd76321..eb436d11d76 100644
--- a/src/backend/postmaster/auxprocess.c
+++ b/src/backend/postmaster/auxprocess.c
@@ -52,6 +52,59 @@ AuxiliaryProcessMain(BackendType auxtype)
 	Assert(IsUnderPostmaster);
 
 	MyBackendType = auxtype;
+	switch (MyBackendType)
+	{
+		case B_STARTUP:
+			StartupProcessMain();
+			proc_exit(1);
+
+		case B_ARCHIVER:
+			PgArchiverMain();
+			proc_exit(1);
+
+		case B_BG_WRITER:
+			BackgroundWriterMain();
+			proc_exit(1);
+
+		case B_CHECKPOINTER:
+			CheckpointerMain();
+			proc_exit(1);
+
+		case B_WAL_WRITER:
+			WalWriterMain();
+			proc_exit(1);
+
+		case B_WAL_RECEIVER:
+			WalReceiverMain();
+			proc_exit(1);
+
+		case B_WAL_SUMMARIZER:
+			WalSummarizerMain();
+			proc_exit(1);
+
+		default:
+			elog(PANIC, "unrecognized process type: %d", (int) MyBackendType);
+			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);
 
@@ -80,7 +133,6 @@ AuxiliaryProcessMain(BackendType auxtype)
 	 */
 	CreateAuxProcessResourceOwner();
 
-
 	/* Initialize backend status information */
 	pgstat_beinit();
 	pgstat_bestart();
@@ -89,41 +141,6 @@ AuxiliaryProcessMain(BackendType auxtype)
 	before_shmem_exit(ShutdownAuxiliaryProcess, 0);
 
 	SetProcessingMode(NormalProcessing);
-
-	switch (MyBackendType)
-	{
-		case B_STARTUP:
-			StartupProcessMain();
-			proc_exit(1);
-
-		case B_ARCHIVER:
-			PgArchiverMain();
-			proc_exit(1);
-
-		case B_BG_WRITER:
-			BackgroundWriterMain();
-			proc_exit(1);
-
-		case B_CHECKPOINTER:
-			CheckpointerMain();
-			proc_exit(1);
-
-		case B_WAL_WRITER:
-			WalWriterMain();
-			proc_exit(1);
-
-		case B_WAL_RECEIVER:
-			WalReceiverMain();
-			proc_exit(1);
-
-		case B_WAL_SUMMARIZER:
-			WalSummarizerMain();
-			proc_exit(1);
-
-		default:
-			elog(PANIC, "unrecognized process type: %d", (int) MyBackendType);
-			proc_exit(1);
-	}
 }
 
 /*
diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c
index 6364b16261f..b3cf8ba02c7 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,8 @@ BackgroundWriterMain(void)
 	bool		prev_hibernate;
 	WritebackContext wb_context;
 
+	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 46197d56f86..a882d6f44c0 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,8 @@ CheckpointerMain(void)
 	sigjmp_buf	local_sigjmp_buf;
 	MemoryContext checkpointer_context;
 
+	AuxiliaryProcessInit();
+
 	CheckpointerShmem->checkpointer_pid = MyProcPid;
 
 	/*
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index bb0eb13a898..e830888d9a2 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"
@@ -210,6 +211,8 @@ PgArchCanRestart(void)
 void
 PgArchiverMain(void)
 {
+	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 44b6c5bb758..bfdc64e91fb 100644
--- a/src/backend/postmaster/startup.c
+++ b/src/backend/postmaster/startup.c
@@ -25,6 +25,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"
@@ -216,6 +217,8 @@ StartupProcExit(int code, Datum arg)
 void
 StartupProcessMain(void)
 {
+	AuxiliaryProcessInit();
+
 	/* Arrange to clean up at startup process exit */
 	on_shmem_exit(StartupProcExit, 0);
 
diff --git a/src/backend/postmaster/walsummarizer.c b/src/backend/postmaster/walsummarizer.c
index 8636d9424ec..3addb9cd41b 100644
--- a/src/backend/postmaster/walsummarizer.c
+++ b/src/backend/postmaster/walsummarizer.c
@@ -32,7 +32,7 @@
 #include "common/blkreftable.h"
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
-#include "postmaster/bgwriter.h"
+#include "postmaster/auxprocess.h"
 #include "postmaster/interrupt.h"
 #include "postmaster/walsummarizer.h"
 #include "replication/walreceiver.h"
@@ -227,6 +227,8 @@ WalSummarizerMain(void)
 	XLogRecPtr	switch_lsn = InvalidXLogRecPtr;
 	TimeLineID	switch_tli = 0;
 
+	AuxiliaryProcessInit();
+
 	ereport(DEBUG1,
 			(errmsg_internal("WAL summarizer started")));
 
diff --git a/src/backend/postmaster/walwriter.c b/src/backend/postmaster/walwriter.c
index e079dc65c88..f0046c53d71 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"
@@ -92,6 +93,8 @@ WalWriterMain(void)
 	int			left_till_hibernate;
 	bool		hibernating;
 
+	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 b80447d15f1..d08751dca02 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,8 @@ WalReceiverMain(void)
 	char	   *sender_host = NULL;
 	int			sender_port = 0;
 
+	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 3e443edde70..8f8ffb25034 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(BackendType auxtype) pg_attribute_noreturn();
+extern void AuxiliaryProcessInit(void);
 
 #endif							/* AUXPROCESS_H */
-- 
2.39.2