PostgreSQL Source Code  git master
bgworker.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "libpq/pqsignal.h"
#include "access/parallel.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "port/atomics.h"
#include "postmaster/bgworker_internals.h"
#include "postmaster/postmaster.h"
#include "replication/logicallauncher.h"
#include "replication/logicalworker.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/lwlock.h"
#include "storage/pg_shmem.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "storage/shmem.h"
#include "tcop/tcopprot.h"
#include "utils/ascii.h"
#include "utils/ps_status.h"
#include "utils/timeout.h"
Include dependency graph for bgworker.c:

Go to the source code of this file.

Data Structures

struct  BackgroundWorkerSlot
 
struct  BackgroundWorkerArray
 
struct  BackgroundWorkerHandle
 

Typedefs

typedef struct BackgroundWorkerSlot BackgroundWorkerSlot
 
typedef struct BackgroundWorkerArray BackgroundWorkerArray
 

Functions

static bgworker_main_type LookupBackgroundWorkerFunction (const char *libraryname, const char *funcname)
 
Size BackgroundWorkerShmemSize (void)
 
void BackgroundWorkerShmemInit (void)
 
static RegisteredBgWorkerFindRegisteredWorkerBySlotNumber (int slotno)
 
void BackgroundWorkerStateChange (void)
 
void ForgetBackgroundWorker (slist_mutable_iter *cur)
 
void ReportBackgroundWorkerPID (RegisteredBgWorker *rw)
 
void ReportBackgroundWorkerExit (slist_mutable_iter *cur)
 
void BackgroundWorkerStopNotifications (pid_t pid)
 
void ResetBackgroundWorkerCrashTimes (void)
 
static bool SanityCheckBackgroundWorker (BackgroundWorker *worker, int elevel)
 
static void bgworker_quickdie (SIGNAL_ARGS)
 
static void bgworker_die (SIGNAL_ARGS)
 
static void bgworker_sigusr1_handler (SIGNAL_ARGS)
 
void StartBackgroundWorker (void)
 
void RegisterBackgroundWorker (BackgroundWorker *worker)
 
bool RegisterDynamicBackgroundWorker (BackgroundWorker *worker, BackgroundWorkerHandle **handle)
 
BgwHandleStatus GetBackgroundWorkerPid (BackgroundWorkerHandle *handle, pid_t *pidp)
 
BgwHandleStatus WaitForBackgroundWorkerStartup (BackgroundWorkerHandle *handle, pid_t *pidp)
 
BgwHandleStatus WaitForBackgroundWorkerShutdown (BackgroundWorkerHandle *handle)
 
void TerminateBackgroundWorker (BackgroundWorkerHandle *handle)
 
const char * GetBackgroundWorkerTypeByPid (pid_t pid)
 

Variables

slist_head BackgroundWorkerList = SLIST_STATIC_INIT(BackgroundWorkerList)
 
static BackgroundWorkerArrayBackgroundWorkerData
 
struct {
   const char *   fn_name
 
