Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4d155d8

Browse files
committed
Detach shared memory from bgworkers without shmem access.
Since the postmaster won't perform a crash-and-restart sequencefor background workers which don't request shared memory access,we'd better make sure that they can't corrupt shared memory.Patch by me, review by Tom Lane.
1 parent04e5025 commit4d155d8

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

‎src/backend/postmaster/bgworker.c

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
#include"postmaster/bgworker_internals.h"
2121
#include"postmaster/postmaster.h"
2222
#include"storage/barrier.h"
23+
#include"storage/dsm.h"
2324
#include"storage/ipc.h"
2425
#include"storage/latch.h"
2526
#include"storage/lwlock.h"
27+
#include"storage/pg_shmem.h"
2628
#include"storage/pmsignal.h"
2729
#include"storage/proc.h"
2830
#include"storage/procsignal.h"
@@ -400,12 +402,16 @@ BackgroundWorkerStopNotifications(pid_t pid)
400402
BackgroundWorker*
401403
BackgroundWorkerEntry(intslotno)
402404
{
405+
staticBackgroundWorkermyEntry;
403406
BackgroundWorkerSlot*slot;
404407

405408
Assert(slotno<BackgroundWorkerData->total_slots);
406409
slot=&BackgroundWorkerData->slot[slotno];
407410
Assert(slot->in_use);
408-
return&slot->worker;/* can't become free while we're still here */
411+
412+
/* must copy this in case we don't intend to retain shmem access */
413+
memcpy(&myEntry,&slot->worker,sizeofmyEntry);
414+
return&myEntry;
409415
}
410416
#endif
411417

@@ -542,6 +548,20 @@ StartBackgroundWorker(void)
542548
snprintf(buf,MAXPGPATH,"bgworker: %s",worker->bgw_name);
543549
init_ps_display(buf,"","","");
544550

551+
/*
552+
* If we're not supposed to have shared memory access, then detach from
553+
* shared memory. If we didn't request shared memory access, the
554+
* postmaster won't force a cluster-wide restart if we exit unexpectedly,
555+
* so we'd better make sure that we don't mess anything up that would
556+
* require that sort of cleanup.
557+
*/
558+
if ((worker->bgw_flags&BGWORKER_SHMEM_ACCESS)==0)
559+
{
560+
on_exit_reset();
561+
dsm_detach_all();
562+
PGSharedMemoryDetach();
563+
}
564+
545565
SetProcessingMode(InitProcessing);
546566

547567
/* Apply PostAuthDelay */
@@ -616,19 +636,29 @@ StartBackgroundWorker(void)
616636
/* We can now handle ereport(ERROR) */
617637
PG_exception_stack=&local_sigjmp_buf;
618638

619-
/* Early initialization */
620-
BaseInit();
621-
622639
/*
623-
* If necessary, create a per-backend PGPROC struct in shared memory,
624-
* except in the EXEC_BACKEND case where this was done in
625-
* SubPostmasterMain. We must do this before we can use LWLocks (and in
626-
* the EXEC_BACKEND case we already had to do some stuff with LWLocks).
640+
* If the background worker request shared memory access, set that up now;
641+
* else, detach all shared memory segments.
627642
*/
628-
#ifndefEXEC_BACKEND
629643
if (worker->bgw_flags&BGWORKER_SHMEM_ACCESS)
644+
{
645+
/*
646+
* Early initialization. Some of this could be useful even for
647+
* background workers that aren't using shared memory, but they can
648+
* call the individual startup routines for those subsystems if needed.
649+
*/
650+
BaseInit();
651+
652+
/*
653+
* Create a per-backend PGPROC struct in shared memory, except in the
654+
* EXEC_BACKEND case where this was done in SubPostmasterMain. We must
655+
* do this before we can use LWLocks (and in the EXEC_BACKEND case we
656+
* already had to do some stuff with LWLocks).
657+
*/
658+
#ifndefEXEC_BACKEND
630659
InitProcess();
631660
#endif
661+
}
632662

633663
/*
634664
* If bgw_main is set, we use that value as the initial entrypoint.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp