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