   bgworker_main_type   fn_addr
 
InternalBGWorkers []
 

Typedef Documentation

◆ BackgroundWorkerArray

◆ BackgroundWorkerSlot

Function Documentation

◆ BackgroundWorkerShmemInit()

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:101
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
#define Assert(condition)
Definition: c.h:670
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:99
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

◆ BackgroundWorkerShmemSize()

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:404
int max_worker_processes
Definition: globals.c:124
#define offsetof(type, field)
Definition: c.h:593

◆ BackgroundWorkerStateChange()

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, BackgroundWorker::bgw_type, BGWORKER_CLASS_PARALLEL, DEBUG1, elog, ereport, errcode(), errmsg(), FindRegisteredWorkerBySlotNumber(), BackgroundWorkerSlot::in_use, kill, LOG, malloc, max_worker_processes, 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);
348  slot->worker.bgw_type, BGW_MAXLEN);
353 
354  /*
355  * Copy various fixed-size fields.
356  *
357  * flags, start_time, and restart_time are examined by the postmaster,
358  * but nothing too bad will happen if they are corrupted. The
359  * remaining fields will only be examined by the child process. It
360  * might crash, but we won't.
361  */
362  rw->rw_worker.bgw_flags = slot->worker.bgw_flags;
366  memcpy(rw->rw_worker.bgw_extra, slot->worker.bgw_extra, BGW_EXTRALEN);
367 
368  /*
369  * Copy the PID to be notified about state changes, but only if the
370  * postmaster knows about a backend with that PID. It isn't an error
371  * if the postmaster doesn't know about the PID, because the backend
372  * that requested the worker could have died (or been killed) just
373  * after doing so. Nonetheless, at least until we get some experience
374  * with how this plays out in the wild, log a message at a relative
375  * high debug level.
376  */
379  {
380  elog(DEBUG1, "worker notification PID %lu is not valid",
381  (long) rw->rw_worker.bgw_notify_pid);
382  rw->rw_worker.bgw_notify_pid = 0;
383  }
384 
385  /* Initialize postmaster bookkeeping. */
386  rw->rw_backend = NULL;
387  rw->rw_pid = 0;
388  rw->rw_child_slot = 0;
389  rw->rw_crashed_at = 0;
390  rw->rw_shmem_slot = slotno;
391  rw->rw_terminate = false;
392 
393  /* Log it! */
394  ereport(DEBUG1,
395  (errmsg("registering background worker \"%s\"",
396  rw->rw_worker.bgw_name)));
397 
399  }
400 }
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:98
struct bkend * rw_backend
#define DEBUG1
Definition: elog.h:25
#define SIGUSR1
Definition: win32_port.h:177
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:94
int errcode(int sqlerrcode)
Definition: elog.c:575
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
#define kill(pid, sig)
Definition: win32_port.h:437
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:96
#define malloc(a)
Definition: header.h:50
Datum bgw_main_arg
Definition: bgworker.h:97
void ReportBackgroundWorkerPID(RegisteredBgWorker *rw)
Definition: bgworker.c:440
bool PostmasterMarkPIDForWorkerNotify(int pid)
Definition: postmaster.c:5924
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
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:93
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
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:99
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:95
#define BGW_EXTRALEN
Definition: bgworker.h:86

◆ BackgroundWorkerStopNotifications()

void BackgroundWorkerStopNotifications ( pid_t  pid)

Definition at line 493 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().

494 {
495  slist_iter siter;
496 
498  {
499  RegisteredBgWorker *rw;
500 
501  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
502  if (rw->rw_worker.bgw_notify_pid == pid)
503  rw->rw_worker.bgw_notify_pid = 0;
504  }
505 }
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:99
BackgroundWorker rw_worker

◆ bgworker_die()

static void bgworker_die ( SIGNAL_ARGS  )
static

Definition at line 675 of file bgworker.c.

References BackgroundWorker::bgw_type, BlockSig, ereport, errcode(), errmsg(), FATAL, MyBgworkerEntry, and PG_SETMASK.

Referenced by StartBackgroundWorker().

