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 *pidp)
 
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 84 of file bgworker.h.

◆ BGW_EXTRALEN

#define BGW_EXTRALEN   128

Definition at line 87 of file bgworker.h.

◆ BGW_MAXLEN

#define BGW_MAXLEN   96

Definition at line 86 of file bgworker.h.

◆ BGW_NEVER_RESTART

#define BGW_NEVER_RESTART   -1

Definition at line 85 of file bgworker.h.

◆ BGWORKER_BACKEND_DATABASE_CONNECTION

#define BGWORKER_BACKEND_DATABASE_CONNECTION   0x0002

Definition at line 60 of file bgworker.h.

◆ BGWORKER_BYPASS_ALLOWCONN

#define BGWORKER_BYPASS_ALLOWCONN   1

Definition at line 155 of file bgworker.h.

◆ BGWORKER_CLASS_PARALLEL

#define BGWORKER_CLASS_PARALLEL   0x0010

Definition at line 68 of file bgworker.h.

◆ BGWORKER_SHMEM_ACCESS

#define BGWORKER_SHMEM_ACCESS   0x0001

Definition at line 53 of file bgworker.h.

Typedef Documentation

◆ BackgroundWorker

◆ BackgroundWorkerHandle

Definition at line 72 of file bgworker.h.

◆ BgwHandleStatus

◆ bgworker_main_type

typedef void(* bgworker_main_type) (Datum main_arg)

Definition at line 72 of file bgworker.h.

Enumeration Type Documentation

◆ BgwHandleStatus

Enumerator
BGWH_STARTED 
BGWH_NOT_YET_STARTED 
BGWH_STOPPED 
BGWH_POSTMASTER_DIED 

Definition at line 103 of file bgworker.h.

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

◆ BgWorkerStartTime

Enumerator
BgWorkerStart_PostmasterStart 
BgWorkerStart_ConsistentState 
BgWorkerStart_RecoveryFinished 

Definition at line 77 of file bgworker.h.

78 {
BgWorkerStartTime
Definition: bgworker.h:78
@ BgWorkerStart_RecoveryFinished
Definition: bgworker.h:81
@ BgWorkerStart_ConsistentState
Definition: bgworker.h:80
@ BgWorkerStart_PostmasterStart
Definition: bgworker.h:79

Function Documentation

◆ BackgroundWorkerBlockSignals()

void BackgroundWorkerBlockSignals ( void  )

Definition at line 5654 of file postmaster.c.

5655 {
5656  sigprocmask(SIG_SETMASK, &BlockSig, NULL);
5657 }
sigset_t BlockSig
Definition: pqsignal.c:23

References BlockSig.

◆ BackgroundWorkerInitializeConnection()

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

Definition at line 5600 of file postmaster.c.

5601 {
5603 
5604  /* XXX is this the right errcode? */
5606  ereport(FATAL,
5607  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5608  errmsg("database connection requirement not indicated during registration")));
5609 
5610  InitPostgres(dbname, InvalidOid, /* database to connect to */
5611  username, InvalidOid, /* role to connect as */
5612  false, /* never honor session_preload_libraries */
5613  (flags & BGWORKER_BYPASS_ALLOWCONN) != 0, /* ignore datallowconn? */
5614  NULL); /* no out_dbname */
5615 
5616  /* it had better not gotten out of "init" mode yet */
5617  if (!IsInitProcessingMode())
5618  ereport(ERROR,
5619  (errmsg("invalid processing mode in background worker")));
5621 }
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:60
#define BGWORKER_BYPASS_ALLOWCONN
Definition: bgworker.h:155
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define FATAL
Definition: elog.h:41
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
@ NormalProcessing
Definition: miscadmin.h:409
#define IsInitProcessingMode()
Definition: miscadmin.h:415
#define SetProcessingMode(mode)
Definition: miscadmin.h:420
const char * username
Definition: pgbench.c:306
#define InvalidOid
Definition: postgres_ext.h:36
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, bool load_session_libraries, bool override_allow_connections, char *out_dbname)
Definition: postinit.c:722
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:193
char * dbname
Definition: streamutil.c:51

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

Referenced by ApplyLauncherMain(), and worker_spi_main().

◆ BackgroundWorkerInitializeConnectionByOid()

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

Definition at line 5627 of file postmaster.c.

