PostgreSQL Source Code  git master
startup.c File Reference
#include "postgres.h"
#include <signal.h>
#include <unistd.h>
#include "access/xlog.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/startup.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pmsignal.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 startupproc_quickdie (SIGNAL_ARGS)
 
static void StartupProcSigUsr1Handler (SIGNAL_ARGS)
 
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 142 of file startup.c.

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

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

143 {
144  /*
145  * Check if we were requested to re-read config file.
146  */
147  if (got_SIGHUP)
148  {
149  got_SIGHUP = false;
151  }
152 
153  /*
154  * Check if we were requested to exit without finishing recovery.
155  */
156  if (shutdown_requested)
157  proc_exit(1);
158 
159  /*
160  * Emergency bailout if postmaster has died. This is to avoid the
161  * necessity for manual cleanup of all postmaster children.
162  */
164  exit(1);
165 }
void ProcessConfigFile(GucContext context)
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:42
void proc_exit(int code)
Definition: ipc.c:104
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:41
bool IsUnderPostmaster
Definition: globals.c:109
#define PostmasterIsAlive()
Definition: pmsignal.h:91
Definition: guc.h:72

◆ IsPromoteTriggered()

bool IsPromoteTriggered ( void  )

Definition at line 237 of file startup.c.

References promote_triggered.

Referenced by CheckForStandbyTrigger().

238 {
239  return promote_triggered;
240 }
static volatile sig_atomic_t promote_triggered
Definition: startup.c:43

◆ PostRestoreCommand()

void PostRestoreCommand ( void  )

Definition at line 231 of file startup.c.

References in_restore_command.

Referenced by RestoreArchivedFile().

232 {
233  in_restore_command = false;
234 }
static volatile sig_atomic_t in_restore_command
Definition: startup.c:49

◆ PreRestoreCommand()

void PreRestoreCommand ( void  )

Definition at line 217 of file startup.c.

References in_restore_command, proc_exit(), and shutdown_requested.

Referenced by RestoreArchivedFile().

