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 164 of file bgworker.c.

165 {
166  bool found;
167 
168  BackgroundWorkerData = ShmemInitStruct("Background Worker Data",
170  &found);
171  if (!IsUnderPostmaster)
172  {
173  slist_iter siter;
174  int slotno = 0;
175 
179 
180  /*
181  * Copy contents of worker list into shared memory. Record the shared
182  * memory slot assigned to each worker. This ensures a 1-to-1
183  * correspondence between the postmaster's private list and the array
184  * in shared memory.
185  */
187  {
189  RegisteredBgWorker *rw;
190 
191  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
192  Assert(slotno < max_worker_processes);
193  slot->in_use = true;
194  slot->terminate = false;
195  slot->pid = InvalidPid;
196  slot->generation = 0;
197  rw->rw_shmem_slot = slotno;
198  rw->rw_worker.bgw_notify_pid = 0; /* might be reinit after crash */
199  memcpy(&slot->worker, &rw->rw_worker, sizeof(BackgroundWorker));
200  ++slotno;
201  }
202 
203  /*
204  * Mark any remaining slots as not in use.
205  */
206  while (slotno < max_worker_processes)
207  {
209 
210  slot->in_use = false;
211  ++slotno;
212  }
213  }
214  else
215  Assert(found);
216 }
slist_head BackgroundWorkerList
Definition: bgworker.c:42
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:148
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 148 of file bgworker.c.

149 {
150  Size size;
151 
152  /* Array of workers is variably sized. */
153  size = offsetof(BackgroundWorkerArray, slot);
155  sizeof(BackgroundWorkerSlot)));
156 
157  return size;
158 }
size_t Size
Definition: c.h:594
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 248 of file bgworker.c.

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

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, MAXPGPATH, 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 520 of file bgworker.c.

521 {
522  slist_iter siter;
523 
525  {
526  RegisteredBgWorker *rw;
527 
528  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
529  if (rw->rw_worker.bgw_notify_pid == pid)
530  rw->rw_worker.bgw_notify_pid = 0;
531  }
532 }

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 730 of file bgworker.c.

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

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

Referenced by StartBackgroundWorker().

◆ FindRegisteredWorkerBySlotNumber()

static RegisteredBgWorker* FindRegisteredWorkerBySlotNumber ( int  slotno)
static

Definition at line 223 of file bgworker.c.

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

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 432 of file bgworker.c.

433 {
434  RegisteredBgWorker *rw;
435  BackgroundWorkerSlot *slot;
436 
437  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
438 
441  Assert(slot->in_use);
442 
443  /*
444  * We need a memory barrier here to make sure that the update of
445  * parallel_terminate_count completes before the store to in_use.
446  */
449 
451  slot->in_use = false;
452 
453  ereport(DEBUG1,
454  (errmsg_internal("unregistering background worker \"%s\"",
455  rw->rw_worker.bgw_name)));
456 
458  free(rw);
459 }
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 547 of file bgworker.c.

548 {
549  slist_mutable_iter iter;
550 
552  {
553  RegisteredBgWorker *rw;
554  BackgroundWorkerSlot *slot;
555 
556  rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
559 
560  /* If it's not yet started, and there's someone waiting ... */
561  if (slot->pid == InvalidPid &&
562  rw->rw_worker.bgw_notify_pid != 0)
563  {
564  /* ... then zap it, and notify the waiter */
565  int notify_pid = rw->rw_worker.bgw_notify_pid;
566 
567  ForgetBackgroundWorker(&iter);
568  if (notify_pid != 0)
569  kill(notify_pid, SIGUSR1);
570  }
571  }
572 }
void ForgetBackgroundWorker(slist_mutable_iter *cur)
Definition: bgworker.c:432
#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 1074 of file bgworker.c.

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

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 1288 of file bgworker.c.

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

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 1254 of file bgworker.c.

1255 {
1256  /*
1257  * If the function is to be loaded from postgres itself, search the
1258  * InternalBGWorkers array.
1259  */
1260  if (strcmp(libraryname, "postgres") == 0)
1261  {
1262  int i;
1263 
1264  for (i = 0; i < lengthof(InternalBGWorkers); i++)
1265  {
1266  if (strcmp(InternalBGWorkers[i].fn_name, funcname) == 0)
1267  return InternalBGWorkers[i].fn_addr;
1268  }
1269 
1270  /* We can only reach this by programming error. */
1271  elog(ERROR, "internal function \"%s\" not found", funcname);
1272  }
1273 
1274  /* Otherwise load from external library. */
1275  return (bgworker_main_type)
1276  load_external_function(libraryname, funcname, true, NULL);
1277 }
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:777
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 878 of file bgworker.c.

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

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 962 of file bgworker.c.

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