5628 {
5630 
5631  /* XXX is this the right errcode? */
5633  ereport(FATAL,
5634  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5635  errmsg("database connection requirement not indicated during registration")));
5636 
5637  InitPostgres(NULL, dboid, /* database to connect to */
5638  NULL, useroid, /* role to connect as */
5639  false, /* never honor session_preload_libraries */
5640  (flags & BGWORKER_BYPASS_ALLOWCONN) != 0, /* ignore datallowconn? */
5641  NULL); /* no out_dbname */
5642 
5643  /* it had better not gotten out of "init" mode yet */
5644  if (!IsInitProcessingMode())
5645  ereport(ERROR,
5646  (errmsg("invalid processing mode in background worker")));
5648 }

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

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

◆ BackgroundWorkerUnblockSignals()

void BackgroundWorkerUnblockSignals ( void  )

Definition at line 5660 of file postmaster.c.

5661 {
5662  sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
5663 }
sigset_t UnBlockSig
Definition: pqsignal.c:22

References UnBlockSig.

Referenced by ApplyLauncherMain(), ApplyWorkerMain(), autoprewarm_database_main(), autoprewarm_main(), ParallelApplyWorkerMain(), ParallelWorkerMain(), StartBackgroundWorker(), test_shm_mq_main(), and worker_spi_main().

◆ GetBackgroundWorkerPid()

BgwHandleStatus GetBackgroundWorkerPid ( BackgroundWorkerHandle handle,
pid_t *  pidp 
)

Definition at line 1071 of file bgworker.c.

1072 {
1073  BackgroundWorkerSlot *slot;
1074  pid_t pid;
1075 
1076  Assert(handle->slot < max_worker_processes);
1077  slot = &BackgroundWorkerData->slot[handle->slot];
1078 
1079  /*
1080  * We could probably arrange to synchronize access to data using memory
1081  * barriers only, but for now, let's just keep it simple and grab the
1082  * lock. It seems unlikely that there will be enough traffic here to
1083  * result in meaningful contention.
1084  */
1085  LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1086 
1087  /*
1088  * The generation number can't be concurrently changed while we hold the
1089  * lock. The pid, which is updated by the postmaster, can change at any
1090  * time, but we assume such changes are atomic. So the value we read
1091  * won't be garbage, but it might be out of date by the time the caller
1092  * examines it (but that's unavoidable anyway).
1093  *
1094  * The in_use flag could be in the process of changing from true to false,
1095  * but if it is already false then it can't change further.
1096  */
1097  if (handle->generation != slot->generation || !slot->in_use)
1098  pid = 0;
1099  else
1100  pid = slot->pid;
1101 
1102  /* All done. */
1103  LWLockRelease(BackgroundWorkerLock);
1104 
1105  if (pid == 0)
1106  return BGWH_STOPPED;
1107  else if (pid == InvalidPid)
1108  return BGWH_NOT_YET_STARTED;
1109  *pidp = pid;
1110  return BGWH_STARTED;
1111 }
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
int max_worker_processes
Definition: globals.c:138
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
@ LW_SHARED
Definition: lwlock.h:117
#define InvalidPid
Definition: miscadmin.h:32
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:101

References Assert(), BackgroundWorkerData, 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().

◆ GetBackgroundWorkerTypeByPid()

const char* GetBackgroundWorkerTypeByPid ( pid_t  pid)

Definition at line 1285 of file bgworker.c.

1286 {
1287  int slotno;
1288  bool found = false;
1289  static char result[BGW_MAXLEN];
1290 
1291  LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1292 
1293  for (slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++)
1294  {
1296 
1297  if (slot->pid > 0 && slot->pid == pid)
1298  {
1299  strcpy(result, slot->worker.bgw_type);
1300  found = true;
1301  break;
1302  }
1303  }
1304 
1305  LWLockRelease(BackgroundWorkerLock);
1306 
1307  if (!found)
1308  return NULL;
1309 
1310  return result;
1311 }
#define BGW_MAXLEN
Definition: bgworker.h:86
BackgroundWorker worker
Definition: bgworker.c:82
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:92

References BackgroundWorkerData, 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().

◆ RegisterBackgroundWorker()

void RegisterBackgroundWorker ( BackgroundWorker worker)

Definition at line 875 of file bgworker.c.

