PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
bgworker_internals.h File Reference
#include "datatype/timestamp.h"
#include "lib/ilist.h"
#include "postmaster/bgworker.h"
Include dependency graph for bgworker_internals.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  RegisteredBgWorker
 

Macros

#define MAX_PARALLEL_WORKER_LIMIT   1024
 

Typedefs

typedef struct RegisteredBgWorker RegisteredBgWorker
 

Functions

Size BackgroundWorkerShmemSize (void)
 
void BackgroundWorkerShmemInit (void)
 
void BackgroundWorkerStateChange (void)
 
void ForgetBackgroundWorker (slist_mutable_iter *cur)
 
void ReportBackgroundWorkerPID (RegisteredBgWorker *)
 
void ReportBackgroundWorkerExit (slist_mutable_iter *cur)
 
void BackgroundWorkerStopNotifications (pid_t pid)
 
void ResetBackgroundWorkerCrashTimes (void)
 
void StartBackgroundWorker (void) pg_attribute_noreturn()
 

Variables

slist_head BackgroundWorkerList
 

Macro Definition Documentation

#define MAX_PARALLEL_WORKER_LIMIT   1024

Definition at line 24 of file bgworker_internals.h.

Referenced by RegisterDynamicBackgroundWorker().

Typedef Documentation

Function Documentation

void BackgroundWorkerShmemInit ( void  )

Definition at line 159 of file bgworker.c.

References Assert, BackgroundWorkerShmemSize(), BackgroundWorker::bgw_notify_pid, slist_iter::cur, BackgroundWorkerSlot::generation, BackgroundWorkerSlot::in_use, InvalidPid, IsUnderPostmaster, max_worker_processes, BackgroundWorkerArray::parallel_register_count, BackgroundWorkerArray::parallel_terminate_count, BackgroundWorkerSlot::pid, RegisteredBgWorker::rw_shmem_slot, RegisteredBgWorker::rw_worker, ShmemInitStruct(), slist_container, slist_foreach, BackgroundWorkerArray::slot, BackgroundWorkerSlot::terminate, BackgroundWorkerArray::total_slots, and BackgroundWorkerSlot::worker.

Referenced by CreateSharedMemoryAndSemaphores().

160 {
161  bool found;
162 
163  BackgroundWorkerData = ShmemInitStruct("Background Worker Data",
165  &found);
166  if (!IsUnderPostmaster)
167  {
168  slist_iter siter;
169  int slotno = 0;
170 
174 
175  /*
176  * Copy contents of worker list into shared memory. Record the shared
177  * memory slot assigned to each worker. This ensures a 1-to-1
178  * correspondence between the postmaster's private list and the array
179  * in shared memory.
180  */
182  {
184  RegisteredBgWorker *rw;
185 
186  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
187  Assert(slotno < max_worker_processes);
188  slot->in_use = true;
189  slot->terminate = false;
190  slot->pid = InvalidPid;
191  slot->generation = 0;
192  rw->rw_shmem_slot = slotno;
193  rw->rw_worker.bgw_notify_pid = 0; /* might be reinit after crash */
194  memcpy(&slot->worker, &rw->rw_worker, sizeof(BackgroundWorker));
195  ++slotno;
196  }
197 
198  /*
199  * Mark any remaining slots as not in use.
200  */
201  while (slotno < max_worker_processes)
202  {
204 
205  slot->in_use = false;
206  ++slotno;
207  }
208  }
209  else
210  Assert(found);
211 }
slist_node * cur
Definition: ilist.h:226
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:143
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
slist_head BackgroundWorkerList
Definition: bgworker.c:43
uint32 parallel_register_count
Definition: bgworker.c:100
bool IsUnderPostmaster
Definition: globals.c:100
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
#define Assert(condition)
Definition: c.h:675
BackgroundWorker worker
Definition: bgworker.c:83
#define slist_foreach(iter, lhead)
Definition: ilist.h:700
uint32 parallel_terminate_count
Definition: bgworker.c:101
pid_t bgw_notify_pid
Definition: bgworker.h:98
int max_worker_processes
Definition: globals.c:124
BackgroundWorker rw_worker
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:102
#define InvalidPid
Definition: miscadmin.h:31
Size BackgroundWorkerShmemSize ( void  )

