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

References BlockSig, and PG_SETMASK.

5732 {
5733  PG_SETMASK(&BlockSig);
5734 }
#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 5685 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().

5686 {
5688 
5689  /* XXX is this the right errcode? */
5691  ereport(FATAL,
5692  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5693  errmsg("database connection requirement not indicated during registration")));
5694 
5696 
5697  /* it had better not gotten out of "init" mode yet */
5698  if (!IsInitProcessingMode())
5699  ereport(ERROR,
5700  (errmsg("invalid processing mode in background worker")));
5702 }
int errcode(int sqlerrcode)
Definition: elog.c:608
#define SetProcessingMode(mode)
Definition: miscadmin.h:380
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:192
#define ERROR
Definition: elog.h:43
#define BGWORKER_BYPASS_ALLOWCONN
Definition: bgworker.h:154
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
static char * username
Definition: initdb.c:133
#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:589
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
char * dbname
Definition: streamutil.c:50
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define IsInitProcessingMode()
Definition: miscadmin.h:375

◆ BackgroundWorkerInitializeConnectionByOid()

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

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

5709 {
5711 
5712  /* XXX is this the right errcode? */
5714  ereport(FATAL,
5715  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5716  errmsg("database connection requirement not indicated during registration")));
5717 
5718  InitPostgres(NULL, dboid, NULL, useroid, NULL, (flags & BGWORKER_BYPASS_ALLOWCONN) != 0);
5719 
5720  /* it had better not gotten out of "init" mode yet */
5721  if (!IsInitProcessingMode())
5722  ereport(ERROR,
5723  (errmsg("invalid processing mode in background worker")));
5725 }
int errcode(int sqlerrcode)
Definition: elog.c:608
#define SetProcessingMode(mode)
Definition: miscadmin.h:380
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:192
#define ERROR
Definition: elog.h:43
#define BGWORKER_BYPASS_ALLOWCONN
Definition: bgworker.h:154
#define FATAL
Definition: elog.h:52
#define ereport(elevel, rest)
Definition: elog.h:141
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, char *out_dbname, bool override_allow_connections)
Definition: postinit.c:589
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define IsInitProcessingMode()
Definition: miscadmin.h:375

◆ BackgroundWorkerUnblockSignals()

void BackgroundWorkerUnblockSignals ( void  )

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

5738 {
5740 }
#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 1044 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().

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

◆ GetBackgroundWorkerTypeByPid()

const char* GetBackgroundWorkerTypeByPid ( pid_t  pid)

Definition at line 1252 of file bgworker.c.

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

Referenced by pg_stat_get_activity().

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

849 {
850  RegisteredBgWorker *rw;
851  static int numworkers = 0;
852 
853  if (!IsUnderPostmaster)
854  ereport(DEBUG1,
855  (errmsg("registering background worker \"%s\"", worker->bgw_name)));
856 
858  strcmp(worker->bgw_library_name, "postgres") != 0)
859  {
860  if (!IsUnderPostmaster)
861  ereport(LOG,
862  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
863  errmsg("background worker \"%s\": must be registered in shared_preload_libraries",
864  worker->bgw_name)));
865  return;
866  }
867 
868  if (!SanityCheckBackgroundWorker(worker, LOG))
869  return;
870 
871  if (worker->bgw_notify_pid != 0)
872  {
873  ereport(LOG,
874  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
875  errmsg("background worker \"%s\": only dynamic background workers can request notification",
876  worker->bgw_name)));
877  return;
878  }
879 
880  /*
881  * Enforce maximum number of workers. Note this is overly restrictive: we
882  * could allow more non-shmem-connected workers, because these don't count
883  * towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
884  * important to relax this restriction.
885  */
886  if (++numworkers > max_worker_processes)
887  {
888  ereport(LOG,
889  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
890  errmsg("too many background workers"),
891  errdetail_plural("Up to %d background worker can be registered with the current settings.",
892  "Up to %d background workers can be registered with the current settings.",
895  errhint("Consider increasing the configuration parameter \"max_worker_processes\".")));
896  return;
897  }
898 
899  /*
900  * Copy the registration data into the registered workers list.
901  */
902  rw = malloc(sizeof(RegisteredBgWorker));
903  if (rw == NULL)
904  {
905  ereport(LOG,
906  (errcode(ERRCODE_OUT_OF_MEMORY),
907  errmsg("out of memory")));
908  return;
909  }
910 
911  rw->rw_worker = *worker;
912  rw->rw_backend = NULL;
913  rw->rw_pid = 0;
914  rw->rw_child_slot = 0;
915  rw->rw_crashed_at = 0;
916  rw->rw_terminate = false;
917 
919 }
struct bkend * rw_backend
#define DEBUG1
Definition: elog.h:25
int errhint(const char *fmt,...)
Definition: elog.c:1071
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1522
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:574
int errcode(int sqlerrcode)
Definition: elog.c:608
#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:109
#define ereport(elevel, rest)
Definition: elog.h:141
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:1049
int errmsg(const char *fmt,...)
Definition: elog.c:822
pid_t bgw_notify_pid
Definition: bgworker.h:99
int max_worker_processes
Definition: globals.c:133
BackgroundWorker rw_worker
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:95

◆ RegisterDynamicBackgroundWorker()

bool RegisterDynamicBackgroundWorker ( BackgroundWorker worker,
BackgroundWorkerHandle **  handle 
)

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

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

◆ TerminateBackgroundWorker()

void TerminateBackgroundWorker ( BackgroundWorkerHandle handle)

Definition at line 1177 of file bgworker.c.

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

Referenced by cleanup_background_workers(), and DestroyParallelContext().

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

◆ WaitForBackgroundWorkerShutdown()

BgwHandleStatus WaitForBackgroundWorkerShutdown ( BackgroundWorkerHandle )

Definition at line 1138 of file bgworker.c.

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

Referenced by apw_start_database_worker(), and WaitForParallelWorkersToExit().

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

◆ WaitForBackgroundWorkerStartup()

BgwHandleStatus WaitForBackgroundWorkerStartup ( BackgroundWorkerHandle handle,
pid_t *  pid 
)

Definition at line 1096 of file bgworker.c.

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

Referenced by apw_start_master_worker(), and worker_spi_launch().

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

Variable Documentation

◆ MyBgworkerEntry