PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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_INTERRUPTIBLE   0x0004
 
#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 charGetBackgroundWorkerTypeByPid (pid_t pid)
 
void TerminateBackgroundWorker (BackgroundWorkerHandle *handle)
 
void TerminateBackgroundWorkersForDatabase (Oid databaseId)
 
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 91 of file bgworker.h.

◆ BGW_EXTRALEN

#define BGW_EXTRALEN   128

Definition at line 94 of file bgworker.h.

◆ BGW_MAXLEN

#define BGW_MAXLEN   96

Definition at line 93 of file bgworker.h.

◆ BGW_NEVER_RESTART

#define BGW_NEVER_RESTART   -1

Definition at line 92 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 166 of file bgworker.h.

◆ BGWORKER_BYPASS_ROLELOGINCHECK

#define BGWORKER_BYPASS_ROLELOGINCHECK   0x0002

Definition at line 167 of file bgworker.h.

◆ BGWORKER_CLASS_PARALLEL

#define BGWORKER_CLASS_PARALLEL   0x0010

Definition at line 75 of file bgworker.h.

◆ BGWORKER_INTERRUPTIBLE

#define BGWORKER_INTERRUPTIBLE   0x0004

Definition at line 67 of file bgworker.h.

◆ BGWORKER_SHMEM_ACCESS

#define BGWORKER_SHMEM_ACCESS   0x0001

Definition at line 53 of file bgworker.h.

Typedef Documentation

◆ BackgroundWorker

◆ BackgroundWorkerHandle

◆ BgwHandleStatus

◆ bgworker_main_type

typedef void(* bgworker_main_type) (Datum main_arg)

Definition at line 79 of file bgworker.h.

Enumeration Type Documentation

◆ BgwHandleStatus

Enumerator
BGWH_STARTED 
BGWH_NOT_YET_STARTED 
BGWH_STOPPED 
BGWH_POSTMASTER_DIED 

Definition at line 110 of file bgworker.h.

111{
112 BGWH_STARTED, /* worker is running */
113 BGWH_NOT_YET_STARTED, /* worker hasn't been started yet */
114 BGWH_STOPPED, /* worker has exited */
115 BGWH_POSTMASTER_DIED, /* postmaster died; worker status unclear */
BgwHandleStatus
Definition bgworker.h:111
@ BGWH_POSTMASTER_DIED
Definition bgworker.h:115
@ BGWH_STARTED
Definition bgworker.h:112
@ BGWH_NOT_YET_STARTED
Definition bgworker.h:113
@ BGWH_STOPPED
Definition bgworker.h:114

◆ BgWorkerStartTime

Enumerator
BgWorkerStart_PostmasterStart 
BgWorkerStart_ConsistentState 
BgWorkerStart_RecoveryFinished 

Definition at line 84 of file bgworker.h.

85{
BgWorkerStartTime
Definition bgworker.h:85
@ BgWorkerStart_RecoveryFinished
Definition bgworker.h:88
@ BgWorkerStart_ConsistentState
Definition bgworker.h:87
@ BgWorkerStart_PostmasterStart
Definition bgworker.h:86

Function Documentation

◆ BackgroundWorkerBlockSignals()

void BackgroundWorkerBlockSignals ( void  )
extern

Definition at line 927 of file bgworker.c.

928{
930}
sigset_t BlockSig
Definition pqsignal.c:23
static int fb(int x)

References BlockSig, and fb().

◆ BackgroundWorkerInitializeConnection()

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

Definition at line 859 of file bgworker.c.

860{
862 bits32 init_flags = 0; /* never honor session_preload_libraries */
863
864 /* ignore datallowconn and ACL_CONNECT? */
865 if (flags & BGWORKER_BYPASS_ALLOWCONN)
867 /* ignore rolcanlogin? */
870
871 /* XXX is this the right errcode? */
875 errmsg("database connection requirement not indicated during registration")));
876
877 InitPostgres(dbname, InvalidOid, /* database to connect to */
878 username, InvalidOid, /* role to connect as */
880 NULL); /* no out_dbname */
881
882 /* it had better not gotten out of "init" mode yet */
885 (errmsg("invalid processing mode in background worker")));
887}
#define BGWORKER_BYPASS_ROLELOGINCHECK
Definition bgworker.h:167
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition bgworker.h:60
#define BGWORKER_BYPASS_ALLOWCONN
Definition bgworker.h:166
uint32 bits32
Definition c.h:567
int errcode(int sqlerrcode)
Definition elog.c:864
int errmsg(const char *fmt,...)
Definition elog.c:1081
#define FATAL
Definition elog.h:41
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
static char * username
Definition initdb.c:153
@ NormalProcessing
Definition miscadmin.h:472
#define IsInitProcessingMode()
Definition miscadmin.h:478
#define SetProcessingMode(mode)
Definition miscadmin.h:483
#define INIT_PG_OVERRIDE_ROLE_LOGIN
Definition miscadmin.h:501
#define INIT_PG_OVERRIDE_ALLOW_CONNS
Definition miscadmin.h:500
#define InvalidOid
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, bits32 flags, char *out_dbname)
Definition postinit.c:718
BackgroundWorker * MyBgworkerEntry
Definition postmaster.c:200
char * dbname
Definition streamutil.c:49

