PostgreSQL Source Code  git master
startup.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "access/xlog.h"
#include "access/xlogrecovery.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/memutils.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)
 
void startup_progress_timeout_handler (void)
 
void disable_startup_progress_timeout (void)
 
void enable_startup_progress_timeout (void)
 
void begin_startup_progress_phase (void)
 
bool has_startup_progress_timeout_expired (long *secs, int *usecs)
 

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
 
static TimestampTz startup_progress_phase_start_time
 
static volatile sig_atomic_t startup_progress_timer_expired = false
 
int log_startup_progress_interval = 10000
 

Macro Definition Documentation

◆ POSTMASTER_POLL_RATE_LIMIT

#define POSTMASTER_POLL_RATE_LIMIT   1024

Definition at line 50 of file startup.c.

Function Documentation

◆ begin_startup_progress_phase()

void begin_startup_progress_phase ( void  )

Definition at line 367 of file startup.c.

368 {
369  /* Feature is disabled. */
371  return;
372 
375 }
void disable_startup_progress_timeout(void)
Definition: startup.c:333
int log_startup_progress_interval
Definition: startup.c:80
void enable_startup_progress_timeout(void)
Definition: startup.c:347

References disable_startup_progress_timeout(), enable_startup_progress_timeout(), and log_startup_progress_interval.

Referenced by PerformWalRecovery(), ResetUnloggedRelations(), and SyncDataDirectory().

◆ disable_startup_progress_timeout()

void disable_startup_progress_timeout ( void  )

Definition at line 333 of file startup.c.

334 {
335  /* Feature is disabled. */
337  return;
338 
341 }
static volatile sig_atomic_t startup_progress_timer_expired
Definition: startup.c:75
void disable_timeout(TimeoutId id, bool keep_indicator)
Definition: timeout.c:689
@ STARTUP_PROGRESS_TIMEOUT
Definition: timeout.h:37

References disable_timeout(), log_startup_progress_interval, STARTUP_PROGRESS_TIMEOUT, and startup_progress_timer_expired.

Referenced by begin_startup_progress_phase(), and EnableStandbyMode().

◆ enable_startup_progress_timeout()

void enable_startup_progress_timeout ( void  )

Definition at line 347 of file startup.c.

348 {
349  TimestampTz fin_time;
350 
351  /* Feature is disabled. */
353  return;
354 
360 }
static TimestampTz startup_progress_phase_start_time
Definition: startup.c:69
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1649
int64 TimestampTz
Definition: timestamp.h:39
void enable_timeout_every(TimeoutId id, TimestampTz fin_time, int delay_ms)
Definition: timeout.c:588
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:85

References enable_timeout_every(), GetCurrentTimestamp(), log_startup_progress_interval, startup_progress_phase_start_time, STARTUP_PROGRESS_TIMEOUT, and TimestampTzPlusMilliseconds.

Referenced by begin_startup_progress_phase().

◆ HandleStartupProcInterrupts()

void HandleStartupProcInterrupts ( void  )

Definition at line 183 of file startup.c.

