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 BackgroundWorkerMain (void) pg_attribute_noreturn()
 

Variables

PGDLLIMPORT 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.

Typedef Documentation

◆ RegisteredBgWorker

Function Documentation

◆ BackgroundWorkerMain()

void BackgroundWorkerMain ( void  )

Definition at line 726 of file bgworker.c.

727 {
728  sigjmp_buf local_sigjmp_buf;
730  bgworker_main_type entrypt;
731 
732  if (worker == NULL)
733  elog(FATAL, "unable to find bgworker entry");
734 
735  IsBackgroundWorker = true;
736 
738  init_ps_display(worker->bgw_name);
739 
741 
742  /* Apply PostAuthDelay */
743  if (PostAuthDelay > 0)
744  pg_usleep(PostAuthDelay * 1000000L);
745 
746  /*
747  * Set up signal handlers.
748  */
750  {
751  /*
752  * SIGINT is used to signal canceling the current action
753  */
757 
758  /* XXX Any other handlers needed here? */
759  }
760  else
761  {
762  pqsignal(SIGINT, SIG_IGN);
764  pqsignal(SIGFPE, SIG_IGN);
765  }
766  pqsignal(SIGTERM, bgworker_die);
767  /* SIGQUIT handler was already set up by InitPostmasterChild */
769 
770  InitializeTimeouts(); /* establishes SIGALRM handler */
771 
775 
776  /*
777  * If an exception is encountered, processing resumes here.
778  *
779  * We just need to clean up, report the error, and go away.
780  */
781  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
782  {
783  /* Since not using PG_TRY, must reset error stack by hand */
784  error_context_stack = NULL;
785 
786  /* Prevent interrupts while cleaning up */
787  HOLD_INTERRUPTS();
788 
789  /*
790  * sigsetjmp will have blocked all signals, but we may need to accept
791  * signals while communicating with our parallel leader. Once we've
792  * done HOLD_INTERRUPTS() it should be safe to unblock signals.
793  */
795 
796  /* Report the error to the parallel leader and the server log */
797  EmitErrorReport();
798 
799  /*
800  * Do we need more cleanup here? For shmem-connected bgworkers, we
801  * will call InitProcess below, which will install ProcKill as exit
802  * callback. That will take care of releasing locks, etc.
803  */
804 
805  /* and go away */
806  proc_exit(1);
807  }
808 
809  /* We can now handle ereport(ERROR) */
810  PG_exception_stack = &local_sigjmp_buf;
811 
812  /*
813  * Create a per-backend PGPROC struct in shared memory. We must do this
814  * before we can use LWLocks or access any shared memory.
815  */
816  InitProcess();
817 
818  /*
819  * Early initialization.
820  */
821  BaseInit();
822 
823  /*
824  * Look up the entry point function, loading its library if necessary.
825  */
827  worker->bgw_function_name);
828 
829  /*
830  * Note that in normal processes, we would call InitPostgres here. For a
831  * worker, however, we don't know what database to connect to, yet; so we
832  * need to wait until the user code does it via
833  * BackgroundWorkerInitializeConnection().
834  */
835 
836  /*
837  * Now invoke the user-defined worker code
838  */
839  entrypt(worker->bgw_main_arg);
840 
841  /* ... and if it returns, we're done */
842  proc_exit(0);
843 }
static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
Definition: bgworker.c:1253
static void bgworker_die(SIGNAL_ARGS)
Definition: bgworker.c:712
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:60
void(* bgworker_main_type)(Datum main_arg)
Definition: bgworker.h:72
void EmitErrorReport(void)
Definition: elog.c:1669
ErrorContextCallback * error_context_stack
Definition: elog.c:95
sigjmp_buf * PG_exception_stack
Definition: elog.c:97
#define FATAL
Definition: elog.h:41
bool IsBackgroundWorker
Definition: globals.c:117
void proc_exit(int code)
Definition: ipc.c:104
@ InitProcessing
Definition: miscadmin.h:411
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:132
#define SetProcessingMode(mode)
Definition: miscadmin.h:423
@ B_BG_WORKER
Definition: miscadmin.h:335
BackendType MyBackendType
Definition: miscinit.c:63
pqsigfunc pqsignal(int signo, pqsigfunc func)
int PostAuthDelay
Definition: postgres.c:100
void FloatExceptionHandler(SIGNAL_ARGS)
Definition: postgres.c:3026
void StatementCancelHandler(SIGNAL_ARGS)
Definition: postgres.c:3005
void BaseInit(void)
Definition: postinit.c:629
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5601
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:194
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
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
char bgw_library_name[MAXPGPATH]
Definition: bgworker.h:96
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 SIGUSR1
Definition: win32_port.h:180
#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().

◆ BackgroundWorkerShmemInit()

void BackgroundWorkerShmemInit ( void  )

Definition at line 165 of file bgworker.c.

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

◆ BackgroundWorkerShmemSize()

Size BackgroundWorkerShmemSize ( void  )

Definition at line 149 of file bgworker.c.

150 {
151  Size size;
152 
153  /* Array of workers is variably sized. */
154  size = offsetof(BackgroundWorkerArray, slot);
156  sizeof(BackgroundWorkerSlot)));
157 
158  return size;
159 }
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 249 of file bgworker.c.

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

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, max_worker_processes, MAXPGPATH, MCXT_ALLOC_NO_OOM, MCXT_ALLOC_ZERO, MemoryContextAllocExtended(), BackgroundWorkerArray::parallel_terminate_count, pg_memory_barrier, pg_read_barrier, BackgroundWorkerSlot::pid, PostmasterContext, 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 523 of file bgworker.c.

524 {
525  slist_iter siter;
526 
528  {
529  RegisteredBgWorker *rw;
530 
531  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
532  if (rw->rw_worker.bgw_notify_pid == pid)
533  rw->rw_worker.bgw_notify_pid = 0;
534  }
535 }

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

Referenced by CleanupBackend(), and CleanupBackgroundWorker().

◆ ForgetBackgroundWorker()

void ForgetBackgroundWorker ( slist_mutable_iter cur)

Definition at line 435 of file bgworker.c.

436 {
437  RegisteredBgWorker *rw;
438  BackgroundWorkerSlot *slot;
439 
440  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
441 
444  Assert(slot->in_use);
445 
446  /*
447  * We need a memory barrier here to make sure that the update of
448  * parallel_terminate_count completes before the store to in_use.
449  */
452 
454  slot->in_use = false;
455 
456  ereport(DEBUG1,
457  (errmsg_internal("unregistering background worker \"%s\"",
458  rw->rw_worker.bgw_name)));
459 
461  pfree(rw);
462 }
struct cursor * cur
Definition: ecpg.c:28
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:1084
void pfree(void *pointer)
Definition: mcxt.c:1456

References Assert(), BackgroundWorkerData, BackgroundWorker::bgw_flags, BackgroundWorker::bgw_name, BGWORKER_CLASS_PARALLEL, cur, DEBUG1, ereport, errmsg_internal(), BackgroundWorkerSlot::in_use, max_worker_processes, BackgroundWorkerArray::parallel_terminate_count, pfree(), 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 550 of file bgworker.c.

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

◆ ReportBackgroundWorkerExit()

void ReportBackgroundWorkerExit ( slist_mutable_iter cur)

Definition at line 489 of file bgworker.c.

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

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

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().

Variable Documentation

◆ BackgroundWorkerList