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

Generated by: LCOV version 1.14