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

Definition at line 111 of file bgworker.h.

◆ 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 5720 of file postmaster.c.

References BlockSig, and PG_SETMASK.

5721 {
5722  PG_SETMASK(&BlockSig);
5723 }
#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 5674 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().

5675 {
5677 
5678  /* XXX is this the right errcode? */
5680  ereport(FATAL,
5681  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5682  errmsg("database connection requirement not indicated during registration")));
5683 
5685 
5686  /* it had better not gotten out of "init" mode yet */
5687  if (!IsInitProcessingMode())
5688  ereport(ERROR,
5689  (errmsg("invalid processing mode in background worker")));
5691 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define SetProcessingMode(mode)
Definition: miscadmin.h:412
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:195
#define ERROR
Definition: elog.h:46
#define BGWORKER_BYPASS_ALLOWCONN
Definition: bgworker.h:154
#define FATAL
Definition: elog.h:49
const char * username
Definition: pgbench.c:280
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, char *out_dbname, bool override_allow_connections)
Definition: postinit.c:579
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
char * dbname
Definition: streamutil.c:51
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define IsInitProcessingMode()
Definition: miscadmin.h:407

◆ BackgroundWorkerInitializeConnectionByOid()

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

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

5698 {
5700 
5701  /* XXX is this the right errcode? */
5703  ereport(FATAL,
5704  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5705  errmsg("database connection requirement not indicated during registration")));
5706 
5707  InitPostgres(NULL, dboid, NULL, useroid, NULL, (flags & BGWORKER_BYPASS_ALLOWCONN) != 0);
5708 
5709  /* it had better not gotten out of "init" mode yet */
5710  if (!IsInitProcessingMode())
5711  ereport(ERROR,
5712  (errmsg("invalid processing mode in background worker")));
5714 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define SetProcessingMode(mode)
Definition: miscadmin.h:412
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:195
#define ERROR
Definition: elog.h:46
#define BGWORKER_BYPASS_ALLOWCONN
Definition: bgworker.h:154
#define FATAL
Definition: elog.h:49
#define ereport(elevel,...)
Definition: elog.h:157
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, char *out_dbname, bool override_allow_connections)
Definition: postinit.c:579
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define IsInitProcessingMode()
Definition: miscadmin.h:407

◆ BackgroundWorkerUnblockSignals()

void BackgroundWorkerUnblockSignals ( void  )

◆ GetBackgroundWorkerPid()

BgwHandleStatus GetBackgroundWorkerPid ( BackgroundWorkerHandle handle,
pid_t *  pidp 
)

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

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

◆ GetBackgroundWorkerTypeByPid()

const char* GetBackgroundWorkerTypeByPid ( pid_t  pid)

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

1300 {
1301  int slotno;
1302  bool found = false;
1303  static char result[BGW_MAXLEN];
1304 
1305  LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1306 
1307  for (slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++)
1308  {
1310 
1311  if (slot->pid > 0 && slot->pid == pid)
1312  {
1313  strcpy(result, slot->worker.bgw_type);
1314  found = true;
1315  break;
1316  }
1317  }
1318 
1319  LWLockRelease(BackgroundWorkerLock);
1320 
1321  if (!found)
1322  return NULL;
1323 
1324  return result;
1325 }
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
BackgroundWorker worker
Definition: bgworker.c:82
#define BGW_MAXLEN
Definition: bgworker.h:85
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:101

◆ RegisterBackgroundWorker()

void RegisterBackgroundWorker ( BackgroundWorker worker)

Definition at line 889 of file bgworker.c.

References 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(), slist_push_head(), and BackgroundWorkerSlot::worker.

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