676 {
678 
679  ereport(FATAL,
680  (errcode(ERRCODE_ADMIN_SHUTDOWN),
681  errmsg("terminating background worker \"%s\" due to administrator command",
683 }
int errcode(int sqlerrcode)
Definition: elog.c:575
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:189
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
sigset_t BlockSig
Definition: pqsignal.c:22
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ bgworker_quickdie()

static void bgworker_quickdie ( SIGNAL_ARGS  )
static

Definition at line 645 of file bgworker.c.

References BlockSig, on_exit_reset(), PG_SETMASK, and SIGQUIT.

Referenced by StartBackgroundWorker().

646 {
647  sigaddset(&BlockSig, SIGQUIT); /* prevent nested calls */
649 
650  /*
651  * We DO NOT want to run proc_exit() callbacks -- we're here because
652  * shared memory may be corrupted, so we don't want to try to clean up our
653  * transaction. Just nail the windows shut and get out of town. Now that
654  * there's an atexit callback to prevent third-party code from breaking
655  * things by calling exit() directly, we have to reset the callbacks
656  * explicitly to make this work as intended.
657  */
658  on_exit_reset();
659 
660  /*
661  * Note we do exit(2) not exit(0). This is to force the postmaster into a
662  * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
663  * backend. This is necessary precisely because we don't clean up our
664  * shared memory state. (The "dead man switch" mechanism in pmsignal.c
665  * should ensure the postmaster sees this as a crash, too, but no harm in
666  * being doubly sure.)
667  */
668  exit(2);
669 }
#define SIGQUIT
Definition: win32_port.h:164
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void on_exit_reset(void)
Definition: ipc.c:396
sigset_t BlockSig
Definition: pqsignal.c:22

◆ bgworker_sigusr1_handler()

static void bgworker_sigusr1_handler ( SIGNAL_ARGS  )
static

Definition at line 692 of file bgworker.c.

References latch_sigusr1_handler().

Referenced by StartBackgroundWorker().

693 {
694  int save_errno = errno;
695 
697 
698  errno = save_errno;
699 }
void latch_sigusr1_handler(void)
Definition: latch.c:1473

◆ FindRegisteredWorkerBySlotNumber()

static RegisteredBgWorker* FindRegisteredWorkerBySlotNumber ( int  slotno)
static

Definition at line 218 of file bgworker.c.

References slist_iter::cur, RegisteredBgWorker::rw_shmem_slot, slist_container, and slist_foreach.

Referenced by BackgroundWorkerStateChange().

219 {
220  slist_iter siter;
221 
223  {
224  RegisteredBgWorker *rw;
225 
226  rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
227  if (rw->rw_shmem_slot == slotno)
228  return rw;
229  }
230 
231  return NULL;
232 }
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

◆ ForgetBackgroundWorker()

void ForgetBackgroundWorker ( slist_mutable_iter cur)

Definition at line 412 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().

413 {
414  RegisteredBgWorker *rw;
415  BackgroundWorkerSlot *slot;
416 
417  rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
418 
423 
424  slot->in_use = false;
425 
426  ereport(DEBUG1,
427  (errmsg("unregistering background worker \"%s\"",
428  rw->rw_worker.bgw_name)));
429 
431  free(rw);
432 }
#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:670
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

◆ GetBackgroundWorkerPid()

BgwHandleStatus GetBackgroundWorkerPid ( BackgroundWorkerHandle handle,
pid_t *  pidp 
)

Definition at line 1047 of file bgworker.c.

References Assert, BGWH_NOT_YET_STARTED, BGWH_STARTED, BGWH_STOPPED, BackgroundWorkerSlot::generation, BackgroundWorkerHandle::generation, InvalidPid, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_worker_processes, BackgroundWorkerSlot::pid, BackgroundWorkerArray::slot, and BackgroundWorkerHandle::slot.

Referenced by check_worker_status(), shm_mq_counterparty_gone(), shm_mq_wait_internal(), WaitForBackgroundWorkerShutdown(), WaitForBackgroundWorkerStartup(), and WaitForReplicationWorkerAttach().

1048 {
1049  BackgroundWorkerSlot *slot;
1050  pid_t pid;
1051 
1052  Assert(handle->slot < max_worker_processes);
1053  slot = &BackgroundWorkerData->slot[handle->slot];
1054 
1055  /*
1056  * We could probably arrange to synchronize access to data using memory
1057  * barriers only, but for now, let's just keep it simple and grab the
1058  * lock. It seems unlikely that there will be enough traffic here to
1059  * result in meaningful contention.
1060  */
1061  LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1062 
1063  /*
1064  * The generation number can't be concurrently changed while we hold the
1065  * lock. The pid, which is updated by the postmaster, can change at any
1066  * time, but we assume such changes are atomic. So the value we read
1067  * won't be garbage, but it might be out of date by the time the caller
1068  * examines it (but that's unavoidable anyway).
1069  */
1070  if (handle->generation != slot->generation)
1071  pid = 0;
1072  else
1073  pid = slot->pid;
1074 
1075  /* All done. */
1076  LWLockRelease(BackgroundWorkerLock);
1077 
1078  if (pid == 0)
1079  return BGWH_STOPPED;
1080  else if (pid == InvalidPid)
1081  return BGWH_NOT_YET_STARTED;
1082  *pidp = pid;
1083  return BGWH_STARTED;
1084 }
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
#define Assert(condition)
Definition: c.h:670
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
int max_worker_processes
Definition: globals.c:124
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:102
#define InvalidPid
Definition: miscadmin.h:31

◆ GetBackgroundWorkerTypeByPid()

const char* GetBackgroundWorkerTypeByPid ( pid_t  pid)

Definition at line 1252 of file bgworker.c.

References BGW_MAXLEN, BackgroundWorker::bgw_type, LW_SHARED, LWLockAcquire(), LWLockRelease(), BackgroundWorkerSlot::pid, BackgroundWorkerArray::slot, BackgroundWorkerArray::total_slots, and BackgroundWorkerSlot::worker.

Referenced by pg_stat_get_activity().

1253 {
1254  int slotno;
1255  bool found = false;
1256  static char result[BGW_MAXLEN];
1257 
1258  LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1259 
1260  for (slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++)
1261  {
1263 
1264  if (slot->pid > 0 && slot->pid == pid)
1265  {
1266  strcpy(result, slot->worker.bgw_type);
1267  found = true;
1268  break;
1269  }
1270  }
1271 
1272  LWLockRelease(BackgroundWorkerLock);
1273 
1274  if (!found)
1275  return NULL;
1276 
1277  return result;
1278 }
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
BackgroundWorker worker
Definition: bgworker.c:83
#define BGW_MAXLEN
Definition: bgworker.h:85
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:102

◆ LookupBackgroundWorkerFunction()

static bgworker_main_type LookupBackgroundWorkerFunction ( const char *  libraryname,
const char *  funcname 
)
static

Definition at line 1218 of file bgworker.c.

References elog, ERROR, fn_addr, fn_name, i, InternalBGWorkers, lengthof, and load_external_function().

Referenced by StartBackgroundWorker().

1219 {
1220  /*
1221  * If the function is to be loaded from postgres itself, search the
1222  * InternalBGWorkers array.
1223  */
1224  if (strcmp(libraryname, "postgres") == 0)
1225  {
1226  int i;
1227 
1228  for (i = 0; i < lengthof(InternalBGWorkers); i++)
1229  {
1230  if (strcmp(InternalBGWorkers[i].fn_name, funcname) == 0)
1231  return InternalBGWorkers[i].fn_addr;
1232  }
1233 
1234  /* We can only reach this by programming error. */
1235  elog(ERROR, "internal function \"%s\" not found", funcname);
1236  }
1237 
1238  /* Otherwise load from external library. */
1239  return (bgworker_main_type)
1240  load_external_function(libraryname, funcname, true, NULL);
1241 }
#define lengthof(array)
Definition: c.h:600
const char * fn_name
Definition: bgworker.c:119
static const struct @22 InternalBGWorkers[]
#define ERROR
Definition: elog.h:43
PGFunction load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition: dfmgr.c:94
bgworker_main_type fn_addr
Definition: bgworker.c:120
int i
#define elog
Definition: elog.h:219
void(* bgworker_main_type)(Datum main_arg)
Definition: bgworker.h:71

◆ RegisterBackgroundWorker()

void RegisterBackgroundWorker ( BackgroundWorker worker)

Definition at line 855 of file bgworker.c.

References BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_name, BackgroundWorker::bgw_notify_pid, DEBUG1, ereport, errcode(), errdetail_plural(), errhint(), errmsg(), IsUnderPostmaster, LOG, malloc, max_worker_processes, process_shared_preload_libraries_in_progress, RegisteredBgWorker::rw_backend, RegisteredBgWorker::rw_child_slot, RegisteredBgWorker::rw_crashed_at, RegisteredBgWorker::rw_lnode, RegisteredBgWorker::rw_pid, RegisteredBgWorker::rw_terminate, RegisteredBgWorker::rw_worker, SanityCheckBackgroundWorker(), slist_push_head(), and BackgroundWorkerSlot::worker.

Referenced by _PG_init(), ApplyLauncherRegister(), and apw_start_master_worker().

856 {
857  RegisteredBgWorker *rw;
858  static int numworkers = 0;
859 
860  if (!IsUnderPostmaster)
861  ereport(DEBUG1,
862  (errmsg("registering background worker \"%s\"", worker->bgw_name)));
863 
865  strcmp(worker->bgw_library_name, "postgres") != 0)
866  {
867  if (!IsUnderPostmaster)
868  ereport(LOG,
869  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
870  errmsg("background worker \"%s\": must be registered in shared_preload_libraries",
871  worker->bgw_name)));
872  return;
873  }
874 
875  if (!SanityCheckBackgroundWorker(worker, LOG))
876  return;
877 
878  if (worker->bgw_notify_pid != 0)
879  {
880  ereport(LOG,
881  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
882  errmsg("background worker \"%s\": only dynamic background workers can request notification",
883  worker->bgw_name)));
884  return;
885  }
886 
887  /*
888  * Enforce maximum number of workers. Note this is overly restrictive: we
889  * could allow more non-shmem-connected workers, because these don't count
890  * towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
891  * important to relax this restriction.
892  */
893  if (++numworkers > max_worker_processes)
894  {
895  ereport(LOG,
896  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
897  errmsg("too many background workers"),
898  errdetail_plural("Up to %d background worker can be registered with the current settings.",
899  "Up to %d background workers can be registered with the current settings.",
902  errhint("Consider increasing the configuration parameter \"max_worker_processes\".")));
903  return;
904  }
905 
906  /*
907  * Copy the registration data into the registered workers list.
908  */
909  rw = malloc(sizeof(RegisteredBgWorker));
910  if (rw == NULL)
911  {
912  ereport(LOG,
913  (errcode(ERRCODE_OUT_OF_MEMORY),
914  errmsg("out of memory")));
915  return;
916  }
917 
918  rw->rw_worker = *worker;
919  rw->rw_backend = NULL;
920  rw->rw_pid = 0;
921  rw->rw_child_slot = 0;
922  rw->rw_crashed_at = 0;
923  rw->rw_terminate = false;
924 
926 }
struct bkend * rw_backend
#define DEBUG1
Definition: elog.h:25
int errhint(const char *fmt,...)
Definition: elog.c:987
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1426
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:574
int errcode(int sqlerrcode)
Definition: elog.c:575
#define LOG
Definition: elog.h:26
#define malloc(a)
Definition: header.h:50
slist_head BackgroundWorkerList
Definition: bgworker.c:43
bool IsUnderPostmaster
Definition: globals.c:101
#define ereport(elevel, rest)
Definition: elog.h:122
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition: bgworker.c:583
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:965
int errmsg(const char *fmt,...)
Definition: elog.c:797
pid_t bgw_notify_pid
Definition: bgworker.h:99
int max_worker_processes
Definition: globals.c:124
BackgroundWorker rw_worker
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:95

