PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

void HandleStartupProcInterrupts ( void  )

Definition at line 148 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().

149 {
150  /*
151  * Check if we were requested to re-read config file.
152  */
153  if (got_SIGHUP)
154  {
155  got_SIGHUP = false;
157  }
158 
159  /*
160  * Check if we were requested to exit without finishing recovery.
161  */
162  if (shutdown_requested)
163  proc_exit(1);
164 
165  /*
166  * Emergency bailout if postmaster has died. This is to avoid the
167  * necessity for manual cleanup of all postmaster children.
168  */
170  exit(1);
171 }
void ProcessConfigFile(GucContext context)
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:42
void proc_exit(int code)
Definition: ipc.c:99
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:41
bool IsUnderPostmaster
Definition: globals.c:100
bool PostmasterIsAlive(void)
Definition: pmsignal.c:272
Definition: guc.h:72
bool IsPromoteTriggered ( void  )

Definition at line 247 of file startup.c.

References promote_triggered.

Referenced by CheckForStandbyTrigger().

248 {
249  return promote_triggered;
250 }
static volatile sig_atomic_t promote_triggered
Definition: startup.c:43
void PostRestoreCommand ( void  )

Definition at line 241 of file startup.c.

References in_restore_command.

Referenced by RestoreArchivedFile().

242 {
243  in_restore_command = false;
244 }
static volatile sig_atomic_t in_restore_command
Definition: startup.c:49
void PreRestoreCommand ( void  )

Definition at line 227 of file startup.c.

References in_restore_command, proc_exit(), and shutdown_requested.

Referenced by RestoreArchivedFile().