876 {
877  RegisteredBgWorker *rw;
878  static int numworkers = 0;
879 
880  if (!IsUnderPostmaster)
881  ereport(DEBUG1,
882  (errmsg_internal("registering background worker \"%s\"", worker->bgw_name)));
883 
885  strcmp(worker->bgw_library_name, "postgres") != 0)
886  {
887  if (!IsUnderPostmaster)
888  ereport(LOG,
889  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
890  errmsg("background worker \"%s\": must be registered in shared_preload_libraries",
891  worker->bgw_name)));
892  return;
893  }
894 
895  if (!SanityCheckBackgroundWorker(worker, LOG))
896  return;
897 
898  if (worker->bgw_notify_pid != 0)
899  {
900  ereport(LOG,
901  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
902  errmsg("background worker \"%s\": only dynamic background workers can request notification",
903  worker->bgw_name)));
904  return;
905  }
906 
907  /*
908  * Enforce maximum number of workers. Note this is overly restrictive: we
909  * could allow more non-shmem-connected workers, because these don't count
910  * towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
911  * important to relax this restriction.
912  */
913  if (++numworkers > max_worker_processes)
914  {
915  ereport(LOG,
916  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
917  errmsg("too many background workers"),
918  errdetail_plural("Up to %d background worker can be registered with the current settings.",
919  "Up to %d background workers can be registered with the current settings.",
922  errhint("Consider increasing the configuration parameter \"max_worker_processes\".")));
923  return;
924  }
925 
926  /*
927  * Copy the registration data into the registered workers list.
928  */
929  rw = malloc(sizeof(RegisteredBgWorker));
930  if (rw == NULL)
931  {
932  ereport(LOG,
933  (errcode(ERRCODE_OUT_OF_MEMORY),
934  errmsg("out of memory")));
935  return;
936  }
937 
938  rw->rw_worker = *worker;
939  rw->rw_backend = NULL;
940  rw->rw_pid = 0;
941  rw->rw_child_slot = 0;
942  rw->rw_crashed_at = 0;
943  rw->rw_terminate = false;
944 
946 }
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition: bgworker.c:655
slist_head BackgroundWorkerList
Definition: bgworker.c:42
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1294
int errhint(const char *fmt,...)
Definition: elog.c:1316
#define LOG
Definition: elog.h:31
#define DEBUG1
Definition: elog.h:30
bool IsUnderPostmaster
Definition: globals.c:113
#define malloc(a)
Definition: header.h:50
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:1006
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1782
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:91
pid_t bgw_notify_pid
Definition: bgworker.h:100
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:96
struct bkend * rw_backend
BackgroundWorker rw_worker

References BackgroundWorkerList, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_name, BackgroundWorker::bgw_notify_pid, DEBUG1, ereport, errcode(), errdetail_plural(), errhint(), errmsg(), errmsg_internal(), 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(), and slist_push_head().

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

◆ RegisterDynamicBackgroundWorker()

bool RegisterDynamicBackgroundWorker ( BackgroundWorker worker,
BackgroundWorkerHandle **  handle 
)

Definition at line 959 of file bgworker.c.

