PostgreSQL Source Code  git master
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-2020, 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 /*
38  * Flags set by interrupt handlers for later service in the redo loop.
39  */
40 static volatile sig_atomic_t got_SIGHUP = false;
41 static volatile sig_atomic_t shutdown_requested = false;
42 static volatile sig_atomic_t promote_triggered = false;
43 
44 /*
45  * Flag set when executing a restore command, to tell SIGTERM signal handler
46  * that it's safe to just proc_exit.
47  */
48 static volatile sig_atomic_t in_restore_command = false;
49 
50 /* Signal handlers */
53 
54 
55 /* --------------------------------
56  * signal handler routines
57  * --------------------------------
58  */
59 
60 /* SIGUSR2: set flag to finish recovery */
61 static void
63 {
64  int save_errno = errno;
65 
66  promote_triggered = true;
68 
69  errno = save_errno;
70 }
71 
72 /* SIGHUP: set flag to re-read config file at next convenient time */
73 static void
75 {
76  int save_errno = errno;
77 
78  got_SIGHUP = true;
80 
81  errno = save_errno;
82 }
83 
84 /* SIGTERM: set flag to abort redo and exit */
85 static void
87 {
88  int save_errno = errno;
89 
91  proc_exit(1);
92  else
93  shutdown_requested = true;
95 
96  errno = save_errno;
97 }
98 
99 /* Handle various signals that might be sent to the startup process */
100 void
102 {
103  /*
104  * Check if we were requested to re-read config file.
105  */
106  if (got_SIGHUP)
107  {
108  got_SIGHUP = false;
110  }
111 
112  /*
113  * Check if we were requested to exit without finishing recovery.
114  */
115  if (shutdown_requested)
116  proc_exit(1);
117 
118  /*
119  * Emergency bailout if postmaster has died. This is to avoid the
120  * necessity for manual cleanup of all postmaster children.
121  */
123  exit(1);
124 
125  /* Process barrier events */
128 }
129 
130 
131 /* ----------------------------------
132  * Startup Process main entry point
133  * ----------------------------------
134  */
135 void
137 {
138  /*
139  * Properly accept or ignore signals the postmaster might send us.
140  */
141  pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
142  pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
143  pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
145  InitializeTimeouts(); /* establishes SIGALRM handler */
149 
150  /*
151  * Reset some signals that are accepted by postmaster but not here
152  */
154 
155  /*
156  * Register timeouts needed for standby mode
157  */
161 
162  /*
163  * Unblock signals (they were blocked when the postmaster forked us)
164  */
166 
167  /*
168  * Do what we came for.
169  */
170  StartupXLOG();
171 
172  /*
173  * Exit normally. Exit code 0 tells postmaster that we completed recovery
174  * successfully.
175  */
176  proc_exit(0);
177 }
178 
179 void
181 {
182  /*
183  * Set in_restore_command to tell the signal handler that we should exit
184  * right away on SIGTERM. We know that we're at a safe point to do that.
185  * Check if we had already received the signal, so that we don't miss a
186  * shutdown request received just before this.
187  */
188  in_restore_command = true;
189  if (shutdown_requested)
190  proc_exit(1);
191 }
192 
193 void
195 {
196  in_restore_command = false;
197 }
198 
199 bool
201 {
202  return promote_triggered;
203 }
204 
205 void
207 {
208  promote_triggered = false;
209 }
void InitializeTimeouts(void)
Definition: timeout.c:346
#define SIGQUIT
Definition: win32_port.h:155
void ResetPromoteTriggered(void)
Definition: startup.c:206
void StandbyTimeoutHandler(void)
Definition: standby.c:585
void ProcessConfigFile(GucContext context)
#define SIGUSR1
Definition: win32_port.h:166
#define SIGCHLD
Definition: win32_port.h:164
void StartupProcessMain(void)
Definition: startup.c:136
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:429
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:41
void proc_exit(int code)
Definition: ipc.c:104
#define SIGPIPE
Definition: win32_port.h:159
#define SIGUSR2
Definition: win32_port.h:167
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:40
void SignalHandlerForCrashExit(SIGNAL_ARGS)
Definition: interrupt.c:72
void PostRestoreCommand(void)
Definition: startup.c:194
void WakeupRecovery(void)
Definition: xlog.c:12300
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48
static volatile sig_atomic_t promote_triggered
Definition: startup.c:42
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:74
bool IsUnderPostmaster
Definition: globals.c:109
#define SIGHUP
Definition: win32_port.h:154
#define PostmasterIsAlive()
Definition: pmsignal.h:91
void StandbyLockTimeoutHandler(void)
Definition: standby.c:598
void StandbyDeadLockHandler(void)
Definition: standby.c:574
bool IsPromoteTriggered(void)
Definition: startup.c:200
sigset_t UnBlockSig
Definition: pqsignal.c:22
void StartupXLOG(void)
Definition: xlog.c:6200
Definition: guc.h:72
#define SIG_IGN
Definition: win32_port.h:151
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:380
#define SIG_DFL
Definition: win32_port.h:149
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:62
#define SIGNAL_ARGS
Definition: c.h:1287
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:86
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:35
void PreRestoreCommand(void)
Definition: startup.c:180
void HandleStartupProcInterrupts(void)
Definition: startup.c:101
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:533