228 {
229  /*
230  * Set in_restore_command to tell the signal handler that we should exit
231  * right away on SIGTERM. We know that we're at a safe point to do that.
232  * Check if we had already received the signal, so that we don't miss a
233  * shutdown request received just before this.
234  */
235  in_restore_command = true;
236  if (shutdown_requested)
237  proc_exit(1);
238 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:42
void proc_exit(int code)
Definition: ipc.c:99
static volatile sig_atomic_t in_restore_command
Definition: startup.c:49
void ResetPromoteTriggered ( void  )

Definition at line 253 of file startup.c.

References promote_triggered.

Referenced by CheckForStandbyTrigger().

254 {
255  promote_triggered = false;
256 }
static volatile sig_atomic_t promote_triggered
Definition: startup.c:43
static void startupproc_quickdie ( SIGNAL_ARGS  )
static

Definition at line 70 of file startup.c.

References BlockSig, on_exit_reset(), and PG_SETMASK.

Referenced by StartupProcessMain().

71 {
73 
74  /*
75  * We DO NOT want to run proc_exit() callbacks -- we're here because
76  * shared memory may be corrupted, so we don't want to try to clean up our
77  * transaction. Just nail the windows shut and get out of town. Now that
78  * there's an atexit callback to prevent third-party code from breaking
79  * things by calling exit() directly, we have to reset the callbacks
80  * explicitly to make this work as intended.
81  */
82  on_exit_reset();
83 
84  /*
85  * Note we do exit(2) not exit(0). This is to force the postmaster into a
86  * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
87  * backend. This is necessary precisely because we don't clean up our
88  * shared memory state. (The "dead man switch" mechanism in pmsignal.c
89  * should ensure the postmaster sees this as a crash, too, but no harm in
90  * being doubly sure.)
91  */
92  exit(2);
93 }
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void on_exit_reset(void)
Definition: ipc.c:396
sigset_t BlockSig
Definition: pqsignal.c:22
void StartupProcessMain ( void  )

Definition at line 179 of file startup.c.

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

Referenced by AuxiliaryProcessMain().

180 {
181  /*
182  * Properly accept or ignore signals the postmaster might send us.
183  */
184  pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
185  pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
186  pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
187  pqsignal(SIGQUIT, startupproc_quickdie); /* hard crash time */
188  InitializeTimeouts(); /* establishes SIGALRM handler */
192 
193  /*
194  * Reset some signals that are accepted by postmaster but not here
195  */
201 
202  /*
203  * Register timeouts needed for standby mode
204  */
208 
209  /*
210  * Unblock signals (they were blocked when the postmaster forked us)
211  */
213 
214  /*
215  * Do what we came for.
216  */
217  StartupXLOG();
218 
219  /*
220  * Exit normally. Exit code 0 tells postmaster that we completed recovery
221  * successfully.
222  */
223  proc_exit(0);
224 }
void InitializeTimeouts(void)
Definition: timeout.c:340
#define SIGUSR1
Definition: win32.h:202
void StandbyTimeoutHandler(void)
Definition: standby.c:558
static void startupproc_quickdie(SIGNAL_ARGS)
Definition: startup.c:70
#define SIGCONT
Definition: win32.h:197
#define SIGWINCH
Definition: win32.h:201
#define SIGTTIN
Definition: win32.h:199
void proc_exit(int code)
Definition: ipc.c:99
#define SIGQUIT
Definition: win32.h:189
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
#define SIG_IGN
Definition: win32.h:185
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:121
void StandbyLockTimeoutHandler(void)
Definition: standby.c:571
void StandbyDeadLockHandler(void)
Definition: standby.c:547
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void StartupProcSigUsr1Handler(SIGNAL_ARGS)
Definition: startup.c:98
void StartupXLOG(void)
Definition: xlog.c:6189
#define SIGPIPE
Definition: win32.h:193
#define SIGHUP
Definition: win32.h:188
#define SIG_DFL
Definition: win32.h:183
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:373
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:109
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:133
#define SIGTTOU
Definition: win32.h:200
#define SIGCHLD
Definition: win32.h:198
#define SIGUSR2
Definition: win32.h:203
static void StartupProcShutdownHandler ( SIGNAL_ARGS  )
static

Definition at line 133 of file startup.c.

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

Referenced by StartupProcessMain().

134 {
135  int save_errno = errno;
136 
137  if (in_restore_command)
138  proc_exit(1);
139  else
140  shutdown_requested = true;
141  WakeupRecovery();
142 
143  errno = save_errno;
144 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:42
void proc_exit(int code)
Definition: ipc.c:99
void WakeupRecovery(void)
Definition: xlog.c:12092
static volatile sig_atomic_t in_restore_command
Definition: startup.c:49
static void StartupProcSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 121 of file startup.c.

References got_SIGHUP, and WakeupRecovery().

Referenced by StartupProcessMain().

122 {
123  int save_errno = errno;
124 
125  got_SIGHUP = true;
126  WakeupRecovery();
127 
128  errno = save_errno;
129 }
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:41
void WakeupRecovery(void)
Definition: xlog.c:12092
static void StartupProcSigUsr1Handler ( SIGNAL_ARGS  )
static

Definition at line 98 of file startup.c.

References latch_sigusr1_handler().

Referenced by StartupProcessMain().

99 {
100  int save_errno = errno;
101 
103 
104  errno = save_errno;
105 }
void latch_sigusr1_handler(void)
Definition: latch.c:1467
static void StartupProcTriggerHandler ( SIGNAL_ARGS  )
static

Definition at line 109 of file startup.c.

References promote_triggered, and WakeupRecovery().

Referenced by StartupProcessMain().

110 {
111  int save_errno = errno;
112 
113  promote_triggered = true;
114  WakeupRecovery();
115 
116  errno = save_errno;
117 }
void WakeupRecovery(void)
Definition: xlog.c:12092
static volatile sig_atomic_t promote_triggered
Definition: startup.c:43

Variable Documentation

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 41 of file startup.c.

Referenced by HandleStartupProcInterrupts(), and StartupProcSigHupHandler().

volatile sig_atomic_t in_restore_command = false
static

Definition at line 49 of file startup.c.

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

volatile sig_atomic_t promote_triggered = false
static

Definition at line 43 of file startup.c.

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

volatile sig_atomic_t shutdown_requested = false
static