LCOV - code coverage report
Current view: top level - src/backend/postmaster - bgworker.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 85.8 % 386 331
Test Date: 2026-04-06 00:18:02 Functions: 96.0 % 25 24
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*--------------------------------------------------------------------
       2              :  * bgworker.c
       3              :  *      POSTGRES pluggable background workers implementation
       4              :  *
       5              :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       6              :  *
       7              :  * IDENTIFICATION
       8              :  *    src/backend/postmaster/bgworker.c
       9              :  *
      10              :  *-------------------------------------------------------------------------
      11              :  */
      12              : 
      13              : #include "postgres.h"
      14              : 
      15              : #include "access/parallel.h"
      16              : #include "libpq/pqsignal.h"
      17              : #include "miscadmin.h"
      18              : #include "pgstat.h"
      19              : #include "port/atomics.h"
      20              : #include "postmaster/bgworker_internals.h"
      21              : #include "postmaster/datachecksum_state.h"
      22              : #include "postmaster/postmaster.h"
      23              : #include "replication/logicallauncher.h"
      24              : #include "replication/logicalworker.h"
      25              : #include "storage/ipc.h"
      26              : #include "storage/latch.h"
      27              : #include "storage/lwlock.h"
      28              : #include "storage/pmsignal.h"
      29              : #include "storage/proc.h"
      30              : #include "storage/procarray.h"
      31              : #include "storage/procsignal.h"
      32              : #include "storage/shmem.h"
      33              : #include "storage/subsystems.h"
      34              : #include "tcop/tcopprot.h"
      35              : #include "utils/ascii.h"
      36              : #include "utils/memutils.h"
      37              : #include "utils/ps_status.h"
      38              : #include "utils/timeout.h"
      39              : #include "utils/wait_event.h"
      40              : 
      41              : /*
      42              :  * The postmaster's list of registered background workers, in private memory.
      43              :  */
      44              : dlist_head  BackgroundWorkerList = DLIST_STATIC_INIT(BackgroundWorkerList);
      45              : 
      46              : /*
      47              :  * BackgroundWorkerSlots exist in shared memory and can be accessed (via
      48              :  * the BackgroundWorkerArray) by both the postmaster and by regular backends.
      49              :  * However, the postmaster cannot take locks, even spinlocks, because this
      50              :  * might allow it to crash or become wedged if shared memory gets corrupted.
      51              :  * Such an outcome is intolerable.  Therefore, we need a lockless protocol
      52              :  * for coordinating access to this data.
      53              :  *
      54              :  * The 'in_use' flag is used to hand off responsibility for the slot between
      55              :  * the postmaster and the rest of the system.  When 'in_use' is false,
      56              :  * the postmaster will ignore the slot entirely, except for the 'in_use' flag
      57              :  * itself, which it may read.  In this state, regular backends may modify the
      58              :  * slot.  Once a backend sets 'in_use' to true, the slot becomes the
      59              :  * responsibility of the postmaster.  Regular backends may no longer modify it,
      60              :  * but the postmaster may examine it.  Thus, a backend initializing a slot
      61              :  * must fully initialize the slot - and insert a write memory barrier - before
      62              :  * marking it as in use.
      63              :  *
      64              :  * As an exception, however, even when the slot is in use, regular backends
      65              :  * may set the 'terminate' flag for a slot, telling the postmaster not
      66              :  * to restart it.  Once the background worker is no longer running, the slot
      67              :  * will be released for reuse.
      68              :  *
      69              :  * In addition to coordinating with the postmaster, backends modifying this
      70              :  * data structure must coordinate with each other.  Since they can take locks,
      71              :  * this is straightforward: any backend wishing to manipulate a slot must
      72              :  * take BackgroundWorkerLock in exclusive mode.  Backends wishing to read
      73              :  * data that might get concurrently modified by other backends should take
      74              :  * this lock in shared mode.  No matter what, backends reading this data
      75              :  * structure must be able to tolerate concurrent modifications by the
      76              :  * postmaster.
      77              :  */
      78              : typedef struct BackgroundWorkerSlot
      79              : {
      80              :     bool        in_use;
      81              :     bool        terminate;
      82              :     pid_t       pid;            /* InvalidPid = not started yet; 0 = dead */
      83              :     uint64      generation;     /* incremented when slot is recycled */
      84              :     BackgroundWorker worker;
      85              : } BackgroundWorkerSlot;
      86              : 
      87              : /*
      88              :  * In order to limit the total number of parallel workers (according to
      89              :  * max_parallel_workers GUC), we maintain the number of active parallel
      90              :  * workers.  Since the postmaster cannot take locks, two variables are used for
      91              :  * this purpose: the number of registered parallel workers (modified by the
      92              :  * backends, protected by BackgroundWorkerLock) and the number of terminated
      93              :  * parallel workers (modified only by the postmaster, lockless).  The active
      94              :  * number of parallel workers is the number of registered workers minus the
      95              :  * terminated ones.  These counters can of course overflow, but it's not
      96              :  * important here since the subtraction will still give the right number.
      97              :  */
      98              : typedef struct BackgroundWorkerArray
      99              : {
     100              :     int         total_slots;
     101              :     uint32      parallel_register_count;
     102              :     uint32      parallel_terminate_count;
     103              :     BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER];
     104              : } BackgroundWorkerArray;
     105              : 
     106              : struct BackgroundWorkerHandle
     107              : {
     108              :     int         slot;
     109              :     uint64      generation;
     110              : };
     111              : 
     112              : static BackgroundWorkerArray *BackgroundWorkerData;
     113              : 
     114              : static void BackgroundWorkerShmemRequest(void *arg);
     115              : static void BackgroundWorkerShmemInit(void *arg);
     116              : 
     117              : const ShmemCallbacks BackgroundWorkerShmemCallbacks = {
     118              :     .request_fn = BackgroundWorkerShmemRequest,
     119              :     .init_fn = BackgroundWorkerShmemInit,
     120              : };
     121              : 
     122              : /*
     123              :  * List of internal background worker entry points.  We need this for
     124              :  * reasons explained in LookupBackgroundWorkerFunction(), below.
     125              :  */
     126              : static const struct
     127              : {
     128              :     const char *fn_name;
     129              :     bgworker_main_type fn_addr;
     130              : }           InternalBGWorkers[] =
     131              : 
     132              : {
     133              :     {
     134              :         .fn_name = "ApplyLauncherMain",
     135              :         .fn_addr = ApplyLauncherMain
     136              :     },
     137              :     {
     138              :         .fn_name = "ApplyWorkerMain",
     139              :         .fn_addr = ApplyWorkerMain
     140              :     },
     141              :     {
     142              :         .fn_name = "ParallelApplyWorkerMain",
     143              :         .fn_addr = ParallelApplyWorkerMain
     144              :     },
     145              :     {
     146              :         .fn_name = "ParallelWorkerMain",
     147              :         .fn_addr = ParallelWorkerMain
     148              :     },
     149              :     {
     150              :         .fn_name = "SequenceSyncWorkerMain",
     151              :         .fn_addr = SequenceSyncWorkerMain
     152              :     },
     153              :     {
     154              :         .fn_name = "TableSyncWorkerMain",
     155              :         .fn_addr = TableSyncWorkerMain
     156              :     },
     157              :     {
     158              :         .fn_name = "DataChecksumsWorkerLauncherMain",
     159              :         .fn_addr = DataChecksumsWorkerLauncherMain
     160              :     },
     161              :     {
     162              :         .fn_name = "DataChecksumsWorkerMain",
     163              :         .fn_addr = DataChecksumsWorkerMain
     164              :     }
     165              : };
     166              : 
     167              : /* Private functions. */
     168              : static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname);
     169              : 
     170              : 
     171              : /*
     172              :  * Register shared memory needed for background workers.
     173              :  */
     174              : static void
     175         1227 : BackgroundWorkerShmemRequest(void *arg)
     176              : {
     177              :     Size        size;
     178              : 
     179              :     /* Array of workers is variably sized. */
     180         1227 :     size = offsetof(BackgroundWorkerArray, slot);
     181         1227 :     size = add_size(size, mul_size(max_worker_processes,
     182              :                                    sizeof(BackgroundWorkerSlot)));
     183         1227 :     ShmemRequestStruct(.name = "Background Worker Data",
     184              :                        .size = size,
     185              :                        .ptr = (void **) &BackgroundWorkerData,
     186              :         );
     187         1227 : }
     188              : 
     189              : /*
     190              :  * Initialize shared memory for background workers.
     191              :  */
     192              : static void
     193         1224 : BackgroundWorkerShmemInit(void *arg)
     194              : {
     195              :     dlist_iter  iter;
     196         1224 :     int         slotno = 0;
     197              : 
     198         1224 :     BackgroundWorkerData->total_slots = max_worker_processes;
     199         1224 :     BackgroundWorkerData->parallel_register_count = 0;
     200         1224 :     BackgroundWorkerData->parallel_terminate_count = 0;
     201              : 
     202              :     /*
     203              :      * Copy contents of worker list into shared memory.  Record the shared
     204              :      * memory slot assigned to each worker.  This ensures a 1-to-1
     205              :      * correspondence between the postmaster's private list and the array in
     206              :      * shared memory.
     207              :      */
     208         2145 :     dlist_foreach(iter, &BackgroundWorkerList)
     209              :     {
     210          921 :         BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
     211              :         RegisteredBgWorker *rw;
     212              : 
     213          921 :         rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
     214              :         Assert(slotno < max_worker_processes);
     215          921 :         slot->in_use = true;
     216          921 :         slot->terminate = false;
     217          921 :         slot->pid = InvalidPid;
     218          921 :         slot->generation = 0;
     219          921 :         rw->rw_shmem_slot = slotno;
     220          921 :         rw->rw_worker.bgw_notify_pid = 0;    /* might be reinit after crash */
     221          921 :         memcpy(&slot->worker, &rw->rw_worker, sizeof(BackgroundWorker));
     222          921 :         ++slotno;
     223              :     }
     224              : 
     225              :     /*
     226              :      * Mark any remaining slots as not in use.
     227              :      */
     228        10086 :     while (slotno < max_worker_processes)
     229              :     {
     230         8862 :         BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
     231              : 
     232         8862 :         slot->in_use = false;
     233         8862 :         ++slotno;
     234              :     }
     235         1224 : }
     236              : 
     237              : /*
     238              :  * Search the postmaster's backend-private list of RegisteredBgWorker objects
     239              :  * for the one that maps to the given slot number.
     240              :  */
     241              : static RegisteredBgWorker *
     242         5514 : FindRegisteredWorkerBySlotNumber(int slotno)
     243              : {
     244              :     dlist_iter  iter;
     245              : 
     246        13551 :     dlist_foreach(iter, &BackgroundWorkerList)
     247              :     {
     248              :         RegisteredBgWorker *rw;
     249              : 
     250        10869 :         rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
     251        10869 :         if (rw->rw_shmem_slot == slotno)
     252         2832 :             return rw;
     253              :     }
     254              : 
     255         2682 :     return NULL;
     256              : }
     257              : 
     258              : /*
     259              :  * Notice changes to shared memory made by other backends.
     260              :  * Accept new worker requests only if allow_new_workers is true.
     261              :  *
     262              :  * This code runs in the postmaster, so we must be very careful not to assume
     263              :  * that shared memory contents are sane.  Otherwise, a rogue backend could
     264              :  * take out the postmaster.
     265              :  */
     266              : void
     267         1656 : BackgroundWorkerStateChange(bool allow_new_workers)
     268              : {
     269              :     int         slotno;
     270              : 
     271              :     /*
     272              :      * The total number of slots stored in shared memory should match our
     273              :      * notion of max_worker_processes.  If it does not, something is very
     274              :      * wrong.  Further down, we always refer to this value as
     275              :      * max_worker_processes, in case shared memory gets corrupted while we're
     276              :      * looping.
     277              :      */
     278         1656 :     if (max_worker_processes != BackgroundWorkerData->total_slots)
     279              :     {
     280            0 :         ereport(LOG,
     281              :                 (errmsg("inconsistent background worker state (\"max_worker_processes\"=%d, total slots=%d)",
     282              :                         max_worker_processes,
     283              :                         BackgroundWorkerData->total_slots)));
     284            0 :         return;
     285              :     }
     286              : 
     287              :     /*
     288              :      * Iterate through slots, looking for newly-registered workers or workers
     289              :      * who must die.
     290              :      */
     291        15048 :     for (slotno = 0; slotno < max_worker_processes; ++slotno)
     292              :     {
     293        13392 :         BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
     294              :         RegisteredBgWorker *rw;
     295              : 
     296        13392 :         if (!slot->in_use)
     297         7878 :             continue;
     298              : 
     299              :         /*
     300              :          * Make sure we don't see the in_use flag before the updated slot
     301              :          * contents.
     302              :          */
     303         5514 :         pg_read_barrier();
     304              : 
     305              :         /* See whether we already know about this worker. */
     306         5514 :         rw = FindRegisteredWorkerBySlotNumber(slotno);
     307         5514 :         if (rw != NULL)
     308              :         {
     309              :             /*
     310              :              * In general, the worker data can't change after it's initially
     311              :              * registered.  However, someone can set the terminate flag.
     312              :              */
     313         2832 :             if (slot->terminate && !rw->rw_terminate)
     314              :             {
     315           12 :                 rw->rw_terminate = true;
     316           12 :                 if (rw->rw_pid != 0)
     317           12 :                     kill(rw->rw_pid, SIGTERM);
     318              :                 else
     319              :                 {
     320              :                     /* Report never-started, now-terminated worker as dead. */
     321            0 :                     ReportBackgroundWorkerPID(rw);
     322              :                 }
     323              :             }
     324         2832 :             continue;
     325              :         }
     326              : 
     327              :         /*
     328              :          * If we aren't allowing new workers, then immediately mark it for
     329              :          * termination; the next stanza will take care of cleaning it up.
     330              :          * Doing this ensures that any process waiting for the worker will get
     331              :          * awoken, even though the worker will never be allowed to run.
     332              :          */
     333         2682 :         if (!allow_new_workers)
     334            4 :             slot->terminate = true;
     335              : 
     336              :         /*
     337              :          * If the worker is marked for termination, we don't need to add it to
     338              :          * the registered workers list; we can just free the slot. However, if
     339              :          * bgw_notify_pid is set, the process that registered the worker may
     340              :          * need to know that we've processed the terminate request, so be sure
     341              :          * to signal it.
     342              :          */
     343         2682 :         if (slot->terminate)
     344            4 :         {
     345              :             int         notify_pid;
     346              : 
     347              :             /*
     348              :              * We need a memory barrier here to make sure that the load of
     349              :              * bgw_notify_pid and the update of parallel_terminate_count
     350              :              * complete before the store to in_use.
     351              :              */
     352            4 :             notify_pid = slot->worker.bgw_notify_pid;
     353            4 :             if ((slot->worker.bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
     354            0 :                 BackgroundWorkerData->parallel_terminate_count++;
     355            4 :             slot->pid = 0;
     356              : 
     357            4 :             pg_memory_barrier();
     358            4 :             slot->in_use = false;
     359              : 
     360            4 :             if (notify_pid != 0)
     361            4 :                 kill(notify_pid, SIGUSR1);
     362              : 
     363            4 :             continue;
     364              :         }
     365              : 
     366              :         /*
     367              :          * Copy the registration data into the registered workers list.
     368              :          */
     369         2678 :         rw = MemoryContextAllocExtended(PostmasterContext,
     370              :                                         sizeof(RegisteredBgWorker),
     371              :                                         MCXT_ALLOC_NO_OOM | MCXT_ALLOC_ZERO);
     372         2678 :         if (rw == NULL)
     373              :         {
     374            0 :             ereport(LOG,
     375              :                     (errcode(ERRCODE_OUT_OF_MEMORY),
     376              :                      errmsg("out of memory")));
     377            0 :             return;
     378              :         }
     379              : 
     380              :         /*
     381              :          * Copy strings in a paranoid way.  If shared memory is corrupted, the
     382              :          * source data might not even be NUL-terminated.
     383              :          */
     384         2678 :         ascii_safe_strlcpy(rw->rw_worker.bgw_name,
     385         2678 :                            slot->worker.bgw_name, BGW_MAXLEN);
     386         2678 :         ascii_safe_strlcpy(rw->rw_worker.bgw_type,
     387         2678 :                            slot->worker.bgw_type, BGW_MAXLEN);
     388         2678 :         ascii_safe_strlcpy(rw->rw_worker.bgw_library_name,
     389         2678 :                            slot->worker.bgw_library_name, MAXPGPATH);
     390         2678 :         ascii_safe_strlcpy(rw->rw_worker.bgw_function_name,
     391         2678 :                            slot->worker.bgw_function_name, BGW_MAXLEN);
     392              : 
     393              :         /*
     394              :          * Copy various fixed-size fields.
     395              :          *
     396              :          * flags, start_time, and restart_time are examined by the postmaster,
     397              :          * but nothing too bad will happen if they are corrupted.  The
     398              :          * remaining fields will only be examined by the child process.  It
     399              :          * might crash, but we won't.
     400              :          */
     401         2678 :         rw->rw_worker.bgw_flags = slot->worker.bgw_flags;
     402         2678 :         rw->rw_worker.bgw_start_time = slot->worker.bgw_start_time;
     403         2678 :         rw->rw_worker.bgw_restart_time = slot->worker.bgw_restart_time;
     404         2678 :         rw->rw_worker.bgw_main_arg = slot->worker.bgw_main_arg;
     405         2678 :         memcpy(rw->rw_worker.bgw_extra, slot->worker.bgw_extra, BGW_EXTRALEN);
     406              : 
     407              :         /*
     408              :          * Copy the PID to be notified about state changes, but only if the
     409              :          * postmaster knows about a backend with that PID.  It isn't an error
     410              :          * if the postmaster doesn't know about the PID, because the backend
     411              :          * that requested the worker could have died (or been killed) just
     412              :          * after doing so.  Nonetheless, at least until we get some experience
     413              :          * with how this plays out in the wild, log a message at a relative
     414              :          * high debug level.
     415              :          */
     416         2678 :         rw->rw_worker.bgw_notify_pid = slot->worker.bgw_notify_pid;
     417         2678 :         if (!PostmasterMarkPIDForWorkerNotify(rw->rw_worker.bgw_notify_pid))
     418              :         {
     419            0 :             elog(DEBUG1, "worker notification PID %d is not valid",
     420              :                  (int) rw->rw_worker.bgw_notify_pid);
     421            0 :             rw->rw_worker.bgw_notify_pid = 0;
     422              :         }
     423              : 
     424              :         /* Initialize postmaster bookkeeping. */
     425         2678 :         rw->rw_pid = 0;
     426         2678 :         rw->rw_crashed_at = 0;
     427         2678 :         rw->rw_shmem_slot = slotno;
     428         2678 :         rw->rw_terminate = false;
     429              : 
     430              :         /* Log it! */
     431         2678 :         ereport(DEBUG1,
     432              :                 (errmsg_internal("registering background worker \"%s\"",
     433              :                                  rw->rw_worker.bgw_name)));
     434              : 
     435         2678 :         dlist_push_head(&BackgroundWorkerList, &rw->rw_lnode);
     436              :     }
     437              : }
     438              : 
     439              : /*
     440              :  * Forget about a background worker that's no longer needed.
     441              :  *
     442              :  * NOTE: The entry is unlinked from BackgroundWorkerList.  If the caller is
     443              :  * iterating through it, better use a mutable iterator!
     444              :  *
     445              :  * Caller is responsible for notifying bgw_notify_pid, if appropriate.
     446              :  *
     447              :  * This function must be invoked only in the postmaster.
     448              :  */
     449              : void
     450         2663 : ForgetBackgroundWorker(RegisteredBgWorker *rw)
     451              : {
     452              :     BackgroundWorkerSlot *slot;
     453              : 
     454              :     Assert(rw->rw_shmem_slot < max_worker_processes);
     455         2663 :     slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
     456              :     Assert(slot->in_use);
     457              : 
     458              :     /*
     459              :      * We need a memory barrier here to make sure that the update of
     460              :      * parallel_terminate_count completes before the store to in_use.
     461              :      */
     462         2663 :     if ((rw->rw_worker.bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
     463         2013 :         BackgroundWorkerData->parallel_terminate_count++;
     464              : 
     465         2663 :     pg_memory_barrier();
     466         2663 :     slot->in_use = false;
     467              : 
     468         2663 :     ereport(DEBUG1,
     469              :             (errmsg_internal("unregistering background worker \"%s\"",
     470              :                              rw->rw_worker.bgw_name)));
     471              : 
     472         2663 :     dlist_delete(&rw->rw_lnode);
     473         2663 :     pfree(rw);
     474         2663 : }
     475              : 
     476              : /*
     477              :  * Report the PID of a newly-launched background worker in shared memory.
     478              :  *
     479              :  * This function should only be called from the postmaster.
     480              :  */
     481              : void
     482         3490 : ReportBackgroundWorkerPID(RegisteredBgWorker *rw)
     483              : {
     484              :     BackgroundWorkerSlot *slot;
     485              : 
     486              :     Assert(rw->rw_shmem_slot < max_worker_processes);
     487         3490 :     slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
     488         3490 :     slot->pid = rw->rw_pid;
     489              : 
     490         3490 :     if (rw->rw_worker.bgw_notify_pid != 0)
     491         2678 :         kill(rw->rw_worker.bgw_notify_pid, SIGUSR1);
     492         3490 : }
     493              : 
     494              : /*
     495              :  * Report that the PID of a background worker is now zero because a
     496              :  * previously-running background worker has exited.
     497              :  *
     498              :  * NOTE: The entry may be unlinked from BackgroundWorkerList.  If the caller
     499              :  * is iterating through it, better use a mutable iterator!
     500              :  *
     501              :  * This function should only be called from the postmaster.
     502              :  */
     503              : void
     504         3165 : ReportBackgroundWorkerExit(RegisteredBgWorker *rw)
     505              : {
     506              :     BackgroundWorkerSlot *slot;
     507              :     int         notify_pid;
     508              : 
     509              :     Assert(rw->rw_shmem_slot < max_worker_processes);
     510         3165 :     slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
     511         3165 :     slot->pid = rw->rw_pid;
     512         3165 :     notify_pid = rw->rw_worker.bgw_notify_pid;
     513              : 
     514              :     /*
     515              :      * If this worker is slated for deregistration, do that before notifying
     516              :      * the process which started it.  Otherwise, if that process tries to
     517              :      * reuse the slot immediately, it might not be available yet.  In theory
     518              :      * that could happen anyway if the process checks slot->pid at just the
     519              :      * wrong moment, but this makes the window narrower.
     520              :      */
     521         3165 :     if (rw->rw_terminate ||
     522          778 :         rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART)
     523         2663 :         ForgetBackgroundWorker(rw);
     524              : 
     525         3165 :     if (notify_pid != 0)
     526         2621 :         kill(notify_pid, SIGUSR1);
     527         3165 : }
     528              : 
     529              : /*
     530              :  * Cancel SIGUSR1 notifications for a PID belonging to an exiting backend.
     531              :  *
     532              :  * This function should only be called from the postmaster.
     533              :  */
     534              : void
     535          337 : BackgroundWorkerStopNotifications(pid_t pid)
     536              : {
     537              :     dlist_iter  iter;
     538              : 
     539         1086 :     dlist_foreach(iter, &BackgroundWorkerList)
     540              :     {
     541              :         RegisteredBgWorker *rw;
     542              : 
     543          749 :         rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
     544          749 :         if (rw->rw_worker.bgw_notify_pid == pid)
     545           44 :             rw->rw_worker.bgw_notify_pid = 0;
     546              :     }
     547          337 : }
     548              : 
     549              : /*
     550              :  * Cancel any not-yet-started worker requests that have waiting processes.
     551              :  *
     552              :  * This is called during a normal ("smart" or "fast") database shutdown.
     553              :  * After this point, no new background workers will be started, so anything
     554              :  * that might be waiting for them needs to be kicked off its wait.  We do
     555              :  * that by canceling the bgworker registration entirely, which is perhaps
     556              :  * overkill, but since we're shutting down it does not matter whether the
     557              :  * registration record sticks around.
     558              :  *
     559              :  * This function should only be called from the postmaster.
     560              :  */
     561              : void
     562          619 : ForgetUnstartedBackgroundWorkers(void)
     563              : {
     564              :     dlist_mutable_iter iter;
     565              : 
     566         1222 :     dlist_foreach_modify(iter, &BackgroundWorkerList)
     567              :     {
     568              :         RegisteredBgWorker *rw;
     569              :         BackgroundWorkerSlot *slot;
     570              : 
     571          603 :         rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
     572              :         Assert(rw->rw_shmem_slot < max_worker_processes);
     573          603 :         slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
     574              : 
     575              :         /* If it's not yet started, and there's someone waiting ... */
     576          603 :         if (slot->pid == InvalidPid &&
     577           56 :             rw->rw_worker.bgw_notify_pid != 0)
     578              :         {
     579              :             /* ... then zap it, and notify the waiter */
     580            0 :             int         notify_pid = rw->rw_worker.bgw_notify_pid;
     581              : 
     582            0 :             ForgetBackgroundWorker(rw);
     583            0 :             if (notify_pid != 0)
     584            0 :                 kill(notify_pid, SIGUSR1);
     585              :         }
     586              :     }
     587          619 : }
     588              : 
     589              : /*
     590              :  * Reset background worker crash state.
     591              :  *
     592              :  * We assume that, after a crash-and-restart cycle, background workers without
     593              :  * the never-restart flag should be restarted immediately, instead of waiting
     594              :  * for bgw_restart_time to elapse.  On the other hand, workers with that flag
     595              :  * should be forgotten immediately, since we won't ever restart them.
     596              :  *
     597              :  * This function should only be called from the postmaster.
     598              :  */
     599              : void
     600            5 : ResetBackgroundWorkerCrashTimes(void)
     601              : {
     602              :     dlist_mutable_iter iter;
     603              : 
     604           10 :     dlist_foreach_modify(iter, &BackgroundWorkerList)
     605              :     {
     606              :         RegisteredBgWorker *rw;
     607              : 
     608            5 :         rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
     609              : 
     610            5 :         if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART)
     611              :         {
     612              :             /*
     613              :              * Workers marked BGW_NEVER_RESTART shouldn't get relaunched after
     614              :              * the crash, so forget about them.  (If we wait until after the
     615              :              * crash to forget about them, and they are parallel workers,
     616              :              * parallel_terminate_count will get incremented after we've
     617              :              * already zeroed parallel_register_count, which would be bad.)
     618              :              */
     619            0 :             ForgetBackgroundWorker(rw);
     620              :         }
     621              :         else
     622              :         {
     623              :             /*
     624              :              * The accounting which we do via parallel_register_count and
     625              :              * parallel_terminate_count would get messed up if a worker marked
     626              :              * parallel could survive a crash and restart cycle. All such
     627              :              * workers should be marked BGW_NEVER_RESTART, and thus control
     628              :              * should never reach this branch.
     629              :              */
     630              :             Assert((rw->rw_worker.bgw_flags & BGWORKER_CLASS_PARALLEL) == 0);
     631              : 
     632              :             /*
     633              :              * Allow this worker to be restarted immediately after we finish
     634              :              * resetting.
     635              :              */
     636            5 :             rw->rw_crashed_at = 0;
     637            5 :             rw->rw_pid = 0;
     638              : 
     639              :             /*
     640              :              * If there was anyone waiting for it, they're history.
     641              :              */
     642            5 :             rw->rw_worker.bgw_notify_pid = 0;
     643              :         }
     644              :     }
     645            5 : }
     646              : 
     647              : /*
     648              :  * Complain about the BackgroundWorker definition using error level elevel.
     649              :  * Return true if it looks ok, false if not (unless elevel >= ERROR, in
     650              :  * which case we won't return at all in the not-OK case).
     651              :  */
     652              : static bool
     653         3484 : SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
     654              : {
     655              :     /* sanity check for flags */
     656              : 
     657              :     /*
     658              :      * We used to support workers not connected to shared memory, but don't
     659              :      * anymore. Thus this is a required flag now. We're not removing the flag
     660              :      * for compatibility reasons and because the flag still provides some
     661              :      * signal when reading code.
     662              :      */
     663         3484 :     if (!(worker->bgw_flags & BGWORKER_SHMEM_ACCESS))
     664              :     {
     665            0 :         ereport(elevel,
     666              :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     667              :                  errmsg("background worker \"%s\": background workers without shared memory access are not supported",
     668              :                         worker->bgw_name)));
     669            0 :         return false;
     670              :     }
     671              : 
     672         3484 :     if (worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION)
     673              :     {
     674         3475 :         if (worker->bgw_start_time == BgWorkerStart_PostmasterStart)
     675              :         {
     676            0 :             ereport(elevel,
     677              :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     678              :                      errmsg("background worker \"%s\": cannot request database access if starting at postmaster start",
     679              :                             worker->bgw_name)));
     680            0 :             return false;
     681              :         }
     682              : 
     683              :         /* XXX other checks? */
     684              :     }
     685              : 
     686              :     /* Interruptible workers require a database connection */
     687         3484 :     if ((worker->bgw_flags & BGWORKER_INTERRUPTIBLE) &&
     688            4 :         !(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
     689              :     {
     690            0 :         ereport(elevel,
     691              :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     692              :                  errmsg("background worker \"%s\": cannot make background workers interruptible without database access",
     693              :                         worker->bgw_name)));
     694            0 :         return false;
     695              :     }
     696              : 
     697         3484 :     if ((worker->bgw_restart_time < 0 &&
     698         2556 :          worker->bgw_restart_time != BGW_NEVER_RESTART) ||
     699         3484 :         (worker->bgw_restart_time > USECS_PER_DAY / 1000))
     700              :     {
     701            0 :         ereport(elevel,
     702              :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     703              :                  errmsg("background worker \"%s\": invalid restart interval",
     704              :                         worker->bgw_name)));
     705            0 :         return false;
     706              :     }
     707              : 
     708              :     /*
     709              :      * Parallel workers may not be configured for restart, because the
     710              :      * parallel_register_count/parallel_terminate_count accounting can't
     711              :      * handle parallel workers lasting through a crash-and-restart cycle.
     712              :      */
     713         3484 :     if (worker->bgw_restart_time != BGW_NEVER_RESTART &&
     714          928 :         (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
     715              :     {
     716            0 :         ereport(elevel,
     717              :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     718              :                  errmsg("background worker \"%s\": parallel workers may not be configured for restart",
     719              :                         worker->bgw_name)));
     720            0 :         return false;
     721              :     }
     722              : 
     723              :     /*
     724              :      * If bgw_type is not filled in, use bgw_name.
     725              :      */
     726         3484 :     if (strcmp(worker->bgw_type, "") == 0)
     727            0 :         strcpy(worker->bgw_type, worker->bgw_name);
     728              : 
     729         3484 :     return true;
     730              : }
     731              : 
     732              : /*
     733              :  * Main entry point for background worker processes.
     734              :  */
     735              : void
     736         3170 : BackgroundWorkerMain(const void *startup_data, size_t startup_data_len)
     737              : {
     738              :     sigjmp_buf  local_sigjmp_buf;
     739              :     BackgroundWorker *worker;
     740              :     bgworker_main_type entrypt;
     741              : 
     742         3170 :     if (startup_data == NULL)
     743            0 :         elog(FATAL, "unable to find bgworker entry");
     744              :     Assert(startup_data_len == sizeof(BackgroundWorker));
     745         3170 :     worker = MemoryContextAlloc(TopMemoryContext, sizeof(BackgroundWorker));
     746         3170 :     memcpy(worker, startup_data, sizeof(BackgroundWorker));
     747              : 
     748              :     /*
     749              :      * Now that we're done reading the startup data, release postmaster's
     750              :      * working memory context.
     751              :      */
     752         3170 :     if (PostmasterContext)
     753              :     {
     754         3170 :         MemoryContextDelete(PostmasterContext);
     755         3170 :         PostmasterContext = NULL;
     756              :     }
     757              : 
     758         3170 :     MyBgworkerEntry = worker;
     759         3170 :     init_ps_display(worker->bgw_name);
     760              : 
     761              :     Assert(GetProcessingMode() == InitProcessing);
     762              : 
     763              :     /* Apply PostAuthDelay */
     764         3170 :     if (PostAuthDelay > 0)
     765            0 :         pg_usleep(PostAuthDelay * 1000000L);
     766              : 
     767              :     /*
     768              :      * Set up signal handlers.
     769              :      */
     770         3170 :     if (worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION)
     771              :     {
     772              :         /*
     773              :          * SIGINT is used to signal canceling the current action
     774              :          */
     775         3161 :         pqsignal(SIGINT, StatementCancelHandler);
     776         3161 :         pqsignal(SIGUSR1, procsignal_sigusr1_handler);
     777         3161 :         pqsignal(SIGFPE, FloatExceptionHandler);
     778              : 
     779              :         /* XXX Any other handlers needed here? */
     780              :     }
     781              :     else
     782              :     {
     783            9 :         pqsignal(SIGINT, SIG_IGN);
     784            9 :         pqsignal(SIGUSR1, SIG_IGN);
     785            9 :         pqsignal(SIGFPE, SIG_IGN);
     786              :     }
     787         3170 :     pqsignal(SIGTERM, die);
     788              :     /* SIGQUIT handler was already set up by InitPostmasterChild */
     789         3170 :     pqsignal(SIGHUP, SIG_IGN);
     790              : 
     791         3170 :     InitializeTimeouts();       /* establishes SIGALRM handler */
     792              : 
     793         3170 :     pqsignal(SIGPIPE, SIG_IGN);
     794         3170 :     pqsignal(SIGUSR2, SIG_IGN);
     795         3170 :     pqsignal(SIGCHLD, SIG_DFL);
     796              : 
     797              :     /*
     798              :      * If an exception is encountered, processing resumes here.
     799              :      *
     800              :      * We just need to clean up, report the error, and go away.
     801              :      */
     802         3170 :     if (sigsetjmp(local_sigjmp_buf, 1) != 0)
     803              :     {
     804              :         /* Since not using PG_TRY, must reset error stack by hand */
     805          160 :         error_context_stack = NULL;
     806              : 
     807              :         /* Prevent interrupts while cleaning up */
     808          160 :         HOLD_INTERRUPTS();
     809              : 
     810              :         /*
     811              :          * sigsetjmp will have blocked all signals, but we may need to accept
     812              :          * signals while communicating with our parallel leader.  Once we've
     813              :          * done HOLD_INTERRUPTS() it should be safe to unblock signals.
     814              :          */
     815          160 :         BackgroundWorkerUnblockSignals();
     816              : 
     817              :         /* Report the error to the parallel leader and the server log */
     818          160 :         EmitErrorReport();
     819              : 
     820              :         /*
     821              :          * Do we need more cleanup here?  For shmem-connected bgworkers, we
     822              :          * will call InitProcess below, which will install ProcKill as exit
     823              :          * callback.  That will take care of releasing locks, etc.
     824              :          */
     825              : 
     826              :         /* and go away */
     827          160 :         proc_exit(1);
     828              :     }
     829              : 
     830              :     /* We can now handle ereport(ERROR) */
     831         3170 :     PG_exception_stack = &local_sigjmp_buf;
     832              : 
     833              :     /*
     834              :      * Create a per-backend PGPROC struct in shared memory.  We must do this
     835              :      * before we can use LWLocks or access any shared memory.
     836              :      */
     837         3170 :     InitProcess();
     838              : 
     839              :     /*
     840              :      * Early initialization.
     841              :      */
     842         3170 :     BaseInit();
     843              : 
     844              :     /*
     845              :      * Look up the entry point function, loading its library if necessary.
     846              :      */
     847         6340 :     entrypt = LookupBackgroundWorkerFunction(worker->bgw_library_name,
     848         3170 :                                              worker->bgw_function_name);
     849              : 
     850              :     /*
     851              :      * Note that in normal processes, we would call InitPostgres here.  For a
     852              :      * worker, however, we don't know what database to connect to, yet; so we
     853              :      * need to wait until the user code does it via
     854              :      * BackgroundWorkerInitializeConnection().
     855              :      */
     856              : 
     857              :     /*
     858              :      * Now invoke the user-defined worker code
     859              :      */
     860         3170 :     entrypt(worker->bgw_main_arg);
     861              : 
     862              :     /* ... and if it returns, we're done */
     863         2040 :     proc_exit(0);
     864              : }
     865              : 
     866              : /*
     867              :  * Connect background worker to a database.
     868              :  */
     869              : void
     870          505 : BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
     871              : {
     872          505 :     BackgroundWorker *worker = MyBgworkerEntry;
     873          505 :     uint32      init_flags = 0; /* never honor session_preload_libraries */
     874              : 
     875              :     /* ignore datallowconn and ACL_CONNECT? */
     876          505 :     if (flags & BGWORKER_BYPASS_ALLOWCONN)
     877            0 :         init_flags |= INIT_PG_OVERRIDE_ALLOW_CONNS;
     878              :     /* ignore rolcanlogin? */
     879          505 :     if (flags & BGWORKER_BYPASS_ROLELOGINCHECK)
     880            0 :         init_flags |= INIT_PG_OVERRIDE_ROLE_LOGIN;
     881              : 
     882              :     /* XXX is this the right errcode? */
     883          505 :     if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
     884            0 :         ereport(FATAL,
     885              :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     886              :                  errmsg("database connection requirement not indicated during registration")));
     887              : 
     888          505 :     InitPostgres(dbname, InvalidOid,    /* database to connect to */
     889              :                  username, InvalidOid,  /* role to connect as */
     890              :                  init_flags,
     891              :                  NULL);         /* no out_dbname */
     892              : 
     893              :     /* it had better not gotten out of "init" mode yet */
     894          505 :     if (!IsInitProcessingMode())
     895            0 :         ereport(ERROR,
     896              :                 (errmsg("invalid processing mode in background worker")));
     897          505 :     SetProcessingMode(NormalProcessing);
     898          505 : }
     899              : 
     900              : /*
     901              :  * Connect background worker to a database using OIDs.
     902              :  */
     903              : void
     904         2656 : BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
     905              : {
     906         2656 :     BackgroundWorker *worker = MyBgworkerEntry;
     907         2656 :     uint32      init_flags = 0; /* never honor session_preload_libraries */
     908              : 
     909              :     /* ignore datallowconn and ACL_CONNECT? */
     910         2656 :     if (flags & BGWORKER_BYPASS_ALLOWCONN)
     911         2033 :         init_flags |= INIT_PG_OVERRIDE_ALLOW_CONNS;
     912              :     /* ignore rolcanlogin? */
     913         2656 :     if (flags & BGWORKER_BYPASS_ROLELOGINCHECK)
     914         2013 :         init_flags |= INIT_PG_OVERRIDE_ROLE_LOGIN;
     915              : 
     916              :     /* XXX is this the right errcode? */
     917         2656 :     if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
     918            0 :         ereport(FATAL,
     919              :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     920              :                  errmsg("database connection requirement not indicated during registration")));
     921              : 
     922         2656 :     InitPostgres(NULL, dboid,   /* database to connect to */
     923              :                  NULL, useroid, /* role to connect as */
     924              :                  init_flags,
     925              :                  NULL);         /* no out_dbname */
     926              : 
     927              :     /* it had better not gotten out of "init" mode yet */
     928         2653 :     if (!IsInitProcessingMode())
     929            0 :         ereport(ERROR,
     930              :                 (errmsg("invalid processing mode in background worker")));
     931         2653 :     SetProcessingMode(NormalProcessing);
     932         2653 : }
     933              : 
     934              : /*
     935              :  * Block/unblock signals in a background worker
     936              :  */
     937              : void
     938            0 : BackgroundWorkerBlockSignals(void)
     939              : {
     940            0 :     sigprocmask(SIG_SETMASK, &BlockSig, NULL);
     941            0 : }
     942              : 
     943              : void
     944         3330 : BackgroundWorkerUnblockSignals(void)
     945              : {
     946         3330 :     sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
     947         3330 : }
     948              : 
     949              : /*
     950              :  * Register a new static background worker.
     951              :  *
     952              :  * This can only be called directly from postmaster or in the _PG_init
     953              :  * function of a module library that's loaded by shared_preload_libraries;
     954              :  * otherwise it will have no effect.
     955              :  */
     956              : void
     957          931 : RegisterBackgroundWorker(BackgroundWorker *worker)
     958              : {
     959              :     RegisteredBgWorker *rw;
     960              :     static int  numworkers = 0;
     961              : 
     962              :     /*
     963              :      * Static background workers can only be registered in the postmaster
     964              :      * process.
     965              :      */
     966          931 :     if (IsUnderPostmaster || !IsPostmasterEnvironment)
     967              :     {
     968              :         /*
     969              :          * In EXEC_BACKEND or single-user mode, we process
     970              :          * shared_preload_libraries in backend processes too.  We cannot
     971              :          * register static background workers at that stage, but many
     972              :          * libraries' _PG_init() functions don't distinguish whether they're
     973              :          * being loaded in the postmaster or in a backend, they just check
     974              :          * process_shared_preload_libraries_in_progress.  It's a bit sloppy,
     975              :          * but for historical reasons we tolerate it.  In EXEC_BACKEND mode,
     976              :          * the background workers should already have been registered when the
     977              :          * library was loaded in postmaster.
     978              :          */
     979            0 :         if (process_shared_preload_libraries_in_progress)
     980            0 :             return;
     981            0 :         ereport(LOG,
     982              :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     983              :                  errmsg("background worker \"%s\": must be registered in \"shared_preload_libraries\"",
     984              :                         worker->bgw_name)));
     985            0 :         return;
     986              :     }
     987              : 
     988              :     /*
     989              :      * Cannot register static background workers after calling
     990              :      * BackgroundWorkerShmemInit().
     991              :      */
     992          931 :     if (BackgroundWorkerData != NULL)
     993            0 :         elog(ERROR, "cannot register background worker \"%s\" after shmem init",
     994              :              worker->bgw_name);
     995              : 
     996          931 :     ereport(DEBUG1,
     997              :             (errmsg_internal("registering background worker \"%s\"", worker->bgw_name)));
     998              : 
     999          931 :     if (!SanityCheckBackgroundWorker(worker, LOG))
    1000            0 :         return;
    1001              : 
    1002          931 :     if (worker->bgw_notify_pid != 0)
    1003              :     {
    1004            0 :         ereport(LOG,
    1005              :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1006              :                  errmsg("background worker \"%s\": only dynamic background workers can request notification",
    1007              :                         worker->bgw_name)));
    1008            0 :         return;
    1009              :     }
    1010              : 
    1011              :     /*
    1012              :      * Enforce maximum number of workers.  Note this is overly restrictive: we
    1013              :      * could allow more non-shmem-connected workers, because these don't count
    1014              :      * towards the MAX_BACKENDS limit elsewhere.  For now, it doesn't seem
    1015              :      * important to relax this restriction.
    1016              :      */
    1017          931 :     if (++numworkers > max_worker_processes)
    1018              :     {
    1019            0 :         ereport(LOG,
    1020              :                 (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
    1021              :                  errmsg("too many background workers"),
    1022              :                  errdetail_plural("Up to %d background worker can be registered with the current settings.",
    1023              :                                   "Up to %d background workers can be registered with the current settings.",
    1024              :                                   max_worker_processes,
    1025              :                                   max_worker_processes),
    1026              :                  errhint("Consider increasing the configuration parameter \"%s\".", "max_worker_processes")));
    1027            0 :         return;
    1028              :     }
    1029              : 
    1030              :     /*
    1031              :      * Copy the registration data into the registered workers list.
    1032              :      */
    1033          931 :     rw = MemoryContextAllocExtended(PostmasterContext,
    1034              :                                     sizeof(RegisteredBgWorker),
    1035              :                                     MCXT_ALLOC_NO_OOM);
    1036          931 :     if (rw == NULL)
    1037              :     {
    1038            0 :         ereport(LOG,
    1039              :                 (errcode(ERRCODE_OUT_OF_MEMORY),
    1040              :                  errmsg("out of memory")));
    1041            0 :         return;
    1042              :     }
    1043              : 
    1044          931 :     rw->rw_worker = *worker;
    1045          931 :     rw->rw_pid = 0;
    1046          931 :     rw->rw_crashed_at = 0;
    1047          931 :     rw->rw_terminate = false;
    1048              : 
    1049          931 :     dlist_push_head(&BackgroundWorkerList, &rw->rw_lnode);
    1050              : }
    1051              : 
    1052              : /*
    1053              :  * Register a new background worker from a regular backend.
    1054              :  *
    1055              :  * Returns true on success and false on failure.  Failure typically indicates
    1056              :  * that no background worker slots are currently available.
    1057              :  *
    1058              :  * If handle != NULL, we'll set *handle to a pointer that can subsequently
    1059              :  * be used as an argument to GetBackgroundWorkerPid().  The caller can
    1060              :  * free this pointer using pfree(), if desired.
    1061              :  */
    1062              : bool
    1063         2553 : RegisterDynamicBackgroundWorker(BackgroundWorker *worker,
    1064              :                                 BackgroundWorkerHandle **handle)
    1065              : {
    1066              :     int         slotno;
    1067         2553 :     bool        success = false;
    1068              :     bool        parallel;
    1069         2553 :     uint64      generation = 0;
    1070              : 
    1071              :     /*
    1072              :      * We can't register dynamic background workers from the postmaster. If
    1073              :      * this is a standalone backend, we're the only process and can't start
    1074              :      * any more.  In a multi-process environment, it might be theoretically
    1075              :      * possible, but we don't currently support it due to locking
    1076              :      * considerations; see comments on the BackgroundWorkerSlot data
    1077              :      * structure.
    1078              :      */
    1079         2553 :     if (!IsUnderPostmaster)
    1080            0 :         return false;
    1081              : 
    1082         2553 :     if (!SanityCheckBackgroundWorker(worker, ERROR))
    1083            0 :         return false;
    1084              : 
    1085         2553 :     parallel = (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0;
    1086              : 
    1087         2553 :     LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
    1088              : 
    1089              :     /*
    1090              :      * If this is a parallel worker, check whether there are already too many
    1091              :      * parallel workers; if so, don't register another one.  Our view of
    1092              :      * parallel_terminate_count may be slightly stale, but that doesn't really
    1093              :      * matter: we would have gotten the same result if we'd arrived here
    1094              :      * slightly earlier anyway.  There's no help for it, either, since the
    1095              :      * postmaster must not take locks; a memory barrier wouldn't guarantee
    1096              :      * anything useful.
    1097              :      */
    1098         2553 :     if (parallel && (BackgroundWorkerData->parallel_register_count -
    1099         2034 :                      BackgroundWorkerData->parallel_terminate_count) >=
    1100              :         max_parallel_workers)
    1101              :     {
    1102              :         Assert(BackgroundWorkerData->parallel_register_count -
    1103              :                BackgroundWorkerData->parallel_terminate_count <=
    1104              :                MAX_PARALLEL_WORKER_LIMIT);
    1105           13 :         LWLockRelease(BackgroundWorkerLock);
    1106           13 :         return false;
    1107              :     }
    1108              : 
    1109              :     /*
    1110              :      * Look for an unused slot.  If we find one, grab it.
    1111              :      */
    1112         8059 :     for (slotno = 0; slotno < BackgroundWorkerData->total_slots; ++slotno)
    1113              :     {
    1114         8046 :         BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
    1115              : 
    1116         8046 :         if (!slot->in_use)
    1117              :         {
    1118         2527 :             memcpy(&slot->worker, worker, sizeof(BackgroundWorker));
    1119         2527 :             slot->pid = InvalidPid; /* indicates not started yet */
    1120         2527 :             slot->generation++;
    1121         2527 :             slot->terminate = false;
    1122         2527 :             generation = slot->generation;
    1123         2527 :             if (parallel)
    1124         2013 :                 BackgroundWorkerData->parallel_register_count++;
    1125              : 
    1126              :             /*
    1127              :              * Make sure postmaster doesn't see the slot as in use before it
    1128              :              * sees the new contents.
    1129              :              */
    1130         2527 :             pg_write_barrier();
    1131              : 
    1132         2527 :             slot->in_use = true;
    1133         2527 :             success = true;
    1134         2527 :             break;
    1135              :         }
    1136              :     }
    1137              : 
    1138         2540 :     LWLockRelease(BackgroundWorkerLock);
    1139              : 
    1140              :     /* If we found a slot, tell the postmaster to notice the change. */
    1141         2540 :     if (success)
    1142         2527 :         SendPostmasterSignal(PMSIGNAL_BACKGROUND_WORKER_CHANGE);
    1143              : 
    1144              :     /*
    1145              :      * If we found a slot and the user has provided a handle, initialize it.
    1146              :      */
    1147         2540 :     if (success && handle)
    1148              :     {
    1149         2527 :         *handle = palloc_object(BackgroundWorkerHandle);
    1150         2527 :         (*handle)->slot = slotno;
    1151         2527 :         (*handle)->generation = generation;
    1152              :     }
    1153              : 
    1154         2540 :     return success;
    1155              : }
    1156              : 
    1157              : /*
    1158              :  * Get the PID of a dynamically-registered background worker.
    1159              :  *
    1160              :  * If the worker is determined to be running, the return value will be
    1161              :  * BGWH_STARTED and *pidp will get the PID of the worker process.  If the
    1162              :  * postmaster has not yet attempted to start the worker, the return value will
    1163              :  * be BGWH_NOT_YET_STARTED.  Otherwise, the return value is BGWH_STOPPED.
    1164              :  *
    1165              :  * BGWH_STOPPED can indicate either that the worker is temporarily stopped
    1166              :  * (because it is configured for automatic restart and exited non-zero),
    1167              :  * or that the worker is permanently stopped (because it exited with exit
    1168              :  * code 0, or was not configured for automatic restart), or even that the
    1169              :  * worker was unregistered without ever starting (either because startup
    1170              :  * failed and the worker is not configured for automatic restart, or because
    1171              :  * TerminateBackgroundWorker was used before the worker was successfully
    1172              :  * started).
    1173              :  */
    1174              : BgwHandleStatus
    1175     10056194 : GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
    1176              : {
    1177              :     BackgroundWorkerSlot *slot;
    1178              :     pid_t       pid;
    1179              : 
    1180              :     Assert(handle->slot < max_worker_processes);
    1181     10056194 :     slot = &BackgroundWorkerData->slot[handle->slot];
    1182              : 
    1183              :     /*
    1184              :      * We could probably arrange to synchronize access to data using memory
    1185              :      * barriers only, but for now, let's just keep it simple and grab the
    1186              :      * lock.  It seems unlikely that there will be enough traffic here to
    1187              :      * result in meaningful contention.
    1188              :      */
    1189     10056194 :     LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
    1190              : 
    1191              :     /*
    1192              :      * The generation number can't be concurrently changed while we hold the
    1193              :      * lock.  The pid, which is updated by the postmaster, can change at any
    1194              :      * time, but we assume such changes are atomic.  So the value we read
    1195              :      * won't be garbage, but it might be out of date by the time the caller
    1196              :      * examines it (but that's unavoidable anyway).
    1197              :      *
    1198              :      * The in_use flag could be in the process of changing from true to false,
    1199              :      * but if it is already false then it can't change further.
    1200              :      */
    1201     10056194 :     if (handle->generation != slot->generation || !slot->in_use)
    1202         2033 :         pid = 0;
    1203              :     else
    1204     10054161 :         pid = slot->pid;
    1205              : 
    1206              :     /* All done. */
    1207     10056194 :     LWLockRelease(BackgroundWorkerLock);
    1208              : 
    1209     10056194 :     if (pid == 0)
    1210         2033 :         return BGWH_STOPPED;
    1211     10054161 :     else if (pid == InvalidPid)
    1212      1203887 :         return BGWH_NOT_YET_STARTED;
    1213      8850274 :     *pidp = pid;
    1214      8850274 :     return BGWH_STARTED;
    1215              : }
    1216              : 
    1217              : /*
    1218              :  * Wait for a background worker to start up.
    1219              :  *
    1220              :  * This is like GetBackgroundWorkerPid(), except that if the worker has not
    1221              :  * yet started, we wait for it to do so; thus, BGWH_NOT_YET_STARTED is never
    1222              :  * returned.  However, if the postmaster has died, we give up and return
    1223              :  * BGWH_POSTMASTER_DIED, since it that case we know that startup will not
    1224              :  * take place.
    1225              :  *
    1226              :  * The caller *must* have set our PID as the worker's bgw_notify_pid,
    1227              :  * else we will not be awoken promptly when the worker's state changes.
    1228              :  */
    1229              : BgwHandleStatus
    1230           32 : WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
    1231              : {
    1232              :     BgwHandleStatus status;
    1233              :     int         rc;
    1234              : 
    1235              :     for (;;)
    1236           23 :     {
    1237              :         pid_t       pid;
    1238              : 
    1239           55 :         CHECK_FOR_INTERRUPTS();
    1240              : 
    1241           55 :         status = GetBackgroundWorkerPid(handle, &pid);
    1242           55 :         if (status == BGWH_STARTED)
    1243           32 :             *pidp = pid;
    1244           55 :         if (status != BGWH_NOT_YET_STARTED)
    1245           32 :             break;
    1246              : 
    1247           23 :         rc = WaitLatch(MyLatch,
    1248              :                        WL_LATCH_SET | WL_POSTMASTER_DEATH, 0,
    1249              :                        WAIT_EVENT_BGWORKER_STARTUP);
    1250              : 
    1251           23 :         if (rc & WL_POSTMASTER_DEATH)
    1252              :         {
    1253            0 :             status = BGWH_POSTMASTER_DIED;
    1254            0 :             break;
    1255              :         }
    1256              : 
    1257           23 :         ResetLatch(MyLatch);
    1258              :     }
    1259              : 
    1260           32 :     return status;
    1261              : }
    1262              : 
    1263              : /*
    1264              :  * Wait for a background worker to stop.
    1265              :  *
    1266              :  * If the worker hasn't yet started, or is running, we wait for it to stop
    1267              :  * and then return BGWH_STOPPED.  However, if the postmaster has died, we give
    1268              :  * up and return BGWH_POSTMASTER_DIED, because it's the postmaster that
    1269              :  * notifies us when a worker's state changes.
    1270              :  *
    1271              :  * The caller *must* have set our PID as the worker's bgw_notify_pid,
    1272              :  * else we will not be awoken promptly when the worker's state changes.
    1273              :  */
    1274              : BgwHandleStatus
    1275         2034 : WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
    1276              : {
    1277              :     BgwHandleStatus status;
    1278              :     int         rc;
    1279              : 
    1280              :     for (;;)
    1281         2427 :     {
    1282              :         pid_t       pid;
    1283              : 
    1284         4461 :         CHECK_FOR_INTERRUPTS();
    1285              : 
    1286         4460 :         status = GetBackgroundWorkerPid(handle, &pid);
    1287         4460 :         if (status == BGWH_STOPPED)
    1288         2033 :             break;
    1289              : 
    1290         2427 :         rc = WaitLatch(MyLatch,
    1291              :                        WL_LATCH_SET | WL_POSTMASTER_DEATH, 0,
    1292              :                        WAIT_EVENT_BGWORKER_SHUTDOWN);
    1293              : 
    1294         2427 :         if (rc & WL_POSTMASTER_DEATH)
    1295              :         {
    1296            0 :             status = BGWH_POSTMASTER_DIED;
    1297            0 :             break;
    1298              :         }
    1299              : 
    1300         2427 :         ResetLatch(MyLatch);
    1301              :     }
    1302              : 
    1303         2033 :     return status;
    1304              : }
    1305              : 
    1306              : /*
    1307              :  * Instruct the postmaster to terminate a background worker.
    1308              :  *
    1309              :  * Note that it's safe to do this without regard to whether the worker is
    1310              :  * still running, or even if the worker may already have exited and been
    1311              :  * unregistered.
    1312              :  */
    1313              : void
    1314            8 : TerminateBackgroundWorker(BackgroundWorkerHandle *handle)
    1315              : {
    1316              :     BackgroundWorkerSlot *slot;
    1317            8 :     bool        signal_postmaster = false;
    1318              : 
    1319              :     Assert(handle->slot < max_worker_processes);
    1320            8 :     slot = &BackgroundWorkerData->slot[handle->slot];
    1321              : 
    1322              :     /* Set terminate flag in shared memory, unless slot has been reused. */
    1323            8 :     LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
    1324            8 :     if (handle->generation == slot->generation)
    1325              :     {
    1326            8 :         slot->terminate = true;
    1327            8 :         signal_postmaster = true;
    1328              :     }
    1329            8 :     LWLockRelease(BackgroundWorkerLock);
    1330              : 
    1331              :     /* Make sure the postmaster notices the change to shared memory. */
    1332            8 :     if (signal_postmaster)
    1333            8 :         SendPostmasterSignal(PMSIGNAL_BACKGROUND_WORKER_CHANGE);
    1334            8 : }
    1335              : 
    1336              : /*
    1337              :  * Look up (and possibly load) a bgworker entry point function.
    1338              :  *
    1339              :  * For functions contained in the core code, we use library name "postgres"
    1340              :  * and consult the InternalBGWorkers array.  External functions are
    1341              :  * looked up, and loaded if necessary, using load_external_function().
    1342              :  *
    1343              :  * The point of this is to pass function names as strings across process
    1344              :  * boundaries.  We can't pass actual function addresses because of the
    1345              :  * possibility that the function has been loaded at a different address
    1346              :  * in a different process.  This is obviously a hazard for functions in
    1347              :  * loadable libraries, but it can happen even for functions in the core code
    1348              :  * on platforms using EXEC_BACKEND (e.g., Windows).
    1349              :  *
    1350              :  * At some point it might be worthwhile to get rid of InternalBGWorkers[]
    1351              :  * in favor of applying load_external_function() for core functions too;
    1352              :  * but that raises portability issues that are not worth addressing now.
    1353              :  */
    1354              : static bgworker_main_type
    1355         3170 : LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
    1356              : {
    1357              :     /*
    1358              :      * If the function is to be loaded from postgres itself, search the
    1359              :      * InternalBGWorkers array.
    1360              :      */
    1361         3170 :     if (strcmp(libraryname, "postgres") == 0)
    1362              :     {
    1363              :         int         i;
    1364              : 
    1365        10912 :         for (i = 0; i < lengthof(InternalBGWorkers); i++)
    1366              :         {
    1367        10912 :             if (strcmp(InternalBGWorkers[i].fn_name, funcname) == 0)
    1368         3152 :                 return InternalBGWorkers[i].fn_addr;
    1369              :         }
    1370              : 
    1371              :         /* We can only reach this by programming error. */
    1372            0 :         elog(ERROR, "internal function \"%s\" not found", funcname);
    1373              :     }
    1374              : 
    1375              :     /* Otherwise load from external library. */
    1376           18 :     return (bgworker_main_type)
    1377           18 :         load_external_function(libraryname, funcname, true, NULL);
    1378              : }
    1379              : 
    1380              : /*
    1381              :  * Given a PID, get the bgw_type of the background worker.  Returns NULL if
    1382              :  * not a valid background worker.
    1383              :  *
    1384              :  * The return value is in static memory belonging to this function, so it has
    1385              :  * to be used before calling this function again.  This is so that the caller
    1386              :  * doesn't have to worry about the background worker locking protocol.
    1387              :  */
    1388              : const char *
    1389          926 : GetBackgroundWorkerTypeByPid(pid_t pid)
    1390              : {
    1391              :     int         slotno;
    1392          926 :     bool        found = false;
    1393              :     static char result[BGW_MAXLEN];
    1394              : 
    1395          926 :     LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
    1396              : 
    1397         1062 :     for (slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++)
    1398              :     {
    1399         1062 :         BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
    1400              : 
    1401         1062 :         if (slot->pid > 0 && slot->pid == pid)
    1402              :         {
    1403          926 :             strcpy(result, slot->worker.bgw_type);
    1404          926 :             found = true;
    1405          926 :             break;
    1406              :         }
    1407              :     }
    1408              : 
    1409          926 :     LWLockRelease(BackgroundWorkerLock);
    1410              : 
    1411          926 :     if (!found)
    1412            0 :         return NULL;
    1413              : 
    1414          926 :     return result;
    1415              : }
    1416              : 
    1417              : /*
    1418              :  * Terminate all background workers connected to the given database, if the
    1419              :  * workers can be interrupted.
    1420              :  */
    1421              : void
    1422            7 : TerminateBackgroundWorkersForDatabase(Oid databaseId)
    1423              : {
    1424            7 :     bool        signal_postmaster = false;
    1425              : 
    1426            7 :     elog(DEBUG1, "attempting worker termination for database %u",
    1427              :          databaseId);
    1428              : 
    1429            7 :     LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
    1430              : 
    1431              :     /*
    1432              :      * Iterate through slots, looking for workers connected to the given
    1433              :      * database.
    1434              :      */
    1435           63 :     for (int slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++)
    1436              :     {
    1437           56 :         BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
    1438              : 
    1439           56 :         if (slot->in_use &&
    1440           14 :             (slot->worker.bgw_flags & BGWORKER_INTERRUPTIBLE))
    1441              :         {
    1442            4 :             PGPROC     *proc = BackendPidGetProc(slot->pid);
    1443              : 
    1444            4 :             if (proc && proc->databaseId == databaseId)
    1445              :             {
    1446            4 :                 slot->terminate = true;
    1447            4 :                 signal_postmaster = true;
    1448              : 
    1449            4 :                 elog(DEBUG1, "termination requested for worker (PID %d) on database %u",
    1450              :                      (int) slot->pid, databaseId);
    1451              :             }
    1452              :         }
    1453              :     }
    1454              : 
    1455            7 :     LWLockRelease(BackgroundWorkerLock);
    1456              : 
    1457              :     /* Make sure the postmaster notices the change to shared memory. */
    1458            7 :     if (signal_postmaster)
    1459            4 :         SendPostmasterSignal(PMSIGNAL_BACKGROUND_WORKER_CHANGE);
    1460            7 : }
        

Generated by: LCOV version 2.0-1