References BackgroundWorker::bgw_flags, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_BYPASS_ALLOWCONN, BGWORKER_BYPASS_ROLELOGINCHECK, dbname, ereport, errcode(), errmsg(), ERROR, FATAL, fb(), 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 
)
extern

Definition at line 893 of file bgworker.c.

894{
896 bits32 init_flags = 0; /* never honor session_preload_libraries */
897
898 /* ignore datallowconn and ACL_CONNECT? */
899 if (flags & BGWORKER_BYPASS_ALLOWCONN)
901 /* ignore rolcanlogin? */
904
905 /* XXX is this the right errcode? */
909 errmsg("database connection requirement not indicated during registration")));
910
911 InitPostgres(NULL, dboid, /* database to connect to */
912 NULL, useroid, /* role to connect as */
914 NULL); /* no out_dbname */
915
916 /* it had better not gotten out of "init" mode yet */
919 (errmsg("invalid processing mode in background worker")));
921}

References BackgroundWorker::bgw_flags, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_BYPASS_ALLOWCONN, BGWORKER_BYPASS_ROLELOGINCHECK, ereport, errcode(), errmsg(), ERROR, FATAL, fb(), 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()

◆ GetBackgroundWorkerPid()

BgwHandleStatus GetBackgroundWorkerPid ( BackgroundWorkerHandle handle,
pid_t pidp 
)
extern

Definition at line 1164 of file bgworker.c.

1165{
1167 pid_t pid;
1168
1169 Assert(handle->slot < max_worker_processes);
1170 slot = &BackgroundWorkerData->slot[handle->slot];
1171
1172 /*
1173 * We could probably arrange to synchronize access to data using memory
1174 * barriers only, but for now, let's just keep it simple and grab the
1175 * lock. It seems unlikely that there will be enough traffic here to
1176 * result in meaningful contention.
1177 */
1179
1180 /*
1181 * The generation number can't be concurrently changed while we hold the
1182 * lock. The pid, which is updated by the postmaster, can change at any
1183 * time, but we assume such changes are atomic. So the value we read
1184 * won't be garbage, but it might be out of date by the time the caller
1185 * examines it (but that's unavoidable anyway).
1186 *
1187 * The in_use flag could be in the process of changing from true to false,
1188 * but if it is already false then it can't change further.
1189 */
1190 if (handle->generation != slot->generation || !slot->in_use)
1191 pid = 0;
1192 else
1193 pid = slot->pid;
1194
1195 /* All done. */
1197
1198 if (pid == 0)
1199 return BGWH_STOPPED;
1200 else if (pid == InvalidPid)
1201 return BGWH_NOT_YET_STARTED;
1202 *pidp = pid;
1203 return BGWH_STARTED;
1204}
static BackgroundWorkerArray * BackgroundWorkerData
Definition bgworker.c:109
#define Assert(condition)
Definition c.h:885
int max_worker_processes
Definition globals.c:144
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1176
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
@ LW_SHARED
Definition lwlock.h:113
#define InvalidPid
Definition miscadmin.h:32
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition bgworker.c:100

References Assert, BackgroundWorkerData, BGWH_NOT_YET_STARTED, BGWH_STARTED, BGWH_STOPPED, fb(), 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)
extern

Definition at line 1378 of file bgworker.c.

