PostgreSQL Source Code  git master
bgworker.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  BackgroundWorker
 

Macros

#define BGWORKER_SHMEM_ACCESS   0x0001
 
#define BGWORKER_BACKEND_DATABASE_CONNECTION   0x0002
 
#define BGWORKER_CLASS_PARALLEL   0x0010
 
#define BGW_DEFAULT_RESTART_INTERVAL   60
 
#define BGW_NEVER_RESTART   -1
 
#define BGW_MAXLEN   96
 
#define BGW_EXTRALEN   128
 
#define BGWORKER_BYPASS_ALLOWCONN   1
 

Typedefs

typedef void(* bgworker_main_type) (Datum main_arg)
 
typedef struct BackgroundWorker BackgroundWorker
 
typedef enum BgwHandleStatus BgwHandleStatus
 
typedef struct BackgroundWorkerHandle BackgroundWorkerHandle
 

Enumerations

enum  BgWorkerStartTime { BgWorkerStart_PostmasterStart, BgWorkerStart_ConsistentState, BgWorkerStart_RecoveryFinished }
 
enum  BgwHandleStatus { BGWH_STARTED, BGWH_NOT_YET_STARTED, BGWH_STOPPED, BGWH_POSTMASTER_DIED }
 

Functions

void RegisterBackgroundWorker (BackgroundWorker *worker)
 
bool RegisterDynamicBackgroundWorker (BackgroundWorker *worker, BackgroundWorkerHandle **handle)
 
BgwHandleStatus GetBackgroundWorkerPid (BackgroundWorkerHandle *handle, pid_t *pidp)
 
BgwHandleStatus WaitForBackgroundWorkerStartup (BackgroundWorkerHandle *handle, pid_t *pid)
 
BgwHandleStatus WaitForBackgroundWorkerShutdown (BackgroundWorkerHandle *)
 
const char * GetBackgroundWorkerTypeByPid (pid_t pid)
 
void TerminateBackgroundWorker (BackgroundWorkerHandle *handle)
 
void BackgroundWorkerInitializeConnection (const char *dbname, const char *username, uint32 flags)
 
void BackgroundWorkerInitializeConnectionByOid (Oid dboid, Oid useroid, uint32 flags)
 
void BackgroundWorkerBlockSignals (void)
 
void BackgroundWorkerUnblockSignals (void)
 

Variables

PGDLLIMPORT BackgroundWorkerMyBgworkerEntry
 

Macro Definition Documentation

◆ BGW_DEFAULT_RESTART_INTERVAL

#define BGW_DEFAULT_RESTART_INTERVAL   60

Definition at line 83 of file bgworker.h.

◆ BGW_EXTRALEN

#define BGW_EXTRALEN   128

Definition at line 86 of file bgworker.h.

Referenced by BackgroundWorkerStateChange().

◆ BGW_MAXLEN

◆ BGW_NEVER_RESTART

◆ BGWORKER_BACKEND_DATABASE_CONNECTION

◆ BGWORKER_BYPASS_ALLOWCONN

#define BGWORKER_BYPASS_ALLOWCONN   1

◆ BGWORKER_CLASS_PARALLEL

◆ BGWORKER_SHMEM_ACCESS

Typedef Documentation

◆ BackgroundWorker

◆ BackgroundWorkerHandle

◆ BgwHandleStatus

◆ bgworker_main_type

typedef void(* bgworker_main_type) (Datum main_arg)

Definition at line 71 of file bgworker.h.

Enumeration Type Documentation

◆ BgwHandleStatus

Enumerator
BGWH_STARTED 
BGWH_NOT_YET_STARTED 
BGWH_STOPPED 
BGWH_POSTMASTER_DIED 

Definition at line 102 of file bgworker.h.

