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_signaled = 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_signaled = 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 /*
100  * Re-read the config file.
101  *
102  * If one of the critical walreceiver options has changed, flag xlog.c
103  * to restart it.
104  */
105 static void
107 {
108  char *conninfo = pstrdup(PrimaryConnInfo);
109  char *slotname = pstrdup(PrimarySlotName);
110  bool tempSlot = wal_receiver_create_temp_slot;
111  bool conninfoChanged;
112  bool slotnameChanged;
113  bool tempSlotChanged = false;
114 
116 
117  conninfoChanged = strcmp(conninfo, PrimaryConnInfo) != 0;
118  slotnameChanged = strcmp(slotname, PrimarySlotName) != 0;
119 
120  /*
121  * wal_receiver_create_temp_slot is used only when we have no slot
122  * configured. We do not need to track this change if it has no effect.
123  */
124  if (!slotnameChanged && strcmp(PrimarySlotName, "") == 0)
125  tempSlotChanged = tempSlot != wal_receiver_create_temp_slot;
126  pfree(conninfo);
127  pfree(slotname);
128 
129  if (conninfoChanged || slotnameChanged || tempSlotChanged)
131 }
132 
133 /* Handle various signals that might be sent to the startup process */
134 void
136 {
137  /*
138  * Process any requests or signals received recently.
139  */
140  if (got_SIGHUP)
141  {
142  got_SIGHUP = false;
144  }
145 
146  /*
147  * Check if we were requested to exit without finishing recovery.
148  */
149  if (shutdown_requested)
150  proc_exit(1);
151 
152  /*
153  * Emergency bailout if postmaster has died. This is to avoid the
154  * necessity for manual cleanup of all postmaster children.
155  */
157  exit(1);
158 
159  /* Process barrier events */
162 }
163 
164 
165 /* ----------------------------------
166  * Startup Process main entry point
167  * ----------------------------------
168  */
169 void
171 {
172  /*
173  * Properly accept or ignore signals the postmaster might send us.
174  */
175  pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
176  pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
177  pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
178  /* SIGQUIT handler was already set up by InitPostmasterChild */
179  InitializeTimeouts(); /* establishes SIGALRM handler */
183 
184  /*
185  * Reset some signals that are accepted by postmaster but not here
186  */
188 
189  /*
190  * Register timeouts needed for standby mode
191  */
195 
196  /*
197  * Unblock signals (they were blocked when the postmaster forked us)
198  */
200 
201  /*
202  * Do what we came for.
203  */
204  StartupXLOG();
205 
206  /*
207  * Exit normally. Exit code 0 tells postmaster that we completed recovery
208  * successfully.
209  */
210  proc_exit(0);
211 }
212 
213 void
215 {
216  /*
217  * Set in_restore_command to tell the signal handler that we should exit
218  * right away on SIGTERM. We know that we're at a safe point to do that.
219  * Check if we had already received the signal, so that we don't miss a
220  * shutdown request received just before this.
221  */
222  in_restore_command = true;
223  if (shutdown_requested)
224  proc_exit(1);
225 }
226 
227 void
229 {
230  in_restore_command = false;
231 }
232 
233 bool
235 {
236  return promote_signaled;
237 }
238 
239 void
241 {
242  promote_signaled = false;
243 }
void InitializeTimeouts(void)
Definition: timeout.c:346
void StandbyTimeoutHandler(void)
Definition: standby.c:607
void ProcessConfigFile(GucContext context)
char * PrimarySlotName
Definition: xlog.c:296
#define SIGUSR1
Definition: win32_port.h:171
#define SIGCHLD
Definition: win32_port.h:169
bool IsPromoteSignaled(void)
Definition: startup.c:234
char * pstrdup(const char *in)
Definition: mcxt.c:1187
void StartupProcessMain(void)
Definition: startup.c:170
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:452
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: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:40
void pfree(void *pointer)
Definition: mcxt.c:1057
void PostRestoreCommand(void)
Definition: startup.c:228
char * PrimaryConnInfo
Definition: xlog.c:295
void WakeupRecovery(void)
Definition: xlog.c:12675
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:74
bool IsUnderPostmaster
Definition: globals.c:109
#define SIGHUP
Definition: win32_port.h:159
#define PostmasterIsAlive()
Definition: pmsignal.h:91
void StandbyLockTimeoutHandler(void)
Definition: standby.c:620
void StandbyDeadLockHandler(void)
Definition: standby.c:596
static void StartupRereadConfig(void)
Definition: startup.c:106
sigset_t UnBlockSig
Definition: pqsignal.c:22
void StartupXLOG(void)
Definition: xlog.c:6305
Definition: guc.h:72
#define SIG_IGN
Definition: win32_port.h:156
bool wal_receiver_create_temp_slot
Definition: xlog.c:298
static volatile sig_atomic_t promote_signaled
Definition: startup.c:42
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
void ResetPromoteSignaled(void)
Definition: startup.c:240
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:380
#define SIG_DFL
Definition: win32_port.h:154
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:62
#define SIGNAL_ARGS
Definition: c.h:1275
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:86
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:35
void StartupRequestWalReceiverRestart(void)
Definition: xlog.c:12528
void PreRestoreCommand(void)
Definition: startup.c:214
void HandleStartupProcInterrupts(void)
Definition: startup.c:135
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:551