184 {
185 #ifdef POSTMASTER_POLL_RATE_LIMIT
186  static uint32 postmaster_poll_count = 0;
187 #endif
188 
189  /*
190  * Process any requests or signals received recently.
191  */
192  if (got_SIGHUP)
193  {
194  got_SIGHUP = false;
196  }
197 
198  /*
199  * Check if we were requested to exit without finishing recovery.
200  */
201  if (shutdown_requested)
202  proc_exit(1);
203 
204  /*
205  * Emergency bailout if postmaster has died. This is to avoid the
206  * necessity for manual cleanup of all postmaster children. Do this less
207  * frequently on systems for which we don't have signals to make that
208  * cheap.
209  */
210  if (IsUnderPostmaster &&
212  postmaster_poll_count++ % POSTMASTER_POLL_RATE_LIMIT == 0 &&
213 #endif
215  exit(1);
216 
217  /* Process barrier events */
220 
221  /* Perform logging of memory contexts of this process */
224 }
#define POSTMASTER_POLL_RATE_LIMIT
Definition: startup.c:50
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:57
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:56
static void StartupRereadConfig(void)
Definition: startup.c:154
unsigned int uint32
Definition: c.h:495
volatile sig_atomic_t LogMemoryContextPending
Definition: globals.c:38
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:37
bool IsUnderPostmaster
Definition: globals.c:115
void proc_exit(int code)
Definition: ipc.c:104
exit(1)
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1199
#define PostmasterIsAlive()
Definition: pmsignal.h:102
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:468

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

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

◆ has_startup_progress_timeout_expired()

bool has_startup_progress_timeout_expired ( long *  secs,
int *  usecs 
)

Definition at line 383 of file startup.c.

384 {
385  long seconds;
386  int useconds;
388 
389  /* No timeout has occurred. */
391  return false;
392 
393  /* Calculate the elapsed time. */
396 
397  *secs = seconds;
398  *usecs = useconds;
400 
401  return true;
402 }
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1725
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1613

References GetCurrentTimestamp(), now(), startup_progress_phase_start_time, startup_progress_timer_expired, and TimestampDifference().

◆ IsPromoteSignaled()

bool IsPromoteSignaled ( void  )

Definition at line 312 of file startup.c.

313 {
314  return promote_signaled;
315 }
static volatile sig_atomic_t promote_signaled
Definition: startup.c:58

References promote_signaled.

Referenced by CheckForStandbyTrigger().

◆ PostRestoreCommand()

void PostRestoreCommand ( void  )

Definition at line 306 of file startup.c.

307 {
308  in_restore_command = false;
309 }
static volatile sig_atomic_t in_restore_command
Definition: startup.c:64

References in_restore_command.

Referenced by RestoreArchivedFile().

◆ PreRestoreCommand()

void PreRestoreCommand ( void  )

Definition at line 292 of file startup.c.

293 {
294  /*
295  * Set in_restore_command to tell the signal handler that we should exit
296  * right away on SIGTERM. We know that we're at a safe point to do that.
297  * Check if we had already received the signal, so that we don't miss a
298  * shutdown request received just before this.
299  */
300  in_restore_command = true;
301  if (shutdown_requested)
302  proc_exit(1);
303 }

References in_restore_command, proc_exit(), and shutdown_requested.

Referenced by RestoreArchivedFile().

◆ ResetPromoteSignaled()

void ResetPromoteSignaled ( void  )

Definition at line 318 of file startup.c.

319 {
320  promote_signaled = false;
321 }

References promote_signaled.

Referenced by CheckForStandbyTrigger().

◆ startup_progress_timeout_handler()

void startup_progress_timeout_handler ( void  )

Definition at line 327 of file startup.c.

328 {
330 }

References startup_progress_timer_expired.

Referenced by StartupXLOG().

◆ StartupProcessMain()

void StartupProcessMain ( void  )

Definition at line 245 of file startup.c.

246 {
247  /* Arrange to clean up at startup process exit */
249 
250  /*
251  * Properly accept or ignore signals the postmaster might send us.
252  */
253  pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
254  pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
255  pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
256  /* SIGQUIT handler was already set up by InitPostmasterChild */
257  InitializeTimeouts(); /* establishes SIGALRM handler */
261 
262  /*
263  * Reset some signals that are accepted by postmaster but not here
264  */
266 
267  /*
268  * Register timeouts needed for standby mode
269  */
273 
274  /*
275  * Unblock signals (they were blocked when the postmaster forked us)
276  */
277  sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
278 
279  /*
280  * Do what we came for.
281  */
282  StartupXLOG();
283 
284  /*
285  * Exit normally. Exit code 0 tells postmaster that we completed recovery
286  * successfully.
287  */
288  proc_exit(0);
289 }
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void StartupProcExit(int code, Datum arg)
Definition: startup.c:232
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:121
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:109
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:97
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
pqsigfunc pqsignal(int signo, pqsigfunc func)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:639
void StandbyTimeoutHandler(void)
Definition: standby.c:945
void StandbyLockTimeoutHandler(void)
Definition: standby.c:954
void StandbyDeadLockHandler(void)
Definition: standby.c:936
void InitializeTimeouts(void)
Definition: timeout.c:474
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:509
@ STANDBY_LOCK_TIMEOUT
Definition: timeout.h:32
@ STANDBY_DEADLOCK_TIMEOUT
Definition: timeout.h:30
@ STANDBY_TIMEOUT
Definition: timeout.h:31
#define SIGCHLD
Definition: win32_port.h:178
#define SIGHUP
Definition: win32_port.h:168
#define SIG_DFL
Definition: win32_port.h:163
#define SIGPIPE
Definition: win32_port.h:173
#define SIGUSR1
Definition: win32_port.h:180
#define SIGUSR2
Definition: win32_port.h:181
#define SIG_IGN
Definition: win32_port.h:165
void StartupXLOG(void)
Definition: xlog.c:5145

References InitializeTimeouts(), on_shmem_exit(), 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().

◆ StartupProcExit()

static void StartupProcExit ( int  code,
Datum  arg 
)
static

Definition at line 232 of file startup.c.

233 {
234  /* Shutdown the recovery environment */
237 }
void ShutdownRecoveryTransactionEnvironment(void)
Definition: standby.c:161
HotStandbyState standbyState
Definition: xlogutils.c:56
@ STANDBY_DISABLED
Definition: xlogutils.h:49

References ShutdownRecoveryTransactionEnvironment(), STANDBY_DISABLED, and standbyState.

Referenced by StartupProcessMain().

◆ StartupProcShutdownHandler()

static void StartupProcShutdownHandler ( SIGNAL_ARGS  )
static

Definition at line 121 of file startup.c.

122 {
123  int save_errno = errno;
124 
125  if (in_restore_command)
126  {
127  /*
128  * If we are in a child process (e.g., forked by system() in
129  * RestoreArchivedFile()), we don't want to call any exit callbacks.
130  * The parent will take care of that.
131  */
132  if (MyProcPid == (int) getpid())
133  proc_exit(1);
134  else
135  {
136  write_stderr_signal_safe("StartupProcShutdownHandler() called in child process\n");
137  _exit(1);
138  }
139  }
140  else
141  shutdown_requested = true;
142  WakeupRecovery();
143 
144  errno = save_errno;
145 }
void write_stderr_signal_safe(const char *str)
Definition: elog.c:3744
int MyProcPid
Definition: globals.c:44
void WakeupRecovery(void)

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

Referenced by StartupProcessMain().

◆ StartupProcSigHupHandler()

static void StartupProcSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 109 of file startup.c.

110 {
111  int save_errno = errno;
112 
113  got_SIGHUP = true;
114  WakeupRecovery();
115 
116  errno = save_errno;
117 }

References got_SIGHUP, and WakeupRecovery().

Referenced by StartupProcessMain().

◆ StartupProcTriggerHandler()

static void StartupProcTriggerHandler ( SIGNAL_ARGS  )
static

Definition at line 97 of file startup.c.

98 {
99  int save_errno = errno;
100 
101  promote_signaled = true;
102  WakeupRecovery();
103 
104  errno = save_errno;
105 }

References promote_signaled, and WakeupRecovery().

Referenced by StartupProcessMain().

◆ StartupRereadConfig()

static void StartupRereadConfig ( void  )
static

Definition at line 154 of file startup.c.

155 {
156  char *conninfo = pstrdup(PrimaryConnInfo);
157  char *slotname = pstrdup(PrimarySlotName);
158  bool tempSlot = wal_receiver_create_temp_slot;
159  bool conninfoChanged;
160  bool slotnameChanged;
161  bool tempSlotChanged = false;
162 
164 
165  conninfoChanged = strcmp(conninfo, PrimaryConnInfo) != 0;
166  slotnameChanged = strcmp(slotname, PrimarySlotName) != 0;
167 
168  /*
169  * wal_receiver_create_temp_slot is used only when we have no slot
170  * configured. We do not need to track this change if it has no effect.
171  */
172  if (!slotnameChanged && strcmp(PrimarySlotName, "") == 0)
173  tempSlotChanged = tempSlot != wal_receiver_create_temp_slot;
174  pfree(conninfo);
175  pfree(slotname);
176 
177  if (conninfoChanged || slotnameChanged || tempSlotChanged)
179 }
@ PGC_SIGHUP
Definition: guc.h:71
void ProcessConfigFile(GucContext context)
char * pstrdup(const char *in)
Definition: mcxt.c:1644
void pfree(void *pointer)
Definition: mcxt.c:1456
void StartupRequestWalReceiverRestart(void)
char * PrimarySlotName
Definition: xlogrecovery.c:97
bool wal_receiver_create_temp_slot
Definition: xlogrecovery.c:98
char * PrimaryConnInfo
Definition: xlogrecovery.c:96

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

Referenced by HandleStartupProcInterrupts().

Variable Documentation

◆ got_SIGHUP

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 56 of file startup.c.

Referenced by HandleStartupProcInterrupts(), and StartupProcSigHupHandler().

◆ in_restore_command

volatile sig_atomic_t in_restore_command = false
static

Definition at line 64 of file startup.c.

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

◆ log_startup_progress_interval

int log_startup_progress_interval = 10000

◆ promote_signaled

volatile sig_atomic_t promote_signaled = false
static

Definition at line 58 of file startup.c.

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

◆ shutdown_requested

volatile sig_atomic_t shutdown_requested = false
static

◆ startup_progress_phase_start_time

TimestampTz startup_progress_phase_start_time
static

◆ startup_progress_timer_expired

volatile sig_atomic_t startup_progress_timer_expired = false
static