◆ RegisterDynamicBackgroundWorker()

bool RegisterDynamicBackgroundWorker ( BackgroundWorker worker,
BackgroundWorkerHandle **  handle 
)

Definition at line 939 of file bgworker.c.

References Assert, BackgroundWorker::bgw_flags, BGWORKER_CLASS_PARALLEL, ERROR, BackgroundWorkerSlot::generation, BackgroundWorkerSlot::in_use, InvalidPid, IsUnderPostmaster, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAX_PARALLEL_WORKER_LIMIT, max_parallel_workers, palloc(), BackgroundWorkerArray::parallel_register_count, BackgroundWorkerArray::parallel_terminate_count, pg_write_barrier, BackgroundWorkerSlot::pid, PMSIGNAL_BACKGROUND_WORKER_CHANGE, SanityCheckBackgroundWorker(), SendPostmasterSignal(), BackgroundWorkerArray::slot, success, BackgroundWorkerSlot::terminate, BackgroundWorkerArray::total_slots, and BackgroundWorkerSlot::worker.

Referenced by apw_start_database_worker(), apw_start_master_worker(), LaunchParallelWorkers(), logicalrep_worker_launch(), setup_background_workers(), and worker_spi_launch().

941 {
942  int slotno;
943  bool success = false;
944  bool parallel;
945  uint64 generation = 0;
946 
947  /*
948  * We can't register dynamic background workers from the postmaster. If
949  * this is a standalone backend, we're the only process and can't start
950  * any more. In a multi-process environment, it might be theoretically
951  * possible, but we don't currently support it due to locking
952  * considerations; see comments on the BackgroundWorkerSlot data
953  * structure.
954  */
955  if (!IsUnderPostmaster)
956  return false;
957 
958  if (!SanityCheckBackgroundWorker(worker, ERROR))
959  return false;
960 
961  parallel = (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0;
962 
963  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
964 
965  /*
966  * If this is a parallel worker, check whether there are already too many
967  * parallel workers; if so, don't register another one. Our view of
968  * parallel_terminate_count may be slightly stale, but that doesn't really
969  * matter: we would have gotten the same result if we'd arrived here
970  * slightly earlier anyway. There's no help for it, either, since the
971  * postmaster must not take locks; a memory barrier wouldn't guarantee
972  * anything useful.
973  */
977  {
981  LWLockRelease(BackgroundWorkerLock);
982  return false;
983  }
984 
985  /*
986  * Look for an unused slot. If we find one, grab it.
987  */
988  for (slotno = 0; slotno < BackgroundWorkerData->total_slots; ++slotno)
989  {
991 
992  if (!slot->in_use)
993  {
994  memcpy(&slot->worker, worker, sizeof(BackgroundWorker));
995  slot->pid = InvalidPid; /* indicates not started yet */
996  slot->generation++;
997  slot->terminate = false;
998  generation = slot->generation;
999  if (parallel)
1001 
1002  /*
1003  * Make sure postmaster doesn't see the slot as in use before it
1004  * sees the new contents.
1005  */
1006  pg_write_barrier();
1007 
1008  slot->in_use = true;
1009  success = true;
1010  break;
1011  }
1012  }
1013 
1014  LWLockRelease(BackgroundWorkerLock);
1015 
1016  /* If we found a slot, tell the postmaster to notice the change. */
1017  if (success)
1019 
1020  /*
1021  * If we found a slot and the user has provided a handle, initialize it.
1022  */
1023  if (success && handle)
1024  {
1025  *handle = palloc(sizeof(BackgroundWorkerHandle));
1026  (*handle)->slot = slotno;
1027  (*handle)->generation = generation;
1028  }
1029 
1030  return success;
1031 }
int max_parallel_workers
Definition: globals.c:125
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
#define ERROR
Definition: elog.h:43
static bool success
Definition: pg_basebackup.c:99
uint32 parallel_register_count
Definition: bgworker.c:100
bool IsUnderPostmaster
Definition: globals.c:101
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition: bgworker.c:583
#define Assert(condition)
Definition: c.h:670
BackgroundWorker worker
Definition: bgworker.c:83
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
void * palloc(Size size)
Definition: mcxt.c:848
uint32 parallel_terminate_count
Definition: bgworker.c:101
#define pg_write_barrier()
Definition: atomics.h:162
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:113
#define MAX_PARALLEL_WORKER_LIMIT
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:102
#define InvalidPid
Definition: miscadmin.h:31

◆ ReportBackgroundWorkerExit()

void ReportBackgroundWorkerExit ( slist_mutable_iter cur)

Definition at line 459 of file bgworker.c.

References Assert, BGW_NEVER_RESTART, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, slist_mutable_iter::cur, ForgetBackgroundWorker(), kill, 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().

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

◆ ReportBackgroundWorkerPID()

void ReportBackgroundWorkerPID ( RegisteredBgWorker rw)

Definition at line 440 of file bgworker.c.

References Assert, BackgroundWorker::bgw_notify_pid, kill, 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().

441 {
442  BackgroundWorkerSlot *slot;
443 
446  slot->pid = rw->rw_pid;
447 
448  if (rw->rw_worker.bgw_notify_pid != 0)
450 }
#define SIGUSR1
Definition: win32_port.h:177
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
#define kill(pid, sig)
Definition: win32_port.h:437
#define Assert(condition)
Definition: c.h:670
pid_t bgw_notify_pid
Definition: bgworker.h:99
int max_worker_processes
Definition: globals.c:124
BackgroundWorker rw_worker
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:102

◆ ResetBackgroundWorkerCrashTimes()

void ResetBackgroundWorkerCrashTimes ( void  )

Definition at line 515 of file bgworker.c.

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

Referenced by PostmasterStateMachine().

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

◆ SanityCheckBackgroundWorker()

static bool SanityCheckBackgroundWorker ( BackgroundWorker worker,
int  elevel 
)
static

Definition at line 583 of file bgworker.c.

References BackgroundWorker::bgw_flags, BackgroundWorker::bgw_name, BGW_NEVER_RESTART, BackgroundWorker::bgw_restart_time, BackgroundWorker::bgw_start_time, BackgroundWorker::bgw_type, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_CLASS_PARALLEL, BGWORKER_SHMEM_ACCESS, BgWorkerStart_PostmasterStart, ereport, errcode(), errmsg(), and USECS_PER_DAY.

Referenced by RegisterBackgroundWorker(), and RegisterDynamicBackgroundWorker().

584 {
585  /* sanity check for flags */
587  {
588  if (!(worker->bgw_flags & BGWORKER_SHMEM_ACCESS))
589  {
590  ereport(elevel,
591  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
592  errmsg("background worker \"%s\": must attach to shared memory in order to request a database connection",
593  worker->bgw_name)));
594  return false;
595  }
596 
598  {
599  ereport(elevel,
600  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
601  errmsg("background worker \"%s\": cannot request database access if starting at postmaster start",
602  worker->bgw_name)));
603  return false;
604  }
605 
606  /* XXX other checks? */
607  }
608 
609  if ((worker->bgw_restart_time < 0 &&
610  worker->bgw_restart_time != BGW_NEVER_RESTART) ||
611  (worker->bgw_restart_time > USECS_PER_DAY / 1000))
612  {
613  ereport(elevel,
614  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
615  errmsg("background worker \"%s\": invalid restart interval",
616  worker->bgw_name)));
617  return false;
618  }
619 
620  /*
621  * Parallel workers may not be configured for restart, because the
622  * parallel_register_count/parallel_terminate_count accounting can't
623  * handle parallel workers lasting through a crash-and-restart cycle.
624  */
625  if (worker->bgw_restart_time != BGW_NEVER_RESTART &&
626  (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0)
627  {
628  ereport(elevel,
629  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
630  errmsg("background worker \"%s\": parallel workers may not be configured for restart",
631  worker->bgw_name)));
632  return false;
633  }
634 
635  /*
636  * If bgw_type is not filled in, use bgw_name.
637  */
638  if (strcmp(worker->bgw_type, "") == 0)
639  strcpy(worker->bgw_type, worker->bgw_name);
640 
641  return true;
642 }
int bgw_restart_time
Definition: bgworker.h:94
int errcode(int sqlerrcode)
Definition: elog.c:575
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
#define USECS_PER_DAY
Definition: timestamp.h:91
#define ereport(elevel, rest)
Definition: elog.h:122
static int elevel
Definition: vacuumlazy.c:136
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:93
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ StartBackgroundWorker()

