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-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 */
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
77 {
78  int save_errno = errno;
79 
80  promote_signaled = true;
82 
83  errno = save_errno;
84 }
85 
86 /* SIGHUP: set flag to re-read config file at next convenient time */
87 static void
89 {
90  int save_errno = errno;
91 
92  got_SIGHUP = true;
94 
95  errno = save_errno;
96 }
97 
98 /* SIGTERM: set flag to abort redo and exit */
99 static void
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
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 
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)
145 }
146 
147 /* Handle various signals that might be sent to the startup process */
148 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;
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 &&
178  postmaster_poll_count++ % POSTMASTER_POLL_RATE_LIMIT == 0 &&
179 #endif
181  exit(1);
182 
183  /* Process barrier events */
186 }
187 
188 
189 /* --------------------------------
190  * signal handler routines
191  * --------------------------------
192  */
193 static void
195 {
196  /* Shutdown the recovery environment */
199 }
200 
201 
202 /* ----------------------------------
203  * Startup Process main entry point
204  * ----------------------------------
205  */
206 void
208 {
209  /* Arrange to clean up at startup process exit */
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 */
223 
224  /*
225  * Reset some signals that are accepted by postmaster but not here
226  */
228 
229  /*
230  * Register timeouts needed for standby mode
231  */
235 
236  /*
237  * Unblock signals (they were blocked when the postmaster forked us)
238  */
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
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
269 {
270  in_restore_command = false;
271 }
272 
273 bool
275 {
276  return promote_signaled;
277 }
278 
279 void
281 {
282  promote_signaled = false;
283 }
void InitializeTimeouts(void)
Definition: timeout.c:435
void StandbyTimeoutHandler(void)
Definition: standby.c:904
void ProcessConfigFile(GucContext context)
char * PrimarySlotName
Definition: xlog.c:300
#define SIGUSR1
Definition: win32_port.h:171
#define SIGCHLD
Definition: win32_port.h:169
bool IsPromoteSignaled(void)
Definition: startup.c:274
char * pstrdup(const char *in)
Definition: mcxt.c:1299
void StartupProcessMain(void)
Definition: startup.c:207
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:453
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:52
void proc_exit(int code)
Definition: ipc.c:104
#define SIGPIPE
Definition: win32_port.h:164
#define SIGUSR2
Definition: win32_port.h:172
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:51
void pfree(void *pointer)
Definition: mcxt.c:1169
void PostRestoreCommand(void)
Definition: startup.c:268
char * PrimaryConnInfo
Definition: xlog.c:299
#define POSTMASTER_POLL_RATE_LIMIT
Definition: startup.c:45
void WakeupRecovery(void)
Definition: xlog.c:12935
static volatile sig_atomic_t in_restore_command
Definition: startup.c:59
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:88
bool IsUnderPostmaster
Definition: globals.c:112
#define SIGHUP
Definition: win32_port.h:159
#define PostmasterIsAlive()
Definition: pmsignal.h:102
void StandbyLockTimeoutHandler(void)
Definition: standby.c:916
void StandbyDeadLockHandler(void)
Definition: standby.c:893
unsigned int uint32
Definition: c.h:441
static void StartupRereadConfig(void)
Definition: startup.c:120
sigset_t UnBlockSig
Definition: pqsignal.c:22
void StartupXLOG(void)
Definition: xlog.c:6450
Definition: guc.h:72
#define SIG_IGN
Definition: win32_port.h:156
bool wal_receiver_create_temp_slot
Definition: xlog.c:302
static volatile sig_atomic_t promote_signaled
Definition: startup.c:53
uintptr_t Datum
Definition: postgres.h:411
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
void ResetPromoteSignaled(void)
Definition: startup.c:280
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:469
#define SIG_DFL
Definition: win32_port.h:154
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:76
#define SIGNAL_ARGS
Definition: c.h:1333
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:100
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:37
void StartupRequestWalReceiverRestart(void)
Definition: xlog.c:12788
void PreRestoreCommand(void)
Definition: startup.c:254
void HandleStartupProcInterrupts(void)
Definition: startup.c:149
void * arg
void ShutdownRecoveryTransactionEnvironment(void)
Definition: standby.c:138
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:642
HotStandbyState standbyState
Definition: xlog.c:212
static void StartupProcExit(int code, Datum arg)
Definition: startup.c:194