PostgreSQL Source Code  git master
startup.c File Reference
#include "postgres.h"
#include "access/xlog.h"
#include "access/xlogutils.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/interrupt.h"
#include "postmaster/startup.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pmsignal.h"
#include "storage/procsignal.h"
#include "storage/standby.h"
#include "utils/guc.h"
#include "utils/timeout.h"
Include dependency graph for startup.c:

Go to the source code of this file.

Macros

#define POSTMASTER_POLL_RATE_LIMIT   1024
 

Functions

static void StartupProcTriggerHandler (SIGNAL_ARGS)
 
static void StartupProcSigHupHandler (SIGNAL_ARGS)
 
static void StartupProcExit (int code, Datum arg)
 
static void StartupProcShutdownHandler (SIGNAL_ARGS)
 
static void StartupRereadConfig (void)
 
void HandleStartupProcInterrupts (void)
 
void StartupProcessMain (void)
 
void PreRestoreCommand (void)
 
void PostRestoreCommand (void)
 
bool IsPromoteSignaled (void)
 
void ResetPromoteSignaled (void)
 

Variables

static volatile sig_atomic_t got_SIGHUP = false
 
static volatile sig_atomic_t shutdown_requested = false
 
static volatile sig_atomic_t promote_signaled = false
 
static volatile sig_atomic_t in_restore_command = false
 

Macro Definition Documentation

◆ POSTMASTER_POLL_RATE_LIMIT

#define POSTMASTER_POLL_RATE_LIMIT   1024

Definition at line 46 of file startup.c.

Referenced by HandleStartupProcInterrupts().

Function Documentation

◆ HandleStartupProcInterrupts()

void HandleStartupProcInterrupts ( void  )

Definition at line 150 of file startup.c.

References got_SIGHUP, IsUnderPostmaster, POSTMASTER_POLL_RATE_LIMIT, PostmasterIsAlive, proc_exit(), ProcessProcSignalBarrier(), ProcSignalBarrierPending, shutdown_requested, and StartupRereadConfig().

Referenced by recoveryApplyDelay(), recoveryPausesHere(), RecoveryRequiresIntParameter(), StartupXLOG(), and WaitForWALToBecomeAvailable().

