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   0x0001
 
#define BGWORKER_BYPASS_ROLELOGINCHECK   0x0002
 

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   0x0001

Definition at line 156 of file bgworker.h.

◆ BGWORKER_BYPASS_ROLELOGINCHECK

#define BGWORKER_BYPASS_ROLELOGINCHECK   0x0002

Definition at line 157 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 4223 of file postmaster.c.

4224 {
4225  sigprocmask(SIG_SETMASK, &BlockSig, NULL);
4226 }
sigset_t BlockSig
Definition: pqsignal.c:23

References BlockSig.

◆ BackgroundWorkerInitializeConnection()

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

Definition at line 4155 of file postmaster.c.

4156 {
4158  bits32 init_flags = 0; /* never honor session_preload_libraries */
4159 
4160  /* ignore datallowconn? */
4161  if (flags & BGWORKER_BYPASS_ALLOWCONN)
4162  init_flags |= INIT_PG_OVERRIDE_ALLOW_CONNS;
4163  /* ignore rolcanlogin? */
4164  if (flags & BGWORKER_BYPASS_ROLELOGINCHECK)
4165  init_flags |= INIT_PG_OVERRIDE_ROLE_LOGIN;
4166 
4167  /* XXX is this the right errcode? */
4169  ereport(FATAL,
4170  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4171  errmsg("database connection requirement not indicated during registration")));
4172 
4173  InitPostgres(dbname, InvalidOid, /* database to connect to */
4174  username, InvalidOid, /* role to connect as */
4175  init_flags,
4176  NULL); /* no out_dbname */
4177 
4178  /* it had better not gotten out of "init" mode yet */
4179  if (!IsInitProcessingMode())
4180  ereport(ERROR,
4181  (errmsg("invalid processing mode in background worker")));
4183 }
#define BGWORKER_BYPASS_ROLELOGINCHECK
Definition: bgworker.h:157
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:60
#define BGWORKER_BYPASS_ALLOWCONN
Definition: bgworker.h:156
uint32 bits32
Definition: c.h:515
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define FATAL
Definition: elog.h:41
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
@ NormalProcessing
Definition: miscadmin.h:449
#define IsInitProcessingMode()
Definition: miscadmin.h:455
#define SetProcessingMode(mode)
Definition: miscadmin.h:460
#define INIT_PG_OVERRIDE_ROLE_LOGIN
Definition: miscadmin.h:478
#define INIT_PG_OVERRIDE_ALLOW_CONNS
Definition: miscadmin.h:477
const char * username
Definition: pgbench.c:296
#define InvalidOid
Definition: postgres_ext.h:36
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, bits32 flags, char *out_dbname)
Definition: postinit.c:736
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:185
char * dbname
Definition: streamutil.c:52

References BackgroundWorker::bgw_flags, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_BYPASS_ALLOWCONN, BGWORKER_BYPASS_ROLELOGINCHECK, dbname, ereport, errcode(), errmsg(), ERROR, FATAL, INIT_PG_OVERRIDE_ALLOW_CONNS, INIT_PG_OVERRIDE_ROLE_LOGIN, 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 4189 of file postmaster.c.

4190 {
4192  bits32 init_flags = 0; /* never honor session_preload_libraries */
4193 
4194  /* ignore datallowconn? */
4195  if (flags & BGWORKER_BYPASS_ALLOWCONN)
4196  init_flags |= INIT_PG_OVERRIDE_ALLOW_CONNS;
4197  /* ignore rolcanlogin? */
4198  if (flags & BGWORKER_BYPASS_ROLELOGINCHECK)
4199  init_flags |= INIT_PG_OVERRIDE_ROLE_LOGIN;
4200 
4201  /* XXX is this the right errcode? */
4203  ereport(FATAL,
4204  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
4205  errmsg("database connection requirement not indicated during registration")));
4206 
4207  InitPostgres(NULL, dboid, /* database to connect to */
4208  NULL, useroid, /* role to connect as */
4209  init_flags,
4210  NULL); /* no out_dbname */
4211 
4212  /* it had better not gotten out of "init" mode yet */
4213  if (!IsInitProcessingMode())
4214  ereport(ERROR,
4215  (errmsg("invalid processing mode in background worker")));
4217 }

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

