PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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"
24#include "storage/ipc.h"
25#include "storage/latch.h"
26#include "storage/lwlock.h"
27#include "storage/pmsignal.h"
28#include "storage/proc.h"
29#include "storage/procarray.h"
30#include "storage/procsignal.h"
31#include "storage/shmem.h"
32#include "tcop/tcopprot.h"
33#include "utils/ascii.h"
34#include "utils/memutils.h"
35#include "utils/ps_status.h"
36#include "utils/timeout.h"
37
38/*
39 * The postmaster's list of registered background workers, in private memory.
40 */
42
43/*
44 * BackgroundWorkerSlots exist in shared memory and can be accessed (via
45 * the BackgroundWorkerArray) by both the postmaster and by regular backends.
46 * However, the postmaster cannot take locks, even spinlocks, because this
47 * might allow it to crash or become wedged if shared memory gets corrupted.
48 * Such an outcome is intolerable. Therefore, we need a lockless protocol
49 * for coordinating access to this data.
50 *
51 * The 'in_use' flag is used to hand off responsibility for the slot between
52 * the postmaster and the rest of the system. When 'in_use' is false,
53 * the postmaster will ignore the slot entirely, except for the 'in_use' flag
54 * itself, which it may read. In this state, regular backends may modify the
55 * slot. Once a backend sets 'in_use' to true, the slot becomes the
56 * responsibility of the postmaster. Regular backends may no longer modify it,
57 * but the postmaster may examine it. Thus, a backend initializing a slot
58 * must fully initialize the slot - and insert a write memory barrier - before
59 * marking it as in use.
60 *
61 * As an exception, however, even when the slot is in use, regular backends
62 * may set the 'terminate' flag for a slot, telling the postmaster not
63 * to restart it. Once the background worker is no longer running, the slot
64 * will be released for reuse.
65 *
66 * In addition to coordinating with the postmaster, backends modifying this
67 * data structure must coordinate with each other. Since they can take locks,
68 * this is straightforward: any backend wishing to manipulate a slot must
69 * take BackgroundWorkerLock in exclusive mode. Backends wishing to read
70 * data that might get concurrently modified by other backends should take
71 * this lock in shared mode. No matter what, backends reading this data
72 * structure must be able to tolerate concurrent modifications by the
73 * postmaster.
74 */
76{
77 bool in_use;
79 pid_t pid; /* InvalidPid = not started yet; 0 = dead */
80 uint64 generation; /* incremented when slot is recycled */
83
84/*
85 * In order to limit the total number of parallel workers (according to
86 * max_parallel_workers GUC), we maintain the number of active parallel
87 * workers. Since the postmaster cannot take locks, two variables are used for
88 * this purpose: the number of registered parallel workers (modified by the
89 * backends, protected by BackgroundWorkerLock) and the number of terminated
90 * parallel workers (modified only by the postmaster, lockless). The active
91 * number of parallel workers is the number of registered workers minus the
92 * terminated ones. These counters can of course overflow, but it's not
93 * important here since the subtraction will still give the right number.
94 */
102
108
110
111/*
112 * List of internal background worker entry points. We need this for
113 * reasons explained in LookupBackgroundWorkerFunction(), below.
114 */
115static const struct
116{
117 const char *fn_name;
120
121{
122 {
123 .fn_name = "ParallelWorkerMain",
124 .fn_addr = ParallelWorkerMain
125 },
126 {
127 .fn_name = "ApplyLauncherMain",
128 .fn_addr = ApplyLauncherMain
129 },
130 {
131 .fn_name = "ApplyWorkerMain",
132 .fn_addr = ApplyWorkerMain
133 },
134 {
135 .fn_name = "ParallelApplyWorkerMain",
136 .fn_addr = ParallelApplyWorkerMain
137 },
138 {
139 .fn_name = "TableSyncWorkerMain",
140 .fn_addr = TableSyncWorkerMain
141 },
142 {
143 .fn_name = "SequenceSyncWorkerMain",
144 .fn_addr = SequenceSyncWorkerMain
145 }
147
148/* Private functions. */
150
151
152/*
153 * Calculate shared memory needed.
154 */
155Size
157{
158 Size size;
159
160 /* Array of workers is variably sized. */
161 size = offsetof(BackgroundWorkerArray, slot);
163 sizeof(BackgroundWorkerSlot)));
164
165 return size;
166}
167
168/*
169 * Initialize shared memory.
170 */
171void
173{
174 bool found;
175
176 BackgroundWorkerData = ShmemInitStruct("Background Worker Data",
178 &found);
180 {
181 dlist_iter iter;
182 int slotno = 0;
183
187
188 /*
189 * Copy contents of worker list into shared memory. Record the shared
190 * memory slot assigned to each worker. This ensures a 1-to-1
191 * correspondence between the postmaster's private list and the array
192 * in shared memory.
193 */
195 {
198
199 rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
201 slot->in_use = true;
202 slot->terminate = false;
203 slot->pid = InvalidPid;
204 slot->generation = 0;
205 rw->rw_shmem_slot = slotno;
206 rw->rw_worker.bgw_notify_pid = 0; /* might be reinit after crash */
207 memcpy(&slot->worker, &rw->rw_worker, sizeof(BackgroundWorker));
208 ++slotno;
209 }
210
211 /*
212 * Mark any remaining slots as not in use.
213 */
215 {
217
218 slot->in_use = false;
219 ++slotno;
220 }
221 }
222 else
223 Assert(found);
224}
225
226/*
227 * Search the postmaster's backend-private list of RegisteredBgWorker objects
228 * for the one that maps to the given slot number.
229 */
230static RegisteredBgWorker *
232{
233 dlist_iter iter;
234
236 {
238
239 rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
240 if (rw->rw_shmem_slot == slotno)
241 return rw;
242 }
243
244 return NULL;
245}
246
247/*
248 * Notice changes to shared memory made by other backends.
249 * Accept new worker requests only if allow_new_workers is true.
250 *
251 * This code runs in the postmaster, so we must be very careful not to assume
252 * that shared memory contents are sane. Otherwise, a rogue backend could
253 * take out the postmaster.
254 */
255void
257{
258 int slotno;
259
260 /*
261 * The total number of slots stored in shared memory should match our
262 * notion of max_worker_processes. If it does not, something is very
263 * wrong. Further down, we always refer to this value as
264 * max_worker_processes, in case shared memory gets corrupted while we're
265 * looping.
266 */
268 {
269 ereport(LOG,
270 (errmsg("inconsistent background worker state (\"max_worker_processes\"=%d, total slots=%d)",
273 return;
274 }
275
276 /*
277 * Iterate through slots, looking for newly-registered workers or workers
278 * who must die.
279 */
281 {
284
285 if (!slot->in_use)
286 continue;
287
288 /*
289 * Make sure we don't see the in_use flag before the updated slot
290 * contents.
291 */
293
294 /* See whether we already know about this worker. */
296 if (rw != NULL)
297 {
298 /*
299 * In general, the worker data can't change after it's initially
300 * registered. However, someone can set the terminate flag.
301 */
302 if (slot->terminate && !rw->rw_terminate)
303 {
304 rw->rw_terminate = true;
305 if (rw->rw_pid != 0)
306 kill(rw->rw_pid, SIGTERM);
307 else
308 {
309 /* Report never-started, now-terminated worker as dead. */
311 }
312 }
313 continue;
314 }
315
316 /*
317 * If we aren't allowing new workers, then immediately mark it for
318 * termination; the next stanza will take care of cleaning it up.
319 * Doing this ensures that any process waiting for the worker will get
320 * awoken, even though the worker will never be allowed to run.
321 */
323 slot->terminate = true;
324
325 /*
326 * If the worker is marked for termination, we don't need to add it to
327 * the registered workers list; we can just free the slot. However, if
328 * bgw_notify_pid is set, the process that registered the worker may
329 * need to know that we've processed the terminate request, so be sure
330 * to signal it.
331 */
332 if (slot->terminate)
333 {
334 int notify_pid;
335
336 /*
337 * We need a memory barrier here to make sure that the load of
338 * bgw_notify_pid and the update of parallel_terminate_count
339 * complete before the store to in_use.
340 */
342 if ((slot->worker.bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
344 slot->pid = 0;
345
347 slot->in_use = false;
348
349 if (notify_pid != 0)
351
352 continue;
353 }
354
355 /*
356 * Copy the registration data into the registered workers list.
357 */
359 sizeof(RegisteredBgWorker),
361 if (rw == NULL)
362 {
363 ereport(LOG,
365 errmsg("out of memory")));
366 return;
367 }
368
369 /*
370 * Copy strings in a paranoid way. If shared memory is corrupted, the
371 * source data might not even be NUL-terminated.
372 */
374 slot->worker.bgw_name, BGW_MAXLEN);
376 slot->worker.bgw_type, BGW_MAXLEN);
381
382 /*
383 * Copy various fixed-size fields.
384 *
385 * flags, start_time, and restart_time are examined by the postmaster,
386 * but nothing too bad will happen if they are corrupted. The
387 * remaining fields will only be examined by the child process. It
388 * might crash, but we won't.
389 */
395
396 /*
397 * Copy the PID to be notified about state changes, but only if the
398 * postmaster knows about a backend with that PID. It isn't an error
399 * if the postmaster doesn't know about the PID, because the backend
400 * that requested the worker could have died (or been killed) just
401 * after doing so. Nonetheless, at least until we get some experience
402 * with how this plays out in the wild, log a message at a relative
403 * high debug level.
404 */
407 {
408 elog(DEBUG1, "worker notification PID %d is not valid",
409 (int) rw->rw_worker.bgw_notify_pid);
411 }
412
413 /* Initialize postmaster bookkeeping. */
414 rw->rw_pid = 0;
415 rw->rw_crashed_at = 0;
416 rw->rw_shmem_slot = slotno;
417 rw->rw_terminate = false;
418
419 /* Log it! */
421 (errmsg_internal("registering background worker \"%s\"",
422 rw->rw_worker.bgw_name)));
423
425 }
426}
427
428/*
429 * Forget about a background worker that's no longer needed.
430 *
431 * NOTE: The entry is unlinked from BackgroundWorkerList. If the caller is
432 * iterating through it, better use a mutable iterator!
433 *
434 * Caller is responsible for notifying bgw_notify_pid, if appropriate.
435 *
436 * This function must be invoked only in the postmaster.
437 */
438void
440{
442
445 Assert(slot->in_use);
446
447 /*
448 * We need a memory barrier here to make sure that the update of
449 * parallel_terminate_count completes before the store to in_use.
450 */
453
455 slot->in_use = false;
456
458 (errmsg_internal("unregistering background worker \"%s\"",
459 rw->rw_worker.bgw_name)));
460
462 pfree(rw);
463}
464
465/*
466 * Report the PID of a newly-launched background worker in shared memory.
467 *
468 * This function should only be called from the postmaster.
469 */
470void
482
483/*
484 * Report that the PID of a background worker is now zero because a
485 * previously-running background worker has exited.
486 *
487 * NOTE: The entry may be unlinked from BackgroundWorkerList. If the caller
488 * is iterating through it, better use a mutable iterator!
489 *
490 * This function should only be called from the postmaster.
491 */
492void
494{
496 int notify_pid;
497
500 slot->pid = rw->rw_pid;
502
503 /*
504 * If this worker is slated for deregistration, do that before notifying
505 * the process which started it. Otherwise, if that process tries to
506 * reuse the slot immediately, it might not be available yet. In theory
507 * that could happen anyway if the process checks slot->pid at just the
508 * wrong moment, but this makes the window narrower.
509 */
510 if (rw->rw_terminate ||
513
514 if (notify_pid != 0)
516}
517
518/*
519 * Cancel SIGUSR1 notifications for a PID belonging to an exiting backend.
520 *
521 * This function should only be called from the postmaster.
522 */
523void
525{
526 dlist_iter iter;
527
529 {
531
532 rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
533 if (rw->rw_worker.bgw_notify_pid == pid)
535 }
536}
537
538/*
539 * Cancel any not-yet-started worker requests that have waiting processes.
540 *
541 * This is called during a normal ("smart" or "fast") database shutdown.
542 * After this point, no new background workers will be started, so anything
543 * that might be waiting for them needs to be kicked off its wait. We do
544 * that by canceling the bgworker registration entirely, which is perhaps
545 * overkill, but since we're shutting down it does not matter whether the
546 * registration record sticks around.
547 *
548 * This function should only be called from the postmaster.
549 */
550void
552{
554
556 {
559
560 rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
563
564 /* If it's not yet started, and there's someone waiting ... */
565 if (slot->pid == InvalidPid &&
566 rw->rw_worker.bgw_notify_pid != 0)
567 {
568 /* ... then zap it, and notify the waiter */
570
572 if (notify_pid != 0)
574 }
575 }
576}
577
578/*
579 * Reset background worker crash state.
580 *
581 * We assume that, after a crash-and-restart cycle, background workers without
582 * the never-restart flag should be restarted immediately, instead of waiting
583 * for bgw_restart_time to elapse. On the other hand, workers with that flag
584 * should be forgotten immediately, since we won't ever restart them.
585 *
586 * This function should only be called from the postmaster.
587 */
588void
590{
592
594 {
596
597 rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
598
600 {
601 /*
602 * Workers marked BGW_NEVER_RESTART shouldn't get relaunched after
603 * the crash, so forget about them. (If we wait until after the
604 * crash to forget about them, and they are parallel workers,
605 * parallel_terminate_count will get incremented after we've
606 * already zeroed parallel_register_count, which would be bad.)
607 */
609 }
610 else
611 {
612 /*
613 * The accounting which we do via parallel_register_count and
614 * parallel_terminate_count would get messed up if a worker marked
615 * parallel could survive a crash and restart cycle. All such
616 * workers should be marked BGW_NEVER_RESTART, and thus control
617 * should never reach this branch.
618 */
620
621 /*
622 * Allow this worker to be restarted immediately after we finish
623 * resetting.
624 */
625 rw->rw_crashed_at = 0;
626 rw->rw_pid = 0;
627
628 /*
629 * If there was anyone waiting for it, they're history.
630 */
632 }
633 }
634}
635
636/*
637 * Complain about the BackgroundWorker definition using error level elevel.
638 * Return true if it looks ok, false if not (unless elevel >= ERROR, in
639 * which case we won't return at all in the not-OK case).
640 */
641static bool
643{
644 /* sanity check for flags */
645
646 /*
647 * We used to support workers not connected to shared memory, but don't
648 * anymore. Thus this is a required flag now. We're not removing the flag
649 * for compatibility reasons and because the flag still provides some
650 * signal when reading code.
651 */
652 if (!(worker->bgw_flags & BGWORKER_SHMEM_ACCESS))
653 {
654 ereport(elevel,
656 errmsg("background worker \"%s\": background workers without shared memory access are not supported",
657 worker->bgw_name)));
658 return false;
659 }
660
662 {
664 {
665 ereport(elevel,
667 errmsg("background worker \"%s\": cannot request database access if starting at postmaster start",
668 worker->bgw_name)));
669 return false;
670 }
671
672 /* XXX other checks? */
673 }
674
675 /* Interruptible workers require a database connection */
676 if ((worker->bgw_flags & BGWORKER_INTERRUPTIBLE) &&
678 {
679 ereport(elevel,
681 errmsg("background worker \"%s\": cannot make background workers interruptible without database access",
682 worker->bgw_name)));
683 return false;
684 }
685
686 if ((worker->bgw_restart_time < 0 &&
688 (worker->bgw_restart_time > USECS_PER_DAY / 1000))
689 {
690 ereport(elevel,
692 errmsg("background worker \"%s\": invalid restart interval",
693 worker->bgw_name)));
694 return false;
695 }
696
697 /*
698 * Parallel workers may not be configured for restart, because the
699 * parallel_register_count/parallel_terminate_count accounting can't
700 * handle parallel workers lasting through a crash-and-restart cycle.
701 */
702 if (worker->bgw_restart_time != BGW_NEVER_RESTART &&
703 (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
704 {
705 ereport(elevel,
707 errmsg("background worker \"%s\": parallel workers may not be configured for restart",
708 worker->bgw_name)));
709 return false;
710 }
711
712 /*
713 * If bgw_type is not filled in, use bgw_name.
714 */
715 if (strcmp(worker->bgw_type, "") == 0)
716 strcpy(worker->bgw_type, worker->bgw_name);
717
718 return true;
719}
720
721/*
722 * Standard SIGTERM handler for background workers
723 */
724static void
726{
728
731 errmsg("terminating background worker \"%s\" due to administrator command",
733}
734
735/*
736 * Main entry point for background worker processes.
737 */
738void
740{
742 BackgroundWorker *worker;
744
745 if (startup_data == NULL)
746 elog(FATAL, "unable to find bgworker entry");
749 memcpy(worker, startup_data, sizeof(BackgroundWorker));
750
751 /*
752 * Now that we're done reading the startup data, release postmaster's
753 * working memory context.
754 */
756 {
759 }
760
761 MyBgworkerEntry = worker;
763 init_ps_display(worker->bgw_name);
764
766
767 /* Apply PostAuthDelay */
768 if (PostAuthDelay > 0)
769 pg_usleep(PostAuthDelay * 1000000L);
770
771 /*
772 * Set up signal handlers.
773 */
775 {
776 /*
777 * SIGINT is used to signal canceling the current action
778 */
782
783 /* XXX Any other handlers needed here? */
784 }
785 else
786 {
790 }
792 /* SIGQUIT handler was already set up by InitPostmasterChild */
794
795 InitializeTimeouts(); /* establishes SIGALRM handler */
796
800
801 /*
802 * If an exception is encountered, processing resumes here.
803 *
804 * We just need to clean up, report the error, and go away.
805 */
806 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
807 {
808 /* Since not using PG_TRY, must reset error stack by hand */
810
811 /* Prevent interrupts while cleaning up */
813
814 /*
815 * sigsetjmp will have blocked all signals, but we may need to accept
816 * signals while communicating with our parallel leader. Once we've
817 * done HOLD_INTERRUPTS() it should be safe to unblock signals.
818 */
820
821 /* Report the error to the parallel leader and the server log */
823
824 /*
825 * Do we need more cleanup here? For shmem-connected bgworkers, we
826 * will call InitProcess below, which will install ProcKill as exit
827 * callback. That will take care of releasing locks, etc.
828 */
829
830 /* and go away */
831 proc_exit(1);
832 }
833
834 /* We can now handle ereport(ERROR) */
836
837 /*
838 * Create a per-backend PGPROC struct in shared memory. We must do this
839 * before we can use LWLocks or access any shared memory.
840 */
841 InitProcess();
842
843 /*
844 * Early initialization.
845 */
846 BaseInit();
847
848 /*
849 * Look up the entry point function, loading its library if necessary.
850 */
852 worker->bgw_function_name);
853
854 /*
855 * Note that in normal processes, we would call InitPostgres here. For a
856 * worker, however, we don't know what database to connect to, yet; so we
857 * need to wait until the user code does it via
858 * BackgroundWorkerInitializeConnection().
859 */
860
861 /*
862 * Now invoke the user-defined worker code
863 */
864 entrypt(worker->bgw_main_arg);
865
866 /* ... and if it returns, we're done */
867 proc_exit(0);
868}
869
870/*
871 * Connect background worker to a database.
872 */
873void
875{
877 bits32 init_flags = 0; /* never honor session_preload_libraries */
878
879 /* ignore datallowconn and ACL_CONNECT? */
880 if (flags & BGWORKER_BYPASS_ALLOWCONN)
882 /* ignore rolcanlogin? */
885
886 /* XXX is this the right errcode? */
890 errmsg("database connection requirement not indicated during registration")));
891
892 InitPostgres(dbname, InvalidOid, /* database to connect to */
893 username, InvalidOid, /* role to connect as */
895 NULL); /* no out_dbname */
896
897 /* it had better not gotten out of "init" mode yet */
900 (errmsg("invalid processing mode in background worker")));
902}
903
904/*
905 * Connect background worker to a database using OIDs.
906 */
907void
909{
911 bits32 init_flags = 0; /* never honor session_preload_libraries */
912
913 /* ignore datallowconn and ACL_CONNECT? */
914 if (flags & BGWORKER_BYPASS_ALLOWCONN)
916 /* ignore rolcanlogin? */
919
920 /* XXX is this the right errcode? */
924 errmsg("database connection requirement not indicated during registration")));
925
926 InitPostgres(NULL, dboid, /* database to connect to */
927 NULL, useroid, /* role to connect as */
929 NULL); /* no out_dbname */
930
931 /* it had better not gotten out of "init" mode yet */
934 (errmsg("invalid processing mode in background worker")));
936}
937
938/*
939 * Block/unblock signals in a background worker
940 */
941void
946
947void
952
953/*
954 * Register a new static background worker.
955 *
956 * This can only be called directly from postmaster or in the _PG_init
957 * function of a module library that's loaded by shared_preload_libraries;
958 * otherwise it will have no effect.
959 */
960void
962{
964 static int numworkers = 0;
965
966 /*
967 * Static background workers can only be registered in the postmaster
968 * process.
969 */
971 {
972 /*
973 * In EXEC_BACKEND or single-user mode, we process
974 * shared_preload_libraries in backend processes too. We cannot
975 * register static background workers at that stage, but many
976 * libraries' _PG_init() functions don't distinguish whether they're
977 * being loaded in the postmaster or in a backend, they just check
978 * process_shared_preload_libraries_in_progress. It's a bit sloppy,
979 * but for historical reasons we tolerate it. In EXEC_BACKEND mode,
980 * the background workers should already have been registered when the
981 * library was loaded in postmaster.
982 */
984 return;
985 ereport(LOG,
987 errmsg("background worker \"%s\": must be registered in \"shared_preload_libraries\"",
988 worker->bgw_name)));
989 return;
990 }
991
992 /*
993 * Cannot register static background workers after calling
994 * BackgroundWorkerShmemInit().
995 */
997 elog(ERROR, "cannot register background worker \"%s\" after shmem init",
998 worker->bgw_name);
999
1001 (errmsg_internal("registering background worker \"%s\"", worker->bgw_name)));
1002
1003 if (!SanityCheckBackgroundWorker(worker, LOG))
1004 return;
1005
1006 if (worker->bgw_notify_pid != 0)
1007 {
1008 ereport(LOG,
1010 errmsg("background worker \"%s\": only dynamic background workers can request notification",
1011 worker->bgw_name)));
1012 return;
1013 }
1014
1015 /*
1016 * Enforce maximum number of workers. Note this is overly restrictive: we
1017 * could allow more non-shmem-connected workers, because these don't count
1018 * towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
1019 * important to relax this restriction.
1020 */
1022 {
1023 ereport(LOG,
1025 errmsg("too many background workers"),
1026 errdetail_plural("Up to %d background worker can be registered with the current settings.",
1027 "Up to %d background workers can be registered with the current settings.",
1030 errhint("Consider increasing the configuration parameter \"%s\".", "max_worker_processes")));
1031 return;
1032 }
1033
1034 /*
1035 * Copy the registration data into the registered workers list.
1036 */
1038 sizeof(RegisteredBgWorker),
1040 if (rw == NULL)
1041 {
1042 ereport(LOG,
1044 errmsg("out of memory")));
1045 return;
1046 }
1047
1048 rw->rw_worker = *worker;
1049 rw->rw_pid = 0;
1050 rw->rw_crashed_at = 0;
1051 rw->rw_terminate = false;
1052
1054}
1055
1056/*
1057 * Register a new background worker from a regular backend.
1058 *
1059 * Returns true on success and false on failure. Failure typically indicates
1060 * that no background worker slots are currently available.
1061 *
1062 * If handle != NULL, we'll set *handle to a pointer that can subsequently
1063 * be used as an argument to GetBackgroundWorkerPid(). The caller can
1064 * free this pointer using pfree(), if desired.
1065 */
1066bool
1068 BackgroundWorkerHandle **handle)
1069{
1070 int slotno;
1071 bool success = false;
1072 bool parallel;
1073 uint64 generation = 0;
1074
1075 /*
1076 * We can't register dynamic background workers from the postmaster. If
1077 * this is a standalone backend, we're the only process and can't start
1078 * any more. In a multi-process environment, it might be theoretically
1079 * possible, but we don't currently support it due to locking
1080 * considerations; see comments on the BackgroundWorkerSlot data
1081 * structure.
1082 */
1083 if (!IsUnderPostmaster)
1084 return false;
1085
1086 if (!SanityCheckBackgroundWorker(worker, ERROR))
1087 return false;
1088
1089 parallel = (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0;
1090
1092
1093 /*
1094 * If this is a parallel worker, check whether there are already too many
1095 * parallel workers; if so, don't register another one. Our view of
1096 * parallel_terminate_count may be slightly stale, but that doesn't really
1097 * matter: we would have gotten the same result if we'd arrived here
1098 * slightly earlier anyway. There's no help for it, either, since the
1099 * postmaster must not take locks; a memory barrier wouldn't guarantee
1100 * anything useful.
1101 */
1105 {
1110 return false;
1111 }
1112
1113 /*
1114 * Look for an unused slot. If we find one, grab it.
1115 */
1117 {
1119
1120 if (!slot->in_use)
1121 {
1122 memcpy(&slot->worker, worker, sizeof(BackgroundWorker));
1123 slot->pid = InvalidPid; /* indicates not started yet */
1124 slot->generation++;
1125 slot->terminate = false;
1126 generation = slot->generation;
1127 if (parallel)
1129
1130 /*
1131 * Make sure postmaster doesn't see the slot as in use before it
1132 * sees the new contents.
1133 */
1135
1136 slot->in_use = true;
1137 success = true;
1138 break;
1139 }
1140 }
1141
1143
1144 /* If we found a slot, tell the postmaster to notice the change. */
1145 if (success)
1147
1148 /*
1149 * If we found a slot and the user has provided a handle, initialize it.
1150 */
1151 if (success && handle)
1152 {
1154 (*handle)->slot = slotno;
1155 (*handle)->generation = generation;
1156 }
1157
1158 return success;
1159}
1160
1161/*
1162 * Get the PID of a dynamically-registered background worker.
1163 *
1164 * If the worker is determined to be running, the return value will be
1165 * BGWH_STARTED and *pidp will get the PID of the worker process. If the
1166 * postmaster has not yet attempted to start the worker, the return value will
1167 * be BGWH_NOT_YET_STARTED. Otherwise, the return value is BGWH_STOPPED.
1168 *
1169 * BGWH_STOPPED can indicate either that the worker is temporarily stopped
1170 * (because it is configured for automatic restart and exited non-zero),
1171 * or that the worker is permanently stopped (because it exited with exit
1172 * code 0, or was not configured for automatic restart), or even that the
1173 * worker was unregistered without ever starting (either because startup
1174 * failed and the worker is not configured for automatic restart, or because
1175 * TerminateBackgroundWorker was used before the worker was successfully
1176 * started).
1177 */
1180{
1182 pid_t pid;
1183
1184 Assert(handle->slot < max_worker_processes);
1185 slot = &BackgroundWorkerData->slot[handle->slot];
1186
1187 /*
1188 * We could probably arrange to synchronize access to data using memory
1189 * barriers only, but for now, let's just keep it simple and grab the
1190 * lock. It seems unlikely that there will be enough traffic here to
1191 * result in meaningful contention.
1192 */
1194
1195 /*
1196 * The generation number can't be concurrently changed while we hold the
1197 * lock. The pid, which is updated by the postmaster, can change at any
1198 * time, but we assume such changes are atomic. So the value we read
1199 * won't be garbage, but it might be out of date by the time the caller
1200 * examines it (but that's unavoidable anyway).
1201 *
1202 * The in_use flag could be in the process of changing from true to false,
1203 * but if it is already false then it can't change further.
1204 */
1205 if (handle->generation != slot->generation || !slot->in_use)
1206 pid = 0;
1207 else
1208 pid = slot->pid;
1209
1210 /* All done. */
1212
1213 if (pid == 0)
1214 return BGWH_STOPPED;
1215 else if (pid == InvalidPid)
1216 return BGWH_NOT_YET_STARTED;
1217 *pidp = pid;
1218 return BGWH_STARTED;
1219}
1220
1221/*
1222 * Wait for a background worker to start up.
1223 *
1224 * This is like GetBackgroundWorkerPid(), except that if the worker has not
1225 * yet started, we wait for it to do so; thus, BGWH_NOT_YET_STARTED is never
1226 * returned. However, if the postmaster has died, we give up and return
1227 * BGWH_POSTMASTER_DIED, since it that case we know that startup will not
1228 * take place.
1229 *
1230 * The caller *must* have set our PID as the worker's bgw_notify_pid,
1231 * else we will not be awoken promptly when the worker's state changes.
1232 */
1235{
1236 BgwHandleStatus status;
1237 int rc;
1238
1239 for (;;)
1240 {
1241 pid_t pid;
1242
1244
1245 status = GetBackgroundWorkerPid(handle, &pid);
1246 if (status == BGWH_STARTED)
1247 *pidp = pid;
1248 if (status != BGWH_NOT_YET_STARTED)
1249 break;
1250
1251 rc = WaitLatch(MyLatch,
1254
1255 if (rc & WL_POSTMASTER_DEATH)
1256 {
1257 status = BGWH_POSTMASTER_DIED;
1258 break;
1259 }
1260
1262 }
1263
1264 return status;
1265}
1266
1267/*
1268 * Wait for a background worker to stop.
1269 *
1270 * If the worker hasn't yet started, or is running, we wait for it to stop
1271 * and then return BGWH_STOPPED. However, if the postmaster has died, we give
1272 * up and return BGWH_POSTMASTER_DIED, because it's the postmaster that
1273 * notifies us when a worker's state changes.
1274 *
1275 * The caller *must* have set our PID as the worker's bgw_notify_pid,
1276 * else we will not be awoken promptly when the worker's state changes.
1277 */
1280{
1281 BgwHandleStatus status;
1282 int rc;
1283
1284 for (;;)
1285 {
1286 pid_t pid;
1287
1289
1290 status = GetBackgroundWorkerPid(handle, &pid);
1291 if (status == BGWH_STOPPED)
1292 break;
1293
1294 rc = WaitLatch(MyLatch,
1297
1298 if (rc & WL_POSTMASTER_DEATH)
1299 {
1300 status = BGWH_POSTMASTER_DIED;
1301 break;
1302 }
1303
1305 }
1306
1307 return status;
1308}
1309
1310/*
1311 * Instruct the postmaster to terminate a background worker.
1312 *
1313 * Note that it's safe to do this without regard to whether the worker is
1314 * still running, or even if the worker may already have exited and been
1315 * unregistered.
1316 */
1317void
1319{
1321 bool signal_postmaster = false;
1322
1323 Assert(handle->slot < max_worker_processes);
1324 slot = &BackgroundWorkerData->slot[handle->slot];
1325
1326 /* Set terminate flag in shared memory, unless slot has been reused. */
1328 if (handle->generation == slot->generation)
1329 {
1330 slot->terminate = true;
1331 signal_postmaster = true;
1332 }
1334
1335 /* Make sure the postmaster notices the change to shared memory. */
1338}
1339
1340/*
1341 * Look up (and possibly load) a bgworker entry point function.
1342 *
1343 * For functions contained in the core code, we use library name "postgres"
1344 * and consult the InternalBGWorkers array. External functions are
1345 * looked up, and loaded if necessary, using load_external_function().
1346 *
1347 * The point of this is to pass function names as strings across process
1348 * boundaries. We can't pass actual function addresses because of the
1349 * possibility that the function has been loaded at a different address
1350 * in a different process. This is obviously a hazard for functions in
1351 * loadable libraries, but it can happen even for functions in the core code
1352 * on platforms using EXEC_BACKEND (e.g., Windows).
1353 *
1354 * At some point it might be worthwhile to get rid of InternalBGWorkers[]
1355 * in favor of applying load_external_function() for core functions too;
1356 * but that raises portability issues that are not worth addressing now.
1357 */
1358static bgworker_main_type
1360{
1361 /*
1362 * If the function is to be loaded from postgres itself, search the
1363 * InternalBGWorkers array.
1364 */
1365 if (strcmp(libraryname, "postgres") == 0)
1366 {
1367 int i;
1368
1369 for (i = 0; i < lengthof(InternalBGWorkers); i++)
1370 {
1372 return InternalBGWorkers[i].fn_addr;
1373 }
1374
1375 /* We can only reach this by programming error. */
1376 elog(ERROR, "internal function \"%s\" not found", funcname);
1377 }
1378
1379 /* Otherwise load from external library. */
1380 return (bgworker_main_type)
1382}
1383
1384/*
1385 * Given a PID, get the bgw_type of the background worker. Returns NULL if
1386 * not a valid background worker.
1387 *
1388 * The return value is in static memory belonging to this function, so it has
1389 * to be used before calling this function again. This is so that the caller
1390 * doesn't have to worry about the background worker locking protocol.
1391 */
1392const char *
1394{
1395 int slotno;
1396 bool found = false;
1397 static char result[BGW_MAXLEN];
1398
1400
1402 {
1404
1405 if (slot->pid > 0 && slot->pid == pid)
1406 {
1407 strcpy(result, slot->worker.bgw_type);
1408 found = true;
1409 break;
1410 }
1411 }
1412
1414
1415 if (!found)
1416 return NULL;
1417
1418 return result;
1419}
1420
1421/*
1422 * Terminate all background workers connected to the given database, if the
1423 * workers can be interrupted.
1424 */
1425void
1427{
1428 bool signal_postmaster = false;
1429
1431
1432 /*
1433 * Iterate through slots, looking for workers connected to the given
1434 * database.
1435 */
1437 {
1439
1440 if (slot->in_use &&
1442 {
1443 PGPROC *proc = BackendPidGetProc(slot->pid);
1444
1445 if (proc && proc->databaseId == databaseId)
1446 {
1447 slot->terminate = true;
1448 signal_postmaster = true;
1449 }
1450 }
1451 }
1452
1454
1455 /* Make sure the postmaster notices the change to shared memory. */
1458}
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:141
#define pg_read_barrier()
Definition atomics.h:154
#define pg_write_barrier()
Definition atomics.h:155
void ParallelWorkerMain(Datum main_arg)
Definition parallel.c:1299
sigset_t UnBlockSig
Definition pqsignal.c:22
sigset_t BlockSig
Definition pqsignal.c:23
void ApplyWorkerMain(Datum main_arg)
Definition worker.c:5925
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition bgworker.c:961
BgwHandleStatus WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition bgworker.c:1234
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition bgworker.c:874
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition bgworker.c:642
void ReportBackgroundWorkerPID(RegisteredBgWorker *rw)
Definition bgworker.c:471
void TerminateBackgroundWorker(BackgroundWorkerHandle *handle)
Definition bgworker.c:1318
static const struct @18 InternalBGWorkers[]
void TerminateBackgroundWorkersForDatabase(Oid databaseId)
Definition bgworker.c:1426
void ReportBackgroundWorkerExit(RegisteredBgWorker *rw)
Definition bgworker.c:493
BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
Definition bgworker.c:1279
void ResetBackgroundWorkerCrashTimes(void)
Definition bgworker.c:589
void BackgroundWorkerShmemInit(void)
Definition bgworker.c:172
void BackgroundWorkerUnblockSignals(void)
Definition bgworker.c:948
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
Definition bgworker.c:908
void BackgroundWorkerBlockSignals(void)
Definition bgworker.c:942
dlist_head BackgroundWorkerList
Definition bgworker.c:41
void ForgetBackgroundWorker(RegisteredBgWorker *rw)
Definition bgworker.c:439
const char * fn_name
Definition bgworker.c:117
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition bgworker.c:1179
static BackgroundWorkerArray * BackgroundWorkerData
Definition bgworker.c:109
static RegisteredBgWorker * FindRegisteredWorkerBySlotNumber(int slotno)
Definition bgworker.c:231
static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
Definition bgworker.c:1359
static void bgworker_die(SIGNAL_ARGS)
Definition bgworker.c:725
void BackgroundWorkerStopNotifications(pid_t pid)
Definition bgworker.c:524
Size BackgroundWorkerShmemSize(void)
Definition bgworker.c:156
void BackgroundWorkerStateChange(bool allow_new_workers)
Definition bgworker.c:256
const char * GetBackgroundWorkerTypeByPid(pid_t pid)
Definition bgworker.c:1393
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition bgworker.c:1067
void BackgroundWorkerMain(const void *startup_data, size_t startup_data_len)
Definition bgworker.c:739
bgworker_main_type fn_addr
Definition bgworker.c:118
void ForgetUnstartedBackgroundWorkers(void)
Definition bgworker.c:551
#define BGW_NEVER_RESTART
Definition bgworker.h:92
#define BGWORKER_BYPASS_ROLELOGINCHECK
Definition bgworker.h:167
#define BGW_EXTRALEN
Definition bgworker.h:94
#define BGWORKER_CLASS_PARALLEL
Definition bgworker.h:75
#define BGWORKER_INTERRUPTIBLE
Definition bgworker.h:67
BgwHandleStatus
Definition bgworker.h:111
@ BGWH_POSTMASTER_DIED
Definition bgworker.h:115
@ BGWH_STARTED
Definition bgworker.h:112
@ BGWH_NOT_YET_STARTED
Definition bgworker.h:113
@ BGWH_STOPPED
Definition bgworker.h:114
@ BgWorkerStart_PostmasterStart
Definition bgworker.h:86
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition bgworker.h:60
#define BGWORKER_BYPASS_ALLOWCONN
Definition bgworker.h:166
#define BGWORKER_SHMEM_ACCESS
Definition bgworker.h:53
void(* bgworker_main_type)(Datum main_arg)
Definition bgworker.h:79
#define BGW_MAXLEN
Definition bgworker.h:93
#define MAX_PARALLEL_WORKER_LIMIT
#define SIGNAL_ARGS
Definition c.h:1363
#define Assert(condition)
Definition c.h:873
#define FLEXIBLE_ARRAY_MEMBER
Definition c.h:480
uint32 bits32
Definition c.h:555
uint64_t uint64
Definition c.h:547
uint32_t uint32
Definition c.h:546
#define lengthof(array)
Definition c.h:803
size_t Size
Definition c.h:619
#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:95
int errmsg_internal(const char *fmt,...)
Definition elog.c:1170
void EmitErrorReport(void)
Definition elog.c:1704
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:1308
int errhint(const char *fmt,...)
Definition elog.c:1330
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
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 elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define MCXT_ALLOC_ZERO
Definition fe_memutils.h:30
#define palloc_object(type)
Definition fe_memutils.h:74
#define MCXT_ALLOC_NO_OOM
Definition fe_memutils.h:29
bool IsUnderPostmaster
Definition globals.c:120
int max_parallel_workers
Definition globals.c:145
bool IsPostmasterEnvironment
Definition globals.c:119
struct Latch * MyLatch
Definition globals.c:63
int max_worker_processes
Definition globals.c:144
#define dlist_foreach(iter, lhead)
Definition ilist.h:623
static void dlist_delete(dlist_node *node)
Definition ilist.h:405
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition ilist.h:347
#define dlist_foreach_modify(iter, lhead)
Definition ilist.h:640
#define DLIST_STATIC_INIT(name)
Definition ilist.h:281
#define dlist_container(type, membername, ptr)
Definition ilist.h:593
#define funcname
static bool success
Definition initdb.c:187
static char * username
Definition initdb.c:153
void proc_exit(int code)
Definition ipc.c:105
int i
Definition isn.c:77
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
void ApplyLauncherMain(Datum main_arg)
Definition launcher.c:1204
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1176
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
@ LW_SHARED
Definition lwlock.h:113
@ LW_EXCLUSIVE
Definition lwlock.h:112
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
void pfree(void *pointer)
Definition mcxt.c:1616
MemoryContext TopMemoryContext
Definition mcxt.c:166
void * MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
Definition mcxt.c:1289
MemoryContext PostmasterContext
Definition mcxt.c:168
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
@ NormalProcessing
Definition miscadmin.h:472
@ InitProcessing
Definition miscadmin.h:471
#define GetProcessingMode()
Definition miscadmin.h:481
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
#define HOLD_INTERRUPTS()
Definition miscadmin.h:134
#define IsInitProcessingMode()
Definition miscadmin.h:478
#define SetProcessingMode(mode)
Definition miscadmin.h:483
@ B_BG_WORKER
Definition miscadmin.h:346
#define INIT_PG_OVERRIDE_ROLE_LOGIN
Definition miscadmin.h:501
#define INIT_PG_OVERRIDE_ALLOW_CONNS
Definition miscadmin.h:500
#define InvalidPid
Definition miscadmin.h:32
BackendType MyBackendType
Definition miscinit.c:64
bool process_shared_preload_libraries_in_progress
Definition miscinit.c:1786
#define MAXPGPATH
void SendPostmasterSignal(PMSignalReason reason)
Definition pmsignal.c:165
@ PMSIGNAL_BACKGROUND_WORKER_CHANGE
Definition pmsignal.h:41
#define pqsignal
Definition port.h:547
int PostAuthDelay
Definition postgres.c:100
void FloatExceptionHandler(SIGNAL_ARGS)
Definition postgres.c:3079
void StatementCancelHandler(SIGNAL_ARGS)
Definition postgres.c:3062
#define InvalidOid
unsigned int Oid
void BaseInit(void)
Definition postinit.c:607
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, bits32 flags, char *out_dbname)
Definition postinit.c:710
BackgroundWorker * MyBgworkerEntry
Definition postmaster.c:200
bool PostmasterMarkPIDForWorkerNotify(int pid)
static int fb(int x)
PGPROC * BackendPidGetProc(int pid)
Definition procarray.c:3159
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition procsignal.c:677
void init_ps_display(const char *fixed_part)
Definition ps_status.c:285
void SequenceSyncWorkerMain(Datum main_arg)
Size add_size(Size s1, Size s2)
Definition shmem.c:482
Size mul_size(Size s1, Size s2)
Definition shmem.c:497
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition shmem.c:378
void pg_usleep(long microsec)
Definition signal.c:53
void InitProcess(void)
Definition proc.c:395
char * dbname
Definition streamutil.c:49
uint32 parallel_terminate_count
Definition bgworker.c:99
uint32 parallel_register_count
Definition bgworker.c:98
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition bgworker.c:100
BackgroundWorker worker
Definition bgworker.c:81
char bgw_function_name[BGW_MAXLEN]
Definition bgworker.h:104
char bgw_name[BGW_MAXLEN]
Definition bgworker.h:98
char bgw_type[BGW_MAXLEN]
Definition bgworker.h:99
BgWorkerStartTime bgw_start_time
Definition bgworker.h:101
char bgw_extra[BGW_EXTRALEN]
Definition bgworker.h:106
pid_t bgw_notify_pid
Definition bgworker.h:107
char bgw_library_name[MAXPGPATH]
Definition bgworker.h:103
Definition proc.h:179
Oid databaseId
Definition proc.h:224
BackgroundWorker rw_worker
dlist_node * cur
Definition ilist.h:179
dlist_node * cur
Definition ilist.h:200
void TableSyncWorkerMain(Datum main_arg)
Definition tablesync.c:1576
void InitializeTimeouts(void)
Definition timeout.c:470
#define WL_LATCH_SET
#define WL_POSTMASTER_DEATH
#define SIGCHLD
Definition win32_port.h:168
#define SIGHUP
Definition win32_port.h:158
#define SIGPIPE
Definition win32_port.h:163
#define kill(pid, sig)
Definition win32_port.h:490
#define SIGUSR1
Definition win32_port.h:170
#define SIGUSR2
Definition win32_port.h:171