v13-0004-Refactor-AuxProcess-startup.patch

text/x-patch

Filename: v13-0004-Refactor-AuxProcess-startup.patch
Type: text/x-patch
Part: 3
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 v13-0004
Subject: Refactor AuxProcess startup
File+
src/backend/postmaster/auxprocess.c 54 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 0
src/backend/postmaster/walwriter.c 3 0
src/backend/replication/walreceiver.c 3 0
src/include/postmaster/auxprocess.h 1 2
From 08e0c8f15b83fdd7544bae2fd7cf83195e4f8da2 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 v13 4/6] Refactor AuxProcess startup

Old:

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

New:

    AuxProcessMain()
      <aux process specific Main function>()
        AuxiliaryProcessMainCommon()
          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>()
      AuxiliaryProcessMainCommon()
        Initialize a bunch of stuff

This commit makes that next commit smaller.
---
 src/backend/postmaster/auxprocess.c    | 90 +++++++++++++++-----------
 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 |  3 +
 src/backend/postmaster/walwriter.c     |  3 +
 src/backend/replication/walreceiver.c  |  3 +
 src/include/postmaster/auxprocess.h    |  3 +-
 9 files changed, 76 insertions(+), 38 deletions(-)

diff --git a/src/backend/postmaster/auxprocess.c b/src/backend/postmaster/auxprocess.c
index 2c86abdb719..b5a35f6c0ea 100644
--- a/src/backend/postmaster/auxprocess.c
+++ b/src/backend/postmaster/auxprocess.c
@@ -27,6 +27,7 @@
 #include "storage/ipc.h"
 #include "storage/proc.h"
 #include "storage/procsignal.h"
+#include "utils/memutils.h"
 #include "utils/ps_status.h"
 
 
@@ -47,6 +48,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);
+	}
+}
+
+/*
+ *	 AuxiliaryProcessMainCommon
+ *
+ *	 Common initialization code for auxiliary processes, such as the bgwriter,
+ *	 walwriter, walreceiver, bootstrapper and the shared memory checker code.
+ */
+void
+AuxiliaryProcessMainCommon(void)
+{
+	/* Release postmaster's working memory context */
+	if (PostmasterContext)
+	{
+		MemoryContextDelete(PostmasterContext);
+		PostmasterContext = NULL;
+	}
+
+	Assert(IsUnderPostmaster);
 
 	init_ps_display(NULL);
 
@@ -75,7 +129,6 @@ AuxiliaryProcessMain(BackendType auxtype)
 	 */
 	CreateAuxProcessResourceOwner();
 
-
 	/* Initialize backend status information */
 	pgstat_beinit();
 	pgstat_bestart();
@@ -84,41 +137,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 da2d95b9261..ad500b7fa2a 100644
--- a/src/backend/postmaster/bgwriter.c
+++ b/src/backend/postmaster/bgwriter.c
@@ -35,6 +35,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"
@@ -90,6 +91,8 @@ BackgroundWriterMain(void)
 	bool		prev_hibernate;
 	WritebackContext wb_context;
 
+	AuxiliaryProcessMainCommon();
+
 	/*
 	 * 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..b8d943a550e 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;
 
+	AuxiliaryProcessMainCommon();
+
 	CheckpointerShmem->checkpointer_pid = MyProcPid;
 
 	/*
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index f97035ca03c..14e45bb7071 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"
@@ -211,6 +212,8 @@ PgArchCanRestart(void)
 void
 PgArchiverMain(void)
 {
+	AuxiliaryProcessMainCommon();
+
 	/*
 	 * 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 8b51e45bad0..ea7db2040f0 100644
--- a/src/backend/postmaster/startup.c
+++ b/src/backend/postmaster/startup.c
@@ -24,6 +24,7 @@
 #include "access/xlogutils.h"
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
+#include "postmaster/auxprocess.h"
 #include "postmaster/startup.h"
 #include "storage/ipc.h"
 #include "storage/pmsignal.h"
@@ -214,6 +215,8 @@ StartupProcExit(int code, Datum arg)
 void
 StartupProcessMain(void)
 {
+	AuxiliaryProcessMainCommon();
+
 	/* 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 8bf8a03471a..8d9ee1f0f9b 100644
--- a/src/backend/postmaster/walsummarizer.c
+++ b/src/backend/postmaster/walsummarizer.c
@@ -33,6 +33,7 @@
 #include "common/blkreftable.h"
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
+#include "postmaster/auxprocess.h"
 #include "postmaster/interrupt.h"
 #include "postmaster/walsummarizer.h"
 #include "replication/walreceiver.h"
@@ -228,6 +229,8 @@ WalSummarizerMain(void)
 	XLogRecPtr	switch_lsn = InvalidXLogRecPtr;
 	TimeLineID	switch_tli = 0;
 
+	AuxiliaryProcessMainCommon();
+
 	ereport(DEBUG1,
 			(errmsg_internal("WAL summarizer started")));
 
diff --git a/src/backend/postmaster/walwriter.c b/src/backend/postmaster/walwriter.c
index e079dc65c88..dc052854db8 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;
 
+	AuxiliaryProcessMainCommon();
+
 	/*
 	 * 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 5a0652c9426..bfe7e4a8894 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -63,6 +63,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"
@@ -195,6 +196,8 @@ WalReceiverMain(void)
 	char	   *sender_host = NULL;
 	int			sender_port = 0;
 
+	AuxiliaryProcessMainCommon();
+
 	/*
 	 * 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..2cbd29534c4 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 AuxiliaryProcessMainCommon(void);
 
 #endif							/* AUXPROCESS_H */
-- 
2.39.2