218 {
219  /*
220  * Set in_restore_command to tell the signal handler that we should exit
221  * right away on SIGTERM. We know that we're at a safe point to do that.
222  * Check if we had already received the signal, so that we don't miss a
223  * shutdown request received just before this.
224  */
225  in_restore_command = true;
226  if (shutdown_requested)
227  proc_exit(1);
228 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:42
void proc_exit(int code)
Definition: ipc.c:104
static volatile sig_atomic_t in_restore_command
Definition: startup.c:49

◆ ResetPromoteTriggered()

void ResetPromoteTriggered ( void  )

Definition at line 243 of file startup.c.

References promote_triggered.

Referenced by CheckForStandbyTrigger().

244 {
245  promote_triggered = false;
246 }
static volatile sig_atomic_t promote_triggered
Definition: startup.c:43

◆ startupproc_quickdie()

static void startupproc_quickdie ( SIGNAL_ARGS  )
static

Definition at line 70 of file startup.c.

Referenced by StartupProcessMain().

71 {
72  /*
73  * We DO NOT want to run proc_exit() or atexit() callbacks -- we're here
74  * because shared memory may be corrupted, so we don't want to try to
75  * clean up our transaction. Just nail the windows shut and get out of
76  * town. The callbacks wouldn't be safe to run from a signal handler,
77  * anyway.
78  *
79  * Note we do _exit(2) not _exit(0). This is to force the postmaster into
80  * a system reset cycle if someone sends a manual SIGQUIT to a random
81  * backend. This is necessary precisely because we don't clean up our
82  * shared memory state. (The "dead man switch" mechanism in pmsignal.c
83  * should ensure the postmaster sees this as a crash, too, but no harm in
84  * being doubly sure.)
85  */
86  _exit(2);
87 }

◆ StartupProcessMain()

void StartupProcessMain ( void  )

Definition at line 173 of file startup.c.

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

Referenced by AuxiliaryProcessMain().

174 {
175  /*
176  * Properly accept or ignore signals the postmaster might send us.
177  */
178  pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
179  pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
180  pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
181  pqsignal(SIGQUIT, startupproc_quickdie); /* hard crash time */
182  InitializeTimeouts(); /* establishes SIGALRM handler */
186 
187  /*
188  * Reset some signals that are accepted by postmaster but not here
189  */
191 
192  /*
193  * Register timeouts needed for standby mode
194  */
198 
199  /*
200  * Unblock signals (they were blocked when the postmaster forked us)
201  */
203 
204  /*
205  * Do what we came for.
206  */
207  StartupXLOG();
208 
209  /*
210  * Exit normally. Exit code 0 tells postmaster that we completed recovery
211  * successfully.
212  */
213  proc_exit(0);
214 }
void InitializeTimeouts(void)
Definition: timeout.c:340
#define SIGQUIT
Definition: win32_port.h:164
void StandbyTimeoutHandler(void)
Definition: standby.c:585
static void startupproc_quickdie(SIGNAL_ARGS)
Definition: startup.c:70
#define SIGUSR1
Definition: win32_port.h:175
#define SIGCHLD
Definition: win32_port.h:173
void proc_exit(int code)
Definition: ipc.c:104
#define SIGPIPE
Definition: win32_port.h:168
#define SIGUSR2
Definition: win32_port.h:176
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:115
#define SIGHUP
Definition: win32_port.h:163
void StandbyLockTimeoutHandler(void)
Definition: standby.c:598
void StandbyDeadLockHandler(void)
Definition: standby.c:574
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void StartupProcSigUsr1Handler(SIGNAL_ARGS)
Definition: startup.c:92
void StartupXLOG(void)
Definition: xlog.c:6195
#define SIG_IGN
Definition: win32_port.h:160
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:373
#define SIG_DFL
Definition: win32_port.h:158
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:103
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:127

◆ StartupProcShutdownHandler()

static void StartupProcShutdownHandler ( SIGNAL_ARGS  )
static

Definition at line 127 of file startup.c.

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

Referenced by StartupProcessMain().

128 {
129  int save_errno = errno;
130 
131  if (in_restore_command)
132  proc_exit(1);
133  else
134  shutdown_requested = true;
135  WakeupRecovery();
136 
137  errno = save_errno;
138 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:42
void proc_exit(int code)
Definition: ipc.c:104
void WakeupRecovery(void)
Definition: xlog.c:12225
static volatile sig_atomic_t in_restore_command
Definition: startup.c:49

◆ StartupProcSigHupHandler()

static void StartupProcSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 115 of file startup.c.

References got_SIGHUP, and WakeupRecovery().

Referenced by StartupProcessMain().

116 {
117  int save_errno = errno;
118 
119  got_SIGHUP = true;
120  WakeupRecovery();
121 
122  errno = save_errno;
123 }
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:41
void WakeupRecovery(void)
Definition: xlog.c:12225

◆ StartupProcSigUsr1Handler()

static void StartupProcSigUsr1Handler ( SIGNAL_ARGS  )
static

Definition at line 92 of file startup.c.

References latch_sigusr1_handler().

Referenced by StartupProcessMain().

93 {
94  int save_errno = errno;
95 
97 
98  errno = save_errno;
99 }
void latch_sigusr1_handler(void)
Definition: latch.c:1515

◆ StartupProcTriggerHandler()

static void StartupProcTriggerHandler ( SIGNAL_ARGS  )
static

Definition at line 103 of file startup.c.

References promote_triggered, and WakeupRecovery().

Referenced by StartupProcessMain().

104 {
105  int save_errno = errno;
106 
107  promote_triggered = true;
108  WakeupRecovery();
109 
110  errno = save_errno;
111 }
void WakeupRecovery(void)
Definition: xlog.c:12225
static volatile sig_atomic_t promote_triggered
Definition: startup.c:43

Variable Documentation

◆ got_SIGHUP

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 41 of file startup.c.

Referenced by HandleStartupProcInterrupts(), and StartupProcSigHupHandler().

◆ in_restore_command

volatile sig_atomic_t in_restore_command = false
static

Definition at line 49 of file startup.c.

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

◆ promote_triggered

volatile sig_atomic_t promote_triggered = false
static

Definition at line 43 of file startup.c.

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

◆ shutdown_requested

volatile sig_atomic_t shutdown_requested = false
static