961 {
962  int slotno;
963  bool success = false;
964  bool parallel;
965  uint64 generation = 0;
966 
967  /*
968  * We can't register dynamic background workers from the postmaster. If
969  * this is a standalone backend, we're the only process and can't start
970  * any more. In a multi-process environment, it might be theoretically
971  * possible, but we don't currently support it due to locking
972  * considerations; see comments on the BackgroundWorkerSlot data
973  * structure.
974  */
975  if (!IsUnderPostmaster)
976  return false;
977 
978  if (!SanityCheckBackgroundWorker(worker, ERROR))
979  return false;
980 
981  parallel = (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0;
982 
983  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
984 
985  /*
986  * If this is a parallel worker, check whether there are already too many
987  * parallel workers; if so, don't register another one. Our view of
988  * parallel_terminate_count may be slightly stale, but that doesn't really
989  * matter: we would have gotten the same result if we'd arrived here
990  * slightly earlier anyway. There's no help for it, either, since the
991  * postmaster must not take locks; a memory barrier wouldn't guarantee
992  * anything useful.
993  */
997  {
1001  LWLockRelease(BackgroundWorkerLock);
1002  return false;
1003  }
1004 
1005  /*
1006  * Look for an unused slot. If we find one, grab it.
1007  */
1008  for (slotno = 0; slotno < BackgroundWorkerData->total_slots; ++slotno)
1009  {
1011 
1012  if (!slot->in_use)
1013  {
1014  memcpy(&slot->worker, worker, sizeof(BackgroundWorker));
1015  slot->pid = InvalidPid; /* indicates not started yet */
1016  slot->generation++;
1017  slot->terminate = false;
1018  generation = slot->generation;
1019  if (parallel)
1021 
1022  /*
1023  * Make sure postmaster doesn't see the slot as in use before it
1024  * sees the new contents.
1025  */
1026  pg_write_barrier();
1027 
1028  slot->in_use = true;
1029  success = true;
1030  break;
1031  }
1032  }
1033 
1034  LWLockRelease(BackgroundWorkerLock);
1035 
1036  /* If we found a slot, tell the postmaster to notice the change. */
1037  if (success)
1039 
1040  /*
1041  * If we found a slot and the user has provided a handle, initialize it.
1042  */
1043  if (success && handle)
1044  {
1045  *handle = palloc(sizeof(BackgroundWorkerHandle));
1046  (*handle)->slot = slotno;
1047  (*handle)->generation = generation;
1048  }
1049 
1050  return success;
1051 }
#define pg_write_barrier()
Definition: atomics.h:154
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:68
#define MAX_PARALLEL_WORKER_LIMIT
int max_parallel_workers
Definition: globals.c:139
static bool success
Definition: initdb.c:187
@ LW_EXCLUSIVE
Definition: lwlock.h:116
void * palloc(Size size)
Definition: mcxt.c:1226
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:181
@ PMSIGNAL_BACKGROUND_WORKER_CHANGE
Definition: pmsignal.h:40
uint32 parallel_terminate_count
Definition: bgworker.c:100
uint32 parallel_register_count
Definition: bgworker.c:99

References Assert(), BackgroundWorkerData, 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_leader_worker(), LaunchParallelWorkers(), logicalrep_worker_launch(), setup_background_workers(), and worker_spi_launch().

◆ TerminateBackgroundWorker()

void TerminateBackgroundWorker ( BackgroundWorkerHandle handle)

Definition at line 1210 of file bgworker.c.

1211 {
1212  BackgroundWorkerSlot *slot;
1213  bool signal_postmaster = false;
1214 
1215  Assert(handle->slot < max_worker_processes);
1216  slot = &BackgroundWorkerData->slot[handle->slot];
1217 
1218  /* Set terminate flag in shared memory, unless slot has been reused. */
1219  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
1220  if (handle->generation == slot->generation)
1221  {
1222  slot->terminate = true;
1223  signal_postmaster = true;
1224  }
1225  LWLockRelease(BackgroundWorkerLock);
1226 
1227  /* Make sure the postmaster notices the change to shared memory. */
1228  if (signal_postmaster)
1230 }

References Assert(), BackgroundWorkerData, 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().

◆ WaitForBackgroundWorkerShutdown()

BgwHandleStatus WaitForBackgroundWorkerShutdown ( BackgroundWorkerHandle handle)

Definition at line 1171 of file bgworker.c.

1172 {
1173  BgwHandleStatus status;
1174  int rc;
1175 
1176  for (;;)
1177  {
1178  pid_t pid;
1179 
1181 
1182  status = GetBackgroundWorkerPid(handle, &pid);
1183  if (status == BGWH_STOPPED)
1184  break;
1185 
1186  rc = WaitLatch(MyLatch,
1189 
1190  if (rc & WL_POSTMASTER_DEATH)
1191  {
1192  status = BGWH_POSTMASTER_DIED;
1193  break;
1194  }
1195 
1197  }
1198 
1199  return status;
1200 }
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1071
struct Latch * MyLatch
Definition: globals.c:58
void ResetLatch(Latch *latch)
Definition: latch.c:699
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:492
#define WL_LATCH_SET
Definition: latch.h:125
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
@ WAIT_EVENT_BGWORKER_SHUTDOWN
Definition: wait_event.h:87

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

Referenced by apw_start_database_worker(), and WaitForParallelWorkersToExit().

◆ WaitForBackgroundWorkerStartup()

BgwHandleStatus WaitForBackgroundWorkerStartup ( BackgroundWorkerHandle handle,
pid_t *  pidp 
)

Definition at line 1126 of file bgworker.c.

1127 {
1128  BgwHandleStatus status;
1129  int rc;
1130 
1131  for (;;)
1132  {
1133  pid_t pid;
1134 
1136 
1137  status = GetBackgroundWorkerPid(handle, &pid);
1138  if (status == BGWH_STARTED)
1139  *pidp = pid;
1140  if (status != BGWH_NOT_YET_STARTED)
1141  break;
1142 
1143  rc = WaitLatch(MyLatch,
1146 
1147  if (rc & WL_POSTMASTER_DEATH)
1148  {
1149  status = BGWH_POSTMASTER_DIED;
1150  break;
1151  }
1152 
1154  }
1155 
1156  return status;
1157 }
@ WAIT_EVENT_BGWORKER_STARTUP
Definition: wait_event.h:88

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

Referenced by apw_start_leader_worker(), and worker_spi_launch().

Variable Documentation

◆ MyBgworkerEntry