PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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-2024, 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 "access/xlogrecovery.h"
24#include "access/xlogutils.h"
25#include "libpq/pqsignal.h"
26#include "miscadmin.h"
28#include "postmaster/startup.h"
29#include "storage/ipc.h"
30#include "storage/pmsignal.h"
31#include "storage/procsignal.h"
32#include "storage/standby.h"
33#include "utils/guc.h"
34#include "utils/memutils.h"
35#include "utils/timeout.h"
36
37
38#ifndef USE_POSTMASTER_DEATH_SIGNAL
39/*
40 * On systems that need to make a system call to find out if the postmaster has
41 * gone away, we'll do so only every Nth call to HandleStartupProcInterrupts().
42 * This only affects how long it takes us to detect the condition while we're
43 * busy replaying WAL. Latch waits and similar which should react immediately
44 * through the usual techniques.
45 */
46#define POSTMASTER_POLL_RATE_LIMIT 1024
47#endif
48
49/*
50 * Flags set by interrupt handlers for later service in the redo loop.
51 */
52static volatile sig_atomic_t got_SIGHUP = false;
53static volatile sig_atomic_t shutdown_requested = false;
54static volatile sig_atomic_t promote_signaled = false;
55
56/*
57 * Flag set when executing a restore command, to tell SIGTERM signal handler
58 * that it's safe to just proc_exit.
59 */
60static volatile sig_atomic_t in_restore_command = false;
61
62/*
63 * Time at which the most recent startup operation started.
64 */
66
67/*
68 * Indicates whether the startup progress interval mentioned by the user is
69 * elapsed or not. TRUE if timeout occurred, FALSE otherwise.
70 */
71static volatile sig_atomic_t startup_progress_timer_expired = false;
72
73/*
74 * Time between progress updates for long-running startup operations.
75 */
76int log_startup_progress_interval = 10000; /* 10 sec */
77
78/* Signal handlers */
81
82/* Callbacks */
83static void StartupProcExit(int code, Datum arg);
84
85
86/* --------------------------------
87 * signal handler routines
88 * --------------------------------
89 */
90
91/* SIGUSR2: set flag to finish recovery */
92static void
94{
95 promote_signaled = true;
97}
98
99/* SIGHUP: set flag to re-read config file at next convenient time */
100static void
102{
103 got_SIGHUP = true;
105}
106
107/* SIGTERM: set flag to abort redo and exit */
108static void
110{
112 proc_exit(1);
113 else
114 shutdown_requested = true;
116}
117
118/*
119 * Re-read the config file.
120 *
121 * If one of the critical walreceiver options has changed, flag xlog.c
122 * to restart it.
123 */
124static void
126{
127 char *conninfo = pstrdup(PrimaryConnInfo);
128 char *slotname = pstrdup(PrimarySlotName);
129 bool tempSlot = wal_receiver_create_temp_slot;
130 bool conninfoChanged;
131 bool slotnameChanged;
132 bool tempSlotChanged = false;
133
135
136 conninfoChanged = strcmp(conninfo, PrimaryConnInfo) != 0;
137 slotnameChanged = strcmp(slotname, PrimarySlotName) != 0;
138
139 /*
140 * wal_receiver_create_temp_slot is used only when we have no slot
141 * configured. We do not need to track this change if it has no effect.
142 */
143 if (!slotnameChanged && strcmp(PrimarySlotName, "") == 0)
144 tempSlotChanged = tempSlot != wal_receiver_create_temp_slot;
145 pfree(conninfo);
146 pfree(slotname);
147
148 if (conninfoChanged || slotnameChanged || tempSlotChanged)
150}
151
152/* Handle various signals that might be sent to the startup process */
153void
155{
156#ifdef POSTMASTER_POLL_RATE_LIMIT
157 static uint32 postmaster_poll_count = 0;
158#endif
159
160 /*
161 * Process any requests or signals received recently.
162 */
163 if (got_SIGHUP)
164 {
165 got_SIGHUP = false;
167 }
168
169 /*
170 * Check if we were requested to exit without finishing recovery.
171 */
173 proc_exit(1);
174
175 /*
176 * Emergency bailout if postmaster has died. This is to avoid the
177 * necessity for manual cleanup of all postmaster children. Do this less
178 * frequently on systems for which we don't have signals to make that
179 * cheap.
180 */
181 if (IsUnderPostmaster &&
183 postmaster_poll_count++ % POSTMASTER_POLL_RATE_LIMIT == 0 &&
184#endif
186 exit(1);
187
188 /* Process barrier events */
191
192 /* Perform logging of memory contexts of this process */
195}
196
197
198/* --------------------------------
199 * signal handler routines
200 * --------------------------------
201 */
202static void
204{
205 /* Shutdown the recovery environment */
208}
209
210
211/* ----------------------------------
212 * Startup Process main entry point
213 * ----------------------------------
214 */
215void
216StartupProcessMain(char *startup_data, size_t startup_data_len)
217{
218 Assert(startup_data_len == 0);
219
222
223 /* Arrange to clean up at startup process exit */
225
226 /*
227 * Properly accept or ignore signals the postmaster might send us.
228 */
229 pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
230 pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
231 pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
232 /* SIGQUIT handler was already set up by InitPostmasterChild */
233 InitializeTimeouts(); /* establishes SIGALRM handler */
237
238 /*
239 * Reset some signals that are accepted by postmaster but not here
240 */
242
243 /*
244 * Register timeouts needed for standby mode
245 */
249
250 /*
251 * Unblock signals (they were blocked when the postmaster forked us)
252 */
253 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
254
255 /*
256 * Do what we came for.
257 */
258 StartupXLOG();
259
260 /*
261 * Exit normally. Exit code 0 tells postmaster that we completed recovery
262 * successfully.
263 */
264 proc_exit(0);
265}
266
267void
269{
270 /*
271 * Set in_restore_command to tell the signal handler that we should exit
272 * right away on SIGTERM. We know that we're at a safe point to do that.
273 * Check if we had already received the signal, so that we don't miss a
274 * shutdown request received just before this.
275 */
276 in_restore_command = true;
278 proc_exit(1);
279}
280
281void
283{
284 in_restore_command = false;
285}
286
287bool
289{
290 return promote_signaled;
291}
292
293void
295{
296 promote_signaled = false;
297}
298
299/*
300 * Set a flag indicating that it's time to log a progress report.
301 */
302void
304{
306}
307
308void
310{
311 /* Feature is disabled. */
313 return;
314
317}
318
319/*
320 * Set the start timestamp of the current operation and enable the timeout.
321 */
322void
324{
325 TimestampTz fin_time;
326
327 /* Feature is disabled. */
329 return;
330
336}
337
338/*
339 * A thin wrapper to first disable and then enable the startup progress
340 * timeout.
341 */
342void
344{
345 /* Feature is disabled. */
347 return;
348
351}
352
353/*
354 * Report whether startup progress timeout has occurred. Reset the timer flag
355 * if it did, set the elapsed time to the out parameters and return true,
356 * otherwise return false.
357 */
358bool
360{
361 long seconds;
362 int useconds;
364
365 /* No timeout has occurred. */
367 return false;
368
369 /* Calculate the elapsed time. */
372
373 *secs = seconds;
374 *usecs = useconds;
376
377 return true;
378}
void AuxiliaryProcessMainCommon(void)
Definition: auxprocess.c:39
sigset_t UnBlockSig
Definition: pqsignal.c:22
void HandleStartupProcInterrupts(void)
Definition: startup.c:154
void disable_startup_progress_timeout(void)
Definition: startup.c:309
#define POSTMASTER_POLL_RATE_LIMIT
Definition: startup.c:46
static volatile sig_atomic_t startup_progress_timer_expired
Definition: startup.c:71
static volatile sig_atomic_t in_restore_command
Definition: startup.c:60
static void StartupProcExit(int code, Datum arg)
Definition: startup.c:203
bool IsPromoteSignaled(void)
Definition: startup.c:288
void PreRestoreCommand(void)
Definition: startup.c:268
void startup_progress_timeout_handler(void)
Definition: startup.c:303
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:53
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:109
void begin_startup_progress_phase(void)
Definition: startup.c:343
void ResetPromoteSignaled(void)
Definition: startup.c:294
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:101
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:52
void StartupProcessMain(char *startup_data, size_t startup_data_len)
Definition: startup.c:216
static TimestampTz startup_progress_phase_start_time
Definition: startup.c:65
static volatile sig_atomic_t promote_signaled
Definition: startup.c:54
void PostRestoreCommand(void)
Definition: startup.c:282
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:93
int log_startup_progress_interval
Definition: startup.c:76
void enable_startup_progress_timeout(void)
Definition: startup.c:323
bool has_startup_progress_timeout_expired(long *secs, int *usecs)
Definition: startup.c:359
static void StartupRereadConfig(void)
Definition: startup.c:125
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1720
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1644
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1608
#define SIGNAL_ARGS
Definition: c.h:1303
#define Assert(condition)
Definition: c.h:812
uint32_t uint32
Definition: c.h:485
int64 TimestampTz
Definition: timestamp.h:39
volatile sig_atomic_t LogMemoryContextPending
Definition: globals.c:40
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:39
bool IsUnderPostmaster
Definition: globals.c:119
void ProcessConfigFile(GucContext context)
Definition: guc-file.l:120
@ PGC_SIGHUP
Definition: guc.h:71
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
void proc_exit(int code)
Definition: ipc.c:104
exit(1)
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void pfree(void *pointer)
Definition: mcxt.c:1521
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1289
@ B_STARTUP
Definition: miscadmin.h:363
BackendType MyBackendType
Definition: miscinit.c:64
void * arg
#define PostmasterIsAlive()
Definition: pmsignal.h:105
pqsigfunc pqsignal(int signo, pqsigfunc func)
uintptr_t Datum
Definition: postgres.h:64
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:496
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:671
void StandbyTimeoutHandler(void)
Definition: standby.c:944
void StandbyLockTimeoutHandler(void)
Definition: standby.c:953
void StandbyDeadLockHandler(void)
Definition: standby.c:935
void ShutdownRecoveryTransactionEnvironment(void)
Definition: standby.c:160
void InitializeTimeouts(void)
Definition: timeout.c:470
void enable_timeout_every(TimeoutId id, TimestampTz fin_time, int delay_ms)
Definition: timeout.c:584
void disable_timeout(TimeoutId id, bool keep_indicator)
Definition: timeout.c:685
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:505
@ STARTUP_PROGRESS_TIMEOUT
Definition: timeout.h:38
@ STANDBY_LOCK_TIMEOUT
Definition: timeout.h:32
@ STANDBY_DEADLOCK_TIMEOUT
Definition: timeout.h:30
@ STANDBY_TIMEOUT
Definition: timeout.h:31
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:85
#define SIGCHLD
Definition: win32_port.h:178
#define SIGHUP
Definition: win32_port.h:168
#define SIG_DFL
Definition: win32_port.h:163
#define SIGPIPE
Definition: win32_port.h:173
#define SIGUSR1
Definition: win32_port.h:180
#define SIGUSR2
Definition: win32_port.h:181
#define SIG_IGN
Definition: win32_port.h:165
void StartupXLOG(void)
Definition: xlog.c:5429
void StartupRequestWalReceiverRestart(void)
char * PrimarySlotName
Definition: xlogrecovery.c:97
void WakeupRecovery(void)
bool wal_receiver_create_temp_slot
Definition: xlogrecovery.c:98
char * PrimaryConnInfo
Definition: xlogrecovery.c:96
HotStandbyState standbyState
Definition: xlogutils.c:53
@ STANDBY_DISABLED
Definition: xlogutils.h:52