1379{
1380 int slotno;
1381 bool found = false;
1382 static char result[BGW_MAXLEN];
1383
1385
1387 {
1389
1390 if (slot->pid > 0 && slot->pid == pid)
1391 {
1392 strcpy(result, slot->worker.bgw_type);
1393 found = true;
1394 break;
1395 }
1396 }
1397
1399
1400 if (!found)
1401 return NULL;
1402
1403 return result;
1404}
#define BGW_MAXLEN
Definition bgworker.h:93
BackgroundWorker worker
Definition bgworker.c:81
char bgw_type[BGW_MAXLEN]
Definition bgworker.h:99

References BackgroundWorkerData, BGW_MAXLEN, BackgroundWorker::bgw_type, fb(), 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)
extern

Definition at line 946 of file bgworker.c.

947{
949 static int numworkers = 0;
950
951 /*
952 * Static background workers can only be registered in the postmaster
953 * process.
954 */
956 {
957 /*
958 * In EXEC_BACKEND or single-user mode, we process
959 * shared_preload_libraries in backend processes too. We cannot
960 * register static background workers at that stage, but many
961 * libraries' _PG_init() functions don't distinguish whether they're
962 * being loaded in the postmaster or in a backend, they just check
963 * process_shared_preload_libraries_in_progress. It's a bit sloppy,
964 * but for historical reasons we tolerate it. In EXEC_BACKEND mode,
965 * the background workers should already have been registered when the
966 * library was loaded in postmaster.
967 */
969 return;
970 ereport(LOG,
972 errmsg("background worker \"%s\": must be registered in \"shared_preload_libraries\"",
973 worker->bgw_name)));
974 return;
975 }
976
977 /*
978 * Cannot register static background workers after calling
979 * BackgroundWorkerShmemInit().
980 */
982 elog(ERROR, "cannot register background worker \"%s\" after shmem init",
983 worker->bgw_name);
984
986 (errmsg_internal("registering background worker \"%s\"", worker->bgw_name)));
987
988 if (!SanityCheckBackgroundWorker(worker, LOG))
989 return;
990
991 if (worker->bgw_notify_pid != 0)
992 {
993 ereport(LOG,
995 errmsg("background worker \"%s\": only dynamic background workers can request notification",
996 worker->bgw_name)));
997 return;
998 }
999
1000 /*
1001 * Enforce maximum number of workers. Note this is overly restrictive: we
1002 * could allow more non-shmem-connected workers, because these don't count
1003 * towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
1004 * important to relax this restriction.
1005 */
1007 {
1008 ereport(LOG,
1010 errmsg("too many background workers"),
1011 errdetail_plural("Up to %d background worker can be registered with the current settings.",
1012 "Up to %d background workers can be registered with the current settings.",
1015 errhint("Consider increasing the configuration parameter \"%s\".", "max_worker_processes")));
1016 return;
1017 }
1018
1019 /*
1020 * Copy the registration data into the registered workers list.
1021 */
1023 sizeof(RegisteredBgWorker),
1025 if (rw == NULL)
1026 {
1027 ereport(LOG,
1029 errmsg("out of memory")));
1030 return;
1031 }
1032
1033 rw->rw_worker = *worker;
1034 rw->rw_pid = 0;
1035 rw->rw_crashed_at = 0;
1036 rw->rw_terminate = false;
1037
1039}
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition bgworker.c:642
dlist_head BackgroundWorkerList
Definition bgworker.c:41
int errmsg_internal(const char *fmt,...)
Definition elog.c:1171
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition elog.c:1309
int errhint(const char *fmt,...)
Definition elog.c:1331
#define LOG
Definition elog.h:31
#define DEBUG1
Definition elog.h:30
#define elog(elevel,...)
Definition elog.h:226
#define MCXT_ALLOC_NO_OOM
Definition fe_memutils.h:29
bool IsUnderPostmaster
Definition globals.c:120
bool IsPostmasterEnvironment
Definition globals.c:119
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition ilist.h:347
void * MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
Definition mcxt.c:1289
MemoryContext PostmasterContext
Definition mcxt.c:168
bool process_shared_preload_libraries_in_progress
Definition miscinit.c:1786
char bgw_name[BGW_MAXLEN]
Definition bgworker.h:98
pid_t bgw_notify_pid
Definition bgworker.h:107
BackgroundWorker rw_worker

References BackgroundWorkerData, BackgroundWorkerList, BackgroundWorker::bgw_name, BackgroundWorker::bgw_notify_pid, DEBUG1, dlist_push_head(), elog, ereport, errcode(), errdetail_plural(), errhint(), errmsg(), errmsg_internal(), ERROR, fb(), IsPostmasterEnvironment, IsUnderPostmaster, LOG, max_worker_processes, MCXT_ALLOC_NO_OOM, MemoryContextAllocExtended(), PostmasterContext, process_shared_preload_libraries_in_progress, RegisteredBgWorker::rw_crashed_at, RegisteredBgWorker::rw_lnode, RegisteredBgWorker::rw_pid, RegisteredBgWorker::rw_terminate, RegisteredBgWorker::rw_worker, and SanityCheckBackgroundWorker().

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

◆ RegisterDynamicBackgroundWorker()

bool RegisterDynamicBackgroundWorker ( BackgroundWorker worker,
BackgroundWorkerHandle **  handle 
)
extern

Definition at line 1052 of file bgworker.c.

1054{
1055 int slotno;
1056 bool success = false;
1057 bool parallel;
1058 uint64 generation = 0;
1059
1060 /*
1061 * We can't register dynamic background workers from the postmaster. If
1062 * this is a standalone backend, we're the only process and can't start
1063 * any more. In a multi-process environment, it might be theoretically
1064 * possible, but we don't currently support it due to locking
1065 * considerations; see comments on the BackgroundWorkerSlot data
1066 * structure.
1067 */
1068 if (!IsUnderPostmaster)
1069 return false;
1070
1071 if (!SanityCheckBackgroundWorker(worker, ERROR))
1072 return false;
1073
1074 parallel = (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0;
1075
1077
1078 /*
1079 * If this is a parallel worker, check whether there are already too many
1080 * parallel workers; if so, don't register another one. Our view of
1081 * parallel_terminate_count may be slightly stale, but that doesn't really
1082 * matter: we would have gotten the same result if we'd arrived here
1083 * slightly earlier anyway. There's no help for it, either, since the
1084 * postmaster must not take locks; a memory barrier wouldn't guarantee
1085 * anything useful.
1086 */
1090 {
1095 return false;
1096 }
1097
1098 /*
1099 * Look for an unused slot. If we find one, grab it.
1100 */
1102 {
1104
1105 if (!slot->in_use)
1106 {
1107 memcpy(&slot->worker, worker, sizeof(BackgroundWorker));
1108 slot->pid = InvalidPid; /* indicates not started yet */
1109 slot->generation++;
1110 slot->terminate = false;
1111 generation = slot->generation;
1112 if (parallel)
1114
1115 /*
1116 * Make sure postmaster doesn't see the slot as in use before it
1117 * sees the new contents.
1118 */
1120
1121 slot->in_use = true;
1122 success = true;
1123 break;
1124 }
1125 }
1126
1128
1129 /* If we found a slot, tell the postmaster to notice the change. */
1130 if (success)
1132
1133 /*
1134 * If we found a slot and the user has provided a handle, initialize it.
1135 */
1136 if (success && handle)
1137 {
1139 (*handle)->slot = slotno;
1140 (*handle)->generation = generation;
1141 }
1142
1143 return success;
1144}
#define pg_write_barrier()
Definition atomics.h:155
#define BGWORKER_CLASS_PARALLEL
Definition bgworker.h:75
#define MAX_PARALLEL_WORKER_LIMIT
uint64_t uint64
Definition c.h:559
#define palloc_object(type)
Definition fe_memutils.h:74
int max_parallel_workers
Definition globals.c:145
static bool success
Definition initdb.c:187
@ LW_EXCLUSIVE
Definition lwlock.h:112
void SendPostmasterSignal(PMSignalReason reason)
Definition pmsignal.c:165
@ PMSIGNAL_BACKGROUND_WORKER_CHANGE
Definition pmsignal.h:41
uint32 parallel_terminate_count
Definition bgworker.c:99
uint32 parallel_register_count
Definition bgworker.c:98

References Assert, BackgroundWorkerData, BackgroundWorker::bgw_flags, BGWORKER_CLASS_PARALLEL, ERROR, fb(), BackgroundWorkerSlot::generation, BackgroundWorkerSlot::in_use, InvalidPid, IsUnderPostmaster, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAX_PARALLEL_WORKER_LIMIT, max_parallel_workers, palloc_object, 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)
extern