void StartBackgroundWorker ( void  )

Definition at line 708 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(), dsm_detach_all(), elog, EmitErrorReport(), error_context_stack, FATAL, FloatExceptionHandler(), HOLD_INTERRUPTS, init_ps_display(), InitializeTimeouts(), InitProcess(), InitProcessing, IsBackgroundWorker, LookupBackgroundWorkerFunction(), MyBgworkerEntry, PG_exception_stack, pg_usleep(), PGSharedMemoryDetach(), PostAuthDelay, pqsignal(), proc_exit(), procsignal_sigusr1_handler(), SetProcessingMode, SIG_DFL, SIG_IGN, SIGCHLD, SIGHUP, SIGPIPE, SIGQUIT, SIGUSR1, SIGUSR2, StatementCancelHandler(), and BackgroundWorkerSlot::worker.

Referenced by BackendRun(), and do_start_bgworker().

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

◆ TerminateBackgroundWorker()

void TerminateBackgroundWorker ( BackgroundWorkerHandle handle)

Definition at line 1177 of file bgworker.c.

References Assert, BackgroundWorkerSlot::generation, BackgroundWorkerHandle::generation, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_worker_processes, PMSIGNAL_BACKGROUND_WORKER_CHANGE, SendPostmasterSignal(), BackgroundWorkerArray::slot, BackgroundWorkerHandle::slot, and BackgroundWorkerSlot::terminate.