103 {
104  BGWH_STARTED, /* worker is running */
105  BGWH_NOT_YET_STARTED, /* worker hasn't been started yet */
106  BGWH_STOPPED, /* worker has exited */
107  BGWH_POSTMASTER_DIED /* postmaster died; worker status unclear */
BgwHandleStatus
Definition: bgworker.h:102

◆ BgWorkerStartTime

Enumerator
BgWorkerStart_PostmasterStart 
BgWorkerStart_ConsistentState 
BgWorkerStart_RecoveryFinished 

Definition at line 76 of file bgworker.h.

Function Documentation

◆ BackgroundWorkerBlockSignals()

void BackgroundWorkerBlockSignals ( void  )

Definition at line 5583 of file postmaster.c.

References BlockSig, and PG_SETMASK.

5584 {
5585  PG_SETMASK(&BlockSig);
5586 }
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
sigset_t BlockSig
Definition: pqsignal.c:22

◆ BackgroundWorkerInitializeConnection()

void BackgroundWorkerInitializeConnection ( const char *  dbname,
const char *  username,
uint32  flags 
)

Definition at line 5537 of file postmaster.c.

References BackgroundWorker::bgw_flags, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_BYPASS_ALLOWCONN, ereport, errcode(), errmsg(), ERROR, FATAL, InitPostgres(), InvalidOid, IsInitProcessingMode, MyBgworkerEntry, NormalProcessing, and SetProcessingMode.

Referenced by ApplyLauncherMain(), and initialize_worker_spi().

5538 {
5540 
5541  /* XXX is this the right errcode? */
5543  ereport(FATAL,
5544  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5545  errmsg("database connection requirement not indicated during registration")));
5546 
5548 
5549  /* it had better not gotten out of "init" mode yet */
5550  if (!IsInitProcessingMode())
5551  ereport(ERROR,
5552  (errmsg("invalid processing mode in background worker")));
5554 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define SetProcessingMode(mode)
Definition: miscadmin.h:378
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:191
#define ERROR
Definition: elog.h:43
#define BGWORKER_BYPASS_ALLOWCONN
Definition: bgworker.h:149
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
static char * username
Definition: initdb.c:132
#define InvalidOid
Definition: postgres_ext.h:36
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, char *out_dbname, bool override_allow_connections)
Definition: postinit.c:565
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
char * dbname
Definition: streamutil.c:51
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define IsInitProcessingMode()
Definition: miscadmin.h:373

◆ BackgroundWorkerInitializeConnectionByOid()

void BackgroundWorkerInitializeConnectionByOid ( Oid  dboid,
Oid  useroid,
uint32  flags 
)

Definition at line 5560 of file postmaster.c.

References BackgroundWorker::bgw_flags, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_BYPASS_ALLOWCONN, ereport, errcode(), errmsg(), ERROR, FATAL, InitPostgres(), IsInitProcessingMode, MyBgworkerEntry, NormalProcessing, and SetProcessingMode.

Referenced by ApplyWorkerMain(), autoprewarm_database_main(), and ParallelWorkerMain().

5561 {
5563 
5564  /* XXX is this the right errcode? */
5566  ereport(FATAL,
5567  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5568  errmsg("database connection requirement not indicated during registration")));
5569 
5570  InitPostgres(NULL, dboid, NULL, useroid, NULL, (flags & BGWORKER_BYPASS_ALLOWCONN) != 0);
5571 
5572  /* it had better not gotten out of "init" mode yet */
5573  if (!IsInitProcessingMode())
5574  ereport(ERROR,
5575  (errmsg("invalid processing mode in background worker")));
5577 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define SetProcessingMode(mode)
Definition: miscadmin.h:378
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:191
#define ERROR
Definition: elog.h:43
#define BGWORKER_BYPASS_ALLOWCONN
Definition: bgworker.h:149
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:122
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, char *out_dbname, bool override_allow_connections)
Definition: postinit.c:565
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define IsInitProcessingMode()
Definition: miscadmin.h:373

◆ BackgroundWorkerUnblockSignals()

void BackgroundWorkerUnblockSignals ( void  )

Definition at line 5589 of file postmaster.c.

References Assert, av, lengthof, MAXPGPATH, PG_SETMASK, snprintf(), and UnBlockSig.

