PostgreSQL Source Code  git master
startup.c File Reference
#include "postgres.h"
#include "access/xlog.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/interrupt.h"
#include "postmaster/startup.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pmsignal.h"
#include "storage/procsignal.h"
#include "storage/standby.h"
#include "utils/guc.h"
#include "utils/timeout.h"
Include dependency graph for startup.c:

Go to the source code of this file.

Functions

static void StartupProcTriggerHandler (SIGNAL_ARGS)
 
static void StartupProcSigHupHandler (SIGNAL_ARGS)
 
static void StartupProcShutdownHandler (SIGNAL_ARGS)
 
void HandleStartupProcInterrupts (void)
 
void StartupProcessMain (void)
 
void PreRestoreCommand (void)
 
void PostRestoreCommand (void)
 
bool IsPromoteTriggered (void)
 
void ResetPromoteTriggered (void)
 

Variables

static volatile sig_atomic_t got_SIGHUP = false
 
static volatile sig_atomic_t shutdown_requested = false
 
static volatile sig_atomic_t promote_triggered = false
 
static volatile sig_atomic_t in_restore_command = false
 

Function Documentation

◆ HandleStartupProcInterrupts()

void HandleStartupProcInterrupts ( void  )

Definition at line 101 of file startup.c.

References got_SIGHUP, IsUnderPostmaster, PGC_SIGHUP, PostmasterIsAlive, proc_exit(), ProcessConfigFile(), ProcessProcSignalBarrier(), ProcSignalBarrierPending, and shutdown_requested.

Referenced by recoveryApplyDelay(), recoveryPausesHere(), ShutdownWalRcv(), StartupXLOG(), and WaitForWALToBecomeAvailable().

102 {
103  /*
104  * Check if we were requested to re-read config file.
105  */
106  if (got_SIGHUP)
107  {
108  got_SIGHUP = false;
110  }
111 
112  /*
113  * Check if we were requested to exit without finishing recovery.
114  */
115  if (shutdown_requested)
116  proc_exit(1);
117 
118  /*
119  * Emergency bailout if postmaster has died. This is to avoid the
120  * necessity for manual cleanup of all postmaster children.
121  */
123  exit(1);
124 
125  /* Process barrier events */
128 }
void ProcessConfigFile(GucContext context)
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:429
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:41
void proc_exit(int code)
Definition: ipc.c:104
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:40
bool IsUnderPostmaster
Definition: globals.c:109
#define PostmasterIsAlive()
Definition: pmsignal.h:91
Definition: guc.h:72
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:35

◆ IsPromoteTriggered()

bool IsPromoteTriggered ( void  )

Definition at line 200 of file startup.c.

References promote_triggered.

Referenced by CheckForStandbyTrigger().

201 {
202  return promote_triggered;
203 }
static volatile sig_atomic_t promote_triggered
Definition: startup.c:42

◆ PostRestoreCommand()

void PostRestoreCommand ( void  )

Definition at line 194 of file startup.c.

References in_restore_command.

Referenced by RestoreArchivedFile().

195 {
196  in_restore_command = false;
197 }
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48

◆ PreRestoreCommand()

void PreRestoreCommand ( void  )

Definition at line 180 of file startup.c.

References in_restore_command, proc_exit(), and shutdown_requested.

Referenced by RestoreArchivedFile().

