LCOV - code coverage report
Current view: top level - src/backend/postmaster - bgworker.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 85.8 % 386 331
Test Date: 2026-07-03 19:57:34 Functions: 96.0 % 25 24
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 63.0 % 254 160

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

Generated by: LCOV version 2.0-1