Skip to content

Commit 2bbc261

Browse files
committed
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. Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://www.postgresql.org/message-id/a102f15f-eac4-4ff2-af02-f9ff209ec66f@iki.fi
1 parent 85ec945 commit 2bbc261

File tree

3 files changed

+27
-31
lines changed

3 files changed

+27
-31
lines changed

src/backend/storage/ipc/pmsignal.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "miscadmin.h"
2525
#include "postmaster/postmaster.h"
2626
#include "replication/walsender.h"
27+
#include "storage/ipc.h"
2728
#include "storage/pmsignal.h"
2829
#include "storage/shmem.h"
2930
#include "utils/memutils.h"
@@ -121,6 +122,8 @@ postmaster_death_handler(SIGNAL_ARGS)
121122

122123
#endif /* USE_POSTMASTER_DEATH_SIGNAL */
123124

125+
static void MarkPostmasterChildInactive(int code, Datum arg);
126+
124127
/*
125128
* PMSignalShmemSize
126129
* Compute space needed for pmsignal.c's shared memory
@@ -316,18 +319,24 @@ IsPostmasterChildWalSender(int slot)
316319
}
317320

318321
/*
319-
* MarkPostmasterChildActive - mark a postmaster child as about to begin
322+
* RegisterPostmasterChildActive - mark a postmaster child as about to begin
320323
* actively using shared memory. This is called in the child process.
324+
*
325+
* This register an shmem exit hook to mark us as inactive again when the
326+
* process exits normally.
321327
*/
322328
void
323-
MarkPostmasterChildActive(void)
329+
RegisterPostmasterChildActive(void)
324330
{
325331
int slot = MyPMChildSlot;
326332

327333
Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
328334
slot--;
329335
Assert(PMSignalState->PMChildFlags[slot] == PM_CHILD_ASSIGNED);
330336
PMSignalState->PMChildFlags[slot] = PM_CHILD_ACTIVE;
337+
338+
/* Arrange to clean up at exit. */
339+
on_shmem_exit(MarkPostmasterChildInactive, 0);
331340
}
332341

333342
/*
@@ -352,8 +361,8 @@ MarkPostmasterChildWalSender(void)
352361
* MarkPostmasterChildInactive - mark a postmaster child as done using
353362
* shared memory. This is called in the child process.
354363
*/
355-
void
356-
MarkPostmasterChildInactive(void)
364+
static void
365+
MarkPostmasterChildInactive(int code, Datum arg)
357366
{
358367
int slot = MyPMChildSlot;
359368

src/backend/storage/lmgr/proc.c

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,19 @@ InitProcess(void)
354354
if (MyProc != NULL)
355355
elog(ERROR, "you already exist");
356356

357+
/*
358+
* Before we start accessing the shared memory in a serious way, mark
359+
* ourselves as an active postmaster child; this is so that the postmaster
360+
* can detect it if we exit without cleaning up. (XXX autovac launcher
361+
* currently doesn't participate in this; it probably should.)
362+
*
363+
* Slot sync worker also does not participate in it, see comments atop
364+
* 'struct bkend' in postmaster.c.
365+
*/
366+
if (IsUnderPostmaster && !AmAutoVacuumLauncherProcess() &&
367+
!AmLogicalSlotSyncWorkerProcess())
368+
RegisterPostmasterChildActive();
369+
357370
/* Decide which list should supply our PGPROC. */
358371
if (AmAutoVacuumLauncherProcess() || AmAutoVacuumWorkerProcess())
359372
procgloballist = &ProcGlobal->autovacFreeProcs;
@@ -406,19 +419,6 @@ InitProcess(void)
406419
*/
407420
Assert(MyProc->procgloballist == procgloballist);
408421

409-
/*
410-
* Now that we have a PGPROC, mark ourselves as an active postmaster
411-
* child; this is so that the postmaster can detect it if we exit without
412-
* cleaning up. (XXX autovac launcher currently doesn't participate in
413-
* this; it probably should.)
414-
*
415-
* Slot sync worker also does not participate in it, see comments atop
416-
* 'struct bkend' in postmaster.c.
417-
*/
418-
if (IsUnderPostmaster && !AmAutoVacuumLauncherProcess() &&
419-
!AmLogicalSlotSyncWorkerProcess())
420-
MarkPostmasterChildActive();
421-
422422
/*
423423
* Initialize all fields of MyProc, except for those previously
424424
* initialized by InitProcGlobal.
@@ -993,18 +993,6 @@ ProcKill(int code, Datum arg)
993993

994994
SpinLockRelease(ProcStructLock);
995995

996-
/*
997-
* This process is no longer present in shared memory in any meaningful
998-
* way, so tell the postmaster we've cleaned up acceptably well. (XXX
999-
* autovac launcher should be included here someday)
1000-
*
1001-
* Slot sync worker is also not a postmaster child, so skip this shared
1002-
* memory related processing here.
1003-
*/
1004-
if (IsUnderPostmaster && !AmAutoVacuumLauncherProcess() &&
1005-
!AmLogicalSlotSyncWorkerProcess())
1006-
MarkPostmasterChildInactive();
1007-
1008996
/* wake autovac launcher if needed -- see comments in FreeWorkerInfo */
1009997
if (AutovacuumLauncherPid != 0)
1010998
kill(AutovacuumLauncherPid, SIGUSR2);

src/include/storage/pmsignal.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ extern QuitSignalReason GetQuitSignalReason(void);
7373
extern int AssignPostmasterChildSlot(void);
7474
extern bool ReleasePostmasterChildSlot(int slot);
7575
extern bool IsPostmasterChildWalSender(int slot);
76-
extern void MarkPostmasterChildActive(void);
77-
extern void MarkPostmasterChildInactive(void);
76+
extern void RegisterPostmasterChildActive(void);
7877
extern void MarkPostmasterChildWalSender(void);
7978
extern bool PostmasterIsAliveInternal(void);
8079
extern void PostmasterDeathSignalInit(void);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy