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

944{
946}
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 875 of file bgworker.c.

876{
878 uint32 init_flags = 0; /* never honor session_preload_libraries */
879
880 /* ignore datallowconn and ACL_CONNECT? */
881 if (flags & BGWORKER_BYPASS_ALLOWCONN)
883 /* ignore rolcanlogin? */
886
887 /* XXX is this the right errcode? */
891 errmsg("database connection requirement not indicated during registration")));
892
893 InitPostgres(dbname, InvalidOid, /* database to connect to */
894 username, InvalidOid, /* role to connect as */
896 NULL); /* no out_dbname */
897
898 /* it had better not gotten out of "init" mode yet */
901 (errmsg("invalid processing mode in background worker")));
903}
#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_t uint32
Definition c.h:624
int errcode(int sqlerrcode)
Definition elog.c:875
#define FATAL
Definition elog.h:42
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
static char * username
Definition initdb.c:153
@ NormalProcessing
Definition miscadmin.h:490
#define IsInitProcessingMode()
Definition miscadmin.h:496
#define SetProcessingMode(mode)
Definition miscadmin.h:501
#define INIT_PG_OVERRIDE_ROLE_LOGIN
Definition miscadmin.h:519
#define INIT_PG_OVERRIDE_ALLOW_CONNS
Definition miscadmin.h:518
static char * errmsg
#define InvalidOid
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, uint32 flags, char *out_dbname)
Definition postinit.c:719
BackgroundWorker * MyBgworkerEntry
Definition postmaster.c:201
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 909 of file bgworker.c.

910{
912 uint32 init_flags = 0; /* never honor session_preload_libraries */
913
914 /* ignore datallowconn and ACL_CONNECT? */
915 if (flags & BGWORKER_BYPASS_ALLOWCONN)
917 /* ignore rolcanlogin? */
920
921 /* XXX is this the right errcode? */
925 errmsg("database connection requirement not indicated during registration")));
926
927 InitPostgres(NULL, dboid, /* database to connect to */
928 NULL, useroid, /* role to connect as */
930 NULL); /* no out_dbname */
931
932 /* it had better not gotten out of "init" mode yet */
935 (errmsg("invalid processing mode in background worker")));
937}

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(), DataChecksumsWorkerLauncherMain(), DataChecksumsWorkerMain(), InitializeLogRepWorker(), ParallelWorkerMain(), RepackWorkerMain(), and worker_spi_main().

◆ BackgroundWorkerUnblockSignals()

◆ GetBackgroundWorkerPid()

BgwHandleStatus GetBackgroundWorkerPid ( BackgroundWorkerHandle handle,
pid_t pidp 
)
extern

Definition at line 1180 of file bgworker.c.

1181{
1183 pid_t pid;
1184
1185 Assert(handle->slot < max_worker_processes);
1186 slot = &BackgroundWorkerData->slot[handle->slot];
1187
1188 /*
1189 * We could probably arrange to synchronize access to data using memory
1190 * barriers only, but for now, let's just keep it simple and grab the
1191 * lock. It seems unlikely that there will be enough traffic here to
1192 * result in meaningful contention.
1193 */
1195
1196 /*
1197 * The generation number can't be concurrently changed while we hold the
1198 * lock. The pid, which is updated by the postmaster, can change at any
1199 * time, but we assume such changes are atomic. So the value we read
1200 * won't be garbage, but it might be out of date by the time the caller
1201 * examines it (but that's unavoidable anyway).
1202 *
1203 * The in_use flag could be in the process of changing from true to false,
1204 * but if it is already false then it can't change further.
1205 */
1206 if (handle->generation != slot->generation || !slot->in_use)
1207 pid = 0;
1208 else
1209 pid = slot->pid;
1210
1211 /* All done. */
1213
1214 if (pid == 0)
1215 return BGWH_STOPPED;
1216 else if (pid == InvalidPid)
1217 return BGWH_NOT_YET_STARTED;
1218 *pidp = pid;
1219 return BGWH_STARTED;
1220}
static BackgroundWorkerArray * BackgroundWorkerData
Definition bgworker.c:113
#define Assert(condition)
Definition c.h:943
int max_worker_processes
Definition globals.c:146
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_SHARED
Definition lwlock.h:105
#define InvalidPid
Definition miscadmin.h:32
BackgroundWorkerSlot slot[FLEXIBLE_ARRAY_MEMBER]
Definition bgworker.c:104

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