151 {
152 #ifdef POSTMASTER_POLL_RATE_LIMIT
153  static uint32 postmaster_poll_count = 0;
154 #endif
155 
156  /*
157  * Process any requests or signals received recently.
158  */
159  if (got_SIGHUP)
160  {
161  got_SIGHUP = false;
163  }
164 
165  /*
166  * Check if we were requested to exit without finishing recovery.
167  */
168  if (shutdown_requested)
169  proc_exit(1);
170 
171  /*
172  * Emergency bailout if postmaster has died. This is to avoid the
173  * necessity for manual cleanup of all postmaster children. Do this less
174  * frequently on systems for which we don't have signals to make that
175  * cheap.
176  */
177  if (IsUnderPostmaster &&
179  postmaster_poll_count++ % POSTMASTER_POLL_RATE_LIMIT == 0 &&
180 #endif
182  exit(1);
183 
184  /* Process barrier events */
187 }
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:453
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:53
void proc_exit(int code)
Definition: ipc.c:104
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:52
#define POSTMASTER_POLL_RATE_LIMIT
Definition: startup.c:46
bool IsUnderPostmaster
Definition: globals.c:112
#define PostmasterIsAlive()
Definition: pmsignal.h:102
unsigned int uint32
Definition: c.h:441
static void StartupRereadConfig(void)
Definition: startup.c:121
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:37

◆ IsPromoteSignaled()

bool IsPromoteSignaled ( void  )

Definition at line 275 of file startup.c.

References promote_signaled.

Referenced by CheckForStandbyTrigger().

276 {
277  return promote_signaled;
278 }
static volatile sig_atomic_t promote_signaled
Definition: startup.c:54

◆ PostRestoreCommand()

void PostRestoreCommand ( void  )

Definition at line 269 of file startup.c.

References in_restore_command.

Referenced by RestoreArchivedFile().

270 {
271  in_restore_command = false;
272 }
static volatile sig_atomic_t in_restore_command
Definition: startup.c:60

◆ PreRestoreCommand()

void PreRestoreCommand ( void  )

Definition at line 255 of file startup.c.

References in_restore_command, proc_exit(), and shutdown_requested.

Referenced by RestoreArchivedFile().

256 {
257  /*
258  * Set in_restore_command to tell the signal handler that we should exit
259  * right away on SIGTERM. We know that we're at a safe point to do that.
260  * Check if we had already received the signal, so that we don't miss a
261  * shutdown request received just before this.
262  */
263  in_restore_command = true;
264  if (shutdown_requested)
265  proc_exit(1);
266 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:53
void proc_exit(int code)
Definition: ipc.c:104
static volatile sig_atomic_t in_restore_command
Definition: startup.c:60

◆ ResetPromoteSignaled()

void ResetPromoteSignaled ( void  )

Definition at line 281 of file startup.c.

References promote_signaled.

Referenced by CheckForStandbyTrigger().

282 {
283  promote_signaled = false;
284 }
static volatile sig_atomic_t promote_signaled
Definition: startup.c:54

◆ StartupProcessMain()

void StartupProcessMain ( void  )

Definition at line 208 of file startup.c.

References InitializeTimeouts(), on_shmem_exit(), PG_SETMASK, pqsignal(), proc_exit(), procsignal_sigusr1_handler(), RegisterTimeout(), SIG_DFL, SIG_IGN, SIGCHLD, SIGHUP, SIGPIPE, SIGUSR1, SIGUSR2, STANDBY_DEADLOCK_TIMEOUT, STANDBY_LOCK_TIMEOUT, STANDBY_TIMEOUT, StandbyDeadLockHandler(), StandbyLockTimeoutHandler(), StandbyTimeoutHandler(), StartupProcExit(), StartupProcShutdownHandler(), StartupProcSigHupHandler(), StartupProcTriggerHandler(), StartupXLOG(), and UnBlockSig.

Referenced by AuxiliaryProcessMain().

209 {
210  /* Arrange to clean up at startup process exit */
212 
213  /*
214  * Properly accept or ignore signals the postmaster might send us.
215  */
216  pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
217  pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
218  pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
219  /* SIGQUIT handler was already set up by InitPostmasterChild */
220  InitializeTimeouts(); /* establishes SIGALRM handler */
224 
225  /*
226  * Reset some signals that are accepted by postmaster but not here
227  */
229 
230  /*
231  * Register timeouts needed for standby mode
232  */
236 
237  /*
238  * Unblock signals (they were blocked when the postmaster forked us)
239  */
241 
242  /*
243  * Do what we came for.
244  */
245  StartupXLOG();
246 
247  /*
248  * Exit normally. Exit code 0 tells postmaster that we completed recovery
249  * successfully.
250  */
251  proc_exit(0);
252 }
void InitializeTimeouts(void)
Definition: timeout.c:435
void StandbyTimeoutHandler(void)
Definition: standby.c:904
#define SIGUSR1
Definition: win32_port.h:179
#define SIGCHLD
Definition: win32_port.h:177
void proc_exit(int code)
Definition: ipc.c:104
#define SIGPIPE
Definition: win32_port.h:172
#define SIGUSR2
Definition: win32_port.h:180
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:89
#define SIGHUP
Definition: win32_port.h:167
void StandbyLockTimeoutHandler(void)
Definition: standby.c:916
void StandbyDeadLockHandler(void)
Definition: standby.c:893
sigset_t UnBlockSig
Definition: pqsignal.c:22
void StartupXLOG(void)
Definition: xlog.c:6617
#define SIG_IGN
Definition: win32_port.h:164
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:469
#define SIG_DFL
Definition: win32_port.h:162
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:77
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:101
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:642
static void StartupProcExit(int code, Datum arg)
Definition: startup.c:195

◆ StartupProcExit()

static void StartupProcExit ( int  code,
Datum  arg 
)
static

Definition at line 195 of file startup.c.

References ShutdownRecoveryTransactionEnvironment(), STANDBY_DISABLED, and standbyState.

Referenced by StartupProcessMain().

196 {
197  /* Shutdown the recovery environment */
200 }
HotStandbyState standbyState
Definition: xlogutils.c:55
void ShutdownRecoveryTransactionEnvironment(void)
Definition: standby.c:138

◆ StartupProcShutdownHandler()

static void StartupProcShutdownHandler ( SIGNAL_ARGS  )
static

Definition at line 101 of file startup.c.

References in_restore_command, proc_exit(), shutdown_requested, and WakeupRecovery().

Referenced by StartupProcessMain().

102 {
103  int save_errno = errno;
104 
105  if (in_restore_command)
106  proc_exit(1);
107  else
108  shutdown_requested = true;
109  WakeupRecovery();
110 
111  errno = save_errno;
112 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:53
void proc_exit(int code)
Definition: ipc.c:104
void WakeupRecovery(void)
Definition: xlog.c:13168
static volatile sig_atomic_t in_restore_command
Definition: startup.c:60

◆ StartupProcSigHupHandler()

static void StartupProcSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 89 of file startup.c.

References got_SIGHUP, and WakeupRecovery().

Referenced by StartupProcessMain().

90 {
91  int save_errno = errno;
92 
93  got_SIGHUP = true;
95 
96  errno = save_errno;
97 }
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:52
void WakeupRecovery(void)
Definition: xlog.c:13168

◆ StartupProcTriggerHandler()

static void StartupProcTriggerHandler ( SIGNAL_ARGS  )
static

Definition at line 77 of file startup.c.

References promote_signaled, and WakeupRecovery().

Referenced by StartupProcessMain().

78 {
79  int save_errno = errno;
80 
81  promote_signaled = true;
83 
84  errno = save_errno;
85 }
void WakeupRecovery(void)
Definition: xlog.c:13168
static volatile sig_atomic_t promote_signaled
Definition: startup.c:54

◆ StartupRereadConfig()

static void StartupRereadConfig ( void  )
static

Definition at line 121 of file startup.c.

References pfree(), PGC_SIGHUP, PrimaryConnInfo, PrimarySlotName, ProcessConfigFile(), pstrdup(), StartupRequestWalReceiverRestart(), and wal_receiver_create_temp_slot.

Referenced by HandleStartupProcInterrupts().

122 {
123  char *conninfo = pstrdup(PrimaryConnInfo);
124  char *slotname = pstrdup(PrimarySlotName);
125  bool tempSlot = wal_receiver_create_temp_slot;
126  bool conninfoChanged;
127  bool slotnameChanged;
128  bool tempSlotChanged = false;
129 
131 
132  conninfoChanged = strcmp(conninfo, PrimaryConnInfo) != 0;
133  slotnameChanged = strcmp(slotname, PrimarySlotName) != 0;
134 
135  /*
136  * wal_receiver_create_temp_slot is used only when we have no slot
137  * configured. We do not need to track this change if it has no effect.
138  */
139  if (!slotnameChanged && strcmp(PrimarySlotName, "") == 0)
140  tempSlotChanged = tempSlot != wal_receiver_create_temp_slot;
141  pfree(conninfo);
142  pfree(slotname);
143 
144  if (conninfoChanged || slotnameChanged || tempSlotChanged)
146 }
void ProcessConfigFile(GucContext context)
char * PrimarySlotName
Definition: xlog.c:288
char * pstrdup(const char *in)
Definition: mcxt.c:1299
void pfree(void *pointer)
Definition: mcxt.c:1169
char * PrimaryConnInfo
Definition: xlog.c:287
Definition: guc.h:72
bool wal_receiver_create_temp_slot
Definition: xlog.c:290
void StartupRequestWalReceiverRestart(void)
Definition: xlog.c:13002

Variable Documentation

◆ got_SIGHUP

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 52 of file startup.c.

Referenced by HandleStartupProcInterrupts(), and StartupProcSigHupHandler().

◆ in_restore_command

volatile sig_atomic_t in_restore_command = false
static

Definition at line 60 of file startup.c.

Referenced by PostRestoreCommand(), PreRestoreCommand(), and StartupProcShutdownHandler().

◆ promote_signaled

volatile sig_atomic_t promote_signaled = false
static

Definition at line 54 of file startup.c.

Referenced by IsPromoteSignaled(), ResetPromoteSignaled(), and StartupProcTriggerHandler().

◆ shutdown_requested

volatile sig_atomic_t shutdown_requested = false
static