Referenced by cleanup_background_workers(), and DestroyParallelContext().

1178 {
1179  BackgroundWorkerSlot *slot;
1180  bool signal_postmaster = false;
1181 
1182  Assert(handle->slot < max_worker_processes);
1183  slot = &BackgroundWorkerData->slot[handle->slot];
1184 
1185  /* Set terminate flag in shared memory, unless slot has been reused. */
1186  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
1187  if (handle->generation == slot->generation)
1188  {
1189  slot->terminate = true;
1190  signal_postmaster = true;
1191  }
1192  LWLockRelease(BackgroundWorkerLock);
1193 
1194  /* Make sure the postmaster notices the change to shared memory. */
1195  if (signal_postmaster)
1197 }
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
#define Assert(condition)
Definition: c.h:670
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
int max_worker_processes
Definition: globals.c:124
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:113
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:102

◆ WaitForBackgroundWorkerShutdown()

BgwHandleStatus WaitForBackgroundWorkerShutdown ( BackgroundWorkerHandle handle)

Definition at line 1138 of file bgworker.c.

References BGWH_POSTMASTER_DIED, BGWH_STOPPED, CHECK_FOR_INTERRUPTS, GetBackgroundWorkerPid(), MyLatch, BackgroundWorkerSlot::pid, ResetLatch(), status(), WAIT_EVENT_BGWORKER_SHUTDOWN, WaitLatch(), WL_LATCH_SET, and WL_POSTMASTER_DEATH.

