|
20 | 20 | #include "postmaster/bgworker_internals.h" |
21 | 21 | #include "postmaster/postmaster.h" |
22 | 22 | #include "storage/barrier.h" |
| 23 | +#include "storage/dsm.h" |
23 | 24 | #include "storage/ipc.h" |
24 | 25 | #include "storage/latch.h" |
25 | 26 | #include "storage/lwlock.h" |
| 27 | +#include "storage/pg_shmem.h" |
26 | 28 | #include "storage/pmsignal.h" |
27 | 29 | #include "storage/proc.h" |
28 | 30 | #include "storage/procsignal.h" |
@@ -400,12 +402,16 @@ BackgroundWorkerStopNotifications(pid_t pid) |
400 | 402 | BackgroundWorker * |
401 | 403 | BackgroundWorkerEntry(int slotno) |
402 | 404 | { |
| 405 | + static BackgroundWorker myEntry; |
403 | 406 | BackgroundWorkerSlot *slot; |
404 | 407 |
|
405 | 408 | Assert(slotno < BackgroundWorkerData->total_slots); |
406 | 409 | slot = &BackgroundWorkerData->slot[slotno]; |
407 | 410 | 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, sizeof myEntry); |
| 414 | + return &myEntry; |
409 | 415 | } |
410 | 416 | #endif |
411 | 417 |
|
@@ -542,6 +548,20 @@ StartBackgroundWorker(void) |
542 | 548 | snprintf(buf, MAXPGPATH, "bgworker: %s", worker->bgw_name); |
543 | 549 | init_ps_display(buf, "", "", ""); |
544 | 550 |
|
| 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 | + |
545 | 565 | SetProcessingMode(InitProcessing); |
546 | 566 |
|
547 | 567 | /* Apply PostAuthDelay */ |
@@ -616,19 +636,29 @@ StartBackgroundWorker(void) |
616 | 636 | /* We can now handle ereport(ERROR) */ |
617 | 637 | PG_exception_stack = &local_sigjmp_buf; |
618 | 638 |
|
619 | | - /* Early initialization */ |
620 | | - BaseInit(); |
621 | | - |
622 | 639 | /* |
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. |
627 | 642 | */ |
628 | | -#ifndef EXEC_BACKEND |
629 | 643 | 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 | +#ifndef EXEC_BACKEND |
630 | 659 | InitProcess(); |
631 | 660 | #endif |
| 661 | + } |
632 | 662 |
|
633 | 663 | /* |
634 | 664 | * If bgw_main is set, we use that value as the initial entrypoint. |
|
0 commit comments