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-2021, PostgreSQL Global Development Group
13 *
14 *
15 * IDENTIFICATION
16 * src/backend/postmaster/startup.c
17 *
18 *-------------------------------------------------------------------------
19 */
20 #include "postgres.h"
21
22 #include "access/xlog.h"
23 #include "libpq/pqsignal.h"
24 #include "miscadmin.h"
25 #include "pgstat.h"
26 #include "postmaster/interrupt.h"
27 #include "postmaster/startup.h"
28 #include "storage/ipc.h"
29 #include "storage/latch.h"
30 #include "storage/pmsignal.h"
31 #include "storage/procsignal.h"
32 #include "storage/standby.h"
33 #include "utils/guc.h"
34 #include "utils/timeout.h"
35
36
37 #ifndef USE_POSTMASTER_DEATH_SIGNAL
38 /*
39 * On systems that need to make a system call to find out if the postmaster has
40 * gone away, we'll do so only every Nth call to HandleStartupProcInterrupts().
41 * This only affects how long it takes us to detect the condition while we're
42 * busy replaying WAL. Latch waits and similar which should react immediately
43 * through the usual techniques.
44 */
45 #define POSTMASTER_POLL_RATE_LIMIT 1024
46 #endif
47
48 /*
49 * Flags set by interrupt handlers for later service in the redo loop.
50 */
51 static volatile sig_atomic_t got_SIGHUP = false;
52 static volatile sig_atomic_t shutdown_requested = false;
53 static volatile sig_atomic_t promote_signaled = false;
54
55 /*
56 * Flag set when executing a restore command, to tell SIGTERM signal handler
57 * that it's safe to just proc_exit.
58 */
59 static volatile sig_atomic_t in_restore_command = false;
60
61 /* Signal handlers */
62 static void StartupProcTriggerHandler(SIGNAL_ARGS);
63 static void StartupProcSigHupHandler(SIGNAL_ARGS);
64
65 /* Callbacks */
66 static void StartupProcExit(int code, Datum arg);
67
68
69 /* --------------------------------
70 * signal handler routines
71 * --------------------------------
72 */
73
74 /* SIGUSR2: set flag to finish recovery */
75 static void
StartupProcTriggerHandler(SIGNAL_ARGS)76 StartupProcTriggerHandler(SIGNAL_ARGS)
77 {
78 int save_errno = errno;
79
80 promote_signaled = true;
81 WakeupRecovery();
82
83 errno = save_errno;
84 }
85
86 /* SIGHUP: set flag to re-read config file at next convenient time */
87 static void
StartupProcSigHupHandler(SIGNAL_ARGS)88 StartupProcSigHupHandler(SIGNAL_ARGS)
89 {
90 int save_errno = errno;
91
92 got_SIGHUP = true;
93 WakeupRecovery();
94
95 errno = save_errno;
96 }
97
98 /* SIGTERM: set flag to abort redo and exit */
99 static void
StartupProcShutdownHandler(SIGNAL_ARGS)100 StartupProcShutdownHandler(SIGNAL_ARGS)
101 {
102 int save_errno = errno;
103
104 if (in_restore_command)
105 proc_exit(1);
106 else
107 shutdown_requested = true;
108 WakeupRecovery();
109
110 errno = save_errno;
111 }
112
113 /*
114 * Re-read the config file.
115 *
116 * If one of the critical walreceiver options has changed, flag xlog.c
117 * to restart it.
118 */
119 static void
StartupRereadConfig(void)120 StartupRereadConfig(void)
121 {
122 char *conninfo = pstrdup(PrimaryConnInfo);
123 char *slotname = pstrdup(PrimarySlotName);
124 bool tempSlot = wal_receiver_create_temp_slot;
125 bool conninfoChanged;
126 bool slotnameChanged;
127 bool tempSlotChanged = false;
128
129 ProcessConfigFile(PGC_SIGHUP);
130
131 conninfoChanged = strcmp(conninfo, PrimaryConnInfo) != 0;
132 slotnameChanged = strcmp(slotname, PrimarySlotName) != 0;
133
134 /*
135 * wal_receiver_create_temp_slot is used only when we have no slot
136 * configured. We do not need to track this change if it has no effect.
137 */
138 if (!slotnameChanged && strcmp(PrimarySlotName, "") == 0)
139 tempSlotChanged = tempSlot != wal_receiver_create_temp_slot;
140 pfree(conninfo);
141 pfree(slotname);
142
143 if (conninfoChanged || slotnameChanged || tempSlotChanged)
144 StartupRequestWalReceiverRestart();
145 }
146
147 /* Handle various signals that might be sent to the startup process */
148 void
HandleStartupProcInterrupts(void)149 HandleStartupProcInterrupts(void)
150 {
151 #ifdef POSTMASTER_POLL_RATE_LIMIT
152 static uint32 postmaster_poll_count = 0;
153 #endif
154
155 /*
156 * Process any requests or signals received recently.
157 */
158 if (got_SIGHUP)
159 {
160 got_SIGHUP = false;
161 StartupRereadConfig();
162 }
163
164 /*
165 * Check if we were requested to exit without finishing recovery.
166 */
167 if (shutdown_requested)
168 proc_exit(1);
169
170 /*
171 * Emergency bailout if postmaster has died. This is to avoid the
172 * necessity for manual cleanup of all postmaster children. Do this less
173 * frequently on systems for which we don't have signals to make that
174 * cheap.
175 */
176 if (IsUnderPostmaster &&
177 #ifdef POSTMASTER_POLL_RATE_LIMIT
178 postmaster_poll_count++ % POSTMASTER_POLL_RATE_LIMIT == 0 &&
179 #endif
180 !PostmasterIsAlive())
181 exit(1);
182
183 /* Process barrier events */
184 if (ProcSignalBarrierPending)
185 ProcessProcSignalBarrier();
186 }
187
188
189 /* --------------------------------
190 * signal handler routines
191 * --------------------------------
192 */
193 static void
StartupProcExit(int code,Datum arg)194 StartupProcExit(int code, Datum arg)
195 {
196 /* Shutdown the recovery environment */
197 if (standbyState != STANDBY_DISABLED)
198 ShutdownRecoveryTransactionEnvironment();
199 }
200
201
202 /* ----------------------------------
203 * Startup Process main entry point
204 * ----------------------------------
205 */
206 void
StartupProcessMain(void)207 StartupProcessMain(void)
208 {
209 /* Arrange to clean up at startup process exit */
210 on_shmem_exit(StartupProcExit, 0);
211
212 /*
213 * Properly accept or ignore signals the postmaster might send us.
214 */
215 pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
216 pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
217 pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
218 /* SIGQUIT handler was already set up by InitPostmasterChild */
219 InitializeTimeouts(); /* establishes SIGALRM handler */
220 pqsignal(SIGPIPE, SIG_IGN);
221 pqsignal(SIGUSR1, procsignal_sigusr1_handler);
222 pqsignal(SIGUSR2, StartupProcTriggerHandler);
223
224 /*
225 * Reset some signals that are accepted by postmaster but not here
226 */
227 pqsignal(SIGCHLD, SIG_DFL);
228
229 /*
230 * Register timeouts needed for standby mode
231 */
232 RegisterTimeout(STANDBY_DEADLOCK_TIMEOUT, StandbyDeadLockHandler);
233 RegisterTimeout(STANDBY_TIMEOUT, StandbyTimeoutHandler);
234 RegisterTimeout(STANDBY_LOCK_TIMEOUT, StandbyLockTimeoutHandler);
235
236 /*
237 * Unblock signals (they were blocked when the postmaster forked us)
238 */
239 PG_SETMASK(&UnBlockSig);
240
241 /*
242 * Do what we came for.
243 */
244 StartupXLOG();
245
246 /*
247 * Exit normally. Exit code 0 tells postmaster that we completed recovery
248 * successfully.
249 */
250 proc_exit(0);
251 }
252
253 void
PreRestoreCommand(void)254 PreRestoreCommand(void)
255 {
256 /*
257 * Set in_restore_command to tell the signal handler that we should exit
258 * right away on SIGTERM. We know that we're at a safe point to do that.
259 * Check if we had already received the signal, so that we don't miss a
260 * shutdown request received just before this.
261 */
262 in_restore_command = true;
263 if (shutdown_requested)
264 proc_exit(1);
265 }
266
267 void
PostRestoreCommand(void)268 PostRestoreCommand(void)
269 {
270 in_restore_command = false;
271 }
272
273 bool
IsPromoteSignaled(void)274 IsPromoteSignaled(void)
275 {
276 return promote_signaled;
277 }
278
279 void
ResetPromoteSignaled(void)280 ResetPromoteSignaled(void)
281 {
282 promote_signaled = false;
283 }
284