Referenced by autoprewarm_database_main(), InitializeLogRepWorker(), ParallelWorkerMain(), and worker_spi_main().

◆ BackgroundWorkerUnblockSignals()

void BackgroundWorkerUnblockSignals ( void  )

Definition at line 4229 of file postmaster.c.

4230 {
4231  sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
4232 }
sigset_t UnBlockSig
Definition: pqsignal.c:22

References UnBlockSig.

Referenced by ApplyLauncherMain(), autoprewarm_database_main(), autoprewarm_main(), BackgroundWorkerMain(), ParallelApplyWorkerMain(), ParallelWorkerMain(), SetupApplyOrSyncWorker(), test_shm_mq_main(), and worker_spi_main().

◆ GetBackgroundWorkerPid()

BgwHandleStatus GetBackgroundWorkerPid ( BackgroundWorkerHandle handle,
pid_t *  pidp 
)

Definition at line 1082 of file bgworker.c.

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

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 1296 of file bgworker.c.

1297 {
1298  int slotno;
1299  bool found = false;
1300  static char result[BGW_MAXLEN];
1301 
1302  LWLockAcquire(BackgroundWorkerLock, LW_SHARED);
1303 
1304  for (slotno = 0; slotno < BackgroundWorkerData->total_slots; slotno++)
1305  {
1307 
1308  if (slot->pid > 0 && slot->pid == pid)
1309  {
1310  strcpy(result, slot->worker.bgw_type);
1311  found = true;
1312  break;
1313  }
1314  }
1315 
1316  LWLockRelease(BackgroundWorkerLock);
1317 
1318  if (!found)
1319  return NULL;
1320 
1321  return result;
1322 }
#define BGW_MAXLEN
Definition: bgworker.h:86
BackgroundWorker worker
Definition: bgworker.c:80
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 862 of file bgworker.c.

863 {
864  RegisteredBgWorker *rw;
865  static int numworkers = 0;
866 
867  /*
868  * Static background workers can only be registered in the postmaster
869  * process.
870  */
872  {
873  /*
874  * In EXEC_BACKEND or single-user mode, we process
875  * shared_preload_libraries in backend processes too. We cannot
876  * register static background workers at that stage, but many
877  * libraries' _PG_init() functions don't distinguish whether they're
878  * being loaded in the postmaster or in a backend, they just check
879  * process_shared_preload_libraries_in_progress. It's a bit sloppy,
880  * but for historical reasons we tolerate it. In EXEC_BACKEND mode,
881  * the background workers should already have been registered when the
882  * library was loaded in postmaster.
883  */
885  return;
886  ereport(LOG,
887  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
888  errmsg("background worker \"%s\": must be registered in shared_preload_libraries",
889  worker->bgw_name)));
890  return;
891  }
892 
893  /*
894  * Cannot register static background workers after calling
895  * BackgroundWorkerShmemInit().
896  */
897  if (BackgroundWorkerData != NULL)
898  elog(ERROR, "cannot register background worker \"%s\" after shmem init",
899  worker->bgw_name);
900 
901  ereport(DEBUG1,
902  (errmsg_internal("registering background worker \"%s\"", worker->bgw_name)));
903 
904  if (!SanityCheckBackgroundWorker(worker, LOG))
905  return;
906 
907  if (worker->bgw_notify_pid != 0)
908  {
909  ereport(LOG,
910  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
911  errmsg("background worker \"%s\": only dynamic background workers can request notification",
912  worker->bgw_name)));
913  return;
914  }
915 
916  /*
917  * Enforce maximum number of workers. Note this is overly restrictive: we
918  * could allow more non-shmem-connected workers, because these don't count
919  * towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
920  * important to relax this restriction.
921  */
922  if (++numworkers > max_worker_processes)
923  {
924  ereport(LOG,
925  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
926  errmsg("too many background workers"),
927  errdetail_plural("Up to %d background worker can be registered with the current settings.",
928  "Up to %d background workers can be registered with the current settings.",
931  errhint("Consider increasing the configuration parameter max_worker_processes.")));
932  return;
933  }
934 
935  /*
936  * Copy the registration data into the registered workers list.
937  */
939  sizeof(RegisteredBgWorker),
941  if (rw == NULL)
942  {
943  ereport(LOG,
944  (errcode(ERRCODE_OUT_OF_MEMORY),
945  errmsg("out of memory")));
946  return;
947  }
948 
949  rw->rw_worker = *worker;
950  rw->rw_backend = NULL;
951  rw->rw_pid = 0;
952  rw->rw_child_slot = 0;
953  rw->rw_crashed_at = 0;
954  rw->rw_terminate = false;
955 
957 }
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition: bgworker.c:637
slist_head BackgroundWorkerList
Definition: bgworker.c:40
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1297
int errhint(const char *fmt,...)
Definition: elog.c:1319
#define LOG
Definition: elog.h:31
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:224
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:17
bool IsUnderPostmaster
Definition: globals.c:117
bool IsPostmasterEnvironment
Definition: globals.c:116
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:1006
void * MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
Definition: mcxt.c:1237
MemoryContext PostmasterContext
Definition: mcxt.c:151
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1778
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:91
pid_t bgw_notify_pid
Definition: bgworker.h:100
struct bkend * rw_backend
BackgroundWorker rw_worker