487 {
488  RegisteredBgWorker *rw;
489  BackgroundWorkerSlot *slot;
490  int notify_pid;
491 
492  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
493 
496  slot->pid = rw->rw_pid;
497  notify_pid = rw->rw_worker.bgw_notify_pid;
498 
499  /*
500  * If this worker is slated for deregistration, do that before notifying
501  * the process which started it. Otherwise, if that process tries to
502  * reuse the slot immediately, it might not be available yet. In theory
503  * that could happen anyway if the process checks slot->pid at just the
504  * wrong moment, but this makes the window narrower.
505  */
506  if (rw->rw_terminate ||
509 
510  if (notify_pid != 0)
511  kill(notify_pid, SIGUSR1);
512 }
#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 585 of file bgworker.c.

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

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 658 of file bgworker.c.

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  if (!(worker->bgw_flags & BGWORKER_SHMEM_ACCESS))
669  {
670  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  return false;
675  }
676 
678  {
680  {
681  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  return false;
686  }
687 
688  /* XXX other checks? */
689  }
690 
691  if ((worker->bgw_restart_time < 0 &&
692  worker->bgw_restart_time != BGW_NEVER_RESTART) ||
693  (worker->bgw_restart_time > USECS_PER_DAY / 1000))
694  {
695  ereport(elevel,
696  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
697  errmsg("background worker \"%s\": invalid restart interval",
698  worker->bgw_name)));
699  return false;
700  }
701 
702  /*
703  * Parallel workers may not be configured for restart, because the
704  * parallel_register_count/parallel_terminate_count accounting can't
705  * handle parallel workers lasting through a crash-and-restart cycle.
706  */
707  if (worker->bgw_restart_time != BGW_NEVER_RESTART &&
708  (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
709  {
710  ereport(elevel,
711  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
712  errmsg("background worker \"%s\": parallel workers may not be configured for restart",
713  worker->bgw_name)));
714  return false;
715  }
716 
717  /*
718  * If bgw_type is not filled in, use bgw_name.
719  */
720  if (strcmp(worker->bgw_type, "") == 0)
721  strcpy(worker->bgw_type, worker->bgw_name);
722 
723  return true;
724 }
@ 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 747 of file bgworker.c.

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

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 1213 of file bgworker.c.

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

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 1174 of file bgworker.c.

1175 {
1176  BgwHandleStatus status;
1177  int rc;
1178 
1179  for (;;)
1180  {
1181  pid_t pid;
1182 
1184 
1185  status = GetBackgroundWorkerPid(handle, &pid);
1186  if (status == BGWH_STOPPED)
1187  break;
1188 
1189  rc = WaitLatch(MyLatch,
1191  WAIT_EVENT_BGWORKER_SHUTDOWN);
1192 
1193  if (rc & WL_POSTMASTER_DEATH)
1194  {
1195  status = BGWH_POSTMASTER_DIED;
1196  break;
1197  }
1198 
1200  }
1201 
1202  return status;
1203 }
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1074
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:697
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:490
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121

References BGWH_POSTMASTER_DIED, BGWH_STOPPED, CHECK_FOR_INTERRUPTS, GetBackgroundWorkerPid(), MyLatch, ResetLatch(), 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 1129 of file bgworker.c.

1130 {
1131  BgwHandleStatus status;
1132  int rc;
1133 
1134  for (;;)
1135  {
1136  pid_t pid;
1137 
1139 
1140  status = GetBackgroundWorkerPid(handle, &pid);
1141  if (status == BGWH_STARTED)
1142  *pidp = pid;
1143  if (status != BGWH_NOT_YET_STARTED)
1144  break;
1145 
1146  rc = WaitLatch(MyLatch,
1148  WAIT_EVENT_BGWORKER_STARTUP);
1149 
1150  if (rc & WL_POSTMASTER_DEATH)
1151  {
1152  status = BGWH_POSTMASTER_DIED;
1153  break;
1154  }
1155 
1157  }
1158 
1159  return status;
1160 }

References BGWH_NOT_YET_STARTED, BGWH_POSTMASTER_DIED, BGWH_STARTED, CHECK_FOR_INTERRUPTS, GetBackgroundWorkerPid(), MyLatch, ResetLatch(), 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
},
{
"TablesyncWorkerMain", TablesyncWorkerMain
}
}
void ParallelApplyWorkerMain(Datum main_arg)
void ParallelWorkerMain(Datum main_arg)
Definition: parallel.c:1280
void ApplyWorkerMain(Datum main_arg)
Definition: worker.c:4685
void ApplyLauncherMain(Datum main_arg)
Definition: launcher.c:1121
void TablesyncWorkerMain(Datum main_arg)
Definition: tablesync.c:1677

Referenced by LookupBackgroundWorkerFunction().