Definition at line 143 of file bgworker.c.

References add_size(), max_worker_processes, mul_size(), and offsetof.

Referenced by BackgroundWorkerShmemInit(), and CreateSharedMemoryAndSemaphores().

144 {
145  Size size;
146 
147  /* Array of workers is variably sized. */
148  size = offsetof(BackgroundWorkerArray, slot);
150  sizeof(BackgroundWorkerSlot)));
151 
152  return size;
153 }
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:356
int max_worker_processes
Definition: globals.c:124
#define offsetof(type, field)
Definition: c.h:555
void BackgroundWorkerStateChange ( void  )

Definition at line 241 of file bgworker.c.

References ascii_safe_strlcpy(), BackgroundWorker::bgw_extra, BGW_EXTRALEN, BackgroundWorker::bgw_flags, BackgroundWorker::bgw_function_name, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_main_arg, BGW_MAXLEN, BackgroundWorker::bgw_name, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, BackgroundWorker::bgw_start_time, BGWORKER_CLASS_PARALLEL, DEBUG1, elog, ereport, errcode(), errmsg(), FindRegisteredWorkerBySlotNumber(), BackgroundWorkerSlot::in_use, LOG, malloc, max_worker_processes, NULL, BackgroundWorkerArray::parallel_terminate_count, pg_memory_barrier, pg_read_barrier, BackgroundWorkerSlot::pid, PostmasterMarkPIDForWorkerNotify(), ReportBackgroundWorkerPID(), RegisteredBgWorker::rw_backend, RegisteredBgWorker::rw_child_slot, RegisteredBgWorker::rw_crashed_at, RegisteredBgWorker::rw_lnode, RegisteredBgWorker::rw_pid, RegisteredBgWorker::rw_shmem_slot, RegisteredBgWorker::rw_terminate, RegisteredBgWorker::rw_worker, SIGUSR1, slist_push_head(), BackgroundWorkerArray::slot, BackgroundWorkerSlot::terminate, BackgroundWorkerArray::total_slots, and BackgroundWorkerSlot::worker.

Referenced by sigusr1_handler().

