PostgreSQL Source Code  git master
bgworker.c
Go to the documentation of this file.
1 /*--------------------------------------------------------------------
2  * bgworker.c
3  * POSTGRES pluggable background workers implementation
4  *
5  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * src/backend/postmaster/bgworker.c
9  *
10  *-------------------------------------------------------------------------
11  */
12 
13 #include "postgres.h"
14 
15 #include "access/parallel.h"
16 #include "libpq/pqsignal.h"
17 #include "miscadmin.h"
18 #include "pgstat.h"
19 #include "port/atomics.h"
21 #include "postmaster/interrupt.h"
22 #include "postmaster/postmaster.h"
25 #include "storage/dsm.h"
26 #include "storage/ipc.h"
27 #include "storage/latch.h"
28 #include "storage/lwlock.h"
29 #include "storage/pg_shmem.h"
30 #include "storage/pmsignal.h"
31 #include "storage/proc.h"
32 #include "storage/procsignal.h"
33 #include "storage/shmem.h"
34 #include "tcop/tcopprot.h"
35 #include "utils/ascii.h"
36 #include "utils/ps_status.h"
37 #include "utils/timeout.h"
38 
39 /*
40  * The postmaster's list of registered background workers, in private memory.
41  */
43 
44 /*
45  * BackgroundWorkerSlots exist in shared memory and can be accessed (via
46  * the BackgroundWorkerArray) by both the postmaster and by regular backends.
47  * However, the postmaster cannot take locks, even spinlocks, because this
48  * might allow it to crash or become wedged if shared memory gets corrupted.
49  * Such an outcome is intolerable. Therefore, we need a lockless protocol
50  * for coordinating access to this data.
51  *
52  * The 'in_use' flag is used to hand off responsibility for the slot between
53  * the postmaster and the rest of the system. When 'in_use' is false,
54  * the postmaster will ignore the slot entirely, except for the 'in_use' flag
55  * itself, which it may read. In this state, regular backends may modify the
56  * slot. Once a backend sets 'in_use' to true, the slot becomes the
57  * responsibility of the postmaster. Regular backends may no longer modify it,
58  * but the postmaster may examine it. Thus, a backend initializing a slot
59  * must fully initialize the slot - and insert a write memory barrier - before
60  * marking it as in use.
61  *
62  * As an exception, however, even when the slot is in use, regular backends
63  * may set the 'terminate' flag for a slot, telling the postmaster not
64  * to restart it. Once the background worker is no longer running, the slot
65  * will be released for reuse.
66  *
67  * In addition to coordinating with the postmaster, backends modifying this
68  * data structure must coordinate with each other. Since they can take locks,
69  * this is straightforward: any backend wishing to manipulate a slot must
70  * take BackgroundWorkerLock in exclusive mode. Backends wishing to read
71  * data that might get concurrently modified by other backends should take
72  * this lock in shared mode. No matter what, backends reading this data
73  * structure must be able to tolerate concurrent modifications by the
74  * postmaster.
75  */
76 typedef struct BackgroundWorkerSlot
77 {
78  bool in_use;
79  bool terminate;
80  pid_t pid; /* InvalidPid = not started yet; 0 = dead */
81  uint64 generation; /* incremented when slot is recycled */
84 
85 /*
86  * In order to limit the total number of parallel workers (according to
87  * max_parallel_workers GUC), we maintain the number of active parallel
88  * workers. Since the postmaster cannot take locks, two variables are used for
89  * this purpose: the number of registered parallel workers (modified by the
90  * backends, protected by BackgroundWorkerLock) and the number of terminated
91  * parallel workers (modified only by the postmaster, lockless). The active
92  * number of parallel workers is the number of registered workers minus the
93  * terminated ones. These counters can of course overflow, but it's not
94  * important here since the subtraction will still give the right number.
95  */
96 typedef struct BackgroundWorkerArray
97 {
103 
105 {
106  int slot;
107  uint64 generation;
108 };
109 
111 
112 /*
113  * List of internal background worker entry points. We need this for
114  * reasons explained in LookupBackgroundWorkerFunction(), below.
115  */
116 static const struct
117 {
118  const char *fn_name;
120 } InternalBGWorkers[] =
121 
122 {
123  {
124  "ParallelWorkerMain", ParallelWorkerMain
125  },
126  {
127  "ApplyLauncherMain", ApplyLauncherMain
128  },
129  {
130  "ApplyWorkerMain", ApplyWorkerMain
131  }
132 };
133 
134 /* Private functions. */
135 static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname);
136 
137 
138 /*
139  * Calculate shared memory needed.
140  */
141 Size
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 }
153 
154 /*
155  * Initialize shared memory.
156  */
157 void
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 
170  BackgroundWorkerData->total_slots = max_worker_processes;
171  BackgroundWorkerData->parallel_register_count = 0;
172  BackgroundWorkerData->parallel_terminate_count = 0;
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  */
180  slist_foreach(siter, &BackgroundWorkerList)
181  {
182  BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
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  {
202  BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
203 
204  slot->in_use = false;
205  ++slotno;
206  }
207  }
208  else
209  Assert(found);
210 }
211 
212 /*
213  * Search the postmaster's backend-private list of RegisteredBgWorker objects
214  * for the one that maps to the given slot number.
215  */
216 static RegisteredBgWorker *
218 {
219  slist_iter siter;
220 
221  slist_foreach(siter, &BackgroundWorkerList)
222  {
223  RegisteredBgWorker *rw;
224 
225  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
226  if (rw->rw_shmem_slot == slotno)
227  return rw;
228  }
229 
230  return NULL;
231 }
232 
233 /*
234  * Notice changes to shared memory made by other backends.
235  * Accept new worker requests only if allow_new_workers is true.
236  *
237  * This code runs in the postmaster, so we must be very careful not to assume
238  * that shared memory contents are sane. Otherwise, a rogue backend could
239  * take out the postmaster.
240  */
241 void
242 BackgroundWorkerStateChange(bool allow_new_workers)
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  */
253  if (max_worker_processes != BackgroundWorkerData->total_slots)
254  {
255  ereport(LOG,
256  (errmsg("inconsistent background worker state (max_worker_processes=%d, total_slots=%d)",
258  BackgroundWorkerData->total_slots)));
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  {
268  BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
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)
329  BackgroundWorkerData->parallel_terminate_count++;
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 
408  slist_push_head(&BackgroundWorkerList, &rw->rw_lnode);
409  }
410 }
411 
412 /*
413  * Forget about a background worker that's no longer needed.
414  *
415  * The worker must be identified by passing an slist_mutable_iter that
416  * points to it. This convention allows deletion of workers during
417  * searches of the worker list, and saves having to search the list again.
418  *
419  * This function must be invoked only in the postmaster.
420  */
421 void
423 {
424  RegisteredBgWorker *rw;
425  BackgroundWorkerSlot *slot;
426 
427  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
428 
430  slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
432  BackgroundWorkerData->parallel_terminate_count++;
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 }
443 
444 /*
445  * Report the PID of a newly-launched background worker in shared memory.
446  *
447  * This function should only be called from the postmaster.
448  */
449 void
451 {
452  BackgroundWorkerSlot *slot;
453 
455  slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
456  slot->pid = rw->rw_pid;
457 
458  if (rw->rw_worker.bgw_notify_pid != 0)
460 }
461 
462 /*
463  * Report that the PID of a background worker is now zero because a
464  * previously-running background worker has exited.
465  *
466  * This function should only be called from the postmaster.
467  */
468 void
470 {
471  RegisteredBgWorker *rw;
472  BackgroundWorkerSlot *slot;
473  int notify_pid;
474 
475  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
476 
478  slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
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 }
496 
497 /*
498  * Cancel SIGUSR1 notifications for a PID belonging to an exiting backend.
499  *
500  * This function should only be called from the postmaster.
501  */
502 void
504 {
505  slist_iter siter;
506 
507  slist_foreach(siter, &BackgroundWorkerList)
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 }
516 
517 /*
518  * Cancel any not-yet-started worker requests that have waiting processes.
519  *
520  * This is called during a normal ("smart" or "fast") database shutdown.
521  * After this point, no new background workers will be started, so anything
522  * that might be waiting for them needs to be kicked off its wait. We do
523  * that by cancelling the bgworker registration entirely, which is perhaps
524  * overkill, but since we're shutting down it does not matter whether the
525  * registration record sticks around.
526  *
527  * This function should only be called from the postmaster.
528  */
529 void
531 {
532  slist_mutable_iter iter;
533 
534  slist_foreach_modify(iter, &BackgroundWorkerList)
535  {
536  RegisteredBgWorker *rw;
537  BackgroundWorkerSlot *slot;
538 
539  rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
541  slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
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 }
556 
557 /*
558  * Reset background worker crash state.
559  *
560  * We assume that, after a crash-and-restart cycle, background workers without
561  * the never-restart flag should be restarted immediately, instead of waiting
562  * for bgw_restart_time to elapse. On the other hand, workers with that flag
563  * should be forgotten immediately, since we won't ever restart them.
564  *
565  * This function should only be called from the postmaster.
566  */
567 void
569 {
570  slist_mutable_iter iter;
571 
572  slist_foreach_modify(iter, &BackgroundWorkerList)
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 }
613 
614 #ifdef EXEC_BACKEND
615 /*
616  * In EXEC_BACKEND mode, workers use this to retrieve their details from
617  * shared memory.
618  */
620 BackgroundWorkerEntry(int slotno)
621 {
622  static BackgroundWorker myEntry;
623  BackgroundWorkerSlot *slot;
624 
625  Assert(slotno < BackgroundWorkerData->total_slots);
626  slot = &BackgroundWorkerData->slot[slotno];
627  Assert(slot->in_use);
628 
629  /* must copy this in case we don't intend to retain shmem access */
630  memcpy(&myEntry, &slot->worker, sizeof myEntry);
631  return &myEntry;
632 }
633 #endif
634 
635 /*
636  * Complain about the BackgroundWorker definition using error level elevel.
637  * Return true if it looks ok, false if not (unless elevel >= ERROR, in
638  * which case we won't return at all in the not-OK case).
639  */
640 static bool
642 {
643  /* sanity check for flags */
645  {
646  if (!(worker->bgw_flags & BGWORKER_SHMEM_ACCESS))
647  {
648  ereport(elevel,
649  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
650  errmsg("background worker \"%s\": must attach to shared memory in order to request a database connection",
651  worker->bgw_name)));
652  return false;
653  }
654 
656  {
657  ereport(elevel,
658  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
659  errmsg("background worker \"%s\": cannot request database access if starting at postmaster start",
660  worker->bgw_name)));
661  return false;
662  }
663 
664  /* XXX other checks? */
665  }
666 
667  if ((worker->bgw_restart_time < 0 &&
668  worker->bgw_restart_time != BGW_NEVER_RESTART) ||
669  (worker->bgw_restart_time > USECS_PER_DAY / 1000))
670  {
671  ereport(elevel,
672  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
673  errmsg("background worker \"%s\": invalid restart interval",
674  worker->bgw_name)));
675  return false;
676  }
677 
678  /*
679  * Parallel workers may not be configured for restart, because the
680  * parallel_register_count/parallel_terminate_count accounting can't
681  * handle parallel workers lasting through a crash-and-restart cycle.
682  */
683  if (worker->bgw_restart_time != BGW_NEVER_RESTART &&
684  (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
685  {
686  ereport(elevel,
687  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
688  errmsg("background worker \"%s\": parallel workers may not be configured for restart",
689  worker->bgw_name)));
690  return false;
691  }
692 
693  /*
694  * If bgw_type is not filled in, use bgw_name.
695  */
696  if (strcmp(worker->bgw_type, "") == 0)
697  strcpy(worker->bgw_type, worker->bgw_name);
698 
699  return true;
700 }
701 
702 /*
703  * Standard SIGTERM handler for background workers
704  */
705 static void
707 {
709 
710  ereport(FATAL,
711  (errcode(ERRCODE_ADMIN_SHUTDOWN),
712  errmsg("terminating background worker \"%s\" due to administrator command",
714 }
715 
716 /*
717  * Start a new background worker
718  *
719  * This is the main entry point for background worker, to be called from
720  * postmaster.
721  */
722 void
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 }
869 
870 /*
871  * Register a new static background worker.
872  *
873  * This can only be called directly from postmaster or in the _PG_init
874  * function of a module library that's loaded by shared_preload_libraries;
875  * otherwise it will have no effect.
876  */
877 void
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 
948  slist_push_head(&BackgroundWorkerList, &rw->rw_lnode);
949 }
950 
951 /*
952  * Register a new background worker from a regular backend.
953  *
954  * Returns true on success and false on failure. Failure typically indicates
955  * that no background worker slots are currently available.
956  *
957  * If handle != NULL, we'll set *handle to a pointer that can subsequently
958  * be used as an argument to GetBackgroundWorkerPid(). The caller can
959  * free this pointer using pfree(), if desired.
960  */
961 bool
963  BackgroundWorkerHandle **handle)
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  */
997  if (parallel && (BackgroundWorkerData->parallel_register_count -
998  BackgroundWorkerData->parallel_terminate_count) >=
1000  {
1001  Assert(BackgroundWorkerData->parallel_register_count -
1002  BackgroundWorkerData->parallel_terminate_count <=
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  {
1013  BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
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)
1023  BackgroundWorkerData->parallel_register_count++;
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 }
1055 
1056 /*
1057  * Get the PID of a dynamically-registered background worker.
1058  *
1059  * If the worker is determined to be running, the return value will be
1060  * BGWH_STARTED and *pidp will get the PID of the worker process. If the
1061  * postmaster has not yet attempted to start the worker, the return value will
1062  * be BGWH_NOT_YET_STARTED. Otherwise, the return value is BGWH_STOPPED.
1063  *
1064  * BGWH_STOPPED can indicate either that the worker is temporarily stopped
1065  * (because it is configured for automatic restart and exited non-zero),
1066  * or that the worker is permanently stopped (because it exited with exit
1067  * code 0, or was not configured for automatic restart), or even that the
1068  * worker was unregistered without ever starting (either because startup
1069  * failed and the worker is not configured for automatic restart, or because
1070  * TerminateBackgroundWorker was used before the worker was successfully
1071  * started).
1072  */
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 }
1115 
1116 /*
1117  * Wait for a background worker to start up.
1118  *
1119  * This is like GetBackgroundWorkerPid(), except that if the worker has not
1120  * yet started, we wait for it to do so; thus, BGWH_NOT_YET_STARTED is never
1121  * returned. However, if the postmaster has died, we give up and return
1122  * BGWH_POSTMASTER_DIED, since it that case we know that startup will not
1123  * take place.
1124  *
1125  * The caller *must* have set our PID as the worker's bgw_notify_pid,
1126  * else we will not be awoken promptly when the worker's state changes.
1127  */
1130 {
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,
1149 
1150  if (rc & WL_POSTMASTER_DEATH)
1151  {
1152  status = BGWH_POSTMASTER_DIED;
1153  break;
1154  }
1155 
1157  }
1158 
1159  return status;
1160 }
1161 
1162 /*
1163  * Wait for a background worker to stop.
1164  *
1165  * If the worker hasn't yet started, or is running, we wait for it to stop
1166  * and then return BGWH_STOPPED. However, if the postmaster has died, we give
1167  * up and return BGWH_POSTMASTER_DIED, because it's the postmaster that
1168  * notifies us when a worker's state changes.
1169  *
1170  * The caller *must* have set our PID as the worker's bgw_notify_pid,
1171  * else we will not be awoken promptly when the worker's state changes.
1172  */
1175 {
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,
1192 
1193  if (rc & WL_POSTMASTER_DEATH)
1194  {
1195  status = BGWH_POSTMASTER_DIED;
1196  break;
1197  }
1198 
1200  }
1201 
1202  return status;
1203 }
1204 
1205 /*
1206  * Instruct the postmaster to terminate a background worker.
1207  *
1208  * Note that it's safe to do this without regard to whether the worker is
1209  * still running, or even if the worker may already have exited and been
1210  * unregistered.
1211  */
1212 void
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 }
1234 
1235 /*
1236  * Look up (and possibly load) a bgworker entry point function.
1237  *
1238  * For functions contained in the core code, we use library name "postgres"
1239  * and consult the InternalBGWorkers array. External functions are
1240  * looked up, and loaded if necessary, using load_external_function().
1241  *
1242  * The point of this is to pass function names as strings across process
1243  * boundaries. We can't pass actual function addresses because of the
1244  * possibility that the function has been loaded at a different address
1245  * in a different process. This is obviously a hazard for functions in
1246  * loadable libraries, but it can happen even for functions in the core code
1247  * on platforms using EXEC_BACKEND (e.g., Windows).
1248  *
1249  * At some point it might be worthwhile to get rid of InternalBGWorkers[]
1250  * in favor of applying load_external_function() for core functions too;
1251  * but that raises portability issues that are not worth addressing now.
1252  */
1253 static bgworker_main_type
1254 LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
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 }
1278 
1279 /*
1280  * Given a PID, get the bgw_type of the background worker. Returns NULL if
1281  * not a valid background worker.
1282  *
1283  * The return value is in static memory belonging to this function, so it has
1284  * to be used before calling this function again. This is so that the caller
1285  * doesn't have to worry about the background worker locking protocol.
1286  */
1287 const char *
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  {
1298  BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
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 }
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:98
const char * GetBackgroundWorkerTypeByPid(pid_t pid)
Definition: bgworker.c:1288
void InitializeTimeouts(void)
Definition: timeout.c:435
struct bkend * rw_backend
int max_parallel_workers
Definition: globals.c:138
BgwHandleStatus WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1129
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:878
#define DEBUG1
Definition: elog.h:25
int errhint(const char *fmt,...)
Definition: elog.c:1156
void ApplyLauncherMain(Datum main_arg)
Definition: launcher.c:803
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1598
#define SIGUSR1
Definition: win32_port.h:171
BackendType MyBackendType
Definition: miscinit.c:62
#define SIGCHLD
Definition: win32_port.h:169
slist_node * cur
Definition: ilist.h:226
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
slist_node * cur
Definition: ilist.h:241
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:593
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:350
void ReportBackgroundWorkerExit(slist_mutable_iter *cur)
Definition: bgworker.c:469
int bgw_restart_time
Definition: bgworker.h:94
struct cursor * cur
Definition: ecpg.c:28
void proc_exit(int code)
Definition: ipc.c:104
int errcode(int sqlerrcode)
Definition: elog.c:698
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
#define SetProcessingMode(mode)
Definition: miscadmin.h:400
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:194
#define kill(pid, sig)
Definition: win32_port.h:454
void BaseInit(void)
Definition: postinit.c:537
void ascii_safe_strlcpy(char *dest, const char *src, size_t destsiz)
Definition: ascii.c:173
#define lengthof(array)
Definition: c.h:734
#define SIGPIPE
Definition: win32_port.h:164
const char * fn_name
Definition: bgworker.c:118
#define SIGUSR2
Definition: win32_port.h:172
#define LOG
Definition: elog.h:26
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
void BackgroundWorkerStopNotifications(pid_t pid)
Definition: bgworker.c:503
void ResetBackgroundWorkerCrashTimes(void)
Definition: bgworker.c:568
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:96
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:142
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:909
void ResetLatch(Latch *latch)
Definition: latch.c:660
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void StatementCancelHandler(SIGNAL_ARGS)
Definition: postgres.c:2931
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:452
bool IsBackgroundWorker
Definition: globals.c:114
void StartBackgroundWorker(void)
Definition: bgworker.c:723
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:735
#define malloc(a)
Definition: header.h:50
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1805
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
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:158
void ReportBackgroundWorkerPID(RegisteredBgWorker *rw)
Definition: bgworker.c:450
#define ERROR
Definition: elog.h:46
BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1174
#define SLIST_STATIC_INIT(name)
Definition: ilist.h:249
struct BackgroundWorkerSlot BackgroundWorkerSlot
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
#define FATAL
Definition: elog.h:49
bool PostmasterMarkPIDForWorkerNotify(int pid)
Definition: postmaster.c:6077
slist_head BackgroundWorkerList
Definition: bgworker.c:42
void InitProcess(void)
Definition: proc.c:303
uint32 parallel_register_count
Definition: bgworker.c:99
bool IsUnderPostmaster
Definition: globals.c:112
#define SIGHUP
Definition: win32_port.h:159
void ForgetBackgroundWorker(slist_mutable_iter *cur)
Definition: bgworker.c:422
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
unsigned int uint32
Definition: c.h:441
BgwHandleStatus
Definition: bgworker.h:102
#define USECS_PER_DAY
Definition: timestamp.h:91
static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
Definition: bgworker.c:1254
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:107
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition: bgworker.c:641
#define SIG_IGN
Definition: win32_port.h:156
static int elevel
Definition: vacuumlazy.c:400
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
#define slist_container(type, membername, ptr)
Definition: ilist.h:693
sigset_t BlockSig
Definition: pqsignal.c:22
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
struct BackgroundWorkerArray BackgroundWorkerArray
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
int PostAuthDelay
Definition: postgres.c:103
void EmitErrorReport(void)
Definition: elog.c:1509
static RegisteredBgWorker * FindRegisteredWorkerBySlotNumber(int slotno)
Definition: bgworker.c:217
void TerminateBackgroundWorker(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1213
#define ereport(elevel,...)
Definition: elog.h:157
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
#define free(a)
Definition: header.h:65
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
#define pg_memory_barrier()
Definition: atomics.h:145
#define SIG_DFL
Definition: win32_port.h:154
#define SIGNAL_ARGS
Definition: c.h:1333
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define Assert(condition)
Definition: c.h:804
BackgroundWorker worker
Definition: bgworker.c:82
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
#define pg_read_barrier()
Definition: atomics.h:158
#define BGW_MAXLEN
Definition: bgworker.h:85
size_t Size
Definition: c.h:540
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:93
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1134
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:962
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
static const struct @15 InternalBGWorkers[]
void dsm_detach_all(void)
Definition: dsm.c:741
sigjmp_buf * PG_exception_stack
Definition: elog.c:95
void ForgetUnstartedBackgroundWorkers(void)
Definition: bgworker.c:530
#define slist_foreach(iter, lhead)
Definition: ilist.h:719
bgworker_main_type fn_addr
Definition: bgworker.c:119
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
void ShutdownLatchSupport(void)
Definition: latch.c:308
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
void ParallelWorkerMain(Datum main_arg)
Definition: parallel.c:1240
uint32 parallel_terminate_count
Definition: bgworker.c:100
pid_t bgw_notify_pid
Definition: bgworker.h:99
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:119
#define elog(elevel,...)
Definition: elog.h:232
int i
void FloatExceptionHandler(SIGNAL_ARGS)
Definition: postgres.c:2952
#define pg_write_barrier()
Definition: atomics.h:159
int max_worker_processes
Definition: globals.c:137
struct Latch * MyLatch
Definition: globals.c:57
static void bgworker_die(SIGNAL_ARGS)
Definition: bgworker.c:706
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:102
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:671
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:153
void init_ps_display(const char *fixed_part)
Definition: ps_status.c:258
void BackgroundWorkerStateChange(bool allow_new_workers)
Definition: bgworker.c:242
#define MAX_PARALLEL_WORKER_LIMIT
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:642
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
BackgroundWorker rw_worker
static bool success
Definition: initdb.c:165
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
#define WL_LATCH_SET
Definition: latch.h:125
void(* bgworker_main_type)(Datum main_arg)
Definition: bgworker.h:71
#define offsetof(type, field)
Definition: c.h:727
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1074
void ApplyWorkerMain(Datum main_arg)
Definition: worker.c:2958
#define InvalidPid
Definition: miscadmin.h:32
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5723