References BackgroundWorkerData, BackgroundWorkerList, BackgroundWorker::bgw_name, BackgroundWorker::bgw_notify_pid, DEBUG1, elog, ereport, errcode(), errdetail_plural(), errhint(), errmsg(), errmsg_internal(), ERROR, IsPostmasterEnvironment, IsUnderPostmaster, LOG, max_worker_processes, MCXT_ALLOC_NO_OOM, MemoryContextAllocExtended(), PostmasterContext, 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 970 of file bgworker.c.

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

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 1221 of file bgworker.c.

1222 {
1223  BackgroundWorkerSlot *slot;
1224  bool signal_postmaster = false;
1225 
1226  Assert(handle->slot < max_worker_processes);
1227  slot = &BackgroundWorkerData->slot[handle->slot];
1228 
1229  /* Set terminate flag in shared memory, unless slot has been reused. */
1230  LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
1231  if (handle->generation == slot->generation)
1232  {
1233  slot->terminate = true;
1234  signal_postmaster = true;
1235  }
1236  LWLockRelease(BackgroundWorkerLock);
1237 
1238  /* Make sure the postmaster notices the change to shared memory. */
1239  if (signal_postmaster)
1241 }

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 1182 of file bgworker.c.

1183 {
1184  BgwHandleStatus status;
1185  int rc;
1186 
1187  for (;;)
1188  {
1189  pid_t pid;
1190 
1192 
1193  status = GetBackgroundWorkerPid(handle, &pid);
1194  if (status == BGWH_STOPPED)
1195  break;
1196 
1197  rc = WaitLatch(MyLatch,
1199  WAIT_EVENT_BGWORKER_SHUTDOWN);
1200 
1201  if (rc & WL_POSTMASTER_DEATH)
1202  {
1203  status = BGWH_POSTMASTER_DIED;
1204  break;
1205  }
1206 
1208  }
1209 
1210  return status;
1211 }
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1082
struct Latch * MyLatch
Definition: globals.c:60
void ResetLatch(Latch *latch)
Definition: latch.c:724
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:517
#define WL_LATCH_SET
Definition: latch.h:127
#define WL_POSTMASTER_DEATH
Definition: latch.h:131
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122

References BGWH_POSTMASTER_DIED, BGWH_STOPPED, CHECK_FOR_INTERRUPTS, GetBackgroundWorkerPid(), MyLatch, ResetLatch(), 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 1137 of file bgworker.c.

1138 {
1139  BgwHandleStatus status;
1140  int rc;
1141 
1142  for (;;)
1143  {
1144  pid_t pid;
1145 
1147 
1148  status = GetBackgroundWorkerPid(handle, &pid);
1149  if (status == BGWH_STARTED)
1150  *pidp = pid;
1151  if (status != BGWH_NOT_YET_STARTED)
1152  break;
1153 
1154  rc = WaitLatch(MyLatch,
1156  WAIT_EVENT_BGWORKER_STARTUP);
1157 
1158  if (rc & WL_POSTMASTER_DEATH)
1159  {
1160  status = BGWH_POSTMASTER_DIED;
1161  break;
1162  }
1163 
1165  }
1166 
1167  return status;
1168 }

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

Referenced by apw_start_leader_worker(), and worker_spi_launch().

Variable Documentation

◆ MyBgworkerEntry