181 {
182  /*
183  * Set in_restore_command to tell the signal handler that we should exit
184  * right away on SIGTERM. We know that we're at a safe point to do that.
185  * Check if we had already received the signal, so that we don't miss a
186  * shutdown request received just before this.
187  */
188  in_restore_command = true;
189  if (shutdown_requested)
190  proc_exit(1);
191 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:41
void proc_exit(int code)
Definition: ipc.c:104
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48

◆ ResetPromoteTriggered()

void ResetPromoteTriggered ( void  )

Definition at line 206 of file startup.c.

References promote_triggered.

Referenced by CheckForStandbyTrigger().

207 {
208  promote_triggered = false;
209 }
static volatile sig_atomic_t promote_triggered
Definition: startup.c:42

◆ StartupProcessMain()

void StartupProcessMain ( void  )

Definition at line 136 of file startup.c.

References InitializeTimeouts(), PG_SETMASK, pqsignal(), proc_exit(), procsignal_sigusr1_handler(), RegisterTimeout(), SIG_DFL, SIG_IGN, SIGCHLD, SIGHUP, SignalHandlerForCrashExit(), SIGPIPE, SIGQUIT, SIGUSR1, SIGUSR2, STANDBY_DEADLOCK_TIMEOUT, STANDBY_LOCK_TIMEOUT, STANDBY_TIMEOUT, StandbyDeadLockHandler(), StandbyLockTimeoutHandler(), StandbyTimeoutHandler(), StartupProcShutdownHandler(), StartupProcSigHupHandler(), StartupProcTriggerHandler(), StartupXLOG(), and UnBlockSig.

Referenced by AuxiliaryProcessMain().

137 {
138  /*
139  * Properly accept or ignore signals the postmaster might send us.
140  */
141  pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
142  pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
143  pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
145  InitializeTimeouts(); /* establishes SIGALRM handler */
149 
150  /*
151  * Reset some signals that are accepted by postmaster but not here
152  */
154 
155  /*
156  * Register timeouts needed for standby mode
157  */
161 
162  /*
163  * Unblock signals (they were blocked when the postmaster forked us)
164  */
166 
167  /*
168  * Do what we came for.
169  */
170  StartupXLOG();
171 
172  /*
173  * Exit normally. Exit code 0 tells postmaster that we completed recovery
174  * successfully.
175  */
176  proc_exit(0);
177 }
void InitializeTimeouts(void)
Definition: timeout.c:346
#define SIGQUIT
Definition: win32_port.h:155
void StandbyTimeoutHandler(void)
Definition: standby.c:585
#define SIGUSR1
Definition: win32_port.h:166
#define SIGCHLD
Definition: win32_port.h:164
void proc_exit(int code)
Definition: ipc.c:104
#define SIGPIPE
Definition: win32_port.h:159
#define SIGUSR2
Definition: win32_port.h:167
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void SignalHandlerForCrashExit(SIGNAL_ARGS)
Definition: interrupt.c:72
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:74
#define SIGHUP
Definition: win32_port.h:154
void StandbyLockTimeoutHandler(void)
Definition: standby.c:598
void StandbyDeadLockHandler(void)
Definition: standby.c:574
sigset_t UnBlockSig
Definition: pqsignal.c:22
void StartupXLOG(void)
Definition: xlog.c:6198
#define SIG_IGN
Definition: win32_port.h:151
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:380
#define SIG_DFL
Definition: win32_port.h:149
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:62
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:86
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:533

◆ StartupProcShutdownHandler()

static void StartupProcShutdownHandler ( SIGNAL_ARGS  )
static

Definition at line 86 of file startup.c.

References in_restore_command, proc_exit(), shutdown_requested, and WakeupRecovery().

Referenced by StartupProcessMain().

87 {
88  int save_errno = errno;
89 
91  proc_exit(1);
92  else
93  shutdown_requested = true;
95 
96  errno = save_errno;
97 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:41
void proc_exit(int code)
Definition: ipc.c:104
void WakeupRecovery(void)
Definition: xlog.c:12302
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48

◆ StartupProcSigHupHandler()

static void StartupProcSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 74 of file startup.c.

References got_SIGHUP, and WakeupRecovery().

Referenced by StartupProcessMain().

75 {
76  int save_errno = errno;
77 
78  got_SIGHUP = true;
80 
81  errno = save_errno;
82 }
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:40
void WakeupRecovery(void)
Definition: xlog.c:12302

◆ StartupProcTriggerHandler()

static void StartupProcTriggerHandler ( SIGNAL_ARGS  )
static

Definition at line 62 of file startup.c.

References promote_triggered, and WakeupRecovery().

Referenced by StartupProcessMain().

63 {
64  int save_errno = errno;
65 
66  promote_triggered = true;
68 
69  errno = save_errno;
70 }
void WakeupRecovery(void)
Definition: xlog.c:12302
static volatile sig_atomic_t promote_triggered
Definition: startup.c:42

Variable Documentation

◆ got_SIGHUP

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 40 of file startup.c.

Referenced by HandleStartupProcInterrupts(), and StartupProcSigHupHandler().

◆ in_restore_command

volatile sig_atomic_t in_restore_command = false
static

Definition at line 48 of file startup.c.

Referenced by PostRestoreCommand(), PreRestoreCommand(), and StartupProcShutdownHandler().

◆ promote_triggered

volatile sig_atomic_t promote_triggered = false
static

Definition at line 42 of file startup.c.

Referenced by IsPromoteTriggered(), ResetPromoteTriggered(), and StartupProcTriggerHandler().

◆ shutdown_requested

volatile sig_atomic_t shutdown_requested = false
static