Age Owner Branch data TLA Line data Source code
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"
20 : : #include "postmaster/bgworker_internals.h"
21 : : #include "postmaster/postmaster.h"
22 : : #include "replication/logicallauncher.h"
23 : : #include "replication/logicalworker.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 : : #include "utils/wait_event.h"
38 : :
39 : : /*
40 : : * The postmaster's list of registered background workers, in private memory.
41 : : */
42 : : dlist_head BackgroundWorkerList = DLIST_STATIC_INIT(BackgroundWorkerList);
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 */
82 : : BackgroundWorker worker;
83 : : } BackgroundWorkerSlot;
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 : : {
98 : : int total_slots;
99 : : uint32 parallel_register_count;
100 : : uint32 parallel_terminate_count;
101 : : BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER];
102 : : } BackgroundWorkerArray;
103 : :
104 : : struct BackgroundWorkerHandle
105 : : {
106 : : int slot;
107 : : uint64 generation;
108 : : };
109 : :
110 : : static BackgroundWorkerArray *BackgroundWorkerData;
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;
119 : : bgworker_main_type fn_addr;
120 : : } InternalBGWorkers[] =
121 : :
122 : : {
123 : : {
124 : : .fn_name = "ParallelWorkerMain",
125 : : .fn_addr = ParallelWorkerMain
126 : : },
127 : : {
128 : : .fn_name = "ApplyLauncherMain",
129 : : .fn_addr = ApplyLauncherMain
130 : : },
131 : : {
132 : : .fn_name = "ApplyWorkerMain",
133 : : .fn_addr = ApplyWorkerMain
134 : : },
135 : : {
136 : : .fn_name = "ParallelApplyWorkerMain",
137 : : .fn_addr = ParallelApplyWorkerMain
138 : : },
139 : : {
140 : : .fn_name = "TableSyncWorkerMain",
141 : : .fn_addr = TableSyncWorkerMain
142 : : },
143 : : {
144 : : .fn_name = "SequenceSyncWorkerMain",
145 : : .fn_addr = SequenceSyncWorkerMain
146 : : }
147 : : };
148 : :
149 : : /* Private functions. */
150 : : static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname);
151 : :
152 : :
153 : : /*
154 : : * Calculate shared memory needed.
155 : : */
156 : : Size
4625 rhaas@postgresql.org 157 :CBC 3297 : BackgroundWorkerShmemSize(void)
158 : : {
159 : : Size size;
160 : :
161 : : /* Array of workers is variably sized. */
162 : 3297 : size = offsetof(BackgroundWorkerArray, slot);
163 : 3297 : size = add_size(size, mul_size(max_worker_processes,
164 : : sizeof(BackgroundWorkerSlot)));
165 : :
166 : 3297 : return size;
167 : : }
168 : :
169 : : /*
170 : : * Initialize shared memory.
171 : : */
172 : : void
173 : 1150 : BackgroundWorkerShmemInit(void)
174 : : {
175 : : bool found;
176 : :
177 : 1150 : BackgroundWorkerData = ShmemInitStruct("Background Worker Data",
178 : : BackgroundWorkerShmemSize(),
179 : : &found);
180 [ + - ]: 1150 : if (!IsUnderPostmaster)
181 : : {
182 : : dlist_iter iter;
183 : 1150 : int slotno = 0;
184 : :
185 : 1150 : BackgroundWorkerData->total_slots = max_worker_processes;
3390 186 : 1150 : BackgroundWorkerData->parallel_register_count = 0;
187 : 1150 : BackgroundWorkerData->parallel_terminate_count = 0;
188 : :
189 : : /*
190 : : * Copy contents of worker list into shared memory. Record the shared
191 : : * memory slot assigned to each worker. This ensures a 1-to-1
192 : : * correspondence between the postmaster's private list and the array
193 : : * in shared memory.
194 : : */
583 heikki.linnakangas@i 195 [ + - + + ]: 2025 : dlist_foreach(iter, &BackgroundWorkerList)
196 : : {
4625 rhaas@postgresql.org 197 : 875 : BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
198 : : RegisteredBgWorker *rw;
199 : :
583 heikki.linnakangas@i 200 : 875 : rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
4625 rhaas@postgresql.org 201 [ - + ]: 875 : Assert(slotno < max_worker_processes);
202 : 875 : slot->in_use = true;
4531 203 : 875 : slot->terminate = false;
4582 204 : 875 : slot->pid = InvalidPid;
205 : 875 : slot->generation = 0;
4625 206 : 875 : rw->rw_shmem_slot = slotno;
4331 bruce@momjian.us 207 : 875 : rw->rw_worker.bgw_notify_pid = 0; /* might be reinit after crash */
4625 rhaas@postgresql.org 208 : 875 : memcpy(&slot->worker, &rw->rw_worker, sizeof(BackgroundWorker));
209 : 875 : ++slotno;
210 : : }
211 : :
212 : : /*
213 : : * Mark any remaining slots as not in use.
214 : : */
215 [ + + ]: 9466 : while (slotno < max_worker_processes)
216 : : {
217 : 8316 : BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
218 : :
219 : 8316 : slot->in_use = false;
220 : 8316 : ++slotno;
221 : : }
222 : : }
223 : : else
4625 rhaas@postgresql.org 224 [ # # ]:UBC 0 : Assert(found);
4625 rhaas@postgresql.org 225 :CBC 1150 : }
226 : :
227 : : /*
228 : : * Search the postmaster's backend-private list of RegisteredBgWorker objects
229 : : * for the one that maps to the given slot number.
230 : : */
231 : : static RegisteredBgWorker *
232 : 4502 : FindRegisteredWorkerBySlotNumber(int slotno)
233 : : {
234 : : dlist_iter iter;
235 : :
583 heikki.linnakangas@i 236 [ + - + + ]: 11023 : dlist_foreach(iter, &BackgroundWorkerList)
237 : : {
238 : : RegisteredBgWorker *rw;
239 : :
240 : 8921 : rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
4625 rhaas@postgresql.org 241 [ + + ]: 8921 : if (rw->rw_shmem_slot == slotno)
242 : 2400 : return rw;
243 : : }
244 : :
245 : 2102 : return NULL;
246 : : }
247 : :
248 : : /*
249 : : * Notice changes to shared memory made by other backends.
250 : : * Accept new worker requests only if allow_new_workers is true.
251 : : *
252 : : * This code runs in the postmaster, so we must be very careful not to assume
253 : : * that shared memory contents are sane. Otherwise, a rogue backend could
254 : : * take out the postmaster.
255 : : */
256 : : void
1907 tgl@sss.pgh.pa.us 257 : 1361 : BackgroundWorkerStateChange(bool allow_new_workers)
258 : : {
259 : : int slotno;
260 : :
261 : : /*
262 : : * The total number of slots stored in shared memory should match our
263 : : * notion of max_worker_processes. If it does not, something is very
264 : : * wrong. Further down, we always refer to this value as
265 : : * max_worker_processes, in case shared memory gets corrupted while we're
266 : : * looping.
267 : : */
4625 rhaas@postgresql.org 268 [ - + ]: 1361 : if (max_worker_processes != BackgroundWorkerData->total_slots)
269 : : {
1927 peter@eisentraut.org 270 [ # # ]:UBC 0 : ereport(LOG,
271 : : (errmsg("inconsistent background worker state (\"max_worker_processes\"=%d, total slots=%d)",
272 : : max_worker_processes,
273 : : BackgroundWorkerData->total_slots)));
4625 rhaas@postgresql.org 274 : 0 : return;
275 : : }
276 : :
277 : : /*
278 : : * Iterate through slots, looking for newly-registered workers or workers
279 : : * who must die.
280 : : */
4625 rhaas@postgresql.org 281 [ + + ]:CBC 12393 : for (slotno = 0; slotno < max_worker_processes; ++slotno)
282 : : {
283 : 11032 : BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
284 : : RegisteredBgWorker *rw;
285 : :
286 [ + + ]: 11032 : if (!slot->in_use)
287 : 6530 : continue;
288 : :
289 : : /*
290 : : * Make sure we don't see the in_use flag before the updated slot
291 : : * contents.
292 : : */
293 : 4502 : pg_read_barrier();
294 : :
295 : : /* See whether we already know about this worker. */
296 : 4502 : rw = FindRegisteredWorkerBySlotNumber(slotno);
297 [ + + ]: 4502 : if (rw != NULL)
298 : : {
299 : : /*
300 : : * In general, the worker data can't change after it's initially
301 : : * registered. However, someone can set the terminate flag.
302 : : */
4531 303 [ + + + - ]: 2400 : if (slot->terminate && !rw->rw_terminate)
304 : : {
305 : 10 : rw->rw_terminate = true;
306 [ + - ]: 10 : if (rw->rw_pid != 0)
307 : 10 : kill(rw->rw_pid, SIGTERM);
308 : : else
309 : : {
310 : : /* Report never-started, now-terminated worker as dead. */
4014 rhaas@postgresql.org 311 :UBC 0 : ReportBackgroundWorkerPID(rw);
312 : : }
313 : : }
4625 rhaas@postgresql.org 314 :CBC 2400 : continue;
315 : : }
316 : :
317 : : /*
318 : : * If we aren't allowing new workers, then immediately mark it for
319 : : * termination; the next stanza will take care of cleaning it up.
320 : : * Doing this ensures that any process waiting for the worker will get
321 : : * awoken, even though the worker will never be allowed to run.
322 : : */
1907 tgl@sss.pgh.pa.us 323 [ + + ]: 2102 : if (!allow_new_workers)
324 : 3 : slot->terminate = true;
325 : :
326 : : /*
327 : : * If the worker is marked for termination, we don't need to add it to
328 : : * the registered workers list; we can just free the slot. However, if
329 : : * bgw_notify_pid is set, the process that registered the worker may
330 : : * need to know that we've processed the terminate request, so be sure
331 : : * to signal it.
332 : : */
4531 rhaas@postgresql.org 333 [ + + ]: 2102 : if (slot->terminate)
334 : 3 : {
335 : : int notify_pid;
336 : :
337 : : /*
338 : : * We need a memory barrier here to make sure that the load of
339 : : * bgw_notify_pid and the update of parallel_terminate_count
340 : : * complete before the store to in_use.
341 : : */
4014 342 : 3 : notify_pid = slot->worker.bgw_notify_pid;
3390 343 [ - + ]: 3 : if ((slot->worker.bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
3390 rhaas@postgresql.org 344 :UBC 0 : BackgroundWorkerData->parallel_terminate_count++;
4014 rhaas@postgresql.org 345 :CBC 3 : slot->pid = 0;
346 : :
1765 tgl@sss.pgh.pa.us 347 : 3 : pg_memory_barrier();
4531 rhaas@postgresql.org 348 : 3 : slot->in_use = false;
349 : :
4014 350 [ + - ]: 3 : if (notify_pid != 0)
351 : 3 : kill(notify_pid, SIGUSR1);
352 : :
4531 353 : 3 : continue;
354 : : }
355 : :
356 : : /*
357 : : * Copy the registration data into the registered workers list.
358 : : */
888 heikki.linnakangas@i 359 : 2099 : rw = MemoryContextAllocExtended(PostmasterContext,
360 : : sizeof(RegisteredBgWorker),
361 : : MCXT_ALLOC_NO_OOM | MCXT_ALLOC_ZERO);
4625 rhaas@postgresql.org 362 [ - + ]: 2099 : if (rw == NULL)
363 : : {
4625 rhaas@postgresql.org 364 [ # # ]:UBC 0 : ereport(LOG,
365 : : (errcode(ERRCODE_OUT_OF_MEMORY),
366 : : errmsg("out of memory")));
367 : 0 : return;
368 : : }
369 : :
370 : : /*
371 : : * Copy strings in a paranoid way. If shared memory is corrupted, the
372 : : * source data might not even be NUL-terminated.
373 : : */
4625 rhaas@postgresql.org 374 :CBC 2099 : ascii_safe_strlcpy(rw->rw_worker.bgw_name,
375 : 2099 : slot->worker.bgw_name, BGW_MAXLEN);
3118 peter_e@gmx.net 376 : 2099 : ascii_safe_strlcpy(rw->rw_worker.bgw_type,
377 : 2099 : slot->worker.bgw_type, BGW_MAXLEN);
4625 rhaas@postgresql.org 378 : 2099 : ascii_safe_strlcpy(rw->rw_worker.bgw_library_name,
986 nathan@postgresql.or 379 : 2099 : slot->worker.bgw_library_name, MAXPGPATH);
4625 rhaas@postgresql.org 380 : 2099 : ascii_safe_strlcpy(rw->rw_worker.bgw_function_name,
381 : 2099 : slot->worker.bgw_function_name, BGW_MAXLEN);
382 : :
383 : : /*
384 : : * Copy various fixed-size fields.
385 : : *
386 : : * flags, start_time, and restart_time are examined by the postmaster,
387 : : * but nothing too bad will happen if they are corrupted. The
388 : : * remaining fields will only be examined by the child process. It
389 : : * might crash, but we won't.
390 : : */
391 : 2099 : rw->rw_worker.bgw_flags = slot->worker.bgw_flags;
392 : 2099 : rw->rw_worker.bgw_start_time = slot->worker.bgw_start_time;
393 : 2099 : rw->rw_worker.bgw_restart_time = slot->worker.bgw_restart_time;
394 : 2099 : rw->rw_worker.bgw_main_arg = slot->worker.bgw_main_arg;
3783 395 : 2099 : memcpy(rw->rw_worker.bgw_extra, slot->worker.bgw_extra, BGW_EXTRALEN);
396 : :
397 : : /*
398 : : * Copy the PID to be notified about state changes, but only if the
399 : : * postmaster knows about a backend with that PID. It isn't an error
400 : : * if the postmaster doesn't know about the PID, because the backend
401 : : * that requested the worker could have died (or been killed) just
402 : : * after doing so. Nonetheless, at least until we get some experience
403 : : * with how this plays out in the wild, log a message at a relative
404 : : * high debug level.
405 : : */
4582 406 : 2099 : rw->rw_worker.bgw_notify_pid = slot->worker.bgw_notify_pid;
407 [ - + ]: 2099 : if (!PostmasterMarkPIDForWorkerNotify(rw->rw_worker.bgw_notify_pid))
408 : : {
1248 peter@eisentraut.org 409 [ # # ]:UBC 0 : elog(DEBUG1, "worker notification PID %d is not valid",
410 : : (int) rw->rw_worker.bgw_notify_pid);
4582 rhaas@postgresql.org 411 : 0 : rw->rw_worker.bgw_notify_pid = 0;
412 : : }
413 : :
414 : : /* Initialize postmaster bookkeeping. */
4625 rhaas@postgresql.org 415 :CBC 2099 : rw->rw_pid = 0;
416 : 2099 : rw->rw_crashed_at = 0;
417 : 2099 : rw->rw_shmem_slot = slotno;
4531 418 : 2099 : rw->rw_terminate = false;
419 : :
420 : : /* Log it! */
3915 421 [ + + ]: 2099 : ereport(DEBUG1,
422 : : (errmsg_internal("registering background worker \"%s\"",
423 : : rw->rw_worker.bgw_name)));
424 : :
583 heikki.linnakangas@i 425 : 2099 : dlist_push_head(&BackgroundWorkerList, &rw->rw_lnode);
426 : : }
427 : : }
428 : :
429 : : /*
430 : : * Forget about a background worker that's no longer needed.
431 : : *
432 : : * NOTE: The entry is unlinked from BackgroundWorkerList. If the caller is
433 : : * iterating through it, better use a mutable iterator!
434 : : *
435 : : * Caller is responsible for notifying bgw_notify_pid, if appropriate.
436 : : *
437 : : * This function must be invoked only in the postmaster.
438 : : */
439 : : void
440 : 2075 : ForgetBackgroundWorker(RegisteredBgWorker *rw)
441 : : {
442 : : BackgroundWorkerSlot *slot;
443 : :
4625 rhaas@postgresql.org 444 [ - + ]: 2075 : Assert(rw->rw_shmem_slot < max_worker_processes);
445 : 2075 : slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
1765 tgl@sss.pgh.pa.us 446 [ - + ]: 2075 : Assert(slot->in_use);
447 : :
448 : : /*
449 : : * We need a memory barrier here to make sure that the update of
450 : : * parallel_terminate_count completes before the store to in_use.
451 : : */
3390 rhaas@postgresql.org 452 [ + + ]: 2075 : if ((rw->rw_worker.bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
453 : 1491 : BackgroundWorkerData->parallel_terminate_count++;
454 : :
1765 tgl@sss.pgh.pa.us 455 : 2075 : pg_memory_barrier();
4625 rhaas@postgresql.org 456 : 2075 : slot->in_use = false;
457 : :
3915 458 [ + + ]: 2075 : ereport(DEBUG1,
459 : : (errmsg_internal("unregistering background worker \"%s\"",
460 : : rw->rw_worker.bgw_name)));
461 : :
583 heikki.linnakangas@i 462 : 2075 : dlist_delete(&rw->rw_lnode);
888 463 : 2075 : pfree(rw);
4625 rhaas@postgresql.org 464 : 2075 : }
465 : :
466 : : /*
467 : : * Report the PID of a newly-launched background worker in shared memory.
468 : : *
469 : : * This function should only be called from the postmaster.
470 : : */
471 : : void
4582 472 : 2861 : ReportBackgroundWorkerPID(RegisteredBgWorker *rw)
473 : : {
474 : : BackgroundWorkerSlot *slot;
475 : :
476 [ - + ]: 2861 : Assert(rw->rw_shmem_slot < max_worker_processes);
477 : 2861 : slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
478 : 2861 : slot->pid = rw->rw_pid;
479 : :
480 [ + + ]: 2861 : if (rw->rw_worker.bgw_notify_pid != 0)
481 : 2099 : kill(rw->rw_worker.bgw_notify_pid, SIGUSR1);
482 : 2861 : }
483 : :
484 : : /*
485 : : * Report that the PID of a background worker is now zero because a
486 : : * previously-running background worker has exited.
487 : : *
488 : : * NOTE: The entry may be unlinked from BackgroundWorkerList. If the caller
489 : : * is iterating through it, better use a mutable iterator!
490 : : *
491 : : * This function should only be called from the postmaster.
492 : : */
493 : : void
583 heikki.linnakangas@i 494 : 2531 : ReportBackgroundWorkerExit(RegisteredBgWorker *rw)
495 : : {
496 : : BackgroundWorkerSlot *slot;
497 : : int notify_pid;
498 : :
3299 rhaas@postgresql.org 499 [ - + ]: 2531 : Assert(rw->rw_shmem_slot < max_worker_processes);
500 : 2531 : slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
501 : 2531 : slot->pid = rw->rw_pid;
3296 502 : 2531 : notify_pid = rw->rw_worker.bgw_notify_pid;
503 : :
504 : : /*
505 : : * If this worker is slated for deregistration, do that before notifying
506 : : * the process which started it. Otherwise, if that process tries to
507 : : * reuse the slot immediately, it might not be available yet. In theory
508 : : * that could happen anyway if the process checks slot->pid at just the
509 : : * wrong moment, but this makes the window narrower.
510 : : */
3299 511 [ + + ]: 2531 : if (rw->rw_terminate ||
512 [ + + ]: 710 : rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART)
583 heikki.linnakangas@i 513 : 2075 : ForgetBackgroundWorker(rw);
514 : :
3296 rhaas@postgresql.org 515 [ + + ]: 2531 : if (notify_pid != 0)
516 : 2038 : kill(notify_pid, SIGUSR1);
3299 517 : 2531 : }
518 : :
519 : : /*
520 : : * Cancel SIGUSR1 notifications for a PID belonging to an exiting backend.
521 : : *
522 : : * This function should only be called from the postmaster.
523 : : */
524 : : void
4582 525 : 281 : BackgroundWorkerStopNotifications(pid_t pid)
526 : : {
527 : : dlist_iter iter;
528 : :
583 heikki.linnakangas@i 529 [ + - + + ]: 929 : dlist_foreach(iter, &BackgroundWorkerList)
530 : : {
531 : : RegisteredBgWorker *rw;
532 : :
533 : 648 : rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
4582 rhaas@postgresql.org 534 [ + + ]: 648 : if (rw->rw_worker.bgw_notify_pid == pid)
535 : 38 : rw->rw_worker.bgw_notify_pid = 0;
536 : : }
537 : 281 : }
538 : :
539 : : /*
540 : : * Cancel any not-yet-started worker requests that have waiting processes.
541 : : *
542 : : * This is called during a normal ("smart" or "fast") database shutdown.
543 : : * After this point, no new background workers will be started, so anything
544 : : * that might be waiting for them needs to be kicked off its wait. We do
545 : : * that by canceling the bgworker registration entirely, which is perhaps
546 : : * overkill, but since we're shutting down it does not matter whether the
547 : : * registration record sticks around.
548 : : *
549 : : * This function should only be called from the postmaster.
550 : : */
551 : : void
1907 tgl@sss.pgh.pa.us 552 : 572 : ForgetUnstartedBackgroundWorkers(void)
553 : : {
554 : : dlist_mutable_iter iter;
555 : :
583 heikki.linnakangas@i 556 [ + - + + ]: 1137 : dlist_foreach_modify(iter, &BackgroundWorkerList)
557 : : {
558 : : RegisteredBgWorker *rw;
559 : : BackgroundWorkerSlot *slot;
560 : :
561 : 565 : rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
1907 tgl@sss.pgh.pa.us 562 [ - + ]: 565 : Assert(rw->rw_shmem_slot < max_worker_processes);
563 : 565 : slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
564 : :
565 : : /* If it's not yet started, and there's someone waiting ... */
566 [ + + ]: 565 : if (slot->pid == InvalidPid &&
567 [ - + ]: 59 : rw->rw_worker.bgw_notify_pid != 0)
568 : : {
569 : : /* ... then zap it, and notify the waiter */
1907 tgl@sss.pgh.pa.us 570 :UBC 0 : int notify_pid = rw->rw_worker.bgw_notify_pid;
571 : :
583 heikki.linnakangas@i 572 : 0 : ForgetBackgroundWorker(rw);
1907 tgl@sss.pgh.pa.us 573 [ # # ]: 0 : if (notify_pid != 0)
574 : 0 : kill(notify_pid, SIGUSR1);
575 : : }
576 : : }
1907 tgl@sss.pgh.pa.us 577 :CBC 572 : }
578 : :
579 : : /*
580 : : * Reset background worker crash state.
581 : : *
582 : : * We assume that, after a crash-and-restart cycle, background workers without
583 : : * the never-restart flag should be restarted immediately, instead of waiting
584 : : * for bgw_restart_time to elapse. On the other hand, workers with that flag
585 : : * should be forgotten immediately, since we won't ever restart them.
586 : : *
587 : : * This function should only be called from the postmaster.
588 : : */
589 : : void
4330 rhaas@postgresql.org 590 : 5 : ResetBackgroundWorkerCrashTimes(void)
591 : : {
592 : : dlist_mutable_iter iter;
593 : :
583 heikki.linnakangas@i 594 [ + - + + ]: 10 : dlist_foreach_modify(iter, &BackgroundWorkerList)
595 : : {
596 : : RegisteredBgWorker *rw;
597 : :
598 : 5 : rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
599 : :
3260 rhaas@postgresql.org 600 [ - + ]: 5 : if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART)
601 : : {
602 : : /*
603 : : * Workers marked BGW_NEVER_RESTART shouldn't get relaunched after
604 : : * the crash, so forget about them. (If we wait until after the
605 : : * crash to forget about them, and they are parallel workers,
606 : : * parallel_terminate_count will get incremented after we've
607 : : * already zeroed parallel_register_count, which would be bad.)
608 : : */
583 heikki.linnakangas@i 609 :UBC 0 : ForgetBackgroundWorker(rw);
610 : : }
611 : : else
612 : : {
613 : : /*
614 : : * The accounting which we do via parallel_register_count and
615 : : * parallel_terminate_count would get messed up if a worker marked
616 : : * parallel could survive a crash and restart cycle. All such
617 : : * workers should be marked BGW_NEVER_RESTART, and thus control
618 : : * should never reach this branch.
619 : : */
3260 rhaas@postgresql.org 620 [ - + ]:CBC 5 : Assert((rw->rw_worker.bgw_flags & BGWORKER_CLASS_PARALLEL) == 0);
621 : :
622 : : /*
623 : : * Allow this worker to be restarted immediately after we finish
624 : : * resetting.
625 : : */
4000 626 : 5 : rw->rw_crashed_at = 0;
233 fujii@postgresql.org 627 : 5 : rw->rw_pid = 0;
628 : :
629 : : /*
630 : : * If there was anyone waiting for it, they're history.
631 : : */
1907 tgl@sss.pgh.pa.us 632 : 5 : rw->rw_worker.bgw_notify_pid = 0;
633 : : }
634 : : }
4330 rhaas@postgresql.org 635 : 5 : }
636 : :
637 : : /*
638 : : * Complain about the BackgroundWorker definition using error level elevel.
639 : : * Return true if it looks ok, false if not (unless elevel >= ERROR, in
640 : : * which case we won't return at all in the not-OK case).
641 : : */
642 : : static bool
4625 643 : 2846 : SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
644 : : {
645 : : /* sanity check for flags */
646 : :
647 : : /*
648 : : * We used to support workers not connected to shared memory, but don't
649 : : * anymore. Thus this is a required flag now. We're not removing the flag
650 : : * for compatibility reasons and because the flag still provides some
651 : : * signal when reading code.
652 : : */
1675 andres@anarazel.de 653 [ - + ]: 2846 : if (!(worker->bgw_flags & BGWORKER_SHMEM_ACCESS))
654 : : {
1675 andres@anarazel.de 655 [ # # ]:UBC 0 : ereport(elevel,
656 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
657 : : errmsg("background worker \"%s\": background workers without shared memory access are not supported",
658 : : worker->bgw_name)));
659 : 0 : return false;
660 : : }
661 : :
1675 andres@anarazel.de 662 [ + + ]:CBC 2846 : if (worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION)
663 : : {
4625 rhaas@postgresql.org 664 [ - + ]: 2837 : if (worker->bgw_start_time == BgWorkerStart_PostmasterStart)
665 : : {
4625 rhaas@postgresql.org 666 [ # # ]:UBC 0 : ereport(elevel,
667 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
668 : : errmsg("background worker \"%s\": cannot request database access if starting at postmaster start",
669 : : worker->bgw_name)));
670 : 0 : return false;
671 : : }
672 : :
673 : : /* XXX other checks? */
674 : : }
675 : :
676 : : /* Interruptible workers require a database connection */
68 michael@paquier.xyz 677 [ + + ]:GNC 2846 : if ((worker->bgw_flags & BGWORKER_INTERRUPTIBLE) &&
678 [ - + ]: 4 : !(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
679 : : {
68 michael@paquier.xyz 680 [ # # ]:UNC 0 : ereport(elevel,
681 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
682 : : errmsg("background worker \"%s\": cannot make background workers interruptible without database access",
683 : : worker->bgw_name)));
684 : 0 : return false;
685 : : }
686 : :
4625 rhaas@postgresql.org 687 [ + + ]:CBC 2846 : if ((worker->bgw_restart_time < 0 &&
688 [ + - ]: 1972 : worker->bgw_restart_time != BGW_NEVER_RESTART) ||
689 [ - + ]: 2846 : (worker->bgw_restart_time > USECS_PER_DAY / 1000))
690 : : {
4625 rhaas@postgresql.org 691 [ # # ]:UBC 0 : ereport(elevel,
692 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
693 : : errmsg("background worker \"%s\": invalid restart interval",
694 : : worker->bgw_name)));
695 : 0 : return false;
696 : : }
697 : :
698 : : /*
699 : : * Parallel workers may not be configured for restart, because the
700 : : * parallel_register_count/parallel_terminate_count accounting can't
701 : : * handle parallel workers lasting through a crash-and-restart cycle.
702 : : */
3260 rhaas@postgresql.org 703 [ + + ]:CBC 2846 : if (worker->bgw_restart_time != BGW_NEVER_RESTART &&
704 [ - + ]: 874 : (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
705 : : {
3260 rhaas@postgresql.org 706 [ # # ]:UBC 0 : ereport(elevel,
707 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
708 : : errmsg("background worker \"%s\": parallel workers may not be configured for restart",
709 : : worker->bgw_name)));
710 : 0 : return false;
711 : : }
712 : :
713 : : /*
714 : : * If bgw_type is not filled in, use bgw_name.
715 : : */
3118 peter_e@gmx.net 716 [ - + ]:CBC 2846 : if (strcmp(worker->bgw_type, "") == 0)
3118 peter_e@gmx.net 717 :UBC 0 : strcpy(worker->bgw_type, worker->bgw_name);
718 : :
4625 rhaas@postgresql.org 719 :CBC 2846 : return true;
720 : : }
721 : :
722 : : /*
723 : : * Main entry point for background worker processes.
724 : : */
725 : : void
387 peter@eisentraut.org 726 : 2543 : BackgroundWorkerMain(const void *startup_data, size_t startup_data_len)
727 : : {
728 : : sigjmp_buf local_sigjmp_buf;
729 : : BackgroundWorker *worker;
730 : : bgworker_main_type entrypt;
731 : :
727 heikki.linnakangas@i 732 [ - + ]: 2543 : if (startup_data == NULL)
4594 rhaas@postgresql.org 733 [ # # ]:UBC 0 : elog(FATAL, "unable to find bgworker entry");
727 heikki.linnakangas@i 734 [ - + ]:CBC 2543 : Assert(startup_data_len == sizeof(BackgroundWorker));
735 : 2543 : worker = MemoryContextAlloc(TopMemoryContext, sizeof(BackgroundWorker));
736 : 2543 : memcpy(worker, startup_data, sizeof(BackgroundWorker));
737 : :
738 : : /*
739 : : * Now that we're done reading the startup data, release postmaster's
740 : : * working memory context.
741 : : */
742 [ + - ]: 2543 : if (PostmasterContext)
743 : : {
744 : 2543 : MemoryContextDelete(PostmasterContext);
745 : 2543 : PostmasterContext = NULL;
746 : : }
747 : :
748 : 2543 : MyBgworkerEntry = worker;
2195 peter@eisentraut.org 749 : 2543 : init_ps_display(worker->bgw_name);
750 : :
621 heikki.linnakangas@i 751 [ - + ]: 2543 : Assert(GetProcessingMode() == InitProcessing);
752 : :
753 : : /* Apply PostAuthDelay */
4594 rhaas@postgresql.org 754 [ - + ]: 2543 : if (PostAuthDelay > 0)
4594 rhaas@postgresql.org 755 :UBC 0 : pg_usleep(PostAuthDelay * 1000000L);
756 : :
757 : : /*
758 : : * Set up signal handlers.
759 : : */
4594 rhaas@postgresql.org 760 [ + + ]:CBC 2543 : if (worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION)
761 : : {
762 : : /*
763 : : * SIGINT is used to signal canceling the current action
764 : : */
765 : 2534 : pqsignal(SIGINT, StatementCancelHandler);
766 : 2534 : pqsignal(SIGUSR1, procsignal_sigusr1_handler);
767 : 2534 : pqsignal(SIGFPE, FloatExceptionHandler);
768 : :
769 : : /* XXX Any other handlers needed here? */
770 : : }
771 : : else
772 : : {
773 : 9 : pqsignal(SIGINT, SIG_IGN);
1840 tmunro@postgresql.or 774 : 9 : pqsignal(SIGUSR1, SIG_IGN);
4594 rhaas@postgresql.org 775 : 9 : pqsignal(SIGFPE, SIG_IGN);
776 : : }
25 heikki.linnakangas@i 777 :GNC 2543 : pqsignal(SIGTERM, die);
778 : : /* SIGQUIT handler was already set up by InitPostmasterChild */
4594 rhaas@postgresql.org 779 :CBC 2543 : pqsignal(SIGHUP, SIG_IGN);
780 : :
4331 bruce@momjian.us 781 : 2543 : InitializeTimeouts(); /* establishes SIGALRM handler */
782 : :
4594 rhaas@postgresql.org 783 : 2543 : pqsignal(SIGPIPE, SIG_IGN);
784 : 2543 : pqsignal(SIGUSR2, SIG_IGN);
785 : 2543 : pqsignal(SIGCHLD, SIG_DFL);
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 [ + + ]: 2543 : if (sigsetjmp(local_sigjmp_buf, 1) != 0)
793 : : {
794 : : /* Since not using PG_TRY, must reset error stack by hand */
795 : 146 : error_context_stack = NULL;
796 : :
797 : : /* Prevent interrupts while cleaning up */
798 : 146 : 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 : : */
2019 tgl@sss.pgh.pa.us 805 : 146 : BackgroundWorkerUnblockSignals();
806 : :
807 : : /* Report the error to the parallel leader and the server log */
4594 rhaas@postgresql.org 808 : 146 : 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 : 146 : proc_exit(1);
818 : : }
819 : :
820 : : /* We can now handle ereport(ERROR) */
821 : 2543 : PG_exception_stack = &local_sigjmp_buf;
822 : :
823 : : /*
824 : : * Create a per-backend PGPROC struct in shared memory. We must do this
825 : : * before we can use LWLocks or access any shared memory.
826 : : */
1675 andres@anarazel.de 827 : 2543 : InitProcess();
828 : :
829 : : /*
830 : : * Early initialization.
831 : : */
832 : 2543 : BaseInit();
833 : :
834 : : /*
835 : : * Look up the entry point function, loading its library if necessary.
836 : : */
3257 tgl@sss.pgh.pa.us 837 : 5086 : entrypt = LookupBackgroundWorkerFunction(worker->bgw_library_name,
838 : 2543 : worker->bgw_function_name);
839 : :
840 : : /*
841 : : * Note that in normal processes, we would call InitPostgres here. For a
842 : : * worker, however, we don't know what database to connect to, yet; so we
843 : : * need to wait until the user code does it via
844 : : * BackgroundWorkerInitializeConnection().
845 : : */
846 : :
847 : : /*
848 : : * Now invoke the user-defined worker code
849 : : */
4594 rhaas@postgresql.org 850 : 2543 : entrypt(worker->bgw_main_arg);
851 : :
852 : : /* ... and if it returns, we're done */
853 : 1488 : proc_exit(0);
854 : : }
855 : :
856 : : /*
857 : : * Connect background worker to a database.
858 : : */
859 : : void
621 heikki.linnakangas@i 860 : 459 : BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
861 : : {
862 : 459 : BackgroundWorker *worker = MyBgworkerEntry;
863 : 459 : bits32 init_flags = 0; /* never honor session_preload_libraries */
864 : :
865 : : /* ignore datallowconn and ACL_CONNECT? */
866 [ - + ]: 459 : if (flags & BGWORKER_BYPASS_ALLOWCONN)
621 heikki.linnakangas@i 867 :UBC 0 : init_flags |= INIT_PG_OVERRIDE_ALLOW_CONNS;
868 : : /* ignore rolcanlogin? */
621 heikki.linnakangas@i 869 [ - + ]:CBC 459 : if (flags & BGWORKER_BYPASS_ROLELOGINCHECK)
621 heikki.linnakangas@i 870 :UBC 0 : init_flags |= INIT_PG_OVERRIDE_ROLE_LOGIN;
871 : :
872 : : /* XXX is this the right errcode? */
621 heikki.linnakangas@i 873 [ - + ]:CBC 459 : if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
621 heikki.linnakangas@i 874 [ # # ]:UBC 0 : ereport(FATAL,
875 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
876 : : errmsg("database connection requirement not indicated during registration")));
877 : :
621 heikki.linnakangas@i 878 :CBC 459 : InitPostgres(dbname, InvalidOid, /* database to connect to */
879 : : username, InvalidOid, /* role to connect as */
880 : : init_flags,
881 : : NULL); /* no out_dbname */
882 : :
883 : : /* it had better not gotten out of "init" mode yet */
884 [ - + ]: 459 : if (!IsInitProcessingMode())
621 heikki.linnakangas@i 885 [ # # ]:UBC 0 : ereport(ERROR,
886 : : (errmsg("invalid processing mode in background worker")));
621 heikki.linnakangas@i 887 :CBC 459 : SetProcessingMode(NormalProcessing);
888 : 459 : }
889 : :
890 : : /*
891 : : * Connect background worker to a database using OIDs.
892 : : */
893 : : void
894 : 2075 : BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
895 : : {
896 : 2075 : BackgroundWorker *worker = MyBgworkerEntry;
897 : 2075 : bits32 init_flags = 0; /* never honor session_preload_libraries */
898 : :
899 : : /* ignore datallowconn and ACL_CONNECT? */
900 [ + + ]: 2075 : if (flags & BGWORKER_BYPASS_ALLOWCONN)
901 : 1491 : init_flags |= INIT_PG_OVERRIDE_ALLOW_CONNS;
902 : : /* ignore rolcanlogin? */
903 [ + + ]: 2075 : if (flags & BGWORKER_BYPASS_ROLELOGINCHECK)
904 : 1491 : init_flags |= INIT_PG_OVERRIDE_ROLE_LOGIN;
905 : :
906 : : /* XXX is this the right errcode? */
907 [ - + ]: 2075 : if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
621 heikki.linnakangas@i 908 [ # # ]:UBC 0 : ereport(FATAL,
909 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
910 : : errmsg("database connection requirement not indicated during registration")));
911 : :
621 heikki.linnakangas@i 912 :CBC 2075 : InitPostgres(NULL, dboid, /* database to connect to */
913 : : NULL, useroid, /* role to connect as */
914 : : init_flags,
915 : : NULL); /* no out_dbname */
916 : :
917 : : /* it had better not gotten out of "init" mode yet */
918 [ - + ]: 2070 : if (!IsInitProcessingMode())
621 heikki.linnakangas@i 919 [ # # ]:UBC 0 : ereport(ERROR,
920 : : (errmsg("invalid processing mode in background worker")));
621 heikki.linnakangas@i 921 :CBC 2070 : SetProcessingMode(NormalProcessing);
922 : 2070 : }
923 : :
924 : : /*
925 : : * Block/unblock signals in a background worker
926 : : */
927 : : void
621 heikki.linnakangas@i 928 :UBC 0 : BackgroundWorkerBlockSignals(void)
929 : : {
930 : 0 : sigprocmask(SIG_SETMASK, &BlockSig, NULL);
931 : 0 : }
932 : :
933 : : void
621 heikki.linnakangas@i 934 :CBC 2689 : BackgroundWorkerUnblockSignals(void)
935 : : {
936 : 2689 : sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
937 : 2689 : }
938 : :
939 : : /*
940 : : * Register a new static background worker.
941 : : *
942 : : * This can only be called directly from postmaster or in the _PG_init
943 : : * function of a module library that's loaded by shared_preload_libraries;
944 : : * otherwise it will have no effect.
945 : : */
946 : : void
4625 rhaas@postgresql.org 947 : 877 : RegisterBackgroundWorker(BackgroundWorker *worker)
948 : : {
949 : : RegisteredBgWorker *rw;
950 : : static int numworkers = 0;
951 : :
952 : : /*
953 : : * Static background workers can only be registered in the postmaster
954 : : * process.
955 : : */
888 heikki.linnakangas@i 956 [ + - - + ]: 877 : if (IsUnderPostmaster || !IsPostmasterEnvironment)
957 : : {
958 : : /*
959 : : * In EXEC_BACKEND or single-user mode, we process
960 : : * shared_preload_libraries in backend processes too. We cannot
961 : : * register static background workers at that stage, but many
962 : : * libraries' _PG_init() functions don't distinguish whether they're
963 : : * being loaded in the postmaster or in a backend, they just check
964 : : * process_shared_preload_libraries_in_progress. It's a bit sloppy,
965 : : * but for historical reasons we tolerate it. In EXEC_BACKEND mode,
966 : : * the background workers should already have been registered when the
967 : : * library was loaded in postmaster.
968 : : */
888 heikki.linnakangas@i 969 [ # # ]:UBC 0 : if (process_shared_preload_libraries_in_progress)
970 : 0 : return;
971 [ # # ]: 0 : ereport(LOG,
972 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
973 : : errmsg("background worker \"%s\": must be registered in \"shared_preload_libraries\"",
974 : : worker->bgw_name)));
4625 rhaas@postgresql.org 975 : 0 : return;
976 : : }
977 : :
978 : : /*
979 : : * Cannot register static background workers after calling
980 : : * BackgroundWorkerShmemInit().
981 : : */
888 heikki.linnakangas@i 982 [ - + ]:CBC 877 : if (BackgroundWorkerData != NULL)
888 heikki.linnakangas@i 983 [ # # ]:UBC 0 : elog(ERROR, "cannot register background worker \"%s\" after shmem init",
984 : : worker->bgw_name);
985 : :
888 heikki.linnakangas@i 986 [ + + ]:CBC 877 : ereport(DEBUG1,
987 : : (errmsg_internal("registering background worker \"%s\"", worker->bgw_name)));
988 : :
4625 rhaas@postgresql.org 989 [ - + ]: 877 : if (!SanityCheckBackgroundWorker(worker, LOG))
4625 rhaas@postgresql.org 990 :UBC 0 : return;
991 : :
4582 rhaas@postgresql.org 992 [ - + ]:CBC 877 : if (worker->bgw_notify_pid != 0)
993 : : {
4582 rhaas@postgresql.org 994 [ # # ]:UBC 0 : ereport(LOG,
995 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
996 : : errmsg("background worker \"%s\": only dynamic background workers can request notification",
997 : : worker->bgw_name)));
998 : 0 : return;
999 : : }
1000 : :
1001 : : /*
1002 : : * Enforce maximum number of workers. Note this is overly restrictive: we
1003 : : * could allow more non-shmem-connected workers, because these don't count
1004 : : * towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
1005 : : * important to relax this restriction.
1006 : : */
4625 rhaas@postgresql.org 1007 [ - + ]:CBC 877 : if (++numworkers > max_worker_processes)
1008 : : {
4625 rhaas@postgresql.org 1009 [ # # ]:UBC 0 : ereport(LOG,
1010 : : (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
1011 : : errmsg("too many background workers"),
1012 : : errdetail_plural("Up to %d background worker can be registered with the current settings.",
1013 : : "Up to %d background workers can be registered with the current settings.",
1014 : : max_worker_processes,
1015 : : max_worker_processes),
1016 : : errhint("Consider increasing the configuration parameter \"%s\".", "max_worker_processes")));
1017 : 0 : return;
1018 : : }
1019 : :
1020 : : /*
1021 : : * Copy the registration data into the registered workers list.
1022 : : */
888 heikki.linnakangas@i 1023 :CBC 877 : rw = MemoryContextAllocExtended(PostmasterContext,
1024 : : sizeof(RegisteredBgWorker),
1025 : : MCXT_ALLOC_NO_OOM);
4625 rhaas@postgresql.org 1026 [ - + ]: 877 : if (rw == NULL)
1027 : : {
4625 rhaas@postgresql.org 1028 [ # # ]:UBC 0 : ereport(LOG,
1029 : : (errcode(ERRCODE_OUT_OF_MEMORY),
1030 : : errmsg("out of memory")));
1031 : 0 : return;
1032 : : }
1033 : :
4625 rhaas@postgresql.org 1034 :CBC 877 : rw->rw_worker = *worker;
1035 : 877 : rw->rw_pid = 0;
1036 : 877 : rw->rw_crashed_at = 0;
4531 1037 : 877 : rw->rw_terminate = false;
1038 : :
583 heikki.linnakangas@i 1039 : 877 : dlist_push_head(&BackgroundWorkerList, &rw->rw_lnode);
1040 : : }
1041 : :
1042 : : /*
1043 : : * Register a new background worker from a regular backend.
1044 : : *
1045 : : * Returns true on success and false on failure. Failure typically indicates
1046 : : * that no background worker slots are currently available.
1047 : : *
1048 : : * If handle != NULL, we'll set *handle to a pointer that can subsequently
1049 : : * be used as an argument to GetBackgroundWorkerPid(). The caller can
1050 : : * free this pointer using pfree(), if desired.
1051 : : */
1052 : : bool
4582 rhaas@postgresql.org 1053 : 1969 : RegisterDynamicBackgroundWorker(BackgroundWorker *worker,
1054 : : BackgroundWorkerHandle **handle)
1055 : : {
1056 : : int slotno;
4331 bruce@momjian.us 1057 : 1969 : bool success = false;
1058 : : bool parallel;
1059 : 1969 : uint64 generation = 0;
1060 : :
1061 : : /*
1062 : : * We can't register dynamic background workers from the postmaster. If
1063 : : * this is a standalone backend, we're the only process and can't start
1064 : : * any more. In a multi-process environment, it might be theoretically
1065 : : * possible, but we don't currently support it due to locking
1066 : : * considerations; see comments on the BackgroundWorkerSlot data
1067 : : * structure.
1068 : : */
4625 rhaas@postgresql.org 1069 [ - + ]: 1969 : if (!IsUnderPostmaster)
4625 rhaas@postgresql.org 1070 :UBC 0 : return false;
1071 : :
4625 rhaas@postgresql.org 1072 [ - + ]:CBC 1969 : if (!SanityCheckBackgroundWorker(worker, ERROR))
4625 rhaas@postgresql.org 1073 :UBC 0 : return false;
1074 : :
3390 rhaas@postgresql.org 1075 :CBC 1969 : parallel = (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0;
1076 : :
4625 1077 : 1969 : LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
1078 : :
1079 : : /*
1080 : : * If this is a parallel worker, check whether there are already too many
1081 : : * parallel workers; if so, don't register another one. Our view of
1082 : : * parallel_terminate_count may be slightly stale, but that doesn't really
1083 : : * matter: we would have gotten the same result if we'd arrived here
1084 : : * slightly earlier anyway. There's no help for it, either, since the
1085 : : * postmaster must not take locks; a memory barrier wouldn't guarantee
1086 : : * anything useful.
1087 : : */
3390 1088 [ + + ]: 1969 : if (parallel && (BackgroundWorkerData->parallel_register_count -
1089 [ + + ]: 1505 : BackgroundWorkerData->parallel_terminate_count) >=
1090 : : max_parallel_workers)
1091 : : {
3260 1092 [ - + ]: 10 : Assert(BackgroundWorkerData->parallel_register_count -
1093 : : BackgroundWorkerData->parallel_terminate_count <=
1094 : : MAX_PARALLEL_WORKER_LIMIT);
3390 1095 : 10 : LWLockRelease(BackgroundWorkerLock);
1096 : 10 : return false;
1097 : : }
1098 : :
1099 : : /*
1100 : : * Look for an unused slot. If we find one, grab it.
1101 : : */
4625 1102 [ + + ]: 6187 : for (slotno = 0; slotno < BackgroundWorkerData->total_slots; ++slotno)
1103 : : {
1104 : 6183 : BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
1105 : :
1106 [ + + ]: 6183 : if (!slot->in_use)
1107 : : {
1108 : 1955 : memcpy(&slot->worker, worker, sizeof(BackgroundWorker));
3189 tgl@sss.pgh.pa.us 1109 : 1955 : slot->pid = InvalidPid; /* indicates not started yet */
4582 rhaas@postgresql.org 1110 : 1955 : slot->generation++;
4531 1111 : 1955 : slot->terminate = false;
4582 1112 : 1955 : generation = slot->generation;
3390 1113 [ + + ]: 1955 : if (parallel)
1114 : 1491 : BackgroundWorkerData->parallel_register_count++;
1115 : :
1116 : : /*
1117 : : * Make sure postmaster doesn't see the slot as in use before it
1118 : : * sees the new contents.
1119 : : */
4625 1120 : 1955 : pg_write_barrier();
1121 : :
1122 : 1955 : slot->in_use = true;
1123 : 1955 : success = true;
1124 : 1955 : break;
1125 : : }
1126 : : }
1127 : :
1128 : 1959 : LWLockRelease(BackgroundWorkerLock);
1129 : :
1130 : : /* If we found a slot, tell the postmaster to notice the change. */
1131 [ + + ]: 1959 : if (success)
1132 : 1955 : SendPostmasterSignal(PMSIGNAL_BACKGROUND_WORKER_CHANGE);
1133 : :
1134 : : /*
1135 : : * If we found a slot and the user has provided a handle, initialize it.
1136 : : */
4582 1137 [ + + + - ]: 1959 : if (success && handle)
1138 : : {
95 michael@paquier.xyz 1139 :GNC 1955 : *handle = palloc_object(BackgroundWorkerHandle);
4582 rhaas@postgresql.org 1140 :CBC 1955 : (*handle)->slot = slotno;
1141 : 1955 : (*handle)->generation = generation;
1142 : : }
1143 : :
4625 1144 : 1959 : return success;
1145 : : }
1146 : :
1147 : : /*
1148 : : * Get the PID of a dynamically-registered background worker.
1149 : : *
1150 : : * If the worker is determined to be running, the return value will be
1151 : : * BGWH_STARTED and *pidp will get the PID of the worker process. If the
1152 : : * postmaster has not yet attempted to start the worker, the return value will
1153 : : * be BGWH_NOT_YET_STARTED. Otherwise, the return value is BGWH_STOPPED.
1154 : : *
1155 : : * BGWH_STOPPED can indicate either that the worker is temporarily stopped
1156 : : * (because it is configured for automatic restart and exited non-zero),
1157 : : * or that the worker is permanently stopped (because it exited with exit
1158 : : * code 0, or was not configured for automatic restart), or even that the
1159 : : * worker was unregistered without ever starting (either because startup
1160 : : * failed and the worker is not configured for automatic restart, or because
1161 : : * TerminateBackgroundWorker was used before the worker was successfully
1162 : : * started).
1163 : : */
1164 : : BgwHandleStatus
4582 1165 : 3330400 : GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
1166 : : {
1167 : : BackgroundWorkerSlot *slot;
1168 : : pid_t pid;
1169 : :
1170 [ - + ]: 3330400 : Assert(handle->slot < max_worker_processes);
1171 : 3330400 : slot = &BackgroundWorkerData->slot[handle->slot];
1172 : :
1173 : : /*
1174 : : * We could probably arrange to synchronize access to data using memory
1175 : : * barriers only, but for now, let's just keep it simple and grab the
1176 : : * lock. It seems unlikely that there will be enough traffic here to
1177 : : * result in meaningful contention.
1178 : : */
1179 : 3330400 : LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1180 : :
1181 : : /*
1182 : : * The generation number can't be concurrently changed while we hold the
1183 : : * lock. The pid, which is updated by the postmaster, can change at any
1184 : : * time, but we assume such changes are atomic. So the value we read
1185 : : * won't be garbage, but it might be out of date by the time the caller
1186 : : * examines it (but that's unavoidable anyway).
1187 : : *
1188 : : * The in_use flag could be in the process of changing from true to false,
1189 : : * but if it is already false then it can't change further.
1190 : : */
3021 1191 [ + + + + ]: 3330400 : if (handle->generation != slot->generation || !slot->in_use)
4582 1192 : 1492 : pid = 0;
1193 : : else
1194 : 3328908 : pid = slot->pid;
1195 : :
1196 : : /* All done. */
1197 : 3330400 : LWLockRelease(BackgroundWorkerLock);
1198 : :
1199 [ + + ]: 3330400 : if (pid == 0)
1200 : 1492 : return BGWH_STOPPED;
1201 [ + + ]: 3328908 : else if (pid == InvalidPid)
1202 : 131282 : return BGWH_NOT_YET_STARTED;
1203 : 3197626 : *pidp = pid;
1204 : 3197626 : return BGWH_STARTED;
1205 : : }
1206 : :
1207 : : /*
1208 : : * Wait for a background worker to start up.
1209 : : *
1210 : : * This is like GetBackgroundWorkerPid(), except that if the worker has not
1211 : : * yet started, we wait for it to do so; thus, BGWH_NOT_YET_STARTED is never
1212 : : * returned. However, if the postmaster has died, we give up and return
1213 : : * BGWH_POSTMASTER_DIED, since it that case we know that startup will not
1214 : : * take place.
1215 : : *
1216 : : * The caller *must* have set our PID as the worker's bgw_notify_pid,
1217 : : * else we will not be awoken promptly when the worker's state changes.
1218 : : */
1219 : : BgwHandleStatus
1220 : 12 : WaitForBackgroundWorkerStartup(BackgroundWorkerHandle *handle, pid_t *pidp)
1221 : : {
1222 : : BgwHandleStatus status;
1223 : : int rc;
1224 : :
1225 : : for (;;)
1226 : 17 : {
1227 : : pid_t pid;
1228 : :
3810 1229 [ - + ]: 29 : CHECK_FOR_INTERRUPTS();
1230 : :
1231 : 29 : status = GetBackgroundWorkerPid(handle, &pid);
1232 [ + + ]: 29 : if (status == BGWH_STARTED)
1233 : 12 : *pidp = pid;
1234 [ + + ]: 29 : if (status != BGWH_NOT_YET_STARTED)
1235 : 12 : break;
1236 : :
1237 : 17 : rc = WaitLatch(MyLatch,
1238 : : WL_LATCH_SET | WL_POSTMASTER_DEATH, 0,
1239 : : WAIT_EVENT_BGWORKER_STARTUP);
1240 : :
1241 [ - + ]: 17 : if (rc & WL_POSTMASTER_DEATH)
1242 : : {
3810 rhaas@postgresql.org 1243 :UBC 0 : status = BGWH_POSTMASTER_DIED;
1244 : 0 : break;
1245 : : }
1246 : :
3810 rhaas@postgresql.org 1247 :CBC 17 : ResetLatch(MyLatch);
1248 : : }
1249 : :
3972 1250 : 12 : return status;
1251 : : }
1252 : :
1253 : : /*
1254 : : * Wait for a background worker to stop.
1255 : : *
1256 : : * If the worker hasn't yet started, or is running, we wait for it to stop
1257 : : * and then return BGWH_STOPPED. However, if the postmaster has died, we give
1258 : : * up and return BGWH_POSTMASTER_DIED, because it's the postmaster that
1259 : : * notifies us when a worker's state changes.
1260 : : *
1261 : : * The caller *must* have set our PID as the worker's bgw_notify_pid,
1262 : : * else we will not be awoken promptly when the worker's state changes.
1263 : : */
1264 : : BgwHandleStatus
1265 : 1492 : WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
1266 : : {
1267 : : BgwHandleStatus status;
1268 : : int rc;
1269 : :
1270 : : for (;;)
1271 : 1729 : {
1272 : : pid_t pid;
1273 : :
3810 1274 [ + + ]: 3221 : CHECK_FOR_INTERRUPTS();
1275 : :
1276 : 3221 : status = GetBackgroundWorkerPid(handle, &pid);
1277 [ + + ]: 3221 : if (status == BGWH_STOPPED)
3510 tgl@sss.pgh.pa.us 1278 : 1492 : break;
1279 : :
3204 andres@anarazel.de 1280 : 1729 : rc = WaitLatch(MyLatch,
1281 : : WL_LATCH_SET | WL_POSTMASTER_DEATH, 0,
1282 : : WAIT_EVENT_BGWORKER_SHUTDOWN);
1283 : :
3810 rhaas@postgresql.org 1284 [ - + ]: 1729 : if (rc & WL_POSTMASTER_DEATH)
1285 : : {
3510 tgl@sss.pgh.pa.us 1286 :UBC 0 : status = BGWH_POSTMASTER_DIED;
1287 : 0 : break;
1288 : : }
1289 : :
3204 andres@anarazel.de 1290 :CBC 1729 : ResetLatch(MyLatch);
1291 : : }
1292 : :
4582 rhaas@postgresql.org 1293 : 1492 : return status;
1294 : : }
1295 : :
1296 : : /*
1297 : : * Instruct the postmaster to terminate a background worker.
1298 : : *
1299 : : * Note that it's safe to do this without regard to whether the worker is
1300 : : * still running, or even if the worker may already have exited and been
1301 : : * unregistered.
1302 : : */
1303 : : void
4531 1304 : 6 : TerminateBackgroundWorker(BackgroundWorkerHandle *handle)
1305 : : {
1306 : : BackgroundWorkerSlot *slot;
4331 bruce@momjian.us 1307 : 6 : bool signal_postmaster = false;
1308 : :
4531 rhaas@postgresql.org 1309 [ - + ]: 6 : Assert(handle->slot < max_worker_processes);
1310 : 6 : slot = &BackgroundWorkerData->slot[handle->slot];
1311 : :
1312 : : /* Set terminate flag in shared memory, unless slot has been reused. */
1313 : 6 : LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
1314 [ + - ]: 6 : if (handle->generation == slot->generation)
1315 : : {
1316 : 6 : slot->terminate = true;
1317 : 6 : signal_postmaster = true;
1318 : : }
1319 : 6 : LWLockRelease(BackgroundWorkerLock);
1320 : :
1321 : : /* Make sure the postmaster notices the change to shared memory. */
1322 [ + - ]: 6 : if (signal_postmaster)
1323 : 6 : SendPostmasterSignal(PMSIGNAL_BACKGROUND_WORKER_CHANGE);
1324 : 6 : }
1325 : :
1326 : : /*
1327 : : * Look up (and possibly load) a bgworker entry point function.
1328 : : *
1329 : : * For functions contained in the core code, we use library name "postgres"
1330 : : * and consult the InternalBGWorkers array. External functions are
1331 : : * looked up, and loaded if necessary, using load_external_function().
1332 : : *
1333 : : * The point of this is to pass function names as strings across process
1334 : : * boundaries. We can't pass actual function addresses because of the
1335 : : * possibility that the function has been loaded at a different address
1336 : : * in a different process. This is obviously a hazard for functions in
1337 : : * loadable libraries, but it can happen even for functions in the core code
1338 : : * on platforms using EXEC_BACKEND (e.g., Windows).
1339 : : *
1340 : : * At some point it might be worthwhile to get rid of InternalBGWorkers[]
1341 : : * in favor of applying load_external_function() for core functions too;
1342 : : * but that raises portability issues that are not worth addressing now.
1343 : : */
1344 : : static bgworker_main_type
3257 tgl@sss.pgh.pa.us 1345 : 2543 : LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
1346 : : {
1347 : : /*
1348 : : * If the function is to be loaded from postgres itself, search the
1349 : : * InternalBGWorkers array.
1350 : : */
1351 [ + + ]: 2543 : if (strcmp(libraryname, "postgres") == 0)
1352 : : {
1353 : : int i;
1354 : :
1355 [ + - ]: 4589 : for (i = 0; i < lengthof(InternalBGWorkers); i++)
1356 : : {
1357 [ + + ]: 4589 : if (strcmp(InternalBGWorkers[i].fn_name, funcname) == 0)
1358 : 2525 : return InternalBGWorkers[i].fn_addr;
1359 : : }
1360 : :
1361 : : /* We can only reach this by programming error. */
3257 tgl@sss.pgh.pa.us 1362 [ # # ]:UBC 0 : elog(ERROR, "internal function \"%s\" not found", funcname);
1363 : : }
1364 : :
1365 : : /* Otherwise load from external library. */
3257 tgl@sss.pgh.pa.us 1366 :CBC 18 : return (bgworker_main_type)
1367 : 18 : load_external_function(libraryname, funcname, true, NULL);
1368 : : }
1369 : :
1370 : : /*
1371 : : * Given a PID, get the bgw_type of the background worker. Returns NULL if
1372 : : * not a valid background worker.
1373 : : *
1374 : : * The return value is in static memory belonging to this function, so it has
1375 : : * to be used before calling this function again. This is so that the caller
1376 : : * doesn't have to worry about the background worker locking protocol.
1377 : : */
1378 : : const char *
3118 peter_e@gmx.net 1379 : 986 : GetBackgroundWorkerTypeByPid(pid_t pid)
1380 : : {
1381 : : int slotno;
1382 : 986 : bool found = false;
1383 : : static char result[BGW_MAXLEN];
1384 : :
1385 : 986 : LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1386 : :
1387 [ + - ]: 1121 : for (slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++)
1388 : : {
1389 : 1121 : BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
1390 : :
1391 [ + + + + ]: 1121 : if (slot->pid > 0 && slot->pid == pid)
1392 : : {
1393 : 986 : strcpy(result, slot->worker.bgw_type);
1394 : 986 : found = true;
1395 : 986 : break;
1396 : : }
1397 : : }
1398 : :
1399 : 986 : LWLockRelease(BackgroundWorkerLock);
1400 : :
1401 [ - + ]: 986 : if (!found)
3118 peter_e@gmx.net 1402 :UBC 0 : return NULL;
1403 : :
3118 peter_e@gmx.net 1404 :CBC 986 : return result;
1405 : : }
1406 : :
1407 : : /*
1408 : : * Terminate all background workers connected to the given database, if the
1409 : : * workers can be interrupted.
1410 : : */
1411 : : void
68 michael@paquier.xyz 1412 :GNC 7 : TerminateBackgroundWorkersForDatabase(Oid databaseId)
1413 : : {
1414 : 7 : bool signal_postmaster = false;
1415 : :
1416 : 7 : LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
1417 : :
1418 : : /*
1419 : : * Iterate through slots, looking for workers connected to the given
1420 : : * database.
1421 : : */
1422 [ + + ]: 63 : for (int slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++)
1423 : : {
1424 : 56 : BackgroundWorkerSlot *slot = &BackgroundWorkerData->slot[slotno];
1425 : :
1426 [ + + ]: 56 : if (slot->in_use &&
1427 [ + + ]: 14 : (slot->worker.bgw_flags & BGWORKER_INTERRUPTIBLE))
1428 : : {
1429 : 4 : PGPROC *proc = BackendPidGetProc(slot->pid);
1430 : :
1431 [ + - + - ]: 4 : if (proc && proc->databaseId == databaseId)
1432 : : {
1433 : 4 : slot->terminate = true;
1434 : 4 : signal_postmaster = true;
1435 : : }
1436 : : }
1437 : : }
1438 : :
1439 : 7 : LWLockRelease(BackgroundWorkerLock);
1440 : :
1441 : : /* Make sure the postmaster notices the change to shared memory. */
1442 [ + + ]: 7 : if (signal_postmaster)
1443 : 4 : SendPostmasterSignal(PMSIGNAL_BACKGROUND_WORKER_CHANGE);
1444 : 7 : }
|