PostgreSQL Source Code  git master
bgworker_internals.h File Reference
#include "datatype/timestamp.h"
#include "lib/ilist.h"
#include "postmaster/bgworker.h"
Include dependency graph for bgworker_internals.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  RegisteredBgWorker
 

Macros

#define MAX_PARALLEL_WORKER_LIMIT   1024
 

Typedefs

typedef struct RegisteredBgWorker RegisteredBgWorker
 

Functions

Size BackgroundWorkerShmemSize (void)
 
void BackgroundWorkerShmemInit (void)
 
void BackgroundWorkerStateChange (bool allow_new_workers)
 
void ForgetBackgroundWorker (slist_mutable_iter *cur)
 
void ReportBackgroundWorkerPID (RegisteredBgWorker *)
 
void ReportBackgroundWorkerExit (slist_mutable_iter *cur)
 
void BackgroundWorkerStopNotifications (pid_t pid)
 
void ForgetUnstartedBackgroundWorkers (void)
 
void ResetBackgroundWorkerCrashTimes (void)
 
void StartBackgroundWorker (void) pg_attribute_noreturn()
 

Variables

slist_head BackgroundWorkerList
 

Macro Definition Documentation

◆ MAX_PARALLEL_WORKER_LIMIT

#define MAX_PARALLEL_WORKER_LIMIT   1024

Definition at line 24 of file bgworker_internals.h.

Referenced by ExecVacuum(), and RegisterDynamicBackgroundWorker().

Typedef Documentation

◆ RegisteredBgWorker

Function Documentation

◆ BackgroundWorkerShmemInit()

void BackgroundWorkerShmemInit ( void  )

Definition at line 158 of file bgworker.c.

References Assert, 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().

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

◆ BackgroundWorkerShmemSize()

Size BackgroundWorkerShmemSize ( void  )

Definition at line 142 of file bgworker.c.

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

Referenced by BackgroundWorkerShmemInit(), and CreateSharedMemoryAndSemaphores().

143 {
144  Size size;
145 
146  /* Array of workers is variably sized. */
147  size = offsetof(BackgroundWorkerArray, slot);
149  sizeof(BackgroundWorkerSlot)));
150 
151  return size;
152 }
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
size_t Size
Definition: c.h:540
int max_worker_processes
Definition: globals.c:137
#define offsetof(type, field)
Definition: c.h:727

◆ BackgroundWorkerStateChange()

void BackgroundWorkerStateChange ( bool  allow_new_workers)

Definition at line 242 of file bgworker.c.

References ascii_safe_strlcpy(), 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 sigusr1_handler().

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

◆ BackgroundWorkerStopNotifications()

void BackgroundWorkerStopNotifications ( pid_t  pid)

Definition at line 503 of file bgworker.c.

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

Referenced by CleanupBackend(), and CleanupBackgroundWorker().

504 {
505  slist_iter siter;
506 
508  {
509  RegisteredBgWorker *rw;
510 
511  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
512  if (rw->rw_worker.bgw_notify_pid == pid)
513  rw->rw_worker.bgw_notify_pid = 0;
514  }
515 }
slist_node * cur
Definition: ilist.h:226
slist_head BackgroundWorkerList
Definition: bgworker.c:42
#define slist_container(type, membername, ptr)
Definition: ilist.h:693
#define slist_foreach(iter, lhead)
Definition: ilist.h:719
pid_t bgw_notify_pid
Definition: bgworker.h:99
BackgroundWorker rw_worker

◆ ForgetBackgroundWorker()

void ForgetBackgroundWorker ( slist_mutable_iter cur)

Definition at line 422 of file bgworker.c.

References Assert, BackgroundWorker::bgw_flags, BackgroundWorker::bgw_name, BGWORKER_CLASS_PARALLEL, slist_mutable_iter::cur, DEBUG1, ereport, errmsg_internal(), free, BackgroundWorkerSlot::in_use, max_worker_processes, BackgroundWorkerArray::parallel_terminate_count, 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().

423 {
424  RegisteredBgWorker *rw;
425  BackgroundWorkerSlot *slot;
426 
427  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
428 
433 
434  slot->in_use = false;
435 
436  ereport(DEBUG1,
437  (errmsg_internal("unregistering background worker \"%s\"",
438  rw->rw_worker.bgw_name)));
439 
441  free(rw);
442 }
#define DEBUG1
Definition: elog.h:25
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
slist_node * cur
Definition: ilist.h:241
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
#define slist_container(type, membername, ptr)
Definition: ilist.h:693
#define ereport(elevel,...)
Definition: elog.h:157
#define free(a)
Definition: header.h:65
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define Assert(condition)
Definition: c.h:804
uint32 parallel_terminate_count
Definition: bgworker.c:100
int max_worker_processes
Definition: globals.c:137
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:671
BackgroundWorker rw_worker
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:101