1395{
1396 int slotno;
1397 bool found = false;
1398 static char result[BGW_MAXLEN];
1399
1401
1403 {
1405
1406 if (slot->pid > 0 && slot->pid == pid)
1407 {
1408 strcpy(result, slot->worker.bgw_type);
1409 found = true;
1410 break;
1411 }
1412 }
1413
1415
1416 if (!found)
1417 return NULL;
1418
1419 return result;
1420}
#define BGW_MAXLEN
Definition bgworker.h:93
uint32 result
BackgroundWorker worker
Definition bgworker.c:85
char bgw_type[BGW_MAXLEN]
Definition bgworker.h:99

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

Referenced by pg_stat_get_activity().

◆ RegisterBackgroundWorker()

void RegisterBackgroundWorker ( BackgroundWorker worker)
extern

Definition at line 962 of file bgworker.c.

963{
965 static int numworkers = 0;
966
967 /*
968 * Static background workers can only be registered in the postmaster
969 * process.
970 */
972 {
973 /*
974 * In EXEC_BACKEND or single-user mode, we process
975 * shared_preload_libraries in backend processes too. We cannot
976 * register static background workers at that stage, but many
977 * libraries' _PG_init() functions don't distinguish whether they're
978 * being loaded in the postmaster or in a backend, they just check
979 * process_shared_preload_libraries_in_progress. It's a bit sloppy,
980 * but for historical reasons we tolerate it. In EXEC_BACKEND mode,
981 * the background workers should already have been registered when the
982 * library was loaded in postmaster.
983 */
985 return;
986 ereport(LOG,
988 errmsg("background worker \"%s\": must be registered in \"shared_preload_libraries\"",
989 worker->bgw_name)));
990 return;
991 }
992
993 /*
994 * Cannot register static background workers after calling
995 * BackgroundWorkerShmemInit().
996 */
998 elog(ERROR, "cannot register background worker \"%s\" after shmem init",
999 worker->bgw_name);
1000
1002 (errmsg_internal("registering background worker \"%s\"", worker->bgw_name)));
1003
1004 if (!SanityCheckBackgroundWorker(worker, LOG))
1005 return;
1006
1007 if (worker->bgw_notify_pid != 0)
1008 {
1009 ereport(LOG,
1011 errmsg("background worker \"%s\": only dynamic background workers can request notification",
1012 worker->bgw_name)));
1013 return;
1014 }
1015
1016 /*
1017 * Enforce maximum number of workers. Note this is overly restrictive: we
1018 * could allow more non-shmem-connected workers, because these don't count
1019 * towards the MAX_BACKENDS limit elsewhere. For now, it doesn't seem
1020 * important to relax this restriction.
1021 */
1023 {
1024 ereport(LOG,
1026 errmsg("too many background workers"),
1027 errdetail_plural("Up to %d background worker can be registered with the current settings.",
1028 "Up to %d background workers can be registered with the current settings.",
1031 errhint("Consider increasing the configuration parameter \"%s\".", "max_worker_processes")));
1032 return;
1033 }
1034
1035 /*
1036 * Copy the registration data into the registered workers list.
1037 */
1039 sizeof(RegisteredBgWorker),
1041 if (rw == NULL)
1042 {
1043 ereport(LOG,
1045 errmsg("out of memory")));
1046 return;
1047 }
1048
1049 rw->rw_worker = *worker;
1050 rw->rw_pid = 0;
1051 rw->rw_crashed_at = 0;
1052 rw->rw_terminate = false;
1053
1055}
static bool SanityCheckBackgroundWorker(BackgroundWorker *worker, int elevel)
Definition bgworker.c:658
dlist_head BackgroundWorkerList
Definition bgworker.c:45
#define LOG
Definition elog.h:32
int errhint(const char *fmt,...) pg_attribute_printf(1
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define DEBUG1
Definition elog.h:31
#define elog(elevel,...)
Definition elog.h:228
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...) pg_attribute_printf(1
#define MCXT_ALLOC_NO_OOM
Definition fe_memutils.h:29
bool IsUnderPostmaster
Definition globals.c:122
bool IsPostmasterEnvironment
Definition globals.c:121
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:1788
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(), apw_start_leader_worker(), and pgsa_start_worker().

◆ RegisterDynamicBackgroundWorker()

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

Definition at line 1068 of file bgworker.c.

1070{
1071 int slotno;
1072 bool success = false;
1073 bool parallel;
1074 uint64 generation = 0;
1075
1076 /*
1077 * We can't register dynamic background workers from the postmaster. If
1078 * this is a standalone backend, we're the only process and can't start
1079 * any more. In a multi-process environment, it might be theoretically
1080 * possible, but we don't currently support it due to locking
1081 * considerations; see comments on the BackgroundWorkerSlot data
1082 * structure.
1083 */
1084 if (!IsUnderPostmaster)
1085 return false;
1086
1087 if (!SanityCheckBackgroundWorker(worker, ERROR))
1088 return false;
1089
1090 parallel = (worker->bgw_flags & BGWORKER_CLASS_PARALLEL) != 0;
1091
1093
1094 /*
1095 * If this is a parallel worker, check whether there are already too many
1096 * parallel workers; if so, don't register another one. Our view of
1097 * parallel_terminate_count may be slightly stale, but that doesn't really
1098 * matter: we would have gotten the same result if we'd arrived here
1099 * slightly earlier anyway. There's no help for it, either, since the
1100 * postmaster must not take locks; a memory barrier wouldn't guarantee
1101 * anything useful.
1102 */
1106 {
1111 return false;
1112 }
1113
1114 /*
1115 * Look for an unused slot. If we find one, grab it.
1116 */
1118 {
1120
1121 if (!slot->in_use)
1122 {
1123 memcpy(&slot->worker, worker, sizeof(BackgroundWorker));
1124 slot->pid = InvalidPid; /* indicates not started yet */
1125 slot->generation++;
1126 slot->terminate = false;
1127 generation = slot->generation;
1128 if (parallel)
1130
1131 /*
1132 * Make sure postmaster doesn't see the slot as in use before it
1133 * sees the new contents.
1134 */
1136
1137 slot->in_use = true;
1138 success = true;
1139 break;
1140 }
1141 }
1142
1144
1145 /* If we found a slot, tell the postmaster to notice the change. */
1146 if (success)
1148
1149 /*
1150 * If we found a slot and the user has provided a handle, initialize it.
1151 */
1152 if (success && handle)
1153 {
1155 (*handle)->slot = slotno;
1156 (*handle)->generation = generation;
1157 }
1158
1159 return success;
1160}
#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:625
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define palloc_object(type)
Definition fe_memutils.h:74
int max_parallel_workers
Definition globals.c:147
static bool success
Definition initdb.c:188
@ LW_EXCLUSIVE
Definition lwlock.h:104
void SendPostmasterSignal(PMSignalReason reason)
Definition pmsignal.c:164
@ PMSIGNAL_BACKGROUND_WORKER_CHANGE
Definition pmsignal.h:42
uint32 parallel_terminate_count
Definition bgworker.c:103
uint32 parallel_register_count
Definition bgworker.c:102

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, memcpy(), 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(), pgsa_start_worker(), ProcessDatabase(), setup_background_workers(), start_repack_decoding_worker(), StartDataChecksumsWorkerLauncher(), and worker_spi_launch().

◆ TerminateBackgroundWorker()

void TerminateBackgroundWorker ( BackgroundWorkerHandle handle)
extern

Definition at line 1319 of file bgworker.c.

1320{
1322 bool signal_postmaster = false;
1323
1324 Assert(handle->slot < max_worker_processes);
1325 slot = &BackgroundWorkerData->slot[handle->slot];
1326
1327 /* Set terminate flag in shared memory, unless slot has been reused. */
1329 if (handle->generation == slot->generation)
1330 {
1331 slot->terminate = true;
1332 signal_postmaster = true;
1333 }
1335
1336 /* Make sure the postmaster notices the change to shared memory. */
1339}

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(), DestroyParallelContext(), and stop_repack_decoding_worker().

◆ TerminateBackgroundWorkersForDatabase()

void TerminateBackgroundWorkersForDatabase ( Oid  databaseId)
extern

Definition at line 1427 of file bgworker.c.

1428{
1429 bool signal_postmaster = false;
1430
1431 elog(DEBUG1, "attempting worker termination for database %u",
1432 databaseId);
1433
1435
1436 /*
1437 * Iterate through slots, looking for workers connected to the given
1438 * database.
1439 */
1441 {
1443
1444 if (slot->in_use &&
1446 {
1447 PGPROC *proc = BackendPidGetProc(slot->pid);
1448
1449 if (proc && proc->databaseId == databaseId)
1450 {
1451 slot->terminate = true;
1452 signal_postmaster = true;
1453
1454 elog(DEBUG1, "termination requested for worker (PID %d) on database %u",
1455 (int) slot->pid, databaseId);
1456 }
1457 }
1458 }
1459
1461
1462 /* Make sure the postmaster notices the change to shared memory. */
1465}
#define BGWORKER_INTERRUPTIBLE
Definition bgworker.h:67
PGPROC * BackendPidGetProc(int pid)
Definition procarray.c:3169
Definition proc.h:179
Oid databaseId
Definition proc.h:201

References BackendPidGetProc(), BackgroundWorkerData, BackgroundWorker::bgw_flags, BGWORKER_INTERRUPTIBLE, PGPROC::databaseId, DEBUG1, elog, 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 1280 of file bgworker.c.

1281{
1282 BgwHandleStatus status;
1283 int rc;
1284
1285 for (;;)
1286 {
1287 pid_t pid;
1288
1290
1291 status = GetBackgroundWorkerPid(handle, &pid);
1292 if (status == BGWH_STOPPED)
1293 break;
1294
1295 rc = WaitLatch(MyLatch,
1298
1299 if (rc & WL_POSTMASTER_DEATH)
1300 {
1301 status = BGWH_POSTMASTER_DIED;
1302 break;
1303 }
1304
1306 }
1307
1308 return status;
1309}
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition bgworker.c:1180
struct Latch * MyLatch
Definition globals.c:65
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:125
#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(), ProcessDatabase(), stop_repack_decoding_worker(), and WaitForParallelWorkersToExit().

◆ WaitForBackgroundWorkerStartup()

BgwHandleStatus WaitForBackgroundWorkerStartup ( BackgroundWorkerHandle handle,
pid_t pidp 
)
extern

Definition at line 1235 of file bgworker.c.

1236{
1237 BgwHandleStatus status;
1238 int rc;
1239
1240 for (;;)
1241 {
1242 pid_t pid;
1243
1245
1246 status = GetBackgroundWorkerPid(handle, &pid);
1247 if (status == BGWH_STARTED)
1248 *pidp = pid;
1249 if (status != BGWH_NOT_YET_STARTED)
1250 break;
1251
1252 rc = WaitLatch(MyLatch,
1255
1256 if (rc & WL_POSTMASTER_DEATH)
1257 {
1258 status = BGWH_POSTMASTER_DIED;
1259 break;
1260 }
1261
1263 }
1264
1265 return status;
1266}

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(), pgsa_start_worker(), ProcessDatabase(), and worker_spi_launch().

Variable Documentation

◆ MyBgworkerEntry