PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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 ProcessStartupProcInterrupts().
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 */
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
98
99/* SIGHUP: set flag to re-read config file at next convenient time */
100static void
106
107/* SIGTERM: set flag to abort redo and exit */
108static void
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);
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)
145 pfree(conninfo);
146 pfree(slotname);
147
150}
151
152/* Process 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 &&
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
217{
219
221
222 /* Arrange to clean up at startup process exit */
224
225 /*
226 * Properly accept or ignore signals the postmaster might send us.
227 */
228 pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
229 pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
230 pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
231 /* SIGQUIT handler was already set up by InitPostmasterChild */
232 InitializeTimeouts(); /* establishes SIGALRM handler */
236
237 /*
238 * Reset some signals that are accepted by postmaster but not here
239 */
241
242 /*
243 * Register timeouts needed for standby mode
244 */
248
249 /*
250 * Unblock signals (they were blocked when the postmaster forked us)
251 */
253
254 /*
255 * Do what we came for.
256 */
257 StartupXLOG();
258
259 /*
260 * Exit normally. Exit code 0 tells postmaster that we completed recovery
261 * successfully.
262 */
263 proc_exit(0);
264}
265
266void
268{
269 /*
270 * Set in_restore_command to tell the signal handler that we should exit
271 * right away on SIGTERM. We know that we're at a safe point to do that.
272 * Check if we had already received the signal, so that we don't miss a
273 * shutdown request received just before this.
274 */
275 in_restore_command = true;
277 proc_exit(1);
278}
279
280void
282{
283 in_restore_command = false;
284}
285
286bool
288{
289 return promote_signaled;
290}
291
292void
294{
295 promote_signaled = false;
296}
297
298/*
299 * Set a flag indicating that it's time to log a progress report.
300 */
301void
306
307void
309{
310 /* Feature is disabled. */
312 return;
313
316}
317
318/*
319 * Set the start timestamp of the current operation and enable the timeout.
320 */
321void
336
337/*
338 * A thin wrapper to first disable and then enable the startup progress
339 * timeout.
340 */
341void
343{
344 /* Feature is disabled. */
346 return;
347
350}
351
352/*
353 * Report whether startup progress timeout has occurred. Reset the timer flag
354 * if it did, set the elapsed time to the out parameters and return true,
355 * otherwise return false.
356 */
357bool
359{
360 long seconds;
361 int useconds;
363
364 /* No timeout has occurred. */
366 return false;
367
368 /* Calculate the elapsed time. */
371
372 *secs = seconds;
373 *usecs = useconds;
375
376 return true;
377}
void AuxiliaryProcessMainCommon(void)
Definition auxprocess.c:39
sigset_t UnBlockSig
Definition pqsignal.c:22
void disable_startup_progress_timeout(void)
Definition startup.c:308
#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:287
void PreRestoreCommand(void)
Definition startup.c:267
void startup_progress_timeout_handler(void)
Definition startup.c:302
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:342
void ProcessStartupProcInterrupts(void)
Definition startup.c:154
void ResetPromoteSignaled(void)
Definition startup.c:293
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition startup.c:101
static volatile sig_atomic_t got_SIGHUP
Definition startup.c:52
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:281
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition startup.c:93
void StartupProcessMain(const void *startup_data, size_t startup_data_len)
Definition startup.c:216
int log_startup_progress_interval
Definition startup.c:76
void enable_startup_progress_timeout(void)
Definition startup.c:322
bool has_startup_progress_timeout_expired(long *secs, int *usecs)
Definition startup.c:358
static void StartupRereadConfig(void)
Definition startup.c:125
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition timestamp.c:1721
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1645
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1609
#define SIGNAL_ARGS
Definition c.h:1363
#define Assert(condition)
Definition c.h:873
uint32_t uint32
Definition c.h:546
int64 TimestampTz
Definition timestamp.h:39
volatile sig_atomic_t LogMemoryContextPending
Definition globals.c:41
volatile sig_atomic_t ProcSignalBarrierPending
Definition globals.c:40
bool IsUnderPostmaster
Definition globals.c:120
void ProcessConfigFile(GucContext context)
Definition guc-file.l:120
@ PGC_SIGHUP
Definition guc.h:75
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:372
void proc_exit(int code)
Definition ipc.c:105
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
void ProcessLogMemoryContextInterrupt(void)
Definition mcxt.c:1340
void * arg
#define PostmasterIsAlive()
Definition pmsignal.h:107
#define pqsignal
Definition port.h:547
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
void ProcessProcSignalBarrier(void)
Definition procsignal.c:499
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition procsignal.c:677
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:161
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:168
#define SIGHUP
Definition win32_port.h:158
#define SIGPIPE
Definition win32_port.h:163
#define SIGUSR1
Definition win32_port.h:170
#define SIGUSR2
Definition win32_port.h:171
void StartupXLOG(void)
Definition xlog.c:5517
void StartupRequestWalReceiverRestart(void)
char * PrimarySlotName
void WakeupRecovery(void)
bool wal_receiver_create_temp_slot
char * PrimaryConnInfo
HotStandbyState standbyState
Definition xlogutils.c:53
@ STANDBY_DISABLED
Definition xlogutils.h:52