◆ ForgetUnstartedBackgroundWorkers()

void ForgetUnstartedBackgroundWorkers ( void  )

Definition at line 530 of file bgworker.c.

References Assert, 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().

531 {
532  slist_mutable_iter iter;
533 
535  {
536  RegisteredBgWorker *rw;
537  BackgroundWorkerSlot *slot;
538 
539  rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
542 
543  /* If it's not yet started, and there's someone waiting ... */
544  if (slot->pid == InvalidPid &&
545  rw->rw_worker.bgw_notify_pid != 0)
546  {
547  /* ... then zap it, and notify the waiter */
548  int notify_pid = rw->rw_worker.bgw_notify_pid;
549 
550  ForgetBackgroundWorker(&iter);
551  if (notify_pid != 0)
552  kill(notify_pid, SIGUSR1);
553  }
554  }
555 }
#define SIGUSR1
Definition: win32_port.h:171
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
slist_node * cur
Definition: ilist.h:241
#define kill(pid, sig)
Definition: win32_port.h:454
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:735
slist_head BackgroundWorkerList
Definition: bgworker.c:42
void ForgetBackgroundWorker(slist_mutable_iter *cur)
Definition: bgworker.c:422
#define slist_container(type, membername, ptr)
Definition: ilist.h:693
#define Assert(condition)
Definition: c.h:804
pid_t bgw_notify_pid
Definition: bgworker.h:99
int max_worker_processes
Definition: globals.c:137
BackgroundWorker rw_worker
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:101
#define InvalidPid
Definition: miscadmin.h:32

◆ ReportBackgroundWorkerExit()

void ReportBackgroundWorkerExit ( slist_mutable_iter cur)

Definition at line 469 of file bgworker.c.

References Assert, BGW_NEVER_RESTART, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, slist_mutable_iter::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().