242 {
243  int slotno;
244 
245  /*
246  * The total number of slots stored in shared memory should match our
247  * notion of max_worker_processes. If it does not, something is very
248  * wrong. Further down, we always refer to this value as
249  * max_worker_processes, in case shared memory gets corrupted while we're
250  * looping.
251  */
253  {
254  elog(LOG,
255  "inconsistent background worker state (max_worker_processes=%d, total_slots=%d",
258  return;
259  }
260 
261  /*
262  * Iterate through slots, looking for newly-registered workers or workers
263  * who must die.
264  */
265  for (slotno = 0; slotno < max_worker_processes; ++slotno)
266  {
268  RegisteredBgWorker *rw;
269 
270  if (!slot->in_use)
271  continue;
272 
273  /*
274  * Make sure we don't see the in_use flag before the updated slot
275  * contents.
276  */
277  pg_read_barrier();
278 
279  /* See whether we already know about this worker. */
281  if (rw != NULL)
282  {
283  /*
284  * In general, the worker data can't change after it's initially
285  * registered. However, someone can set the terminate flag.
286  */
287  if (slot->terminate && !rw->rw_terminate)
288  {
289  rw->rw_terminate = true;
290  if (rw->rw_pid != 0)
291  kill(rw->rw_pid, SIGTERM);
292  else
293  {
294  /* Report never-started, now-terminated worker as dead. */
296  }
297  }
298  continue;
299  }
300 
301  /*
302  * If the worker is marked for termination, we don't need to add it to
303  * the registered workers list; we can just free the slot. However, if
304  * bgw_notify_pid is set, the process that registered the worker may
305  * need to know that we've processed the terminate request, so be sure
306  * to signal it.
307  */
308  if (slot->terminate)
309  {
310  int notify_pid;
311 
312  /*
313  * We need a memory barrier here to make sure that the load of
314  * bgw_notify_pid and the update of parallel_terminate_count
315  * complete before the store to in_use.
316  */
317  notify_pid = slot->worker.bgw_notify_pid;
318  if ((slot->worker.bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
321  slot->pid = 0;
322  slot->in_use = false;
323  if (notify_pid != 0)
324  kill(notify_pid, SIGUSR1);
325 
326  continue;
327  }
328 
329  /*
330  * Copy the registration data into the registered workers list.
331  */
332  rw = malloc(sizeof(RegisteredBgWorker));
333  if (rw == NULL)
334  {
335  ereport(LOG,
336  (errcode(ERRCODE_OUT_OF_MEMORY),
337  errmsg("out of memory")));
338  return;
339  }
340 
341  /*
342  * Copy strings in a paranoid way. If shared memory is corrupted, the
343  * source data might not even be NUL-terminated.
344  */
346  slot->worker.bgw_name, BGW_MAXLEN);
351 
352  /*
353  * Copy various fixed-size fields.
354  *
355  * flags, start_time, and restart_time are examined by the postmaster,
356  * but nothing too bad will happen if they are corrupted. The
357  * remaining fields will only be examined by the child process. It
358  * might crash, but we won't.
359  */
360  rw->rw_worker.bgw_flags = slot->worker.bgw_flags;
364  memcpy(rw->rw_worker.bgw_extra, slot->worker.bgw_extra, BGW_EXTRALEN);
365 
366  /*
367  * Copy the PID to be notified about state changes, but only if the
368  * postmaster knows about a backend with that PID. It isn't an error
369  * if the postmaster doesn't know about the PID, because the backend
370  * that requested the worker could have died (or been killed) just
371  * after doing so. Nonetheless, at least until we get some experience
372  * with how this plays out in the wild, log a message at a relative
373  * high debug level.
374  */
377  {
378  elog(DEBUG1, "worker notification PID %lu is not valid",
379  (long) rw->rw_worker.bgw_notify_pid);
380  rw->rw_worker.bgw_notify_pid = 0;
381  }
382 
383  /* Initialize postmaster bookkeeping. */
384  rw->rw_backend = NULL;
385  rw->rw_pid = 0;
386  rw->rw_child_slot = 0;
387  rw->rw_crashed_at = 0;
388  rw->rw_shmem_slot = slotno;
389  rw->rw_terminate = false;
390 
391  /* Log it! */
392  ereport(DEBUG1,
393  (errmsg("registering background worker \"%s\"",
394  rw->rw_worker.bgw_name)));
395 
397  }
398 }
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:97
struct bkend * rw_backend
#define SIGUSR1
Definition: win32.h:202
#define DEBUG1
Definition: elog.h:25
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:574
int bgw_restart_time
Definition: bgworker.h:93
int errcode(int sqlerrcode)
Definition: elog.c:575
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
void ascii_safe_strlcpy(char *dest, const char *src, size_t destsiz)
Definition: ascii.c:173
#define LOG
Definition: elog.h:26
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:95
#define malloc(a)
Definition: header.h:50
Datum bgw_main_arg
Definition: bgworker.h:96
void ReportBackgroundWorkerPID(RegisteredBgWorker *rw)
Definition: bgworker.c:438
bool PostmasterMarkPIDForWorkerNotify(int pid)
Definition: postmaster.c:5850
slist_head BackgroundWorkerList
Definition: bgworker.c:43
#define ereport(elevel, rest)
Definition: elog.h:122
static RegisteredBgWorker * FindRegisteredWorkerBySlotNumber(int slotno)
Definition: bgworker.c:218
#define pg_memory_barrier()
Definition: atomics.h:148
#define NULL
Definition: c.h:229
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
BackgroundWorker worker
Definition: bgworker.c:83
#define pg_read_barrier()
Definition: atomics.h:161
#define BGW_MAXLEN
Definition: bgworker.h:85
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:92
int errmsg(const char *fmt,...)
Definition: elog.c:797
uint32 parallel_terminate_count
Definition: bgworker.c:101
pid_t bgw_notify_pid
Definition: bgworker.h:98
int max_worker_processes
Definition: globals.c:124
#define elog
Definition: elog.h:219
BackgroundWorker rw_worker
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:102
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:94
#define BGW_EXTRALEN
Definition: bgworker.h:86
void BackgroundWorkerStopNotifications ( pid_t  pid)

Definition at line 491 of file bgworker.c.

References BackgroundWorker::bgw_notify_pid, slist_iter::cur, RegisteredBgWorker::rw_worker, slist_container, and slist_foreach.

Referenced by CleanupBackend(), and CleanupBackgroundWorker().

492 {
493  slist_iter siter;
494 
496  {
497  RegisteredBgWorker *rw;
498 
499  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
500  if (rw->rw_worker.bgw_notify_pid == pid)
501  rw->rw_worker.bgw_notify_pid = 0;
502  }
503 }
slist_node * cur
Definition: ilist.h:226
slist_head BackgroundWorkerList
Definition: bgworker.c:43
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
#define slist_foreach(iter, lhead)
Definition: ilist.h:700
pid_t bgw_notify_pid
Definition: bgworker.h:98
BackgroundWorker rw_worker
void ForgetBackgroundWorker ( slist_mutable_iter cur)

Definition at line 410 of file bgworker.c.

References Assert, BackgroundWorker::bgw_flags, BackgroundWorker::bgw_name, BGWORKER_CLASS_PARALLEL, slist_mutable_iter::cur, DEBUG1, ereport, errmsg(), free, BackgroundWorkerSlot::in_use, max_worker_processes, BackgroundWorkerArray::parallel_terminate_count, RegisteredBgWorker::rw_shmem_slot, RegisteredBgWorker::rw_worker, slist_container, slist_delete_current(), and BackgroundWorkerArray::slot.

Referenced by DetermineSleepTime(), maybe_start_bgworkers(), ReportBackgroundWorkerExit(), and ResetBackgroundWorkerCrashTimes().

411 {
412  RegisteredBgWorker *rw;
413  BackgroundWorkerSlot *slot;
414 
415  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
416 
421 
422  slot->in_use = false;
423 
424  ereport(DEBUG1,
425  (errmsg("unregistering background worker \"%s\"",
426  rw->rw_worker.bgw_name)));
427 
429  free(rw);
430 }
#define DEBUG1
Definition: elog.h:25
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
slist_node * cur
Definition: ilist.h:241
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
#define ereport(elevel, rest)
Definition: elog.h:122
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
#define free(a)
Definition: header.h:65
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
uint32 parallel_terminate_count
Definition: bgworker.c:101
int max_worker_processes
Definition: globals.c:124
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:652
BackgroundWorker rw_worker
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:102
void ReportBackgroundWorkerExit ( slist_mutable_iter cur)

Definition at line 457 of file bgworker.c.

References Assert, BGW_NEVER_RESTART, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, slist_mutable_iter::cur, ForgetBackgroundWorker(), max_worker_processes, BackgroundWorkerSlot::pid, RegisteredBgWorker::rw_pid, RegisteredBgWorker::rw_shmem_slot, RegisteredBgWorker::rw_terminate, RegisteredBgWorker::rw_worker, SIGUSR1, slist_container, and BackgroundWorkerArray::slot.

Referenced by CleanupBackgroundWorker().

458 {
459  RegisteredBgWorker *rw;
460  BackgroundWorkerSlot *slot;
461  int notify_pid;
462 
463  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
464 
467  slot->pid = rw->rw_pid;
468  notify_pid = rw->rw_worker.bgw_notify_pid;
469 
470  /*
471  * If this worker is slated for deregistration, do that before notifying
472  * the process which started it. Otherwise, if that process tries to
473  * reuse the slot immediately, it might not be available yet. In theory
474  * that could happen anyway if the process checks slot->pid at just the
475  * wrong moment, but this makes the window narrower.
476  */
477  if (rw->rw_terminate ||
480 
481  if (notify_pid != 0)
482  kill(notify_pid, SIGUSR1);
483 }
#define SIGUSR1
Definition: win32.h:202
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
slist_node * cur
Definition: ilist.h:241
int bgw_restart_time
Definition: bgworker.h:93
void ForgetBackgroundWorker(slist_mutable_iter *cur)
Definition: bgworker.c:410
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
#define Assert(condition)
Definition: c.h:675
pid_t bgw_notify_pid
Definition: bgworker.h:98
int max_worker_processes
Definition: globals.c:124
BackgroundWorker rw_worker
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:102
void ReportBackgroundWorkerPID ( RegisteredBgWorker )

Definition at line 438 of file bgworker.c.

References Assert, BackgroundWorker::bgw_notify_pid, max_worker_processes, BackgroundWorkerSlot::pid, RegisteredBgWorker::rw_pid, RegisteredBgWorker::rw_shmem_slot, RegisteredBgWorker::rw_worker, SIGUSR1, and BackgroundWorkerArray::slot.

Referenced by BackgroundWorkerStateChange(), and do_start_bgworker().

439 {
440  BackgroundWorkerSlot *slot;
441 
442  Assert(rw->rw_shmem_slot < max_worker_processes);
443  slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
444  slot->pid = rw->rw_pid;
445 
446  if (rw->rw_worker.bgw_notify_pid != 0)
447  kill(rw->rw_worker.bgw_notify_pid, SIGUSR1);
448 }
#define SIGUSR1
Definition: win32.h:202
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
#define Assert(condition)
Definition: c.h:675
int max_worker_processes
Definition: globals.c:124
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:102
void ResetBackgroundWorkerCrashTimes ( void  )

Definition at line 513 of file bgworker.c.

References Assert, BackgroundWorker::bgw_flags, BGW_NEVER_RESTART, BackgroundWorker::bgw_restart_time, BGWORKER_CLASS_PARALLEL, slist_mutable_iter::cur, ForgetBackgroundWorker(), RegisteredBgWorker::rw_crashed_at, RegisteredBgWorker::rw_worker, slist_container, and slist_foreach_modify.

Referenced by PostmasterStateMachine().

514 {
515  slist_mutable_iter iter;
516 
518  {
519  RegisteredBgWorker *rw;
520 
521  rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
522 
524  {
525  /*
526  * Workers marked BGW_NVER_RESTART shouldn't get relaunched after
527  * the crash, so forget about them. (If we wait until after the
528  * crash to forget about them, and they are parallel workers,
529  * parallel_terminate_count will get incremented after we've
530  * already zeroed parallel_register_count, which would be bad.)
531  */
532  ForgetBackgroundWorker(&iter);
533  }
534  else
535  {
536  /*
537  * The accounting which we do via parallel_register_count and
538  * parallel_terminate_count would get messed up if a worker marked
539  * parallel could survive a crash and restart cycle. All such
540  * workers should be marked BGW_NEVER_RESTART, and thus control
541  * should never reach this branch.
542  */
544 
545  /*
546  * Allow this worker to be restarted immediately after we finish
547  * resetting.
548  */
549  rw->rw_crashed_at = 0;
550  }
551  }
552 }
slist_node * cur
Definition: ilist.h:241
int bgw_restart_time
Definition: bgworker.h:93
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:716
slist_head BackgroundWorkerList
Definition: bgworker.c:43
void ForgetBackgroundWorker(slist_mutable_iter *cur)
Definition: bgworker.c:410
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
#define Assert(condition)
Definition: c.h:675
BackgroundWorker rw_worker
void StartBackgroundWorker ( void  )

Definition at line 700 of file bgworker.c.

References BaseInit(), BackgroundWorker::bgw_flags, BackgroundWorker::bgw_function_name, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_main_arg, BackgroundWorker::bgw_name, BGWORKER_BACKEND_DATABASE_CONNECTION, bgworker_die(), bgworker_quickdie(), BGWORKER_SHMEM_ACCESS, bgworker_sigusr1_handler(), buf, dsm_detach_all(), elog, EmitErrorReport(), error_context_stack, FATAL, FloatExceptionHandler(), HOLD_INTERRUPTS, init_ps_display(), InitializeTimeouts(), InitProcess(), InitProcessing, IsBackgroundWorker, LookupBackgroundWorkerFunction(), MAXPGPATH, MyBgworkerEntry, NULL, PG_exception_stack, pg_usleep(), PGSharedMemoryDetach(), PostAuthDelay, pqsignal(), proc_exit(), procsignal_sigusr1_handler(), SetProcessingMode, SIG_DFL, SIG_IGN, SIGCHLD, SIGHUP, SIGPIPE, SIGQUIT, SIGUSR1, SIGUSR2, snprintf(), and StatementCancelHandler().

Referenced by do_start_bgworker().

701 {
702  sigjmp_buf local_sigjmp_buf;
703  char buf[MAXPGPATH];
705  bgworker_main_type entrypt;
706 
707  if (worker == NULL)
708  elog(FATAL, "unable to find bgworker entry");
709 
710  IsBackgroundWorker = true;
711 
712  /* Identify myself via ps */
713  snprintf(buf, MAXPGPATH, "bgworker: %s", worker->bgw_name);
714  init_ps_display(buf, "", "", "");
715 
716  /*
717  * If we're not supposed to have shared memory access, then detach from
718  * shared memory. If we didn't request shared memory access, the
719  * postmaster won't force a cluster-wide restart if we exit unexpectedly,
720  * so we'd better make sure that we don't mess anything up that would
721  * require that sort of cleanup.
722  */
723  if ((worker->bgw_flags & BGWORKER_SHMEM_ACCESS) == 0)
724  {
725  dsm_detach_all();
727  }
728 
730 
731  /* Apply PostAuthDelay */
732  if (PostAuthDelay > 0)
733  pg_usleep(PostAuthDelay * 1000000L);
734 
735  /*
736  * Set up signal handlers.
737  */
739  {
740  /*
741  * SIGINT is used to signal canceling the current action
742  */
746 
747  /* XXX Any other handlers needed here? */
748  }
749  else
750  {
751  pqsignal(SIGINT, SIG_IGN);
753  pqsignal(SIGFPE, SIG_IGN);
754  }
755  pqsignal(SIGTERM, bgworker_die);
757 
759  InitializeTimeouts(); /* establishes SIGALRM handler */
760 
764 
765  /*
766  * If an exception is encountered, processing resumes here.
767  *
768  * See notes in postgres.c about the design of this coding.
769  */
770  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
771  {
772  /* Since not using PG_TRY, must reset error stack by hand */
774 
775  /* Prevent interrupts while cleaning up */
776  HOLD_INTERRUPTS();
777 
778  /* Report the error to the server log */
779  EmitErrorReport();
780 
781  /*
782  * Do we need more cleanup here? For shmem-connected bgworkers, we
783  * will call InitProcess below, which will install ProcKill as exit
784  * callback. That will take care of releasing locks, etc.
785  */
786 
787  /* and go away */
788  proc_exit(1);
789  }
790 
791  /* We can now handle ereport(ERROR) */
792  PG_exception_stack = &local_sigjmp_buf;
793 
794  /*
795  * If the background worker request shared memory access, set that up now;
796  * else, detach all shared memory segments.
797  */
798  if (worker->bgw_flags & BGWORKER_SHMEM_ACCESS)
799  {
800  /*
801  * Early initialization. Some of this could be useful even for
802  * background workers that aren't using shared memory, but they can
803  * call the individual startup routines for those subsystems if
804  * needed.
805  */
806  BaseInit();
807 
808  /*
809  * Create a per-backend PGPROC struct in shared memory, except in the
810  * EXEC_BACKEND case where this was done in SubPostmasterMain. We must
811  * do this before we can use LWLocks (and in the EXEC_BACKEND case we
812  * already had to do some stuff with LWLocks).
813  */
814 #ifndef EXEC_BACKEND
815  InitProcess();
816 #endif
817  }
818 
819  /*
820  * Look up the entry point function, loading its library if necessary.
821  */
823  worker->bgw_function_name);
824 
825  /*
826  * Note that in normal processes, we would call InitPostgres here. For a
827  * worker, however, we don't know what database to connect to, yet; so we
828  * need to wait until the user code does it via
829  * BackgroundWorkerInitializeConnection().
830  */
831 
832  /*
833  * Now invoke the user-defined worker code
834  */
835  entrypt(worker->bgw_main_arg);
836 
837  /* ... and if it returns, we're done */
838  proc_exit(0);
839 }
void InitializeTimeouts(void)
Definition: timeout.c:340
static void bgworker_sigusr1_handler(SIGNAL_ARGS)
Definition: bgworker.c:684
#define SIGUSR1
Definition: win32.h:202
void proc_exit(int code)
Definition: ipc.c:99
#define SetProcessingMode(mode)
Definition: miscadmin.h:371
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:189
void BaseInit(void)
Definition: postinit.c:517
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:95
#define SIGQUIT
Definition: win32.h:189
void PGSharedMemoryDetach(void)
Definition: sysv_shmem.c:789
void StatementCancelHandler(SIGNAL_ARGS)
Definition: postgres.c:2654
bool IsBackgroundWorker
Definition: globals.c:102
ErrorContextCallback * error_context_stack
Definition: elog.c:88
void pg_usleep(long microsec)
Definition: signal.c:53
Datum bgw_main_arg
Definition: bgworker.h:96
#define SIG_IGN
Definition: win32.h:185
#define FATAL
Definition: elog.h:52
void(* bgworker_main_type)(Datum main_arg)
Definition: bgworker.h:71
#define MAXPGPATH
void InitProcess(void)
Definition: proc.c:287
static char * buf
Definition: pg_test_fsync.c:66
static bgworker_main_type LookupBackgroundWorkerFunction(const char *libraryname, const char *funcname)
Definition: bgworker.c:1212
int PostAuthDelay
Definition: postgres.c:97
void EmitErrorReport(void)
Definition: elog.c:1446
#define SIGPIPE
Definition: win32.h:193
#define SIGHUP
Definition: win32.h:188
#define SIG_DFL
Definition: win32.h:183
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
#define NULL
Definition: c.h:229
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
void dsm_detach_all(void)
Definition: dsm.c:666
sigjmp_buf * PG_exception_stack
Definition: elog.c:90
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
void FloatExceptionHandler(SIGNAL_ARGS)
Definition: postgres.c:2675
static void bgworker_die(SIGNAL_ARGS)
Definition: bgworker.c:667
#define elog
Definition: elog.h:219
static void bgworker_quickdie(SIGNAL_ARGS)
Definition: bgworker.c:637
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:260
#define SIGCHLD
Definition: win32.h:198
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:94
#define SIGUSR2
Definition: win32.h:203
void init_ps_display(const char *username, const char *dbname, const char *host_info, const char *initial_str)
Definition: ps_status.c:244

Variable Documentation

slist_head BackgroundWorkerList