Definition at line 1303 of file bgworker.c.

1304{
1306 bool signal_postmaster = false;
1307
1308 Assert(handle->slot < max_worker_processes);
1309 slot = &BackgroundWorkerData->slot[handle->slot];
1310
1311 /* Set terminate flag in shared memory, unless slot has been reused. */
1313 if (handle->generation == slot->generation)
1314 {
1315 slot->terminate = true;
1316 signal_postmaster = true;
1317 }
1319
1320 /* Make sure the postmaster notices the change to shared memory. */
1323}

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

◆ TerminateBackgroundWorkersForDatabase()

void TerminateBackgroundWorkersForDatabase ( Oid  databaseId)
extern

Definition at line 1411 of file bgworker.c.

1412{
1413 bool signal_postmaster = false;
1414
1416
1417 /*
1418 * Iterate through slots, looking for workers connected to the given
1419 * database.
1420 */
1422 {
1424
1425 if (slot->in_use &&
1427 {
1428 PGPROC *proc = BackendPidGetProc(slot->pid);
1429
1430 if (proc && proc->databaseId == databaseId)
1431 {
1432 slot->terminate = true;
1433 signal_postmaster = true;
1434 }
1435 }
1436 }
1437
1439
1440 /* Make sure the postmaster notices the change to shared memory. */
1443}
#define BGWORKER_INTERRUPTIBLE
Definition bgworker.h:67
PGPROC * BackendPidGetProc(int pid)
Definition procarray.c:3160
Definition proc.h:180
Oid databaseId
Definition proc.h:225

References BackendPidGetProc(), BackgroundWorkerData, BackgroundWorker::bgw_flags, BGWORKER_INTERRUPTIBLE, PGPROC::databaseId, fb(), BackgroundWorkerSlot::in_use, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), BackgroundWorkerSlot::pid, PMSIGNAL_BACKGROUND_WORKER_CHANGE, SendPostmasterSignal(), BackgroundWorkerArray::slot, BackgroundWorkerSlot::terminate, BackgroundWorkerArray::total_slots, and BackgroundWorkerSlot::worker.

Referenced by CountOtherDBBackends().

◆ WaitForBackgroundWorkerShutdown()

BgwHandleStatus WaitForBackgroundWorkerShutdown ( BackgroundWorkerHandle handle)
extern

Definition at line 1264 of file bgworker.c.

1265{
1266 BgwHandleStatus status;
1267 int rc;
1268
1269 for (;;)
1270 {
1271 pid_t pid;
1272
1274
1275 status = GetBackgroundWorkerPid(handle, &pid);
1276 if (status == BGWH_STOPPED)
1277 break;
1278
1279 rc = WaitLatch(MyLatch,
1282
1283 if (rc & WL_POSTMASTER_DEATH)
1284 {
1285 status = BGWH_POSTMASTER_DIED;
1286 break;
1287 }
1288
1290 }
1291
1292 return status;
1293}
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition bgworker.c:1164
struct Latch * MyLatch
Definition globals.c:63
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
#define WL_LATCH_SET
#define WL_POSTMASTER_DEATH

References BGWH_POSTMASTER_DIED, BGWH_STOPPED, CHECK_FOR_INTERRUPTS, fb(), 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 
)
extern

Definition at line 1219 of file bgworker.c.

1220{
1221 BgwHandleStatus status;
1222 int rc;
1223
1224 for (;;)
1225 {
1226 pid_t pid;
1227
1229
1230 status = GetBackgroundWorkerPid(handle, &pid);
1231 if (status == BGWH_STARTED)
1232 *pidp = pid;
1233 if (status != BGWH_NOT_YET_STARTED)
1234 break;
1235
1236 rc = WaitLatch(MyLatch,
1239
1240 if (rc & WL_POSTMASTER_DEATH)
1241 {
1242 status = BGWH_POSTMASTER_DIED;
1243 break;
1244 }
1245
1247 }
1248
1249 return status;
1250}

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

Referenced by apw_start_leader_worker(), and worker_spi_launch().

Variable Documentation

◆ MyBgworkerEntry