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