470 {
471  RegisteredBgWorker *rw;
472  BackgroundWorkerSlot *slot;
473  int notify_pid;
474 
475  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
476 
479  slot->pid = rw->rw_pid;
480  notify_pid = rw->rw_worker.bgw_notify_pid;
481 
482  /*
483  * If this worker is slated for deregistration, do that before notifying
484  * the process which started it. Otherwise, if that process tries to
485  * reuse the slot immediately, it might not be available yet. In theory
486  * that could happen anyway if the process checks slot->pid at just the
487  * wrong moment, but this makes the window narrower.
488  */
489  if (rw->rw_terminate ||
492 
493  if (notify_pid != 0)
494  kill(notify_pid, SIGUSR1);
495 }
#define SIGUSR1
Definition: win32_port.h:171
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
slist_node * cur
Definition: ilist.h:241
int bgw_restart_time
Definition: bgworker.h:94
#define kill(pid, sig)
Definition: win32_port.h:454
void ForgetBackgroundWorker(slist_mutable_iter *cur)
Definition: bgworker.c:422
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
#define slist_container(type, membername, ptr)
Definition: ilist.h:693
#define Assert(condition)
Definition: c.h:804
pid_t bgw_notify_pid
Definition: bgworker.h:99
int max_worker_processes
Definition: globals.c:137
BackgroundWorker rw_worker
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:101

◆ ReportBackgroundWorkerPID()

void ReportBackgroundWorkerPID ( RegisteredBgWorker )

Definition at line 450 of file bgworker.c.

References Assert, BackgroundWorker::bgw_notify_pid, kill, max_worker_processes, BackgroundWorkerSlot::pid, RegisteredBgWorker::rw_pid, RegisteredBgWorker::rw_shmem_slot, RegisteredBgWorker::rw_worker, SIGUSR1, and BackgroundWorkerArray::slot.

Referenced by BackgroundWorkerStateChange(), and do_start_bgworker().

451 {
452  BackgroundWorkerSlot *slot;
453 
454  Assert(rw->rw_shmem_slot < max_worker_processes);
455  slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
456  slot->pid = rw->rw_pid;
457 
458  if (rw->rw_worker.bgw_notify_pid != 0)
459  kill(rw->rw_worker.bgw_notify_pid, SIGUSR1);
460 }
#define SIGUSR1
Definition: win32_port.h:171
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
#define kill(pid, sig)
Definition: win32_port.h:454
#define Assert(condition)
Definition: c.h:804
int max_worker_processes
Definition: globals.c:137
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:101

◆ ResetBackgroundWorkerCrashTimes()

void ResetBackgroundWorkerCrashTimes ( void  )

Definition at line 568 of file bgworker.c.

References Assert, BackgroundWorker::bgw_flags, BGW_NEVER_RESTART, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, BGWORKER_CLASS_PARALLEL, slist_mutable_iter::cur, ForgetBackgroundWorker(), BackgroundWorkerSlot::in_use, RegisteredBgWorker::rw_crashed_at, RegisteredBgWorker::rw_worker, slist_container, slist_foreach_modify, BackgroundWorkerArray::slot, and BackgroundWorkerSlot::worker.

Referenced by PostmasterStateMachine().

569 {
570  slist_mutable_iter iter;
571 
573  {
574  RegisteredBgWorker *rw;
575 
576  rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
577 
579  {
580  /*
581  * Workers marked BGW_NEVER_RESTART shouldn't get relaunched after
582  * the crash, so forget about them. (If we wait until after the
583  * crash to forget about them, and they are parallel workers,
584  * parallel_terminate_count will get incremented after we've
585  * already zeroed parallel_register_count, which would be bad.)
586  */
587  ForgetBackgroundWorker(&iter);
588  }
589  else
590  {
591  /*
592  * The accounting which we do via parallel_register_count and
593  * parallel_terminate_count would get messed up if a worker marked
594  * parallel could survive a crash and restart cycle. All such
595  * workers should be marked BGW_NEVER_RESTART, and thus control
596  * should never reach this branch.
597  */
599 
600  /*
601  * Allow this worker to be restarted immediately after we finish
602  * resetting.
603  */
604  rw->rw_crashed_at = 0;
605 
606  /*
607  * If there was anyone waiting for it, they're history.
608  */
609  rw->rw_worker.bgw_notify_pid = 0;
610  }
611  }
612 }
slist_node * cur
Definition: ilist.h:241
int bgw_restart_time
Definition: bgworker.h:94
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:735
slist_head BackgroundWorkerList
Definition: bgworker.c:42
void ForgetBackgroundWorker(slist_mutable_iter *cur)
Definition: bgworker.c:422
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
#define slist_container(type, membername, ptr)
Definition: ilist.h:693
#define Assert(condition)
Definition: c.h:804
pid_t bgw_notify_pid
Definition: bgworker.h:99
BackgroundWorker rw_worker

◆ StartBackgroundWorker()

void StartBackgroundWorker ( void  )

Definition at line 723 of file bgworker.c.

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(), BGWORKER_SHMEM_ACCESS, dsm_detach_all(), elog, EmitErrorReport(), error_context_stack, FATAL, FloatExceptionHandler(), HOLD_INTERRUPTS, init_ps_display(), InitializeTimeouts(), InitProcess(), InitProcessing, IsBackgroundWorker, LookupBackgroundWorkerFunction(), MyBackendType, MyBgworkerEntry, PG_exception_stack, pg_usleep(), PGSharedMemoryDetach(), PostAuthDelay, pqsignal(), proc_exit(), procsignal_sigusr1_handler(), SetProcessingMode, ShutdownLatchSupport(), SIG_DFL, SIG_IGN, SIGCHLD, SIGHUP, SIGPIPE, SIGUSR1, SIGUSR2, StatementCancelHandler(), and BackgroundWorkerSlot::worker.

Referenced by BackendRun(), and do_start_bgworker().

724 {
725  sigjmp_buf local_sigjmp_buf;
727  bgworker_main_type entrypt;
728 
729  if (worker == NULL)
730  elog(FATAL, "unable to find bgworker entry");
731 
732  IsBackgroundWorker = true;
733 
735  init_ps_display(worker->bgw_name);
736 
737  /*
738  * If we're not supposed to have shared memory access, then detach from
739  * shared memory. If we didn't request shared memory access, the
740  * postmaster won't force a cluster-wide restart if we exit unexpectedly,
741  * so we'd better make sure that we don't mess anything up that would
742  * require that sort of cleanup.
743  */
744  if ((worker->bgw_flags & BGWORKER_SHMEM_ACCESS) == 0)
745  {
747  dsm_detach_all();
749  }
750 
752 
753  /* Apply PostAuthDelay */
754  if (PostAuthDelay > 0)
755  pg_usleep(PostAuthDelay * 1000000L);
756 
757  /*
758  * Set up signal handlers.
759  */
761  {
762  /*
763  * SIGINT is used to signal canceling the current action
764  */
768 
769  /* XXX Any other handlers needed here? */
770  }
771  else
772  {
773  pqsignal(SIGINT, SIG_IGN);
775  pqsignal(SIGFPE, SIG_IGN);
776  }
777  pqsignal(SIGTERM, bgworker_die);
778  /* SIGQUIT handler was already set up by InitPostmasterChild */
780 
781  InitializeTimeouts(); /* establishes SIGALRM handler */
782 
786 
787  /*
788  * If an exception is encountered, processing resumes here.
789  *
790  * We just need to clean up, report the error, and go away.
791  */
792  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
793  {
794  /* Since not using PG_TRY, must reset error stack by hand */
795  error_context_stack = NULL;
796 
797  /* Prevent interrupts while cleaning up */
798  HOLD_INTERRUPTS();
799 
800  /*
801  * sigsetjmp will have blocked all signals, but we may need to accept
802  * signals while communicating with our parallel leader. Once we've
803  * done HOLD_INTERRUPTS() it should be safe to unblock signals.
804  */
806 
807  /* Report the error to the parallel leader and the server log */
808  EmitErrorReport();
809 
810  /*
811  * Do we need more cleanup here? For shmem-connected bgworkers, we
812  * will call InitProcess below, which will install ProcKill as exit
813  * callback. That will take care of releasing locks, etc.
814  */
815 
816  /* and go away */
817  proc_exit(1);
818  }
819 
820  /* We can now handle ereport(ERROR) */
821  PG_exception_stack = &local_sigjmp_buf;
822 
823  /*
824  * If the background worker request shared memory access, set that up now;
825  * else, detach all shared memory segments.
826  */
827  if (worker->bgw_flags & BGWORKER_SHMEM_ACCESS)
828  {
829  /*
830  * Early initialization. Some of this could be useful even for
831  * background workers that aren't using shared memory, but they can
832  * call the individual startup routines for those subsystems if
833  * needed.
834  */
835  BaseInit();
836 
837  /*
838  * Create a per-backend PGPROC struct in shared memory, except in the
839  * EXEC_BACKEND case where this was done in SubPostmasterMain. We must
840  * do this before we can use LWLocks (and in the EXEC_BACKEND case we
841  * already had to do some stuff with LWLocks).
842  */
843 #ifndef EXEC_BACKEND
844  InitProcess();
845 #endif
846  }
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 }
void InitializeTimeouts(void)
Definition: timeout.c:435
#define SIGUSR1
Definition: win32_port.h:171
BackendType MyBackendType
Definition: miscinit.c:62
#define SIGCHLD
Definition: win32_port.h:169
void proc_exit(int code)
Definition: ipc.c:104
#define SetProcessingMode(mode)
Definition: miscadmin.h:400
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:194
void BaseInit(void)
Definition: postinit.c:537
#define SIGPIPE
Definition: win32_port.h:164
#define SIGUSR2
Definition: win32_port.h:172
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:96
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:909
void StatementCancelHandler(SIGNAL_ARGS)
Definition: postgres.c:2949
bool IsBackgroundWorker
Definition: globals.c:114
ErrorContextCallback * error_context_stack
Definition: elog.c:93
void pg_usleep(long microsec)
Definition: signal.c:53
Datum bgw_main_arg
Definition: bgworker.h:97
#define FATAL
Definition: elog.h:49
void InitProcess(void)
Definition: proc.c:303
#define SIGHUP
Definition: win32_port.h:159
static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
Definition: bgworker.c:1254
#define SIG_IGN
Definition: win32_port.h:156
int PostAuthDelay
Definition: postgres.c:103
void EmitErrorReport(void)
Definition: elog.c:1509
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
#define SIG_DFL
Definition: win32_port.h:154
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
void dsm_detach_all(void)
Definition: dsm.c:741
sigjmp_buf * PG_exception_stack
Definition: elog.c:95
void ShutdownLatchSupport(void)
Definition: latch.c:308
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:119
#define elog(elevel,...)
Definition: elog.h:232
void FloatExceptionHandler(SIGNAL_ARGS)
Definition: postgres.c:2970
static void bgworker_die(SIGNAL_ARGS)
Definition: bgworker.c:706
void init_ps_display(const char *fixed_part)
Definition: ps_status.c:258
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:642
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:95
void(* bgworker_main_type)(Datum main_arg)
Definition: bgworker.h:71
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5723

Variable Documentation

◆ BackgroundWorkerList

slist_head BackgroundWorkerList