PostgreSQL Source Code  git master
pmsignal.h File Reference
#include <signal.h>
Include dependency graph for pmsignal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PostmasterIsAlive()   PostmasterIsAliveInternal()
 

Typedefs

typedef struct PMSignalData PMSignalData
 

Enumerations

enum  PMSignalReason {
  PMSIGNAL_RECOVERY_STARTED, PMSIGNAL_BEGIN_HOT_STANDBY, PMSIGNAL_ROTATE_LOGFILE, PMSIGNAL_START_AUTOVAC_LAUNCHER,
  PMSIGNAL_START_AUTOVAC_WORKER, PMSIGNAL_BACKGROUND_WORKER_CHANGE, PMSIGNAL_START_WALRECEIVER, PMSIGNAL_ADVANCE_STATE_MACHINE,
  NUM_PMSIGNALS
}
 
enum  QuitSignalReason { PMQUIT_NOT_SENT = 0, PMQUIT_FOR_CRASH, PMQUIT_FOR_STOP }
 

Functions

Size PMSignalShmemSize (void)
 
void PMSignalShmemInit (void)
 
void SendPostmasterSignal (PMSignalReason reason)
 
bool CheckPostmasterSignal (PMSignalReason reason)
 
void SetQuitSignalReason (QuitSignalReason reason)
 
QuitSignalReason GetQuitSignalReason (void)
 
int AssignPostmasterChildSlot (void)
 
bool ReleasePostmasterChildSlot (int slot)
 
bool IsPostmasterChildWalSender (int slot)
 
void MarkPostmasterChildActive (void)
 
void MarkPostmasterChildInactive (void)
 
void MarkPostmasterChildWalSender (void)
 
bool PostmasterIsAliveInternal (void)
 
void PostmasterDeathSignalInit (void)
 

Macro Definition Documentation

◆ PostmasterIsAlive

#define PostmasterIsAlive ( )    PostmasterIsAliveInternal()

Typedef Documentation

◆ PMSignalData

typedef struct PMSignalData PMSignalData

Definition at line 58 of file pmsignal.h.

Enumeration Type Documentation

◆ PMSignalReason

Enumerator
PMSIGNAL_RECOVERY_STARTED 
PMSIGNAL_BEGIN_HOT_STANDBY 
PMSIGNAL_ROTATE_LOGFILE 
PMSIGNAL_START_AUTOVAC_LAUNCHER 
PMSIGNAL_START_AUTOVAC_WORKER 
PMSIGNAL_BACKGROUND_WORKER_CHANGE 
PMSIGNAL_START_WALRECEIVER 
PMSIGNAL_ADVANCE_STATE_MACHINE 
NUM_PMSIGNALS 

Definition at line 33 of file pmsignal.h.

34 {
35  PMSIGNAL_RECOVERY_STARTED, /* recovery has started */
36  PMSIGNAL_BEGIN_HOT_STANDBY, /* begin Hot Standby */
37  PMSIGNAL_ROTATE_LOGFILE, /* send SIGUSR1 to syslogger to rotate logfile */
38  PMSIGNAL_START_AUTOVAC_LAUNCHER, /* start an autovacuum launcher */
39  PMSIGNAL_START_AUTOVAC_WORKER, /* start an autovacuum worker */
40  PMSIGNAL_BACKGROUND_WORKER_CHANGE, /* background worker state change */
41  PMSIGNAL_START_WALRECEIVER, /* start a walreceiver */
42  PMSIGNAL_ADVANCE_STATE_MACHINE, /* advance postmaster's state machine */
43 
44  NUM_PMSIGNALS /* Must be last value of enum! */
PMSignalReason
Definition: pmsignal.h:33

◆ QuitSignalReason

Enumerator
PMQUIT_NOT_SENT 
PMQUIT_FOR_CRASH 
PMQUIT_FOR_STOP 

Definition at line 50 of file pmsignal.h.

51 {
52  PMQUIT_NOT_SENT = 0, /* postmaster hasn't sent SIGQUIT */
53  PMQUIT_FOR_CRASH, /* some other backend bought the farm */
54  PMQUIT_FOR_STOP /* immediate stop was commanded */
QuitSignalReason
Definition: pmsignal.h:50

Function Documentation

◆ AssignPostmasterChildSlot()

int AssignPostmasterChildSlot ( void  )

Definition at line 219 of file pmsignal.c.

References elog, FATAL, PMSignalData::next_child_flag, PMSignalData::num_child_flags, PM_CHILD_ASSIGNED, PM_CHILD_UNUSED, PMSignalData::PMChildFlags, and PMSignalState.

Referenced by assign_backendlist_entry(), BackendStartup(), and StartAutovacuumWorker().

220 {
221  int slot = PMSignalState->next_child_flag;
222  int n;
223 
224  /*
225  * Scan for a free slot. We track the last slot assigned so as not to
226  * waste time repeatedly rescanning low-numbered slots.
227  */
228  for (n = PMSignalState->num_child_flags; n > 0; n--)
229  {
230  if (--slot < 0)
231  slot = PMSignalState->num_child_flags - 1;
233  {
236  return slot + 1;
237  }
238  }
239 
240  /* Out of slots ... should never happen, else postmaster.c messed up */
241  elog(FATAL, "no free slots in PMChildFlags array");
242  return 0; /* keep compiler quiet */
243 }
int next_child_flag
Definition: pmsignal.c:78
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition: pmsignal.c:82
#define PM_CHILD_ASSIGNED
Definition: pmsignal.c:65
int num_child_flags
Definition: pmsignal.c:77
#define FATAL
Definition: elog.h:49
#define PM_CHILD_UNUSED
Definition: pmsignal.c:64
#define elog(elevel,...)
Definition: elog.h:232
sig_atomic_t PMChildFlags[FLEXIBLE_ARRAY_MEMBER]
Definition: pmsignal.c:79

◆ CheckPostmasterSignal()

bool CheckPostmasterSignal ( PMSignalReason  reason)

Definition at line 170 of file pmsignal.c.

References PMSignalData::PMSignalFlags, and PMSignalState.

Referenced by sigusr1_handler().

171 {
172  /* Careful here --- don't clear flag if we haven't seen it set */
173  if (PMSignalState->PMSignalFlags[reason])
174  {
175  PMSignalState->PMSignalFlags[reason] = false;
176  return true;
177  }
178  return false;
179 }
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition: pmsignal.c:82
sig_atomic_t PMSignalFlags[NUM_PMSIGNALS]
Definition: pmsignal.c:73

◆ GetQuitSignalReason()

QuitSignalReason GetQuitSignalReason ( void  )

Definition at line 201 of file pmsignal.c.

References IsUnderPostmaster, PMQUIT_NOT_SENT, PMSignalState, and PMSignalData::sigquit_reason.

Referenced by quickdie().

202 {
203  /* This is called in signal handlers, so be extra paranoid. */
204  if (!IsUnderPostmaster || PMSignalState == NULL)
205  return PMQUIT_NOT_SENT;
207 }
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition: pmsignal.c:82
QuitSignalReason sigquit_reason
Definition: pmsignal.c:75
bool IsUnderPostmaster
Definition: globals.c:112

◆ IsPostmasterChildWalSender()

bool IsPostmasterChildWalSender ( int  slot)

Definition at line 275 of file pmsignal.c.

References Assert, PMSignalData::num_child_flags, PM_CHILD_WALSENDER, PMSignalData::PMChildFlags, and PMSignalState.

Referenced by CountChildren(), and SignalSomeChildren().

276 {
277  Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
278  slot--;
279 
281  return true;
282  else
283  return false;
284 }
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition: pmsignal.c:82
#define Assert(condition)
Definition: c.h:804
sig_atomic_t PMChildFlags[FLEXIBLE_ARRAY_MEMBER]
Definition: pmsignal.c:79
#define PM_CHILD_WALSENDER
Definition: pmsignal.c:67

◆ MarkPostmasterChildActive()

void MarkPostmasterChildActive ( void  )

Definition at line 291 of file pmsignal.c.

References Assert, MyPMChildSlot, PMSignalData::num_child_flags, PM_CHILD_ACTIVE, PM_CHILD_ASSIGNED, PMSignalData::PMChildFlags, and PMSignalState.

Referenced by InitProcess().

292 {
293  int slot = MyPMChildSlot;
294 
295  Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
296  slot--;
299 }
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition: pmsignal.c:82
#define PM_CHILD_ACTIVE
Definition: pmsignal.c:66
#define PM_CHILD_ASSIGNED
Definition: pmsignal.c:65
#define Assert(condition)
Definition: c.h:804
int MyPMChildSlot
Definition: globals.c:48
sig_atomic_t PMChildFlags[FLEXIBLE_ARRAY_MEMBER]
Definition: pmsignal.c:79

◆ MarkPostmasterChildInactive()

void MarkPostmasterChildInactive ( void  )

Definition at line 324 of file pmsignal.c.

References Assert, MyPMChildSlot, PMSignalData::num_child_flags, PM_CHILD_ACTIVE, PM_CHILD_ASSIGNED, PM_CHILD_WALSENDER, PMSignalData::PMChildFlags, and PMSignalState.

Referenced by ProcKill().

325 {
326  int slot = MyPMChildSlot;
327 
328  Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
329  slot--;
333 }
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition: pmsignal.c:82
#define PM_CHILD_ACTIVE
Definition: pmsignal.c:66
#define PM_CHILD_ASSIGNED
Definition: pmsignal.c:65
#define Assert(condition)
Definition: c.h:804
int MyPMChildSlot
Definition: globals.c:48
sig_atomic_t PMChildFlags[FLEXIBLE_ARRAY_MEMBER]
Definition: pmsignal.c:79
#define PM_CHILD_WALSENDER
Definition: pmsignal.c:67

◆ MarkPostmasterChildWalSender()

void MarkPostmasterChildWalSender ( void  )

Definition at line 307 of file pmsignal.c.

References am_walsender, Assert, MyPMChildSlot, PMSignalData::num_child_flags, PM_CHILD_ACTIVE, PM_CHILD_WALSENDER, PMSignalData::PMChildFlags, and PMSignalState.

308 {
309  int slot = MyPMChildSlot;
310 
312 
313  Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
314  slot--;
317 }
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition: pmsignal.c:82
#define PM_CHILD_ACTIVE
Definition: pmsignal.c:66
bool am_walsender
Definition: walsender.c:115
#define Assert(condition)
Definition: c.h:804
int MyPMChildSlot
Definition: globals.c:48
sig_atomic_t PMChildFlags[FLEXIBLE_ARRAY_MEMBER]
Definition: pmsignal.c:79
#define PM_CHILD_WALSENDER
Definition: pmsignal.c:67

◆ PMSignalShmemInit()

void PMSignalShmemInit ( void  )

Definition at line 134 of file pmsignal.c.

References MaxLivePostmasterChildren(), MemSet, PMSignalData::num_child_flags, PMSignalShmemSize(), PMSignalState, ShmemInitStruct(), and unvolatize.

Referenced by CreateSharedMemoryAndSemaphores().

135 {
136  bool found;
137 
139  ShmemInitStruct("PMSignalState", PMSignalShmemSize(), &found);
140 
141  if (!found)
142  {
143  /* initialize all flags to zeroes */
146  }
147 }
Size PMSignalShmemSize(void)
Definition: pmsignal.c:119
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition: pmsignal.c:82
#define MemSet(start, val, len)
Definition: c.h:1008
int num_child_flags
Definition: pmsignal.c:77
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
int MaxLivePostmasterChildren(void)
Definition: postmaster.c:5661
#define unvolatize(underlying_type, expr)
Definition: c.h:1245

◆ PMSignalShmemSize()

Size PMSignalShmemSize ( void  )

Definition at line 119 of file pmsignal.c.

References add_size(), MaxLivePostmasterChildren(), mul_size(), offsetof, and PMSignalData::PMChildFlags.

Referenced by CreateSharedMemoryAndSemaphores(), and PMSignalShmemInit().

120 {
121  Size size;
122 
123  size = offsetof(PMSignalData, PMChildFlags);
125  sizeof(sig_atomic_t)));
126 
127  return size;
128 }
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
int MaxLivePostmasterChildren(void)
Definition: postmaster.c:5661
size_t Size
Definition: c.h:540
#define offsetof(type, field)
Definition: c.h:727

◆ PostmasterDeathSignalInit()

void PostmasterDeathSignalInit ( void  )

Definition at line 405 of file pmsignal.c.

References elog, ERROR, and pqsignal().

Referenced by InitPostmasterChild().

406 {
407 #ifdef USE_POSTMASTER_DEATH_SIGNAL
408  int signum = POSTMASTER_DEATH_SIGNAL;
409 
410  /* Register our signal handler. */
411  pqsignal(signum, postmaster_death_handler);
412 
413  /* Request a signal on parent exit. */
414 #if defined(PR_SET_PDEATHSIG)
415  if (prctl(PR_SET_PDEATHSIG, signum) < 0)
416  elog(ERROR, "could not request parent death signal: %m");
417 #elif defined(PROC_PDEATHSIG_CTL)
418  if (procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signum) < 0)
419  elog(ERROR, "could not request parent death signal: %m");
420 #else
421 #error "USE_POSTMASTER_DEATH_SIGNAL set, but there is no mechanism to request the signal"
422 #endif
423 
424  /*
425  * Just in case the parent was gone already and we missed it, we'd better
426  * check the slow way on the first call.
427  */
428  postmaster_possibly_dead = true;
429 #endif /* USE_POSTMASTER_DEATH_SIGNAL */
430 }
#define ERROR
Definition: elog.h:46
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
#define elog(elevel,...)
Definition: elog.h:232