890 {
891  RegisteredBgWorker *rw;
892  static int numworkers = 0;
893 
894  if (!IsUnderPostmaster)
895  ereport(DEBUG1,
896  (errmsg_internal("registering background worker \"%s\"", worker->bgw_name)));
897 
899  strcmp(worker->bgw_library_name, "postgres") != 0)
900  {
901  if (!IsUnderPostmaster)
902  ereport(LOG,
903  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
904  errmsg("background worker \"%s\": must be registered in shared_preload_libraries",
905  worker->bgw_name)));
906  return;
907  }
908 
909  if (!SanityCheckBackgroundWorker(worker, LOG))
910  return;
911 
912  if (worker->bgw_notify_pid != 0)
913  {
914  ereport(LOG,
915  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
916  errmsg("background worker \"%s\": only dynamic background workers can request notification",
917  worker->bgw_name)));
918  return;
919  }
920 
921  /*
922  * Enforce maximum number of workers. Note this is overly restrictive: we
923  * could allow more non-shmem-connected workers, because these don't count
924  * towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
925  * important to relax this restriction.
926  */
927  if (++numworkers > max_worker_processes)
928  {
929  ereport(LOG,
930  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
931  errmsg("too many background workers"),
932  errdetail_plural("Up to %d background worker can be registered with the current settings.",
933  "Up to %d background workers can be registered with the current settings.",
936  errhint("Consider increasing the configuration parameter \"max_worker_processes\".")));
937  return;
938  }
939 
940  /*
941  * Copy the registration data into the registered workers list.
942  */
943  rw = malloc(sizeof(RegisteredBgWorker));
944  if (rw == NULL)
945  {
946  ereport(LOG,
947  (errcode(ERRCODE_OUT_OF_MEMORY),
948  errmsg("out of memory")));
949  return;
950  }
951 
952  rw->rw_worker = *worker;
953  rw->rw_backend = NULL;
954  rw->rw_pid = 0;
955  rw->rw_child_slot = 0;
956  rw->rw_crashed_at = 0;
957  rw->rw_terminate = false;
958 
960 }
struct bkend * rw_backend
#define DEBUG1
Definition: elog.h:25
int errhint(const char *fmt,...)
Definition: elog.c:1156
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1598
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:593
int errcode(int sqlerrcode)
Definition: elog.c:698
#define LOG
Definition: elog.h:26
#define malloc(a)
Definition: header.h:50
slist_head BackgroundWorkerList
Definition: bgworker.c:42
bool IsUnderPostmaster
Definition: globals.c:112
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition: bgworker.c:652
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
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:1134
int errmsg(const char *fmt,...)
Definition: elog.c:909
pid_t bgw_notify_pid
Definition: bgworker.h:99
int max_worker_processes
Definition: globals.c:137
BackgroundWorker rw_worker
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:95

◆ RegisterDynamicBackgroundWorker()

bool RegisterDynamicBackgroundWorker ( BackgroundWorker worker,
BackgroundWorkerHandle **  handle 
)

Definition at line 973 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_leader_worker(), LaunchParallelWorkers(), logicalrep_worker_launch(), setup_background_workers(), and worker_spi_launch().

