PostgreSQL Source Code  git master
bgworker.c File Reference
#include "postgres.h"
#include "access/parallel.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "port/atomics.h"
#include "postmaster/bgworker_internals.h"
#include "postmaster/interrupt.h"
#include "postmaster/postmaster.h"
#include "replication/logicallauncher.h"
#include "replication/logicalworker.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/lwlock.h"
#include "storage/pg_shmem.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "storage/shmem.h"
#include "tcop/tcopprot.h"
#include "utils/ascii.h"
#include "utils/ps_status.h"
#include "utils/timeout.h"
Include dependency graph for bgworker.c:

Go to the source code of this file.

Data Structures

struct  BackgroundWorkerSlot
 
struct  BackgroundWorkerArray
 
struct  BackgroundWorkerHandle
 

Typedefs

typedef struct BackgroundWorkerSlot BackgroundWorkerSlot
 
typedef struct BackgroundWorkerArray BackgroundWorkerArray
 

Functions

static bgworker_main_type LookupBackgroundWorkerFunction (const char *libraryname, const char *funcname)
 
Size BackgroundWorkerShmemSize (void)
 
void BackgroundWorkerShmemInit (void)
 
static RegisteredBgWorkerFindRegisteredWorkerBySlotNumber (int slotno)
 
void BackgroundWorkerStateChange (bool allow_new_workers)
 
void ForgetBackgroundWorker (slist_mutable_iter *cur)
 
void ReportBackgroundWorkerPID (RegisteredBgWorker *rw)
 
void ReportBackgroundWorkerExit (slist_mutable_iter *cur)
 
void BackgroundWorkerStopNotifications (pid_t pid)
 
void ForgetUnstartedBackgroundWorkers (void)
 
void ResetBackgroundWorkerCrashTimes (void)
 
static bool SanityCheckBackgroundWorker (BackgroundWorker *worker, int elevel)
 
static void bgworker_die (SIGNAL_ARGS)
 
void StartBackgroundWorker (void)
 
void RegisterBackgroundWorker (BackgroundWorker *worker)
 
bool RegisterDynamicBackgroundWorker (BackgroundWorker *worker, BackgroundWorkerHandle **handle)
 
BgwHandleStatus GetBackgroundWorkerPid (BackgroundWorkerHandle *handle, pid_t *pidp)
 
BgwHandleStatus WaitForBackgroundWorkerStartup (BackgroundWorkerHandle *handle, pid_t *pidp)
 
BgwHandleStatus WaitForBackgroundWorkerShutdown (BackgroundWorkerHandle *handle)
 
void TerminateBackgroundWorker (BackgroundWorkerHandle *handle)
 
const char * GetBackgroundWorkerTypeByPid (pid_t pid)
 

Variables

slist_head BackgroundWorkerList = SLIST_STATIC_INIT(BackgroundWorkerList)
 
static BackgroundWorkerArrayBackgroundWorkerData
 
struct {
   const char *   fn_name
 
   bgworker_main_type   fn_addr
 
InternalBGWorkers []
 

Typedef Documentation

◆ BackgroundWorkerArray

◆ BackgroundWorkerSlot

Function Documentation

◆ BackgroundWorkerShmemInit()

void BackgroundWorkerShmemInit ( void  )

Definition at line 161 of file bgworker.c.

162 {
163  bool found;
164 
165  BackgroundWorkerData = ShmemInitStruct("Background Worker Data",
167  &found);
168  if (!IsUnderPostmaster)
169  {
170  slist_iter siter;
171  int slotno = 0;
172 
176 
177  /*
178  * Copy contents of worker list into shared memory. Record the shared
179  * memory slot assigned to each worker. This ensures a 1-to-1
180  * correspondence between the postmaster's private list and the array
181  * in shared memory.
182  */
184  {
186  RegisteredBgWorker *rw;
187 
188  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
189  Assert(slotno < max_worker_processes);
190  slot->in_use = true;
191  slot->terminate = false;
192  slot->pid = InvalidPid;
193  slot->generation = 0;
194  rw->rw_shmem_slot = slotno;
195  rw->rw_worker.bgw_notify_pid = 0; /* might be reinit after crash */
196  memcpy(&slot->worker, &rw->rw_worker, sizeof(BackgroundWorker));
197  ++slotno;
198  }
199 
200  /*
201  * Mark any remaining slots as not in use.
202  */
203  while (slotno < max_worker_processes)
204  {
206 
207  slot->in_use = false;
208  ++slotno;
209  }
210  }
211  else
212  Assert(found);
213 }
slist_head BackgroundWorkerList
Definition: bgworker.c:42
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:145
bool IsUnderPostmaster
Definition: globals.c:113
int max_worker_processes
Definition: globals.c:138
#define slist_container(type, membername, ptr)
Definition: ilist.h:1106
#define slist_foreach(iter, lhead)
Definition: ilist.h:1132
Assert(fmt[strlen(fmt) - 1] !='\n')
#define InvalidPid
Definition: miscadmin.h:32
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
uint32 parallel_terminate_count
Definition: bgworker.c:100
uint32 parallel_register_count
Definition: bgworker.c:99
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:101
BackgroundWorker worker
Definition: bgworker.c:82
pid_t bgw_notify_pid
Definition: bgworker.h:100
BackgroundWorker rw_worker
slist_node * cur
Definition: ilist.h:259

References Assert(), BackgroundWorkerData, BackgroundWorkerList, BackgroundWorkerShmemSize(), BackgroundWorker::bgw_notify_pid, slist_iter::cur, BackgroundWorkerSlot::generation, BackgroundWorkerSlot::in_use, InvalidPid, IsUnderPostmaster, max_worker_processes, BackgroundWorkerArray::parallel_register_count, BackgroundWorkerArray::parallel_terminate_count, BackgroundWorkerSlot::pid, RegisteredBgWorker::rw_shmem_slot, RegisteredBgWorker::rw_worker, ShmemInitStruct(), slist_container, slist_foreach, BackgroundWorkerArray::slot, BackgroundWorkerSlot::terminate, BackgroundWorkerArray::total_slots, and BackgroundWorkerSlot::worker.

Referenced by CreateSharedMemoryAndSemaphores().

◆ BackgroundWorkerShmemSize()

Size BackgroundWorkerShmemSize ( void  )

Definition at line 145 of file bgworker.c.

146 {
147  Size size;
148 
149  /* Array of workers is variably sized. */
150  size = offsetof(BackgroundWorkerArray, slot);
152  sizeof(BackgroundWorkerSlot)));
153 
154  return size;
155 }
size_t Size
Definition: c.h:589
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519