◆ PostmasterIsAliveInternal()

bool PostmasterIsAliveInternal ( void  )

Definition at line 344 of file pmsignal.c.

References EAGAIN, elog, EWOULDBLOCK, FATAL, postmaster_alive_fds, POSTMASTER_FD_WATCH, and read.

Referenced by WaitEventSetWait().

345 {
346 #ifdef USE_POSTMASTER_DEATH_SIGNAL
347  /*
348  * Reset the flag before checking, so that we don't miss a signal if
349  * postmaster dies right after the check. If postmaster was indeed dead,
350  * we'll re-arm it before returning to caller.
351  */
352  postmaster_possibly_dead = false;
353 #endif
354 
355 #ifndef WIN32
356  {
357  char c;
358  ssize_t rc;
359 
361 
362  /*
363  * In the usual case, the postmaster is still alive, and there is no
364  * data in the pipe.
365  */
366  if (rc < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
367  return true;
368  else
369  {
370  /*
371  * Postmaster is dead, or something went wrong with the read()
372  * call.
373  */
374 
375 #ifdef USE_POSTMASTER_DEATH_SIGNAL
376  postmaster_possibly_dead = true;
377 #endif
378 
379  if (rc < 0)
380  elog(FATAL, "read on postmaster death monitoring pipe failed: %m");
381  else if (rc > 0)
382  elog(FATAL, "unexpected data in postmaster death monitoring pipe");
383 
384  return false;
385  }
386  }
387 
388 #else /* WIN32 */
389  if (WaitForSingleObject(PostmasterHandle, 0) == WAIT_TIMEOUT)
390  return true;
391  else
392  {
393 #ifdef USE_POSTMASTER_DEATH_SIGNAL
394  postmaster_possibly_dead = true;
395 #endif
396  return false;
397  }
398 #endif /* WIN32 */
399 }
#define EAGAIN
Definition: win32_port.h:341
#define FATAL
Definition: elog.h:49
char * c
int postmaster_alive_fds[2]
Definition: postmaster.c:569
#define elog(elevel,...)
Definition: elog.h:232
#define EWOULDBLOCK
Definition: win32_port.h:349
#define POSTMASTER_FD_WATCH
Definition: postmaster.h:43
#define read(a, b, c)
Definition: win32.h:13

◆ ReleasePostmasterChildSlot()

bool ReleasePostmasterChildSlot ( int  slot)

Definition at line 253 of file pmsignal.c.

References Assert, PMSignalData::num_child_flags, PM_CHILD_ASSIGNED, PM_CHILD_UNUSED, PMSignalData::PMChildFlags, and PMSignalState.

Referenced by BackendStartup(), CleanupBackend(), CleanupBackgroundWorker(), do_start_bgworker(), HandleChildCrash(), and StartAutovacuumWorker().

254 {
255  bool result;
256 
257  Assert(slot > 0 && slot <= PMSignalState->num_child_flags);
258  slot--;
259 
260  /*
261  * Note: the slot state might already be unused, because the logic in
262  * postmaster.c is such that this might get called twice when a child
263  * crashes. So we don't try to Assert anything about the state.
264  */
265  result = (PMSignalState->PMChildFlags[slot] == PM_CHILD_ASSIGNED);
267  return result;
268 }
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition: pmsignal.c:82
#define PM_CHILD_ASSIGNED
Definition: pmsignal.c:65
#define Assert(condition)
Definition: c.h:804
#define PM_CHILD_UNUSED
Definition: pmsignal.c:64
sig_atomic_t PMChildFlags[FLEXIBLE_ARRAY_MEMBER]
Definition: pmsignal.c:79

◆ SendPostmasterSignal()

void SendPostmasterSignal ( PMSignalReason  reason)

Definition at line 153 of file pmsignal.c.

References IsUnderPostmaster, kill, PMSignalData::PMSignalFlags, PMSignalState, PostmasterPid, and SIGUSR1.

Referenced by AutoVacLauncherMain(), CheckRecoveryConsistency(), do_start_worker(), GetNewMultiXactId(), GetNewTransactionId(), pg_rotate_logfile(), pg_rotate_logfile_v2(), RegisterDynamicBackgroundWorker(), RequestXLogStreaming(), SetMultiXactIdLimit(), SetTransactionIdLimit(), StartupXLOG(), and TerminateBackgroundWorker().

154 {
155  /* If called in a standalone backend, do nothing */
156  if (!IsUnderPostmaster)
157  return;
158  /* Atomically set the proper flag */
159  PMSignalState->PMSignalFlags[reason] = true;
160  /* Send signal to postmaster */
162 }
#define SIGUSR1
Definition: win32_port.h:171
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition: pmsignal.c:82
#define kill(pid, sig)
Definition: win32_port.h:454
bool IsUnderPostmaster
Definition: globals.c:112
pid_t PostmasterPid
Definition: globals.c:98
sig_atomic_t PMSignalFlags[NUM_PMSIGNALS]
Definition: pmsignal.c:73

◆ SetQuitSignalReason()

void SetQuitSignalReason ( QuitSignalReason  reason)

Definition at line 190 of file pmsignal.c.

References PMSignalState, and PMSignalData::sigquit_reason.

Referenced by HandleChildCrash(), and pmdie().

191 {
192  PMSignalState->sigquit_reason = reason;
193 }
NON_EXEC_STATIC volatile PMSignalData * PMSignalState
Definition: pmsignal.c:82
QuitSignalReason sigquit_reason
Definition: pmsignal.c:75