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-2024, 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/postmaster.h"
24 #include "storage/dsm.h"
25 #include "storage/ipc.h"
26 #include "storage/latch.h"
27 #include "storage/lwlock.h"
28 #include "storage/pg_shmem.h"
29 #include "storage/pmsignal.h"
30 #include "storage/proc.h"
31 #include "storage/procsignal.h"
32 #include "storage/shmem.h"
33 #include "tcop/tcopprot.h"
34 #include "utils/ascii.h"
35 #include "utils/memutils.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  "ParallelApplyWorkerMain", ParallelApplyWorkerMain
134  },
135  {
136  "TablesyncWorkerMain", TablesyncWorkerMain
137  }
138 };
139 
140 /* Private functions. */
141 static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname);
142 
143 
144 /*
145  * Calculate shared memory needed.
146  */
147 Size
149 {
150  Size size;
151 
152  /* Array of workers is variably sized. */
153  size = offsetof(BackgroundWorkerArray, slot);
155  sizeof(BackgroundWorkerSlot)));
156 
157  return size;
158 }
159 
160 /*
161  * Initialize shared memory.
162  */
163 void
165 {
166  bool found;
167 
168  BackgroundWorkerData = ShmemInitStruct("Background Worker Data",
170  &found);
171  if (!IsUnderPostmaster)
172  {
173  slist_iter siter;
174  int slotno = 0;
175 
179 
180  /*
181  * Copy contents of worker list into shared memory. Record the shared
182  * memory slot assigned to each worker. This ensures a 1-to-1
183  * correspondence between the postmaster's private list and the array
184  * in shared memory.
185  */
187  {
189  RegisteredBgWorker *rw;
190 
191  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
192  Assert(slotno < max_worker_processes);
193  slot->in_use = true;
194  slot->terminate = false;
195  slot->pid = InvalidPid;
196  slot->generation = 0;
197  rw->rw_shmem_slot = slotno;
198  rw->rw_worker.bgw_notify_pid = 0; /* might be reinit after crash */
199  memcpy(&slot->worker, &rw->rw_worker, sizeof(BackgroundWorker));
200  ++slotno;
201  }
202 
203  /*
204  * Mark any remaining slots as not in use.
205  */
206  while (slotno < max_worker_processes)
207  {
209 
210  slot->in_use = false;
211  ++slotno;
212  }
213  }
214  else
215  Assert(found);
216 }
217 
218 /*
219  * Search the postmaster's backend-private list of RegisteredBgWorker objects
220  * for the one that maps to the given slot number.
221  */
222 static RegisteredBgWorker *
224 {
225  slist_iter siter;
226 
228  {
229  RegisteredBgWorker *rw;
230 
231  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
232  if (rw->rw_shmem_slot == slotno)
233  return rw;
234  }
235 
236  return NULL;
237 }
238 
239 /*
240  * Notice changes to shared memory made by other backends.
241  * Accept new worker requests only if allow_new_workers is true.
242  *
243  * This code runs in the postmaster, so we must be very careful not to assume
244  * that shared memory contents are sane. Otherwise, a rogue backend could
245  * take out the postmaster.
246  */
247 void
248 BackgroundWorkerStateChange(bool allow_new_workers)
249 {
250  int slotno;
251 
252  /*
253  * The total number of slots stored in shared memory should match our
254  * notion of max_worker_processes. If it does not, something is very
255  * wrong. Further down, we always refer to this value as
256  * max_worker_processes, in case shared memory gets corrupted while we're
257  * looping.
258  */
260  {
261  ereport(LOG,
262  (errmsg("inconsistent background worker state (max_worker_processes=%d, total_slots=%d)",
265  return;
266  }
267 
268  /*
269  * Iterate through slots, looking for newly-registered workers or workers
270  * who must die.
271  */
272  for (slotno = 0; slotno < max_worker_processes; ++slotno)
273  {
275  RegisteredBgWorker *rw;
276 
277  if (!slot->in_use)
278  continue;
279 
280  /*
281  * Make sure we don't see the in_use flag before the updated slot
282  * contents.
283  */
284  pg_read_barrier();
285 
286  /* See whether we already know about this worker. */
288  if (rw != NULL)
289  {
290  /*
291  * In general, the worker data can't change after it's initially
292  * registered. However, someone can set the terminate flag.
293  */
294  if (slot->terminate && !rw->rw_terminate)
295  {
296  rw->rw_terminate = true;
297  if (rw->rw_pid != 0)
298  kill(rw->rw_pid, SIGTERM);
299  else
300  {
301  /* Report never-started, now-terminated worker as dead. */
303  }
304  }
305  continue;
306  }
307 
308  /*
309  * If we aren't allowing new workers, then immediately mark it for
310  * termination; the next stanza will take care of cleaning it up.
311  * Doing this ensures that any process waiting for the worker will get
312  * awoken, even though the worker will never be allowed to run.
313  */
314  if (!allow_new_workers)
315  slot->terminate = true;
316 
317  /*
318  * If the worker is marked for termination, we don't need to add it to
319  * the registered workers list; we can just free the slot. However, if
320  * bgw_notify_pid is set, the process that registered the worker may
321  * need to know that we've processed the terminate request, so be sure
322  * to signal it.
323  */
324  if (slot->terminate)
325  {
326  int notify_pid;
327 
328  /*
329  * We need a memory barrier here to make sure that the load of
330  * bgw_notify_pid and the update of parallel_terminate_count
331  * complete before the store to in_use.
332  */
333  notify_pid = slot->worker.bgw_notify_pid;
334  if ((slot->worker.bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
336  slot->pid = 0;
337 
339  slot->in_use = false;
340 
341  if (notify_pid != 0)
342  kill(notify_pid, SIGUSR1);
343 
344  continue;
345  }
346 
347  /*
348  * Copy the registration data into the registered workers list.
349  */
351  sizeof(RegisteredBgWorker),
353  if (rw == NULL)
354  {
355  ereport(LOG,
356  (errcode(ERRCODE_OUT_OF_MEMORY),
357  errmsg("out of memory")));
358  return;
359  }
360 
361  /*
362  * Copy strings in a paranoid way. If shared memory is corrupted, the
363  * source data might not even be NUL-terminated.
364  */
366  slot->worker.bgw_name, BGW_MAXLEN);
368  slot->worker.bgw_type, BGW_MAXLEN);
373 
374  /*
375  * Copy various fixed-size fields.
376  *
377  * flags, start_time, and restart_time are examined by the postmaster,
378  * but nothing too bad will happen if they are corrupted. The
379  * remaining fields will only be examined by the child process. It
380  * might crash, but we won't.
381  */
382  rw->rw_worker.bgw_flags = slot->worker.bgw_flags;
386  memcpy(rw->rw_worker.bgw_extra, slot->worker.bgw_extra, BGW_EXTRALEN);
387 
388  /*
389  * Copy the PID to be notified about state changes, but only if the
390  * postmaster knows about a backend with that PID. It isn't an error
391  * if the postmaster doesn't know about the PID, because the backend
392  * that requested the worker could have died (or been killed) just
393  * after doing so. Nonetheless, at least until we get some experience
394  * with how this plays out in the wild, log a message at a relative
395  * high debug level.
396  */
399  {
400  elog(DEBUG1, "worker notification PID %d is not valid",
401  (int) rw->rw_worker.bgw_notify_pid);
402  rw->rw_worker.bgw_notify_pid = 0;
403  }
404 
405  /* Initialize postmaster bookkeeping. */
406  rw->rw_backend = NULL;
407  rw->rw_pid = 0;
408  rw->rw_child_slot = 0;
409  rw->rw_crashed_at = 0;
410  rw->rw_shmem_slot = slotno;
411  rw->rw_terminate = false;
412 
413  /* Log it! */
414  ereport(DEBUG1,
415  (errmsg_internal("registering background worker \"%s\"",
416  rw->rw_worker.bgw_name)));
417 
419  }
420 }
421 
422 /*
423  * Forget about a background worker that's no longer needed.
424  *
425  * The worker must be identified by passing an slist_mutable_iter that
426  * points to it. This convention allows deletion of workers during
427  * searches of the worker list, and saves having to search the list again.
428  *
429  * Caller is responsible for notifying bgw_notify_pid, if appropriate.
430  *
431  * This function must be invoked only in the postmaster.
432  */
433 void
435 {
436  RegisteredBgWorker *rw;
437  BackgroundWorkerSlot *slot;
438 
439  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
440 
443  Assert(slot->in_use);
444 
445  /*
446  * We need a memory barrier here to make sure that the update of
447  * parallel_terminate_count completes before the store to in_use.
448  */
451 
453  slot->in_use = false;
454 
455  ereport(DEBUG1,
456  (errmsg_internal("unregistering background worker \"%s\"",
457  rw->rw_worker.bgw_name)));
458 
460  pfree(rw);
461 }
462 
463 /*
464  * Report the PID of a newly-launched background worker in shared memory.
465  *
466  * This function should only be called from the postmaster.
467  */
468 void
470 {
471  BackgroundWorkerSlot *slot;
472 
475  slot->pid = rw->rw_pid;
476 
477  if (rw->rw_worker.bgw_notify_pid != 0)
479 }
480 
481 /*
482  * Report that the PID of a background worker is now zero because a
483  * previously-running background worker has exited.
484  *
485  * This function should only be called from the postmaster.
486  */
487 void
489 {
490  RegisteredBgWorker *rw;
491  BackgroundWorkerSlot *slot;
492  int notify_pid;
493 
494  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
495 
498  slot->pid = rw->rw_pid;
499  notify_pid = rw->rw_worker.bgw_notify_pid;
500 
501  /*
502  * If this worker is slated for deregistration, do that before notifying
503  * the process which started it. Otherwise, if that process tries to
504  * reuse the slot immediately, it might not be available yet. In theory
505  * that could happen anyway if the process checks slot->pid at just the
506  * wrong moment, but this makes the window narrower.
507  */
508  if (rw->rw_terminate ||
511 
512  if (notify_pid != 0)
513  kill(notify_pid, SIGUSR1);
514 }
515 
516 /*
517  * Cancel SIGUSR1 notifications for a PID belonging to an exiting backend.
518  *
519  * This function should only be called from the postmaster.
520  */
521 void
523 {
524  slist_iter siter;
525 
527  {
528  RegisteredBgWorker *rw;
529 
530  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
531  if (rw->rw_worker.bgw_notify_pid == pid)
532  rw->rw_worker.bgw_notify_pid = 0;
533  }
534 }
535 
536 /*
537  * Cancel any not-yet-started worker requests that have waiting processes.
538  *
539  * This is called during a normal ("smart" or "fast") database shutdown.
540  * After this point, no new background workers will be started, so anything
541  * that might be waiting for them needs to be kicked off its wait. We do
542  * that by canceling the bgworker registration entirely, which is perhaps
543  * overkill, but since we're shutting down it does not matter whether the
544  * registration record sticks around.
545  *
546  * This function should only be called from the postmaster.
547  */
548 void
550 {
551  slist_mutable_iter iter;
552 
554  {
555  RegisteredBgWorker *rw;
556  BackgroundWorkerSlot *slot;
557 
558  rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
561 
562  /* If it's not yet started, and there's someone waiting ... */
563  if (slot->pid == InvalidPid &&
564  rw->rw_worker.bgw_notify_pid != 0)
565  {
566  /* ... then zap it, and notify the waiter */
567  int notify_pid = rw->rw_worker.bgw_notify_pid;
568 
569  ForgetBackgroundWorker(&iter);
570  if (notify_pid != 0)
571  kill(notify_pid, SIGUSR1);
572  }
573  }
574 }
575 
576 /*
577  * Reset background worker crash state.
578  *
579  * We assume that, after a crash-and-restart cycle, background workers without
580  * the never-restart flag should be restarted immediately, instead of waiting
581  * for bgw_restart_time to elapse. On the other hand, workers with that flag
582  * should be forgotten immediately, since we won't ever restart them.
583  *
584  * This function should only be called from the postmaster.
585  */
586 void
588 {
589  slist_mutable_iter iter;
590 
592  {
593  RegisteredBgWorker *rw;
594 
595  rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
596 
598  {
599  /*
600  * Workers marked BGW_NEVER_RESTART shouldn't get relaunched after
601  * the crash, so forget about them. (If we wait until after the
602  * crash to forget about them, and they are parallel workers,
603  * parallel_terminate_count will get incremented after we've
604  * already zeroed parallel_register_count, which would be bad.)
605  */
606  ForgetBackgroundWorker(&iter);
607  }
608  else
609  {
610  /*
611  * The accounting which we do via parallel_register_count and
612  * parallel_terminate_count would get messed up if a worker marked
613  * parallel could survive a crash and restart cycle. All such
614  * workers should be marked BGW_NEVER_RESTART, and thus control
615  * should never reach this branch.
616  */
618 
619  /*
620  * Allow this worker to be restarted immediately after we finish
621  * resetting.
622  */
623  rw->rw_crashed_at = 0;
624 
625  /*
626  * If there was anyone waiting for it, they're history.
627  */
628  rw->rw_worker.bgw_notify_pid = 0;
629  }
630  }
631 }
632 
633 /*
634  * Complain about the BackgroundWorker definition using error level elevel.
635  * Return true if it looks ok, false if not (unless elevel >= ERROR, in
636  * which case we won't return at all in the not-OK case).
637  */
638 static bool
640 {
641  /* sanity check for flags */
642 
643  /*
644  * We used to support workers not connected to shared memory, but don't
645  * anymore. Thus this is a required flag now. We're not removing the flag
646  * for compatibility reasons and because the flag still provides some
647  * signal when reading code.
648  */
649  if (!(worker->bgw_flags & BGWORKER_SHMEM_ACCESS))
650  {
651  ereport(elevel,
652  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
653  errmsg("background worker \"%s\": background workers without shared memory access are not supported",
654  worker->bgw_name)));
655  return false;
656  }
657 
659  {
661  {
662  ereport(elevel,
663  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
664  errmsg("background worker \"%s\": cannot request database access if starting at postmaster start",
665  worker->bgw_name)));
666  return false;
667  }
668 
669  /* XXX other checks? */
670  }
671 
672  if ((worker->bgw_restart_time < 0 &&
673  worker->bgw_restart_time != BGW_NEVER_RESTART) ||
674  (worker->bgw_restart_time > USECS_PER_DAY / 1000))
675  {
676  ereport(elevel,
677  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
678  errmsg("background worker \"%s\": invalid restart interval",
679  worker->bgw_name)));
680  return false;
681  }
682 
683  /*
684  * Parallel workers may not be configured for restart, because the
685  * parallel_register_count/parallel_terminate_count accounting can't
686  * handle parallel workers lasting through a crash-and-restart cycle.
687  */
688  if (worker->bgw_restart_time != BGW_NEVER_RESTART &&
689  (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
690  {
691  ereport(elevel,
692  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
693  errmsg("background worker \"%s\": parallel workers may not be configured for restart",
694  worker->bgw_name)));
695  return false;
696  }
697 
698  /*
699  * If bgw_type is not filled in, use bgw_name.
700  */
701  if (strcmp(worker->bgw_type, "") == 0)
702  strcpy(worker->bgw_type, worker->bgw_name);
703 
704  return true;
705 }
706 
707 /*
708  * Standard SIGTERM handler for background workers
709  */
710 static void
712 {
713  sigprocmask(SIG_SETMASK, &BlockSig, NULL);
714 
715  ereport(FATAL,
716  (errcode(ERRCODE_ADMIN_SHUTDOWN),
717  errmsg("terminating background worker \"%s\" due to administrator command",
719 }
720 
721 /*
722  * Main entry point for background worker processes.
723  */
724 void
726 {
727  sigjmp_buf local_sigjmp_buf;
729  bgworker_main_type entrypt;
730 
731  if (worker == NULL)
732  elog(FATAL, "unable to find bgworker entry");
733 
734  IsBackgroundWorker = true;
735 
737  init_ps_display(worker->bgw_name);
738 
740 
741  /* Apply PostAuthDelay */
742  if (PostAuthDelay > 0)
743  pg_usleep(PostAuthDelay * 1000000L);
744 
745  /*
746  * Set up signal handlers.
747  */
749  {
750  /*
751  * SIGINT is used to signal canceling the current action
752  */
756 
757  /* XXX Any other handlers needed here? */
758  }
759  else
760  {
761  pqsignal(SIGINT, SIG_IGN);
763  pqsignal(SIGFPE, SIG_IGN);
764  }
765  pqsignal(SIGTERM, bgworker_die);
766  /* SIGQUIT handler was already set up by InitPostmasterChild */
768 
769  InitializeTimeouts(); /* establishes SIGALRM handler */
770 
774 
775  /*
776  * If an exception is encountered, processing resumes here.
777  *
778  * We just need to clean up, report the error, and go away.
779  */
780  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
781  {
782  /* Since not using PG_TRY, must reset error stack by hand */
783  error_context_stack = NULL;
784 
785  /* Prevent interrupts while cleaning up */
786  HOLD_INTERRUPTS();
787 
788  /*
789  * sigsetjmp will have blocked all signals, but we may need to accept
790  * signals while communicating with our parallel leader. Once we've
791  * done HOLD_INTERRUPTS() it should be safe to unblock signals.
792  */
794 
795  /* Report the error to the parallel leader and the server log */
796  EmitErrorReport();
797 
798  /*
799  * Do we need more cleanup here? For shmem-connected bgworkers, we
800  * will call InitProcess below, which will install ProcKill as exit
801  * callback. That will take care of releasing locks, etc.
802  */
803 
804  /* and go away */
805  proc_exit(1);
806  }
807 
808  /* We can now handle ereport(ERROR) */
809  PG_exception_stack = &local_sigjmp_buf;
810 
811  /*
812  * Create a per-backend PGPROC struct in shared memory. We must do this
813  * before we can use LWLocks or access any shared memory.
814  */
815  InitProcess();
816 
817  /*
818  * Early initialization.
819  */
820  BaseInit();
821 
822  /*
823  * Look up the entry point function, loading its library if necessary.
824  */
826  worker->bgw_function_name);
827 
828  /*
829  * Note that in normal processes, we would call InitPostgres here. For a
830  * worker, however, we don't know what database to connect to, yet; so we
831  * need to wait until the user code does it via
832  * BackgroundWorkerInitializeConnection().
833  */
834 
835  /*
836  * Now invoke the user-defined worker code
837  */
838  entrypt(worker->bgw_main_arg);
839 
840  /* ... and if it returns, we're done */
841  proc_exit(0);
842 }
843 
844 /*
845  * Register a new static background worker.
846  *
847  * This can only be called directly from postmaster or in the _PG_init
848  * function of a module library that's loaded by shared_preload_libraries;
849  * otherwise it will have no effect.
850  */
851 void
853 {
854  RegisteredBgWorker *rw;
855  static int numworkers = 0;
856 
857  /*
858  * Static background workers can only be registered in the postmaster
859  * process.
860  */
862  {
863  /*
864  * In EXEC_BACKEND or single-user mode, we process
865  * shared_preload_libraries in backend processes too. We cannot
866  * register static background workers at that stage, but many
867  * libraries' _PG_init() functions don't distinguish whether they're
868  * being loaded in the postmaster or in a backend, they just check
869  * process_shared_preload_libraries_in_progress. It's a bit sloppy,
870  * but for historical reasons we tolerate it. In EXEC_BACKEND mode,
871  * the background workers should already have been registered when the
872  * library was loaded in postmaster.
873  */
875  return;
876  ereport(LOG,
877  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
878  errmsg("background worker \"%s\": must be registered in shared_preload_libraries",
879  worker->bgw_name)));
880  return;
881  }
882 
883  /*
884  * Cannot register static background workers after calling
885  * BackgroundWorkerShmemInit().
886  */
887  if (BackgroundWorkerData != NULL)
888  elog(ERROR, "cannot register background worker \"%s\" after shmem init",
889  worker->bgw_name);
890 
891  ereport(DEBUG1,
892  (errmsg_internal("registering background worker \"%s\"", worker->bgw_name)));
893 
894  if (!SanityCheckBackgroundWorker(worker, LOG))
895  return;
896 
897  if (worker->bgw_notify_pid != 0)
898  {
899  ereport(LOG,
900  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
901  errmsg("background worker \"%s\": only dynamic background workers can request notification",
902  worker->bgw_name)));
903  return;
904  }
905 
906  /*
907  * Enforce maximum number of workers. Note this is overly restrictive: we
908  * could allow more non-shmem-connected workers, because these don't count
909  * towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
910  * important to relax this restriction.
911  */
912  if (++numworkers > max_worker_processes)
913  {
914  ereport(LOG,
915  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
916  errmsg("too many background workers"),
917  errdetail_plural("Up to %d background worker can be registered with the current settings.",
918  "Up to %d background workers can be registered with the current settings.",
921  errhint("Consider increasing the configuration parameter max_worker_processes.")));
922  return;
923  }
924 
925  /*
926  * Copy the registration data into the registered workers list.
927  */
929  sizeof(RegisteredBgWorker),
931  if (rw == NULL)
932  {
933  ereport(LOG,
934  (errcode(ERRCODE_OUT_OF_MEMORY),
935  errmsg("out of memory")));
936  return;
937  }
938 
939  rw->rw_worker = *worker;
940  rw->rw_backend = NULL;
941  rw->rw_pid = 0;
942  rw->rw_child_slot = 0;
943  rw->rw_crashed_at = 0;
944  rw->rw_terminate = false;
945 
947 }
948 
949 /*
950  * Register a new background worker from a regular backend.
951  *
952  * Returns true on success and false on failure. Failure typically indicates
953  * that no background worker slots are currently available.
954  *
955  * If handle != NULL, we'll set *handle to a pointer that can subsequently
956  * be used as an argument to GetBackgroundWorkerPid(). The caller can
957  * free this pointer using pfree(), if desired.
958  */
959 bool
961  BackgroundWorkerHandle **handle)
962 {
963  int slotno;
964  bool success = false;
965  bool parallel;
966  uint64 generation = 0;
967 
968  /*
969  * We can't register dynamic background workers from the postmaster. If
970  * this is a standalone backend, we're the only process and can't start
971  * any more. In a multi-process environment, it might be theoretically
972  * possible, but we don't currently support it due to locking
973  * considerations; see comments on the BackgroundWorkerSlot data
974  * structure.
975  */
976  if (!IsUnderPostmaster)
977  return false;
978 
979  if (!SanityCheckBackgroundWorker(worker, ERROR))
980  return false;
981 
982  parallel = (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0;
983 
984  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
985 
986  /*
987  * If this is a parallel worker, check whether there are already too many
988  * parallel workers; if so, don't register another one. Our view of
989  * parallel_terminate_count may be slightly stale, but that doesn't really
990  * matter: we would have gotten the same result if we'd arrived here
991  * slightly earlier anyway. There's no help for it, either, since the
992  * postmaster must not take locks; a memory barrier wouldn't guarantee
993  * anything useful.
994  */
998  {
1002  LWLockRelease(BackgroundWorkerLock);
1003  return false;
1004  }
1005 
1006  /*
1007  * Look for an unused slot. If we find one, grab it.
1008  */
1009  for (slotno = 0; slotno < BackgroundWorkerData->total_slots; ++slotno)
1010  {
1012 
1013  if (!slot->in_use)
1014  {
1015  memcpy(&slot->worker, worker, sizeof(BackgroundWorker));
1016  slot->pid = InvalidPid; /* indicates not started yet */
1017  slot->generation++;
1018  slot->terminate = false;
1019  generation = slot->generation;
1020  if (parallel)
1022 
1023  /*
1024  * Make sure postmaster doesn't see the slot as in use before it
1025  * sees the new contents.
1026  */
1027  pg_write_barrier();
1028 
1029  slot->in_use = true;
1030  success = true;
1031  break;
1032  }
1033  }
1034 
1035  LWLockRelease(BackgroundWorkerLock);
1036 
1037  /* If we found a slot, tell the postmaster to notice the change. */
1038  if (success)
1040 
1041  /*
1042  * If we found a slot and the user has provided a handle, initialize it.
1043  */
1044  if (success && handle)
1045  {
1046  *handle = palloc(sizeof(BackgroundWorkerHandle));
1047  (*handle)->slot = slotno;
1048  (*handle)->generation = generation;
1049  }
1050 
1051  return success;
1052 }
1053 
1054 /*
1055  * Get the PID of a dynamically-registered background worker.
1056  *
1057  * If the worker is determined to be running, the return value will be
1058  * BGWH_STARTED and *pidp will get the PID of the worker process. If the
1059  * postmaster has not yet attempted to start the worker, the return value will
1060  * be BGWH_NOT_YET_STARTED. Otherwise, the return value is BGWH_STOPPED.
1061  *
1062  * BGWH_STOPPED can indicate either that the worker is temporarily stopped
1063  * (because it is configured for automatic restart and exited non-zero),
1064  * or that the worker is permanently stopped (because it exited with exit
1065  * code 0, or was not configured for automatic restart), or even that the
1066  * worker was unregistered without ever starting (either because startup
1067  * failed and the worker is not configured for automatic restart, or because
1068  * TerminateBackgroundWorker was used before the worker was successfully
1069  * started).
1070  */
1073 {
1074  BackgroundWorkerSlot *slot;
1075  pid_t pid;
1076 
1077  Assert(handle->slot < max_worker_processes);
1078  slot = &BackgroundWorkerData->slot[handle->slot];
1079 
1080  /*
1081  * We could probably arrange to synchronize access to data using memory
1082  * barriers only, but for now, let's just keep it simple and grab the
1083  * lock. It seems unlikely that there will be enough traffic here to
1084  * result in meaningful contention.
1085  */
1086  LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1087 
1088  /*
1089  * The generation number can't be concurrently changed while we hold the
1090  * lock. The pid, which is updated by the postmaster, can change at any
1091  * time, but we assume such changes are atomic. So the value we read
1092  * won't be garbage, but it might be out of date by the time the caller
1093  * examines it (but that's unavoidable anyway).
1094  *
1095  * The in_use flag could be in the process of changing from true to false,
1096  * but if it is already false then it can't change further.
1097  */
1098  if (handle->generation != slot->generation || !slot->in_use)
1099  pid = 0;
1100  else
1101  pid = slot->pid;
1102 
1103  /* All done. */
1104  LWLockRelease(BackgroundWorkerLock);
1105 
1106  if (pid == 0)
1107  return BGWH_STOPPED;
1108  else if (pid == InvalidPid)
1109  return BGWH_NOT_YET_STARTED;
1110  *pidp = pid;
1111  return BGWH_STARTED;
1112 }
1113 
1114 /*
1115  * Wait for a background worker to start up.
1116  *
1117  * This is like GetBackgroundWorkerPid(), except that if the worker has not
1118  * yet started, we wait for it to do so; thus, BGWH_NOT_YET_STARTED is never
1119  * returned. However, if the postmaster has died, we give up and return
1120  * BGWH_POSTMASTER_DIED, since it that case we know that startup will not
1121  * take place.
1122  *
1123  * The caller *must* have set our PID as the worker's bgw_notify_pid,
1124  * else we will not be awoken promptly when the worker's state changes.
1125  */
1128 {
1129  BgwHandleStatus status;
1130  int rc;
1131 
1132  for (;;)
1133  {
1134  pid_t pid;
1135 
1137 
1138  status = GetBackgroundWorkerPid(handle, &pid);
1139  if (status == BGWH_STARTED)
1140  *pidp = pid;
1141  if (status != BGWH_NOT_YET_STARTED)
1142  break;
1143 
1144  rc = WaitLatch(MyLatch,
1146  WAIT_EVENT_BGWORKER_STARTUP);
1147 
1148  if (rc & WL_POSTMASTER_DEATH)
1149  {
1150  status = BGWH_POSTMASTER_DIED;
1151  break;
1152  }
1153 
1155  }
1156 
1157  return status;
1158 }
1159 
1160 /*
1161  * Wait for a background worker to stop.
1162  *
1163  * If the worker hasn't yet started, or is running, we wait for it to stop
1164  * and then return BGWH_STOPPED. However, if the postmaster has died, we give
1165  * up and return BGWH_POSTMASTER_DIED, because it's the postmaster that
1166  * notifies us when a worker's state changes.
1167  *
1168  * The caller *must* have set our PID as the worker's bgw_notify_pid,
1169  * else we will not be awoken promptly when the worker's state changes.
1170  */
1173 {
1174  BgwHandleStatus status;
1175  int rc;
1176 
1177  for (;;)
1178  {
1179  pid_t pid;
1180 
1182 
1183  status = GetBackgroundWorkerPid(handle, &pid);
1184  if (status == BGWH_STOPPED)
1185  break;
1186 
1187  rc = WaitLatch(MyLatch,
1189  WAIT_EVENT_BGWORKER_SHUTDOWN);
1190 
1191  if (rc & WL_POSTMASTER_DEATH)
1192  {
1193  status = BGWH_POSTMASTER_DIED;
1194  break;
1195  }
1196 
1198  }
1199 
1200  return status;
1201 }
1202 
1203 /*
1204  * Instruct the postmaster to terminate a background worker.
1205  *
1206  * Note that it's safe to do this without regard to whether the worker is
1207  * still running, or even if the worker may already have exited and been
1208  * unregistered.
1209  */
1210 void
1212 {
1213  BackgroundWorkerSlot *slot;
1214  bool signal_postmaster = false;
1215 
1216  Assert(handle->slot < max_worker_processes);
1217  slot = &BackgroundWorkerData->slot[handle->slot];
1218 
1219  /* Set terminate flag in shared memory, unless slot has been reused. */
1220  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
1221  if (handle->generation == slot->generation)
1222  {
1223  slot->terminate = true;
1224  signal_postmaster = true;
1225  }
1226  LWLockRelease(BackgroundWorkerLock);
1227 
1228  /* Make sure the postmaster notices the change to shared memory. */
1229  if (signal_postmaster)
1231 }
1232 
1233 /*
1234  * Look up (and possibly load) a bgworker entry point function.
1235  *
1236  * For functions contained in the core code, we use library name "postgres"
1237  * and consult the InternalBGWorkers array. External functions are
1238  * looked up, and loaded if necessary, using load_external_function().
1239  *
1240  * The point of this is to pass function names as strings across process
1241  * boundaries. We can't pass actual function addresses because of the
1242  * possibility that the function has been loaded at a different address
1243  * in a different process. This is obviously a hazard for functions in
1244  * loadable libraries, but it can happen even for functions in the core code
1245  * on platforms using EXEC_BACKEND (e.g., Windows).
1246  *
1247  * At some point it might be worthwhile to get rid of InternalBGWorkers[]
1248  * in favor of applying load_external_function() for core functions too;
1249  * but that raises portability issues that are not worth addressing now.
1250  */
1251 static bgworker_main_type
1252 LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
1253 {
1254  /*
1255  * If the function is to be loaded from postgres itself, search the
1256  * InternalBGWorkers array.
1257  */
1258  if (strcmp(libraryname, "postgres") == 0)
1259  {
1260  int i;
1261 
1262  for (i = 0; i < lengthof(InternalBGWorkers); i++)
1263  {
1264  if (strcmp(InternalBGWorkers[i].fn_name, funcname) == 0)
1265  return InternalBGWorkers[i].fn_addr;
1266  }
1267 
1268  /* We can only reach this by programming error. */
1269  elog(ERROR, "internal function \"%s\" not found", funcname);
1270  }
1271 
1272  /* Otherwise load from external library. */
1273  return (bgworker_main_type)
1274  load_external_function(libraryname, funcname, true, NULL);
1275 }
1276 
1277 /*
1278  * Given a PID, get the bgw_type of the background worker. Returns NULL if
1279  * not a valid background worker.
1280  *
1281  * The return value is in static memory belonging to this function, so it has
1282  * to be used before calling this function again. This is so that the caller
1283  * doesn't have to worry about the background worker locking protocol.
1284  */
1285 const char *
1287 {
1288  int slotno;
1289  bool found = false;
1290  static char result[BGW_MAXLEN];
1291 
1292  LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1293 
1294  for (slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++)
1295  {
1297 
1298  if (slot->pid > 0 && slot->pid == pid)
1299  {
1300  strcpy(result, slot->worker.bgw_type);
1301  found = true;
1302  break;
1303  }
1304  }
1305 
1306  LWLockRelease(BackgroundWorkerLock);
1307 
1308  if (!found)
1309  return NULL;
1310 
1311  return result;
1312 }
void ParallelApplyWorkerMain(Datum main_arg)
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
#define pg_write_barrier()
Definition: atomics.h:154
void ParallelWorkerMain(Datum main_arg)
Definition: parallel.c:1284
sigset_t BlockSig
Definition: pqsignal.c:23
void ApplyWorkerMain(Datum main_arg)
Definition: worker.c:4710
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:852
static RegisteredBgWorker * FindRegisteredWorkerBySlotNumber(int slotno)
Definition: bgworker.c:223
BgwHandleStatus WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1127
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition: bgworker.c:639
void ReportBackgroundWorkerPID(RegisteredBgWorker *rw)
Definition: bgworker.c:469
void TerminateBackgroundWorker(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1211
BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
Definition: bgworker.c:1172
void ResetBackgroundWorkerCrashTimes(void)
Definition: bgworker.c:587
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:164
void BackgroundWorkerMain(void)
Definition: bgworker.c:725
struct BackgroundWorkerSlot BackgroundWorkerSlot
const char * GetBackgroundWorkerTypeByPid(pid_t pid)
Definition: bgworker.c:1286
void ForgetBackgroundWorker(slist_mutable_iter *cur)
Definition: bgworker.c:434
slist_head BackgroundWorkerList
Definition: bgworker.c:42
const char * fn_name
Definition: bgworker.c:118
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1072
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
Definition: bgworker.c:1252
static void bgworker_die(SIGNAL_ARGS)
Definition: bgworker.c:711
void BackgroundWorkerStopNotifications(pid_t pid)
Definition: bgworker.c:522
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:148
void BackgroundWorkerStateChange(bool allow_new_workers)
Definition: bgworker.c:248
static const struct @15 InternalBGWorkers[]
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:960
void ReportBackgroundWorkerExit(slist_mutable_iter *cur)
Definition: bgworker.c:488
bgworker_main_type fn_addr
Definition: bgworker.c:119
void ForgetUnstartedBackgroundWorkers(void)
Definition: bgworker.c:549
struct BackgroundWorkerArray BackgroundWorkerArray
#define BGW_NEVER_RESTART
Definition: bgworker.h:85
#define BGW_EXTRALEN
Definition: bgworker.h:87
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:68
BgwHandleStatus
Definition: bgworker.h:104
@ BGWH_POSTMASTER_DIED
Definition: bgworker.h:108
@ BGWH_STARTED
Definition: bgworker.h:105
@ BGWH_NOT_YET_STARTED
Definition: bgworker.h:106
@ BGWH_STOPPED
Definition: bgworker.h:107
@ BgWorkerStart_PostmasterStart
Definition: bgworker.h:79
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:60
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:53
void(* bgworker_main_type)(Datum main_arg)
Definition: bgworker.h:72
#define BGW_MAXLEN
Definition: bgworker.h:86
#define MAX_PARALLEL_WORKER_LIMIT
unsigned int uint32
Definition: c.h:495
#define SIGNAL_ARGS
Definition: c.h:1334
#define FLEXIBLE_ARRAY_MEMBER
Definition: c.h:387
#define lengthof(array)
Definition: c.h:777
size_t Size
Definition: c.h:594
#define USECS_PER_DAY
Definition: timestamp.h:131
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:105
struct cursor * cur
Definition: ecpg.c:28
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1162
void EmitErrorReport(void)
Definition: elog.c:1675
ErrorContextCallback * error_context_stack
Definition: elog.c:95
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1300
int errhint(const char *fmt,...)
Definition: elog.c:1322
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1075
sigjmp_buf * PG_exception_stack
Definition: elog.c:97
#define LOG
Definition: elog.h:31
#define FATAL
Definition: elog.h:41
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#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
bool IsUnderPostmaster
Definition: globals.c:116
bool IsBackgroundWorker
Definition: globals.c:118
int max_parallel_workers
Definition: globals.c:142
bool IsPostmasterEnvironment
Definition: globals.c:115
struct Latch * MyLatch
Definition: globals.c:59
int max_worker_processes
Definition: globals.c:141
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:1084
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:1148
#define SLIST_STATIC_INIT(name)
Definition: ilist.h:283
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:1006
#define slist_container(type, membername, ptr)
Definition: ilist.h:1106
#define slist_foreach(iter, lhead)
Definition: ilist.h:1132
#define funcname
Definition: indent_codes.h:69
static bool success
Definition: initdb.c:184
void proc_exit(int code)
Definition: ipc.c:104
int i
Definition: isn.c:73
void ResetLatch(Latch *latch)
Definition: latch.c:725
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:518
#define WL_LATCH_SET
Definition: latch.h:127
#define WL_POSTMASTER_DEATH
Definition: latch.h:131
void ApplyLauncherMain(Datum main_arg)
Definition: launcher.c:1129
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_SHARED
Definition: lwlock.h:117
@ LW_EXCLUSIVE
Definition: lwlock.h:116
void pfree(void *pointer)
Definition: mcxt.c:1431
void * MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
Definition: mcxt.c:1112
MemoryContext PostmasterContext
Definition: mcxt.c:143
void * palloc(Size size)
Definition: mcxt.c:1201
@ InitProcessing
Definition: miscadmin.h:410
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:133
#define SetProcessingMode(mode)
Definition: miscadmin.h:422
@ B_BG_WORKER
Definition: miscadmin.h:332
#define InvalidPid
Definition: miscadmin.h:32
BackendType MyBackendType
Definition: miscinit.c:64
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1779
#define MAXPGPATH
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:181
@ PMSIGNAL_BACKGROUND_WORKER_CHANGE
Definition: pmsignal.h:40
pqsigfunc pqsignal(int signo, pqsigfunc func)
int PostAuthDelay
Definition: postgres.c:100
void FloatExceptionHandler(SIGNAL_ARGS)
Definition: postgres.c:3018
void StatementCancelHandler(SIGNAL_ARGS)
Definition: postgres.c:3001
void BaseInit(void)
Definition: postinit.c:631
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5696
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:194
bool PostmasterMarkPIDForWorkerNotify(int pid)
Definition: postmaster.c:6046
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:639
void init_ps_display(const char *fixed_part)
Definition: ps_status.c:242
Size add_size(Size s1, Size s2)
Definition: shmem.c:494
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:388
Size mul_size(Size s1, Size s2)
Definition: shmem.c:511
void pg_usleep(long microsec)
Definition: signal.c:53
void InitProcess(void)
Definition: proc.c:299
uint32 parallel_terminate_count
Definition: bgworker.c:100
uint32 parallel_register_count
Definition: bgworker.c:99
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:101
BackgroundWorker worker
Definition: bgworker.c:82
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:97
Datum bgw_main_arg
Definition: bgworker.h:98
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:91
int bgw_restart_time
Definition: bgworker.h:95
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:92
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:94
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:99
pid_t bgw_notify_pid
Definition: bgworker.h:100
char bgw_library_name[MAXPGPATH]
Definition: bgworker.h:96
struct bkend * rw_backend
BackgroundWorker rw_worker
slist_node * cur
Definition: ilist.h:259
slist_node * cur
Definition: ilist.h:274
void TablesyncWorkerMain(Datum main_arg)
Definition: tablesync.c:1694
void InitializeTimeouts(void)
Definition: timeout.c:470
#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 kill(pid, sig)
Definition: win32_port.h:485
#define SIGUSR1
Definition: win32_port.h:180
#define SIGUSR2
Definition: win32_port.h:181
#define SIG_IGN
Definition: win32_port.h:165