v3-0007-Use-an-shmem_exit-callback-to-remove-backend-from.patch
text/x-patch
Filename: v3-0007-Use-an-shmem_exit-callback-to-remove-backend-from.patch
Type: text/x-patch
Part: 6
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 v3-0007
Subject: Use an shmem_exit callback to remove backend from PMChildFlags on exit
| File | + | − |
|---|---|---|
| src/backend/storage/ipc/pmsignal.c | 8 | 2 |
| src/backend/storage/lmgr/proc.c | 13 | 25 |
| src/include/storage/pmsignal.h | 0 | 1 |
From e71056398cd19c2800e174e9a4356eb2dd5456f1 Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Thu, 1 Aug 2024 15:58:41 +0300
Subject: [PATCH v3 07/12] Use an shmem_exit callback to remove backend from
PMChildFlags on exit
This seems nicer than having to duplicate the logic between
InitProcess() and ProcKill() for which child processes have a
PMChildFlags slot.
Move the MarkPostmasterChildActive() call earlier in InitProcess(),
out of the section protected by the spinlock.
---
src/backend/storage/ipc/pmsignal.c | 10 ++++++--
src/backend/storage/lmgr/proc.c | 38 ++++++++++--------------------
src/include/storage/pmsignal.h | 1 -
3 files changed, 21 insertions(+), 28 deletions(-)
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index 27844b46a2..cb99e77476 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -24,6 +24,7 @@
#include "miscadmin.h"
#include "postmaster/postmaster.h"
#include "replication/walsender.h"
+#include "storage/ipc.h"
#include "storage/pmsignal.h"
#include "storage/shmem.h"
#include "utils/memutils.h"
@@ -121,6 +122,8 @@ postmaster_death_handler(SIGNAL_ARGS)
#endif /* USE_POSTMASTER_DEATH_SIGNAL */
+static void MarkPostmasterChildInactive(int code, Datum arg);
+
/*
* PMSignalShmemSize
* Compute space needed for pmsignal.c's shared memory
@@ -328,6 +331,9 @@ MarkPostmasterChildActive(void)
slot--;
Assert(PMSignalState->PMChildFlags[slot] == PM_CHILD_ASSIGNED);
PMSignalState->PMChildFlags[slot] = PM_CHILD_ACTIVE;
+
+ /* Arrange to clean up at exit. */
+ on_shmem_exit(MarkPostmasterChildInactive, 0);
}
/*
@@ -352,8 +358,8 @@ MarkPostmasterChildWalSender(void)
* MarkPostmasterChildInactive - mark a postmaster child as done using
* shared memory. This is called in the child process.
*/
-void
-MarkPostmasterChildInactive(void)
+static void
+MarkPostmasterChildInactive(int code, Datum arg)
{
int slot = MyPMChildSlot;
diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c
index 1b23efb26f..37b1c67600 100644
--- a/src/backend/storage/lmgr/proc.c
+++ b/src/backend/storage/lmgr/proc.c
@@ -307,6 +307,19 @@ InitProcess(void)
if (MyProc != NULL)
elog(ERROR, "you already exist");
+ /*
+ * Before we start accessing the shared memory in a serious way, mark
+ * ourselves as an active postmaster child; this is so that the postmaster
+ * can detect it if we exit without cleaning up. (XXX autovac launcher
+ * currently doesn't participate in this; it probably should.)
+ *
+ * Slot sync worker also does not participate in it, see comments atop
+ * 'struct bkend' in postmaster.c.
+ */
+ if (IsUnderPostmaster && !AmAutoVacuumLauncherProcess() &&
+ !AmLogicalSlotSyncWorkerProcess())
+ MarkPostmasterChildActive();
+
/* Decide which list should supply our PGPROC. */
if (AmAutoVacuumLauncherProcess() || AmAutoVacuumWorkerProcess())
procgloballist = &ProcGlobal->autovacFreeProcs;
@@ -359,19 +372,6 @@ InitProcess(void)
*/
Assert(MyProc->procgloballist == procgloballist);
- /*
- * Now that we have a PGPROC, mark ourselves as an active postmaster
- * child; this is so that the postmaster can detect it if we exit without
- * cleaning up. (XXX autovac launcher currently doesn't participate in
- * this; it probably should.)
- *
- * Slot sync worker also does not participate in it, see comments atop
- * 'struct bkend' in postmaster.c.
- */
- if (IsUnderPostmaster && !AmAutoVacuumLauncherProcess() &&
- !AmLogicalSlotSyncWorkerProcess())
- MarkPostmasterChildActive();
-
/*
* Initialize all fields of MyProc, except for those previously
* initialized by InitProcGlobal.
@@ -941,18 +941,6 @@ ProcKill(int code, Datum arg)
SpinLockRelease(ProcStructLock);
- /*
- * This process is no longer present in shared memory in any meaningful
- * way, so tell the postmaster we've cleaned up acceptably well. (XXX
- * autovac launcher should be included here someday)
- *
- * Slot sync worker is also not a postmaster child, so skip this shared
- * memory related processing here.
- */
- if (IsUnderPostmaster && !AmAutoVacuumLauncherProcess() &&
- !AmLogicalSlotSyncWorkerProcess())
- MarkPostmasterChildInactive();
-
/* wake autovac launcher if needed -- see comments in FreeWorkerInfo */
if (AutovacuumLauncherPid != 0)
kill(AutovacuumLauncherPid, SIGUSR2);
diff --git a/src/include/storage/pmsignal.h b/src/include/storage/pmsignal.h
index 0c9a7e32a8..3b9336b83c 100644
--- a/src/include/storage/pmsignal.h
+++ b/src/include/storage/pmsignal.h
@@ -74,7 +74,6 @@ extern int AssignPostmasterChildSlot(void);
extern bool ReleasePostmasterChildSlot(int slot);
extern bool IsPostmasterChildWalSender(int slot);
extern void MarkPostmasterChildActive(void);
-extern void MarkPostmasterChildInactive(void);
extern void MarkPostmasterChildWalSender(void);
extern bool PostmasterIsAliveInternal(void);
extern void PostmasterDeathSignalInit(void);
--
2.39.2