975 {
976  int slotno;
977  bool success = false;
978  bool parallel;
979  uint64 generation = 0;
980 
981  /*
982  * We can't register dynamic background workers from the postmaster. If
983  * this is a standalone backend, we're the only process and can't start
984  * any more. In a multi-process environment, it might be theoretically
985  * possible, but we don't currently support it due to locking
986  * considerations; see comments on the BackgroundWorkerSlot data
987  * structure.
988  */
989  if (!IsUnderPostmaster)
990  return false;
991 
992  if (!SanityCheckBackgroundWorker(worker, ERROR))
993  return false;
994 
995  parallel = (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0;
996 
997  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
998 
999  /*
1000  * If this is a parallel worker, check whether there are already too many
1001  * parallel workers; if so, don't register another one. Our view of
1002  * parallel_terminate_count may be slightly stale, but that doesn't really
1003  * matter: we would have gotten the same result if we'd arrived here
1004  * slightly earlier anyway. There's no help for it, either, since the
1005  * postmaster must not take locks; a memory barrier wouldn't guarantee
1006  * anything useful.
1007  */
1008  if (parallel && (BackgroundWorkerData->parallel_register_count -
1011  {
1015  LWLockRelease(BackgroundWorkerLock);
1016  return false;
1017  }
1018 
1019  /*
1020  * Look for an unused slot. If we find one, grab it.
1021  */
1022  for (slotno = 0; slotno < BackgroundWorkerData->total_slots; ++slotno)
1023  {
1025 
1026  if (!slot->in_use)
1027  {
1028  memcpy(&slot->worker, worker, sizeof(BackgroundWorker));
1029  slot->pid = InvalidPid; /* indicates not started yet */
1030  slot->generation++;
1031  slot->terminate = false;
1032  generation = slot->generation;
1033  if (parallel)
1035 
1036  /*
1037  * Make sure postmaster doesn't see the slot as in use before it
1038  * sees the new contents.
1039  */
1040  pg_write_barrier();
1041 
1042  slot->in_use = true;
1043  success = true;
1044  break;
1045  }
1046  }
1047 
1048  LWLockRelease(BackgroundWorkerLock);
1049 
1050  /* If we found a slot, tell the postmaster to notice the change. */
1051  if (success)
1053 
1054  /*
1055  * If we found a slot and the user has provided a handle, initialize it.
1056  */
1057  if (success && handle)
1058  {
1059  *handle = palloc(sizeof(BackgroundWorkerHandle));
1060  (*handle)->slot = slotno;
1061  (*handle)->generation = generation;
1062  }
1063 
1064  return success;
1065 }
int max_parallel_workers
Definition: globals.c:138
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
#define BGWORKER_CLASS_PARALLEL
Definition: bgworker.h:67
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
#define ERROR
Definition: elog.h:46
uint32 parallel_register_count
Definition: bgworker.c:99
bool IsUnderPostmaster
Definition: globals.c:112
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition: bgworker.c:652
#define Assert(condition)
Definition: c.h:804
BackgroundWorker worker
Definition: bgworker.c:82
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
void * palloc(Size size)
Definition: mcxt.c:1062
uint32 parallel_terminate_count
Definition: bgworker.c:100
#define pg_write_barrier()
Definition: atomics.h:159
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:153
#define MAX_PARALLEL_WORKER_LIMIT
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:101
static bool success
Definition: initdb.c:165
#define InvalidPid
Definition: miscadmin.h:32

◆ TerminateBackgroundWorker()

void TerminateBackgroundWorker ( BackgroundWorkerHandle handle)

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

1225 {
1226  BackgroundWorkerSlot *slot;
1227  bool signal_postmaster = false;
1228 
1229  Assert(handle->slot < max_worker_processes);
1230  slot = &BackgroundWorkerData->slot[handle->slot];
1231 
1232  /* Set terminate flag in shared memory, unless slot has been reused. */
1233  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
1234  if (handle->generation == slot->generation)
1235  {
1236  slot->terminate = true;
1237  signal_postmaster = true;
1238  }
1239  LWLockRelease(BackgroundWorkerLock);
1240 
1241  /* Make sure the postmaster notices the change to shared memory. */
1242  if (signal_postmaster)
1244 }
static BackgroundWorkerArray * BackgroundWorkerData
Definition: bgworker.c:110
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
int max_worker_processes
Definition: globals.c:137
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:153
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition: bgworker.c:101

◆ WaitForBackgroundWorkerShutdown()

BgwHandleStatus WaitForBackgroundWorkerShutdown ( BackgroundWorkerHandle )

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

1186 {
1188  int rc;
1189 
1190  for (;;)
1191  {
1192  pid_t pid;
1193 
1195 
1196  status = GetBackgroundWorkerPid(handle, &pid);
1197  if (status == BGWH_STOPPED)
1198  break;
1199 
1200  rc = WaitLatch(MyLatch,
1203 
1204  if (rc & WL_POSTMASTER_DEATH)
1205  {
1206  status = BGWH_POSTMASTER_DIED;
1207  break;
1208  }
1209 
1211  }
1212 
1213  return status;
1214 }
void ResetLatch(Latch *latch)
Definition: latch.c:660
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:452
BgwHandleStatus
Definition: bgworker.h:102
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
struct Latch * MyLatch
Definition: globals.c:57
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define WL_LATCH_SET
Definition: latch.h:125
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1085

◆ WaitForBackgroundWorkerStartup()

BgwHandleStatus WaitForBackgroundWorkerStartup ( BackgroundWorkerHandle handle,
pid_t *  pid 
)

Definition at line 1140 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_leader_worker(), and worker_spi_launch().

1141 {
1143  int rc;
1144 
1145  for (;;)
1146  {
1147  pid_t pid;
1148 
1150 
1151  status = GetBackgroundWorkerPid(handle, &pid);
1152  if (status == BGWH_STARTED)
1153  *pidp = pid;
1154  if (status != BGWH_NOT_YET_STARTED)
1155  break;
1156 
1157  rc = WaitLatch(MyLatch,
1160 
1161  if (rc & WL_POSTMASTER_DEATH)
1162  {
1163  status = BGWH_POSTMASTER_DIED;
1164  break;
1165  }
1166 
1168  }
1169 
1170  return status;
1171 }
void ResetLatch(Latch *latch)
Definition: latch.c:660
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:452
BgwHandleStatus
Definition: bgworker.h:102
#define WL_POSTMASTER_DEATH
Definition: latch.h:129
struct Latch * MyLatch
Definition: globals.c:57
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227
#define WL_LATCH_SET
Definition: latch.h:125
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1085

Variable Documentation

◆ MyBgworkerEntry