Referenced by ApplyLauncherMain(), ApplyWorkerMain(), autoprewarm_database_main(), autoprewarm_main(), initialize_worker_spi(), ParallelWorkerMain(), and test_shm_mq_main().

5590 {
5592 }
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
sigset_t UnBlockSig
Definition: pqsignal.c:22

◆ GetBackgroundWorkerPid()

BgwHandleStatus GetBackgroundWorkerPid ( BackgroundWorkerHandle handle,
pid_t *  pidp 
)

Definition at line 1051 of file bgworker.c.

References Assert, BGWH_NOT_YET_STARTED, BGWH_STARTED, BGWH_STOPPED, BackgroundWorkerSlot::generation, BackgroundWorkerHandle::generation, BackgroundWorkerSlot::in_use, 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(), WaitForParallelWorkersToAttach(), WaitForParallelWorkersToFinish(), and WaitForReplicationWorkerAttach().

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

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

◆ 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:1518
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:110
#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:134
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:135
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define ERROR
Definition: elog.h:43
static bool success
uint32 parallel_register_count
Definition: bgworker.c:100
bool IsUnderPostmaster
Definition: globals.c:110
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition: bgworker.c:583
#define Assert(condition)
Definition: c.h:699
BackgroundWorker worker
Definition: bgworker.c:83
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
void * palloc(Size size)
Definition: mcxt.c:924
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

◆ TerminateBackgroundWorker()

void TerminateBackgroundWorker ( BackgroundWorkerHandle handle)

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

1185 {
1186  BackgroundWorkerSlot *slot;
1187  bool signal_postmaster = false;
1188 
1189  Assert(handle->slot < max_worker_processes);
1190  slot = &BackgroundWorkerData->slot[handle->slot];
1191 
1192  /* Set terminate flag in shared memory, unless slot has been reused. */
1193  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
1194  if (handle->generation == slot->generation)
1195  {
1196  slot->terminate = true;
1197  signal_postmaster = true;
1198  }
1199  LWLockRelease(BackgroundWorkerLock);
1200 
1201  /* Make sure the postmaster notices the change to shared memory. */
1202  if (signal_postmaster)
1204 }
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:111
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define Assert(condition)
Definition: c.h:699
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
int max_worker_processes
Definition: globals.c:134
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:113
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:102

◆ WaitForBackgroundWorkerShutdown()

BgwHandleStatus WaitForBackgroundWorkerShutdown ( BackgroundWorkerHandle )

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

1146 {
1148  int rc;
1149 
1150  for (;;)
1151  {
1152  pid_t pid;
1153 
1155 
1156  status = GetBackgroundWorkerPid(handle, &pid);
1157  if (status == BGWH_STOPPED)
1158  break;
1159 
1160  rc = WaitLatch(MyLatch,
1163 
1164  if (rc & WL_POSTMASTER_DEATH)
1165  {
1166  status = BGWH_POSTMASTER_DIED;
1167  break;
1168  }
1169 
1171  }
1172 
1173  return status;
1174 }
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:55
#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:1051

◆ WaitForBackgroundWorkerStartup()

BgwHandleStatus WaitForBackgroundWorkerStartup ( BackgroundWorkerHandle handle,
pid_t *  pid 
)

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

1104 {
1106  int rc;
1107 
1108  for (;;)
1109  {
1110  pid_t pid;
1111 
1113 
1114  status = GetBackgroundWorkerPid(handle, &pid);
1115  if (status == BGWH_STARTED)
1116  *pidp = pid;
1117  if (status != BGWH_NOT_YET_STARTED)
1118  break;
1119 
1120  rc = WaitLatch(MyLatch,
1123 
1124  if (rc & WL_POSTMASTER_DEATH)
1125  {
1126  status = BGWH_POSTMASTER_DIED;
1127  break;
1128  }
1129 
1131  }
1132 
1133  return status;
1134 }
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:55
#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:1051

Variable Documentation

◆ MyBgworkerEntry