Referenced by apw_start_database_worker(), and WaitForParallelWorkersToExit().

1139 {
1141  int rc;
1142 
1143  for (;;)
1144  {
1145  pid_t pid;
1146 
1148 
1149  status = GetBackgroundWorkerPid(handle, &pid);
1150  if (status == BGWH_STOPPED)
1151  break;
1152 
1153  rc = WaitLatch(MyLatch,
1156 
1157  if (rc & WL_POSTMASTER_DEATH)
1158  {
1159  status = BGWH_POSTMASTER_DIED;
1160  break;
1161  }
1162 
1164  }
1165 
1166  return status;
1167 }
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:336
BgwHandleStatus
Definition: bgworker.h:102
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
struct Latch * MyLatch
Definition: globals.c:52
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define WL_LATCH_SET
Definition: latch.h:124
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1047

◆ WaitForBackgroundWorkerStartup()

BgwHandleStatus WaitForBackgroundWorkerStartup ( BackgroundWorkerHandle handle,
pid_t *  pidp 
)

Definition at line 1096 of file bgworker.c.

References BGWH_NOT_YET_STARTED, BGWH_POSTMASTER_DIED, BGWH_STARTED, CHECK_FOR_INTERRUPTS, GetBackgroundWorkerPid(), MyLatch, BackgroundWorkerSlot::pid, ResetLatch(), status(), WAIT_EVENT_BGWORKER_STARTUP, WaitLatch(), WL_LATCH_SET, and WL_POSTMASTER_DEATH.

Referenced by apw_start_master_worker(), and worker_spi_launch().

1097 {
1099  int rc;
1100 
1101  for (;;)
1102  {
1103  pid_t pid;
1104 
1106 
1107  status = GetBackgroundWorkerPid(handle, &pid);
1108  if (status == BGWH_STARTED)
1109  *pidp = pid;
1110  if (status != BGWH_NOT_YET_STARTED)
1111  break;
1112 
1113  rc = WaitLatch(MyLatch,
1116 
1117  if (rc & WL_POSTMASTER_DEATH)
1118  {
1119  status = BGWH_POSTMASTER_DIED;
1120  break;
1121  }
1122 
1124  }
1125 
1126  return status;
1127 }
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:336
BgwHandleStatus
Definition: bgworker.h:102
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
struct Latch * MyLatch
Definition: globals.c:52
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define WL_LATCH_SET
Definition: latch.h:124
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1047

Variable Documentation

◆ BackgroundWorkerData

BackgroundWorkerArray* BackgroundWorkerData
static

Definition at line 111 of file bgworker.c.

◆ BackgroundWorkerList

slist_head BackgroundWorkerList = SLIST_STATIC_INIT(BackgroundWorkerList)

◆ fn_addr

Definition at line 120 of file bgworker.c.

Referenced by LookupBackgroundWorkerFunction().

◆ fn_name

const char* fn_name

Definition at line 119 of file bgworker.c.

Referenced by LookupBackgroundWorkerFunction().

◆ InternalBGWorkers

const { ... } InternalBGWorkers[]
Initial value:
=
{
{
"ParallelWorkerMain", ParallelWorkerMain
},
{
"ApplyLauncherMain", ApplyLauncherMain
},
{
"ApplyWorkerMain", ApplyWorkerMain
}
}
void ApplyLauncherMain(Datum main_arg)
Definition: launcher.c:883
void ParallelWorkerMain(Datum main_arg)
Definition: parallel.c:956
void ApplyWorkerMain(Datum main_arg)
Definition: worker.c:1492

Referenced by LookupBackgroundWorkerFunction().