References add_size(), max_worker_processes, and mul_size().

Referenced by BackgroundWorkerShmemInit(), and CalculateShmemSize().

◆ BackgroundWorkerStateChange()

void BackgroundWorkerStateChange ( bool  allow_new_workers)

Definition at line 245 of file bgworker.c.

246 {
247  int slotno;
248 
249  /*
250  * The total number of slots stored in shared memory should match our
251  * notion of max_worker_processes. If it does not, something is very
252  * wrong. Further down, we always refer to this value as
253  * max_worker_processes, in case shared memory gets corrupted while we're
254  * looping.
255  */
257  {
258  ereport(LOG,
259  (errmsg("inconsistent background worker state (max_worker_processes=%d, total_slots=%d)",
262  return;
263  }
264 
265  /*
266  * Iterate through slots, looking for newly-registered workers or workers
267  * who must die.
268  */
269  for (slotno = 0; slotno < max_worker_processes; ++slotno)
270  {
272  RegisteredBgWorker *rw;
273 
274  if (!slot->in_use)
275  continue;
276 
277  /*
278  * Make sure we don't see the in_use flag before the updated slot
279  * contents.
280  */
281  pg_read_barrier();
282 
283  /* See whether we already know about this worker. */
285  if (rw != NULL)
286  {
287  /*
288  * In general, the worker data can't change after it's initially
289  * registered. However, someone can set the terminate flag.
290  */
291  if (slot->terminate && !rw->rw_terminate)
292  {
293  rw->rw_terminate = true;
294  if (rw->rw_pid != 0)
295  kill(rw->rw_pid, SIGTERM);
296  else
297  {
298  /* Report never-started, now-terminated worker as dead. */
300  }
301  }
302  continue;
303  }
304 
305  /*
306  * If we aren't allowing new workers, then immediately mark it for
307  * termination; the next stanza will take care of cleaning it up.
308  * Doing this ensures that any process waiting for the worker will get
309  * awoken, even though the worker will never be allowed to run.
310  */
311  if (!allow_new_workers)
312  slot->terminate = true;
313 
314  /*
315  * If the worker is marked for termination, we don't need to add it to
316  * the registered workers list; we can just free the slot. However, if
317  * bgw_notify_pid is set, the process that registered the worker may
318  * need to know that we've processed the terminate request, so be sure
319  * to signal it.
320  */
321  if (slot->terminate)
322  {
323  int notify_pid;
324 
325  /*
326  * We need a memory barrier here to make sure that the load of
327  * bgw_notify_pid and the update of parallel_terminate_count
328  * complete before the store to in_use.
329  */
330  notify_pid = slot->worker.bgw_notify_pid;
331  if ((slot->worker.bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
333  slot->pid = 0;
334 
336  slot->in_use = false;
337 
338  if (notify_pid != 0)
339  kill(notify_pid, SIGUSR1);
340 
341  continue;
342  }
343 
344  /*
345  * Copy the registration data into the registered workers list.
346  */
347  rw = malloc(sizeof(RegisteredBgWorker));
348  if (rw == NULL)
349  {
350  ereport(LOG,
351  (errcode(ERRCODE_OUT_OF_MEMORY),
352  errmsg("out of memory")));
353  return;
354  }
355 
356  /*
357  * Copy strings in a paranoid way. If shared memory is corrupted, the
358  * source data might not even be NUL-terminated.
359  */
361  slot->worker.bgw_name, BGW_MAXLEN);
363  slot->worker.bgw_type, BGW_MAXLEN);
368 
369  /*
370  * Copy various fixed-size fields.
371  *
372  * flags, start_time, and restart_time are examined by the postmaster,
373  * but nothing too bad will happen if they are corrupted. The
374  * remaining fields will only be examined by the child process. It
375  * might crash, but we won't.
376  */
377  rw->rw_worker.bgw_flags = slot->worker.bgw_flags;
381  memcpy(rw->rw_worker.bgw_extra, slot->worker.bgw_extra, BGW_EXTRALEN);
382 
383  /*
384  * Copy the PID to be notified about state changes, but only if the
385  * postmaster knows about a backend with that PID. It isn't an error
386  * if the postmaster doesn't know about the PID, because the backend
387  * that requested the worker could have died (or been killed) just
388  * after doing so. Nonetheless, at least until we get some experience
389  * with how this plays out in the wild, log a message at a relative
390  * high debug level.
391  */
394  {
395  elog(DEBUG1, "worker notification PID %d is not valid",
396  (int) rw->rw_worker.bgw_notify_pid);
397  rw->rw_worker.bgw_notify_pid = 0;
398  }
399 
400  /* Initialize postmaster bookkeeping. */
401  rw->rw_backend = NULL;
402  rw->rw_pid = 0;
403  rw->rw_child_slot = 0;
404  rw->rw_crashed_at = 0;
405  rw->rw_shmem_slot = slotno;
406  rw->rw_terminate = false;
407 
408  /* Log it! */
409  ereport(DEBUG1,
410  (errmsg_internal("registering background worker \"%s\"",
411  rw->rw_worker.bgw_name)));
412 
414  }
415 }
void ascii_safe_strlcpy(char *dest, const char *src, size_t destsiz)
Definition: ascii.c:174
#define pg_memory_barrier()
Definition: atomics.h:140
#define pg_read_barrier()
Definition: atomics.h:153
static RegisteredBgWorker * FindRegisteredWorkerBySlotNumber(int slotno)
Definition: bgworker.c:220
void ReportBackgroundWorkerPID(RegisteredBgWorker *rw)
Definition: bgworker.c:464
#define BGW_EXTRALEN
Definition: bgworker.h:87
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:68
#define BGW_MAXLEN
Definition: bgworker.h:86
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define LOG
Definition: elog.h:31
#define DEBUG1
Definition: elog.h:30
#define ereport(elevel,...)
Definition: elog.h:149
#define malloc(a)
Definition: header.h:50
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:1006
bool PostmasterMarkPIDForWorkerNotify(int pid)
Definition: postmaster.c:6013
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:97
Datum bgw_main_arg
Definition: bgworker.h:98
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:91
int bgw_restart_time
Definition: bgworker.h:95
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:92
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:94
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:99
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:96
struct bkend * rw_backend
#define kill(pid, sig)
Definition: win32_port.h:489
#define SIGUSR1
Definition: win32_port.h:188

References ascii_safe_strlcpy(), BackgroundWorkerData, BackgroundWorkerList, BackgroundWorker::bgw_extra, BGW_EXTRALEN, BackgroundWorker::bgw_flags, BackgroundWorker::bgw_function_name, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_main_arg, BGW_MAXLEN, BackgroundWorker::bgw_name, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, BackgroundWorker::bgw_start_time, BackgroundWorker::bgw_type, BGWORKER_CLASS_PARALLEL, DEBUG1, elog(), ereport, errcode(), errmsg(), errmsg_internal(), FindRegisteredWorkerBySlotNumber(), BackgroundWorkerSlot::in_use, kill, LOG, malloc, max_worker_processes, BackgroundWorkerArray::parallel_terminate_count, pg_memory_barrier, pg_read_barrier, BackgroundWorkerSlot::pid, PostmasterMarkPIDForWorkerNotify(), ReportBackgroundWorkerPID(), RegisteredBgWorker::rw_backend, RegisteredBgWorker::rw_child_slot, RegisteredBgWorker::rw_crashed_at, RegisteredBgWorker::rw_lnode, RegisteredBgWorker::rw_pid, RegisteredBgWorker::rw_shmem_slot, RegisteredBgWorker::rw_terminate, RegisteredBgWorker::rw_worker, SIGUSR1, slist_push_head(), BackgroundWorkerArray::slot, BackgroundWorkerSlot::terminate, BackgroundWorkerArray::total_slots, and BackgroundWorkerSlot::worker.

Referenced by process_pm_pmsignal().

◆ BackgroundWorkerStopNotifications()

void BackgroundWorkerStopNotifications ( pid_t  pid)

Definition at line 517 of file bgworker.c.

518 {
519  slist_iter siter;
520 
522  {
523  RegisteredBgWorker *rw;
524 
525  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
526  if (rw->rw_worker.bgw_notify_pid == pid)
527  rw->rw_worker.bgw_notify_pid = 0;
528  }
529 }

References BackgroundWorkerList, BackgroundWorker::bgw_notify_pid, slist_iter::cur, RegisteredBgWorker::rw_worker, slist_container, and slist_foreach.

Referenced by CleanupBackend(), and CleanupBackgroundWorker().

◆ bgworker_die()

static void bgworker_die ( SIGNAL_ARGS  )
static

Definition at line 727 of file bgworker.c.

728 {
729  sigprocmask(SIG_SETMASK, &BlockSig, NULL);
730 
731  ereport(FATAL,
732  (errcode(ERRCODE_ADMIN_SHUTDOWN),
733  errmsg("terminating background worker \"%s\" due to administrator command",
735 }
sigset_t BlockSig
Definition: pqsignal.c:23
#define FATAL
Definition: elog.h:41
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:193

References BackgroundWorker::bgw_type, BlockSig, ereport, errcode(), errmsg(), FATAL, and MyBgworkerEntry.

Referenced by StartBackgroundWorker().

◆ FindRegisteredWorkerBySlotNumber()

static RegisteredBgWorker* FindRegisteredWorkerBySlotNumber ( int  slotno)
static

Definition at line 220 of file bgworker.c.

221 {
222  slist_iter siter;
223 
225  {
226  RegisteredBgWorker *rw;
227 
228  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
229  if (rw->rw_shmem_slot == slotno)
230  return rw;
231  }
232 
233  return NULL;
234 }

References BackgroundWorkerList, slist_iter::cur, RegisteredBgWorker::rw_shmem_slot, slist_container, and slist_foreach.

Referenced by BackgroundWorkerStateChange().

◆ ForgetBackgroundWorker()

void ForgetBackgroundWorker ( slist_mutable_iter cur)

Definition at line 429 of file bgworker.c.

430 {
431  RegisteredBgWorker *rw;
432  BackgroundWorkerSlot *slot;
433 
434  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
435 
438  Assert(slot->in_use);
439 
440  /*
441  * We need a memory barrier here to make sure that the update of
442  * parallel_terminate_count completes before the store to in_use.
443  */
446 
448  slot->in_use = false;
449 
450  ereport(DEBUG1,
451  (errmsg_internal("unregistering background worker \"%s\"",
452  rw->rw_worker.bgw_name)));
453 
455  free(rw);
456 }
struct cursor * cur
Definition: ecpg.c:28
#define free(a)
Definition: header.h:65
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:1084

References Assert(), BackgroundWorkerData, BackgroundWorker::bgw_flags, BackgroundWorker::bgw_name, BGWORKER_CLASS_PARALLEL, cur, DEBUG1, ereport, errmsg_internal(), free, BackgroundWorkerSlot::in_use, max_worker_processes, BackgroundWorkerArray::parallel_terminate_count, pg_memory_barrier, RegisteredBgWorker::rw_shmem_slot, RegisteredBgWorker::rw_worker, slist_container, slist_delete_current(), and BackgroundWorkerArray::slot.

Referenced by DetermineSleepTime(), ForgetUnstartedBackgroundWorkers(), maybe_start_bgworkers(), ReportBackgroundWorkerExit(), and ResetBackgroundWorkerCrashTimes().

◆ ForgetUnstartedBackgroundWorkers()

void ForgetUnstartedBackgroundWorkers ( void  )

Definition at line 544 of file bgworker.c.

545 {
546  slist_mutable_iter iter;
547 
549  {
550  RegisteredBgWorker *rw;
551  BackgroundWorkerSlot *slot;
552 
553  rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
556 
557  /* If it's not yet started, and there's someone waiting ... */
558  if (slot->pid == InvalidPid &&
559  rw->rw_worker.bgw_notify_pid != 0)
560  {
561  /* ... then zap it, and notify the waiter */
562  int notify_pid = rw->rw_worker.bgw_notify_pid;
563 
564  ForgetBackgroundWorker(&iter);
565  if (notify_pid != 0)
566  kill(notify_pid, SIGUSR1);
567  }
568  }
569 }
void ForgetBackgroundWorker(slist_mutable_iter *cur)
Definition: bgworker.c:429
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:1148
slist_node * cur
Definition: ilist.h:274

References Assert(), BackgroundWorkerData, BackgroundWorkerList, BackgroundWorker::bgw_notify_pid, slist_mutable_iter::cur, ForgetBackgroundWorker(), InvalidPid, kill, max_worker_processes, BackgroundWorkerSlot::pid, RegisteredBgWorker::rw_shmem_slot, RegisteredBgWorker::rw_worker, SIGUSR1, slist_container, slist_foreach_modify, and BackgroundWorkerArray::slot.

Referenced by PostmasterStateMachine().

◆ GetBackgroundWorkerPid()

BgwHandleStatus GetBackgroundWorkerPid ( BackgroundWorkerHandle handle,
pid_t *  pidp 
)

Definition at line 1071 of file bgworker.c.

1072 {
1073  BackgroundWorkerSlot *slot;
1074  pid_t pid;
1075 
1076  Assert(handle->slot < max_worker_processes);
1077  slot = &BackgroundWorkerData->slot[handle->slot];
1078 
1079  /*
1080  * We could probably arrange to synchronize access to data using memory
1081  * barriers only, but for now, let's just keep it simple and grab the
1082  * lock. It seems unlikely that there will be enough traffic here to
1083  * result in meaningful contention.
1084  */
1085  LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1086 
1087  /*
1088  * The generation number can't be concurrently changed while we hold the
1089  * lock. The pid, which is updated by the postmaster, can change at any
1090  * time, but we assume such changes are atomic. So the value we read
1091  * won't be garbage, but it might be out of date by the time the caller
1092  * examines it (but that's unavoidable anyway).
1093  *
1094  * The in_use flag could be in the process of changing from true to false,
1095  * but if it is already false then it can't change further.
1096  */
1097  if (handle->generation != slot->generation || !slot->in_use)
1098  pid = 0;
1099  else
1100  pid = slot->pid;
1101 
1102  /* All done. */
1103  LWLockRelease(BackgroundWorkerLock);
1104 
1105  if (pid == 0)
1106  return BGWH_STOPPED;
1107  else if (pid == InvalidPid)
1108  return BGWH_NOT_YET_STARTED;
1109  *pidp = pid;
1110  return BGWH_STARTED;
1111 }
@ BGWH_STARTED
Definition: bgworker.h:105
@ BGWH_NOT_YET_STARTED
Definition: bgworker.h:106
@ BGWH_STOPPED
Definition: bgworker.h:107
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
@ LW_SHARED
Definition: lwlock.h:116

References Assert(), BackgroundWorkerData, BGWH_NOT_YET_STARTED, BGWH_STARTED, BGWH_STOPPED, BackgroundWorkerSlot::generation, BackgroundWorkerHandle::generation, BackgroundWorkerSlot::in_use, InvalidPid, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_worker_processes, BackgroundWorkerSlot::pid, BackgroundWorkerArray::slot, and BackgroundWorkerHandle::slot.

Referenced by check_worker_status(), shm_mq_counterparty_gone(), shm_mq_wait_internal(), WaitForBackgroundWorkerShutdown(), WaitForBackgroundWorkerStartup(), WaitForParallelWorkersToAttach(), WaitForParallelWorkersToFinish(), and WaitForReplicationWorkerAttach().

◆ GetBackgroundWorkerTypeByPid()

const char* GetBackgroundWorkerTypeByPid ( pid_t  pid)

Definition at line 1285 of file bgworker.c.

1286 {
1287  int slotno;
1288  bool found = false;
1289  static char result[BGW_MAXLEN];
1290 
1291  LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1292 
1293  for (slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++)
1294  {
1296 
1297  if (slot->pid > 0 && slot->pid == pid)
1298  {
1299  strcpy(result, slot->worker.bgw_type);
1300  found = true;
1301  break;
1302  }
1303  }
1304 
1305  LWLockRelease(BackgroundWorkerLock);
1306 
1307  if (!found)
1308  return NULL;
1309 
1310  return result;
1311 }

References BackgroundWorkerData, BGW_MAXLEN, BackgroundWorker::bgw_type, LW_SHARED, LWLockAcquire(), LWLockRelease(), BackgroundWorkerSlot::pid, BackgroundWorkerArray::slot, BackgroundWorkerArray::total_slots, and BackgroundWorkerSlot::worker.

Referenced by pg_stat_get_activity().

◆ LookupBackgroundWorkerFunction()

static bgworker_main_type LookupBackgroundWorkerFunction ( const char *  libraryname,
const char *  funcname 
)
static

Definition at line 1251 of file bgworker.c.

1252 {
1253  /*
1254  * If the function is to be loaded from postgres itself, search the
1255  * InternalBGWorkers array.
1256  */
1257  if (strcmp(libraryname, "postgres") == 0)
1258  {
1259  int i;
1260 
1261  for (i = 0; i < lengthof(InternalBGWorkers); i++)
1262  {
1263  if (strcmp(InternalBGWorkers[i].fn_name, funcname) == 0)
1264  return InternalBGWorkers[i].fn_addr;
1265  }
1266 
1267  /* We can only reach this by programming error. */
1268  elog(ERROR, "internal function \"%s\" not found", funcname);
1269  }
1270 
1271  /* Otherwise load from external library. */
1272  return (bgworker_main_type)
1273  load_external_function(libraryname, funcname, true, NULL);
1274 }
const char * fn_name
Definition: bgworker.c:118
static const struct @15 InternalBGWorkers[]
bgworker_main_type fn_addr
Definition: bgworker.c:119
void(* bgworker_main_type)(Datum main_arg)
Definition: bgworker.h:72
#define lengthof(array)
Definition: c.h:772
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
#define ERROR
Definition: elog.h:39
#define funcname
Definition: indent_codes.h:69
int i
Definition: isn.c:73

References elog(), ERROR, fn_addr, fn_name, funcname, i, InternalBGWorkers, lengthof, and load_external_function().

Referenced by StartBackgroundWorker().

◆ RegisterBackgroundWorker()

void RegisterBackgroundWorker ( BackgroundWorker worker)

Definition at line 875 of file bgworker.c.

876 {
877  RegisteredBgWorker *rw;
878  static int numworkers = 0;
879 
880  if (!IsUnderPostmaster)
881  ereport(DEBUG1,
882  (errmsg_internal("registering background worker \"%s\"", worker->bgw_name)));
883 
885  strcmp(worker->bgw_library_name, "postgres") != 0)
886  {
887  if (!IsUnderPostmaster)
888  ereport(LOG,
889  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
890  errmsg("background worker \"%s\": must be registered in shared_preload_libraries",
891  worker->bgw_name)));
892  return;
893  }
894 
895  if (!SanityCheckBackgroundWorker(worker, LOG))
896  return;
897 
898  if (worker->bgw_notify_pid != 0)
899  {
900  ereport(LOG,
901  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
902  errmsg("background worker \"%s\": only dynamic background workers can request notification",
903  worker->bgw_name)));
904  return;
905  }
906 
907  /*
908  * Enforce maximum number of workers. Note this is overly restrictive: we
909  * could allow more non-shmem-connected workers, because these don't count
910  * towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
911  * important to relax this restriction.
912  */
913  if (++numworkers > max_worker_processes)
914  {
915  ereport(LOG,
916  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
917  errmsg("too many background workers"),
918  errdetail_plural("Up to %d background worker can be registered with the current settings.",
919  "Up to %d background workers can be registered with the current settings.",
922  errhint("Consider increasing the configuration parameter \"max_worker_processes\".")));
923  return;
924  }
925 
926  /*
927  * Copy the registration data into the registered workers list.
928  */
929  rw = malloc(sizeof(RegisteredBgWorker));
930  if (rw == NULL)
931  {
932  ereport(LOG,
933  (errcode(ERRCODE_OUT_OF_MEMORY),
934  errmsg("out of memory")));
935  return;
936  }
937 
938  rw->rw_worker = *worker;
939  rw->rw_backend = NULL;
940  rw->rw_pid = 0;
941  rw->rw_child_slot = 0;
942  rw->rw_crashed_at = 0;
943  rw->rw_terminate = false;
944 
946 }
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition: bgworker.c:655
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1294
int errhint(const char *fmt,...)
Definition: elog.c:1316
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1782

References BackgroundWorkerList, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_name, BackgroundWorker::bgw_notify_pid, DEBUG1, ereport, errcode(), errdetail_plural(), errhint(), errmsg(), errmsg_internal(), IsUnderPostmaster, LOG, malloc, max_worker_processes, process_shared_preload_libraries_in_progress, RegisteredBgWorker::rw_backend, RegisteredBgWorker::rw_child_slot, RegisteredBgWorker::rw_crashed_at, RegisteredBgWorker::rw_lnode, RegisteredBgWorker::rw_pid, RegisteredBgWorker::rw_terminate, RegisteredBgWorker::rw_worker, SanityCheckBackgroundWorker(), and slist_push_head().

Referenced by _PG_init(), ApplyLauncherRegister(), and apw_start_leader_worker().

◆ RegisterDynamicBackgroundWorker()

bool RegisterDynamicBackgroundWorker ( BackgroundWorker worker,
BackgroundWorkerHandle **  handle 
)

Definition at line 959 of file bgworker.c.

961 {
962  int slotno;
963  bool success = false;
964  bool parallel;
965  uint64 generation = 0;
966 
967  /*
968  * We can't register dynamic background workers from the postmaster. If
969  * this is a standalone backend, we're the only process and can't start
970  * any more. In a multi-process environment, it might be theoretically
971  * possible, but we don't currently support it due to locking
972  * considerations; see comments on the BackgroundWorkerSlot data
973  * structure.
974  */
975  if (!IsUnderPostmaster)
976  return false;
977 
978  if (!SanityCheckBackgroundWorker(worker, ERROR))
979  return false;
980 
981  parallel = (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0;
982 
983  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
984 
985  /*
986  * If this is a parallel worker, check whether there are already too many
987  * parallel workers; if so, don't register another one. Our view of
988  * parallel_terminate_count may be slightly stale, but that doesn't really
989  * matter: we would have gotten the same result if we'd arrived here
990  * slightly earlier anyway. There's no help for it, either, since the
991  * postmaster must not take locks; a memory barrier wouldn't guarantee
992  * anything useful.
993  */
997  {
1001  LWLockRelease(BackgroundWorkerLock);
1002  return false;
1003  }
1004 
1005  /*
1006  * Look for an unused slot. If we find one, grab it.
1007  */
1008  for (slotno = 0; slotno < BackgroundWorkerData->total_slots; ++slotno)
1009  {
1011 
1012  if (!slot->in_use)
1013  {
1014  memcpy(&slot->worker, worker, sizeof(BackgroundWorker));
1015  slot->pid = InvalidPid; /* indicates not started yet */
1016  slot->generation++;
1017  slot->terminate = false;
1018  generation = slot->generation;
1019  if (parallel)
1021 
1022  /*
1023  * Make sure postmaster doesn't see the slot as in use before it
1024  * sees the new contents.
1025  */
1026  pg_write_barrier();
1027 
1028  slot->in_use = true;
1029  success = true;
1030  break;
1031  }
1032  }
1033 
1034  LWLockRelease(BackgroundWorkerLock);
1035 
1036  /* If we found a slot, tell the postmaster to notice the change. */
1037  if (success)
1039 
1040  /*
1041  * If we found a slot and the user has provided a handle, initialize it.
1042  */
1043  if (success && handle)
1044  {
1045  *handle = palloc(sizeof(BackgroundWorkerHandle));
1046  (*handle)->slot = slotno;
1047  (*handle)->generation = generation;
1048  }
1049 
1050  return success;
1051 }
#define pg_write_barrier()
Definition: atomics.h:154
#define MAX_PARALLEL_WORKER_LIMIT
int max_parallel_workers
Definition: globals.c:139
static bool success
Definition: initdb.c:187
@ LW_EXCLUSIVE
Definition: lwlock.h:115
void * palloc(Size size)
Definition: mcxt.c:1210
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:181
@ PMSIGNAL_BACKGROUND_WORKER_CHANGE
Definition: pmsignal.h:40

References Assert(), BackgroundWorkerData, BackgroundWorker::bgw_flags, BGWORKER_CLASS_PARALLEL, ERROR, BackgroundWorkerSlot::generation, BackgroundWorkerSlot::in_use, InvalidPid, IsUnderPostmaster, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAX_PARALLEL_WORKER_LIMIT, max_parallel_workers, palloc(), BackgroundWorkerArray::parallel_register_count, BackgroundWorkerArray::parallel_terminate_count, pg_write_barrier, BackgroundWorkerSlot::pid, PMSIGNAL_BACKGROUND_WORKER_CHANGE, SanityCheckBackgroundWorker(), SendPostmasterSignal(), BackgroundWorkerArray::slot, success, BackgroundWorkerSlot::terminate, BackgroundWorkerArray::total_slots, and BackgroundWorkerSlot::worker.

Referenced by apw_start_database_worker(), apw_start_leader_worker(), LaunchParallelWorkers(), logicalrep_worker_launch(), setup_background_workers(), and worker_spi_launch().

◆ ReportBackgroundWorkerExit()

void ReportBackgroundWorkerExit ( slist_mutable_iter cur)

Definition at line 483 of file bgworker.c.

484 {
485  RegisteredBgWorker *rw;
486  BackgroundWorkerSlot *slot;
487  int notify_pid;
488 
489  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
490 
493  slot->pid = rw->rw_pid;
494  notify_pid = rw->rw_worker.bgw_notify_pid;
495 
496  /*
497  * If this worker is slated for deregistration, do that before notifying
498  * the process which started it. Otherwise, if that process tries to
499  * reuse the slot immediately, it might not be available yet. In theory
500  * that could happen anyway if the process checks slot->pid at just the
501  * wrong moment, but this makes the window narrower.
502  */
503  if (rw->rw_terminate ||
506 
507  if (notify_pid != 0)
508  kill(notify_pid, SIGUSR1);
509 }
#define BGW_NEVER_RESTART
Definition: bgworker.h:85

References Assert(), BackgroundWorkerData, BGW_NEVER_RESTART, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, cur, ForgetBackgroundWorker(), kill, max_worker_processes, BackgroundWorkerSlot::pid, RegisteredBgWorker::rw_pid, RegisteredBgWorker::rw_shmem_slot, RegisteredBgWorker::rw_terminate, RegisteredBgWorker::rw_worker, SIGUSR1, slist_container, and BackgroundWorkerArray::slot.

Referenced by CleanupBackgroundWorker().

◆ ReportBackgroundWorkerPID()

◆ ResetBackgroundWorkerCrashTimes()

void ResetBackgroundWorkerCrashTimes ( void  )

Definition at line 582 of file bgworker.c.

583 {
584  slist_mutable_iter iter;
585 
587  {
588  RegisteredBgWorker *rw;
589 
590  rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
591 
593  {
594  /*
595  * Workers marked BGW_NEVER_RESTART shouldn't get relaunched after
596  * the crash, so forget about them. (If we wait until after the
597  * crash to forget about them, and they are parallel workers,
598  * parallel_terminate_count will get incremented after we've
599  * already zeroed parallel_register_count, which would be bad.)
600  */
601  ForgetBackgroundWorker(&iter);
602  }
603  else
604  {
605  /*
606  * The accounting which we do via parallel_register_count and
607  * parallel_terminate_count would get messed up if a worker marked
608  * parallel could survive a crash and restart cycle. All such
609  * workers should be marked BGW_NEVER_RESTART, and thus control
610  * should never reach this branch.
611  */
613 
614  /*
615  * Allow this worker to be restarted immediately after we finish
616  * resetting.
617  */
618  rw->rw_crashed_at = 0;
619 
620  /*
621  * If there was anyone waiting for it, they're history.
622  */
623  rw->rw_worker.bgw_notify_pid = 0;
624  }
625  }
626 }

References Assert(), BackgroundWorkerList, BackgroundWorker::bgw_flags, BGW_NEVER_RESTART, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, BGWORKER_CLASS_PARALLEL, slist_mutable_iter::cur, ForgetBackgroundWorker(), RegisteredBgWorker::rw_crashed_at, RegisteredBgWorker::rw_worker, slist_container, and slist_foreach_modify.

Referenced by PostmasterStateMachine().

◆ SanityCheckBackgroundWorker()

static bool SanityCheckBackgroundWorker ( BackgroundWorker worker,
int  elevel 
)
static

Definition at line 655 of file bgworker.c.

656 {
657  /* sanity check for flags */
658 
659  /*
660  * We used to support workers not connected to shared memory, but don't
661  * anymore. Thus this is a required flag now. We're not removing the flag
662  * for compatibility reasons and because the flag still provides some
663  * signal when reading code.
664  */
665  if (!(worker->bgw_flags & BGWORKER_SHMEM_ACCESS))
666  {
667  ereport(elevel,
668  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
669  errmsg("background worker \"%s\": background workers without shared memory access are not supported",
670  worker->bgw_name)));
671  return false;
672  }
673 
675  {
677  {
678  ereport(elevel,
679  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
680  errmsg("background worker \"%s\": cannot request database access if starting at postmaster start",
681  worker->bgw_name)));
682  return false;
683  }
684 
685  /* XXX other checks? */
686  }
687 
688  if ((worker->bgw_restart_time < 0 &&
689  worker->bgw_restart_time != BGW_NEVER_RESTART) ||
690  (worker->bgw_restart_time > USECS_PER_DAY / 1000))
691  {
692  ereport(elevel,
693  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
694  errmsg("background worker \"%s\": invalid restart interval",
695  worker->bgw_name)));
696  return false;
697  }
698 
699  /*
700  * Parallel workers may not be configured for restart, because the
701  * parallel_register_count/parallel_terminate_count accounting can't
702  * handle parallel workers lasting through a crash-and-restart cycle.
703  */
704  if (worker->bgw_restart_time != BGW_NEVER_RESTART &&
705  (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
706  {
707  ereport(elevel,
708  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
709  errmsg("background worker \"%s\": parallel workers may not be configured for restart",
710  worker->bgw_name)));
711  return false;
712  }
713 
714  /*
715  * If bgw_type is not filled in, use bgw_name.
716  */
717  if (strcmp(worker->bgw_type, "") == 0)
718  strcpy(worker->bgw_type, worker->bgw_name);
719 
720  return true;
721 }
@ BgWorkerStart_PostmasterStart
Definition: bgworker.h:79
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:60
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:53
#define USECS_PER_DAY
Definition: timestamp.h:130

References BackgroundWorker::bgw_flags, BackgroundWorker::bgw_name, BGW_NEVER_RESTART, BackgroundWorker::bgw_restart_time, BackgroundWorker::bgw_start_time, BackgroundWorker::bgw_type, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_CLASS_PARALLEL, BGWORKER_SHMEM_ACCESS, BgWorkerStart_PostmasterStart, ereport, errcode(), errmsg(), and USECS_PER_DAY.

Referenced by RegisterBackgroundWorker(), and RegisterDynamicBackgroundWorker().

◆ StartBackgroundWorker()

void StartBackgroundWorker ( void  )

Definition at line 744 of file bgworker.c.

745 {
746  sigjmp_buf local_sigjmp_buf;
748  bgworker_main_type entrypt;
749 
750  if (worker == NULL)
751  elog(FATAL, "unable to find bgworker entry");
752 
753  IsBackgroundWorker = true;
754 
756  init_ps_display(worker->bgw_name);
757 
759 
760  /* Apply PostAuthDelay */
761  if (PostAuthDelay > 0)
762  pg_usleep(PostAuthDelay * 1000000L);
763 
764  /*
765  * Set up signal handlers.
766  */
768  {
769  /*
770  * SIGINT is used to signal canceling the current action
771  */
775 
776  /* XXX Any other handlers needed here? */
777  }
778  else
779  {
780  pqsignal(SIGINT, SIG_IGN);
782  pqsignal(SIGFPE, SIG_IGN);
783  }
784  pqsignal(SIGTERM, bgworker_die);
785  /* SIGQUIT handler was already set up by InitPostmasterChild */
787 
788  InitializeTimeouts(); /* establishes SIGALRM handler */
789 
793 
794  /*
795  * If an exception is encountered, processing resumes here.
796  *
797  * We just need to clean up, report the error, and go away.
798  */
799  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
800  {
801  /* Since not using PG_TRY, must reset error stack by hand */
802  error_context_stack = NULL;
803 
804  /* Prevent interrupts while cleaning up */
805  HOLD_INTERRUPTS();
806 
807  /*
808  * sigsetjmp will have blocked all signals, but we may need to accept
809  * signals while communicating with our parallel leader. Once we've
810  * done HOLD_INTERRUPTS() it should be safe to unblock signals.
811  */
813 
814  /* Report the error to the parallel leader and the server log */
815  EmitErrorReport();
816 
817  /*
818  * Do we need more cleanup here? For shmem-connected bgworkers, we
819  * will call InitProcess below, which will install ProcKill as exit
820  * callback. That will take care of releasing locks, etc.
821  */
822 
823  /* and go away */
824  proc_exit(1);
825  }
826 
827  /* We can now handle ereport(ERROR) */
828  PG_exception_stack = &local_sigjmp_buf;
829 
830  /*
831  * Create a per-backend PGPROC struct in shared memory, except in the
832  * EXEC_BACKEND case where this was done in SubPostmasterMain. We must do
833  * this before we can use LWLocks (and in the EXEC_BACKEND case we already
834  * had to do some stuff with LWLocks).
835  */
836 #ifndef EXEC_BACKEND
837  InitProcess();
838 #endif
839 
840  /*
841  * Early initialization.
842  */
843  BaseInit();
844 
845  /*
846  * Look up the entry point function, loading its library if necessary.
847  */
849  worker->bgw_function_name);
850 
851  /*
852  * Note that in normal processes, we would call InitPostgres here. For a
853  * worker, however, we don't know what database to connect to, yet; so we
854  * need to wait until the user code does it via
855  * BackgroundWorkerInitializeConnection().
856  */
857 
858  /*
859  * Now invoke the user-defined worker code
860  */
861  entrypt(worker->bgw_main_arg);
862 
863  /* ... and if it returns, we're done */
864  proc_exit(0);
865 }
static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
Definition: bgworker.c:1251
static void bgworker_die(SIGNAL_ARGS)
Definition: bgworker.c:727
void EmitErrorReport(void)
Definition: elog.c:1669
ErrorContextCallback * error_context_stack
Definition: elog.c:95
sigjmp_buf * PG_exception_stack
Definition: elog.c:97
bool IsBackgroundWorker
Definition: globals.c:115
void proc_exit(int code)
Definition: ipc.c:104
@ InitProcessing
Definition: miscadmin.h:399
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:132
#define SetProcessingMode(mode)
Definition: miscadmin.h:411
@ B_BG_WORKER
Definition: miscadmin.h:324
BackendType MyBackendType
Definition: miscinit.c:63
pqsigfunc pqsignal(int signo, pqsigfunc func)
int PostAuthDelay
Definition: postgres.c:95
void FloatExceptionHandler(SIGNAL_ARGS)
Definition: postgres.c:2990
void StatementCancelHandler(SIGNAL_ARGS)
Definition: postgres.c:2969
void BaseInit(void)
Definition: postinit.c:633
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5660
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:639
void init_ps_display(const char *fixed_part)
Definition: ps_status.c:242
void pg_usleep(long microsec)
Definition: signal.c:53
void InitProcess(void)
Definition: proc.c:297
void InitializeTimeouts(void)
Definition: timeout.c:474
#define SIGCHLD
Definition: win32_port.h:186
#define SIGHUP
Definition: win32_port.h:176
#define SIG_DFL
Definition: win32_port.h:171
#define SIGPIPE
Definition: win32_port.h:181
#define SIGUSR2
Definition: win32_port.h:189
#define SIG_IGN
Definition: win32_port.h:173

References B_BG_WORKER, BackgroundWorkerUnblockSignals(), BaseInit(), BackgroundWorker::bgw_flags, BackgroundWorker::bgw_function_name, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_main_arg, BackgroundWorker::bgw_name, BGWORKER_BACKEND_DATABASE_CONNECTION, bgworker_die(), elog(), EmitErrorReport(), error_context_stack, FATAL, FloatExceptionHandler(), HOLD_INTERRUPTS, init_ps_display(), InitializeTimeouts(), InitProcess(), InitProcessing, IsBackgroundWorker, LookupBackgroundWorkerFunction(), MyBackendType, MyBgworkerEntry, PG_exception_stack, pg_usleep(), PostAuthDelay, pqsignal(), proc_exit(), procsignal_sigusr1_handler(), SetProcessingMode, SIG_DFL, SIG_IGN, SIGCHLD, SIGHUP, SIGPIPE, SIGUSR1, SIGUSR2, and StatementCancelHandler().

Referenced by do_start_bgworker().

◆ TerminateBackgroundWorker()

void TerminateBackgroundWorker ( BackgroundWorkerHandle handle)

Definition at line 1210 of file bgworker.c.

1211 {
1212  BackgroundWorkerSlot *slot;
1213  bool signal_postmaster = false;
1214 
1215  Assert(handle->slot < max_worker_processes);
1216  slot = &BackgroundWorkerData->slot[handle->slot];
1217 
1218  /* Set terminate flag in shared memory, unless slot has been reused. */
1219  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
1220  if (handle->generation == slot->generation)
1221  {
1222  slot->terminate = true;
1223  signal_postmaster = true;
1224  }
1225  LWLockRelease(BackgroundWorkerLock);
1226 
1227  /* Make sure the postmaster notices the change to shared memory. */
1228  if (signal_postmaster)
1230 }

References Assert(), BackgroundWorkerData, BackgroundWorkerSlot::generation, BackgroundWorkerHandle::generation, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_worker_processes, PMSIGNAL_BACKGROUND_WORKER_CHANGE, SendPostmasterSignal(), BackgroundWorkerArray::slot, BackgroundWorkerHandle::slot, and BackgroundWorkerSlot::terminate.

Referenced by cleanup_background_workers(), and DestroyParallelContext().

◆ WaitForBackgroundWorkerShutdown()

BgwHandleStatus WaitForBackgroundWorkerShutdown ( BackgroundWorkerHandle handle)

Definition at line 1171 of file bgworker.c.

1172 {
1174  int rc;
1175 
1176  for (;;)
1177  {
1178  pid_t pid;
1179 
1181 
1182  status = GetBackgroundWorkerPid(handle, &pid);
1183  if (status == BGWH_STOPPED)
1184  break;
1185 
1186  rc = WaitLatch(MyLatch,
1189 
1190  if (rc & WL_POSTMASTER_DEATH)
1191  {
1193  break;
1194  }
1195 
1197  }
1198 
1199  return status;
1200 }
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1071
BgwHandleStatus
Definition: bgworker.h:104
@ BGWH_POSTMASTER_DIED
Definition: bgworker.h:108
struct Latch * MyLatch
Definition: globals.c:58
void ResetLatch(Latch *latch)
Definition: latch.c:699
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:492
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
@ WAIT_EVENT_BGWORKER_SHUTDOWN
Definition: wait_event.h:87

References BGWH_POSTMASTER_DIED, BGWH_STOPPED, CHECK_FOR_INTERRUPTS, GetBackgroundWorkerPid(), MyLatch, ResetLatch(), status(), WAIT_EVENT_BGWORKER_SHUTDOWN, WaitLatch(), WL_LATCH_SET, and WL_POSTMASTER_DEATH.

Referenced by apw_start_database_worker(), and WaitForParallelWorkersToExit().

◆ WaitForBackgroundWorkerStartup()

BgwHandleStatus WaitForBackgroundWorkerStartup ( BackgroundWorkerHandle handle,
pid_t *  pidp 
)

Definition at line 1126 of file bgworker.c.

1127 {
1129  int rc;
1130 
1131  for (;;)
1132  {
1133  pid_t pid;
1134 
1136 
1137  status = GetBackgroundWorkerPid(handle, &pid);
1138  if (status == BGWH_STARTED)
1139  *pidp = pid;
1141  break;
1142 
1143  rc = WaitLatch(MyLatch,
1146 
1147  if (rc & WL_POSTMASTER_DEATH)
1148  {
1150  break;
1151  }
1152 
1154  }
1155 
1156  return status;
1157 }
@ WAIT_EVENT_BGWORKER_STARTUP
Definition: wait_event.h:88

References BGWH_NOT_YET_STARTED, BGWH_POSTMASTER_DIED, BGWH_STARTED, CHECK_FOR_INTERRUPTS, GetBackgroundWorkerPid(), MyLatch, ResetLatch(), status(), WAIT_EVENT_BGWORKER_STARTUP, WaitLatch(), WL_LATCH_SET, and WL_POSTMASTER_DEATH.

Referenced by apw_start_leader_worker(), and worker_spi_launch().

Variable Documentation

◆ BackgroundWorkerData

◆ BackgroundWorkerList

◆ fn_addr

Definition at line 119 of file bgworker.c.

Referenced by LookupBackgroundWorkerFunction().

◆ fn_name

const char* fn_name

Definition at line 118 of file bgworker.c.

Referenced by LookupBackgroundWorkerFunction().

◆ 

const { ... } InternalBGWorkers[]
Initial value:
=
{
{
"ParallelWorkerMain", ParallelWorkerMain
},
{
"ApplyLauncherMain", ApplyLauncherMain
},
{
"ApplyWorkerMain", ApplyWorkerMain
},
{
"ParallelApplyWorkerMain", ParallelApplyWorkerMain
}
}
void ParallelApplyWorkerMain(Datum main_arg)
void ParallelWorkerMain(Datum main_arg)
Definition: parallel.c:1262
void ApplyWorkerMain(Datum main_arg)
Definition: worker.c:4473
void ApplyLauncherMain(Datum main_arg)
Definition: launcher.c:1078

Referenced by LookupBackgroundWorkerFunction().