PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
startup.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * startup.c
4  *
5  * The Startup process initialises the server and performs any recovery
6  * actions that have been specified. Notice that there is no "main loop"
7  * since the Startup process ends as soon as initialisation is complete.
8  * (in standby mode, one can think of the replay loop as a main loop,
9  * though.)
10  *
11  *
12  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
13  *
14  *
15  * IDENTIFICATION
16  * src/backend/postmaster/startup.c
17  *
18  *-------------------------------------------------------------------------
19  */
20 #include "postgres.h"
21 
22 #include <signal.h>
23 #include <unistd.h>
24 
25 #include "access/xlog.h"
26 #include "libpq/pqsignal.h"
27 #include "miscadmin.h"
28 #include "pgstat.h"
29 #include "postmaster/startup.h"
30 #include "storage/ipc.h"
31 #include "storage/latch.h"
32 #include "storage/pmsignal.h"
33 #include "storage/standby.h"
34 #include "utils/guc.h"
35 #include "utils/timeout.h"
36 
37 
38 /*
39  * Flags set by interrupt handlers for later service in the redo loop.
40  */
41 static volatile sig_atomic_t got_SIGHUP = false;
42 static volatile sig_atomic_t shutdown_requested = false;
43 static volatile sig_atomic_t promote_triggered = false;
44 
45 /*
46  * Flag set when executing a restore command, to tell SIGTERM signal handler
47  * that it's safe to just proc_exit.
48  */
49 static volatile sig_atomic_t in_restore_command = false;
50 
51 /* Signal handlers */
56 
57 
58 /* --------------------------------
59  * signal handler routines
60  * --------------------------------
61  */
62 
63 /*
64  * startupproc_quickdie() occurs when signalled SIGQUIT by the postmaster.
65  *
66  * Some backend has bought the farm,
67  * so we need to stop what we're doing and exit.
68  */
69 static void
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 }
94 
95 
96 /* SIGUSR1: let latch facility handle the signal */
97 static void
99 {
100  int save_errno = errno;
101 
103 
104  errno = save_errno;
105 }
106 
107 /* SIGUSR2: set flag to finish recovery */
108 static void
110 {
111  int save_errno = errno;
112 
113  promote_triggered = true;
114  WakeupRecovery();
115 
116  errno = save_errno;
117 }
118 
119 /* SIGHUP: set flag to re-read config file at next convenient time */
120 static void
122 {
123  int save_errno = errno;
124 
125  got_SIGHUP = true;
126  WakeupRecovery();
127 
128  errno = save_errno;
129 }
130 
131 /* SIGTERM: set flag to abort redo and exit */
132 static void
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 }
145 
146 /* Handle SIGHUP and SIGTERM signals of startup process */
147 void
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 }
172 
173 
174 /* ----------------------------------
175  * Startup Process main entry point
176  * ----------------------------------
177  */
178 void
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 }
225 
226 void
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 }
239 
240 void
242 {
243  in_restore_command = false;
244 }
245 
246 bool
248 {
249  return promote_triggered;
250 }
251 
252 void
254 {
255  promote_triggered = false;
256 }
void InitializeTimeouts(void)
Definition: timeout.c:340
#define SIGUSR1
Definition: win32.h:202
void ResetPromoteTriggered(void)
Definition: startup.c:253
void StandbyTimeoutHandler(void)
Definition: standby.c:558
static void startupproc_quickdie(SIGNAL_ARGS)
Definition: startup.c:70
#define SIGCONT
Definition: win32.h:197
void ProcessConfigFile(GucContext context)
#define SIGWINCH
Definition: win32.h:201
void StartupProcessMain(void)
Definition: startup.c:179
#define SIGTTIN
Definition: win32.h:199
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:42
void proc_exit(int code)
Definition: ipc.c:99
#define SIGQUIT
Definition: win32.h:189
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:41
#define SIG_IGN
Definition: win32.h:185
void PostRestoreCommand(void)
Definition: startup.c:241
void on_exit_reset(void)
Definition: ipc.c:396
void WakeupRecovery(void)
Definition: xlog.c:12195
static volatile sig_atomic_t in_restore_command
Definition: startup.c:49
static volatile sig_atomic_t promote_triggered
Definition: startup.c:43
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:121
bool IsUnderPostmaster
Definition: globals.c:101
bool PostmasterIsAlive(void)
Definition: pmsignal.c:272
void StandbyLockTimeoutHandler(void)
Definition: standby.c:571
void StandbyDeadLockHandler(void)
Definition: standby.c:547
bool IsPromoteTriggered(void)
Definition: startup.c:247
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void StartupProcSigUsr1Handler(SIGNAL_ARGS)
Definition: startup.c:98
void StartupXLOG(void)
Definition: xlog.c:6282
Definition: guc.h:72
sigset_t BlockSig
Definition: pqsignal.c:22
#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
#define SIGNAL_ARGS
Definition: c.h:1068
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:133
#define SIGTTOU
Definition: win32.h:200
void PreRestoreCommand(void)
Definition: startup.c:227
void HandleStartupProcInterrupts(void)
Definition: startup.c:148
void latch_sigusr1_handler(void)
Definition: latch.c:1473
#define SIGCHLD
Definition: win32.h:198
#define SIGUSR2
Definition: win32.h:203