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/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 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 47 of file startup.c.

Function Documentation

◆ begin_startup_progress_phase()

void begin_startup_progress_phase ( void  )

Definition at line 320 of file startup.c.

321 {
322  TimestampTz fin_time;
323 
324  /* Feature is disabled. */
326  return;
327 
335 }
static volatile sig_atomic_t startup_progress_timer_expired
Definition: startup.c:72
static TimestampTz startup_progress_phase_start_time
Definition: startup.c:66
int log_startup_progress_interval
Definition: startup.c:77
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
int64 TimestampTz
Definition: timestamp.h:39
void enable_timeout_every(TimeoutId id, TimestampTz fin_time, int delay_ms)
Definition: timeout.c:575
void disable_timeout(TimeoutId id, bool keep_indicator)
Definition: timeout.c:676
@ STARTUP_PROGRESS_TIMEOUT
Definition: timeout.h:36
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:56

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

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

◆ HandleStartupProcInterrupts()

void HandleStartupProcInterrupts ( void  )

Definition at line 167 of file startup.c.

168 {
169 #ifdef POSTMASTER_POLL_RATE_LIMIT
170  static uint32 postmaster_poll_count = 0;
171 #endif
172 
173  /*
174  * Process any requests or signals received recently.
175  */
176  if (got_SIGHUP)
177  {
178  got_SIGHUP = false;
180  }
181 
182  /*
183  * Check if we were requested to exit without finishing recovery.
184  */
185  if (shutdown_requested)
186  proc_exit(1);
187 
188  /*
189  * Emergency bailout if postmaster has died. This is to avoid the
190  * necessity for manual cleanup of all postmaster children. Do this less
191  * frequently on systems for which we don't have signals to make that
192  * cheap.
193  */
194  if (IsUnderPostmaster &&
196  postmaster_poll_count++ % POSTMASTER_POLL_RATE_LIMIT == 0 &&
197 #endif
199  exit(1);
200 
201  /* Process barrier events */
204 
205  /* Perform logging of memory contexts of this process */
208 }
#define POSTMASTER_POLL_RATE_LIMIT
Definition: startup.c:47
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:54
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:53
static void StartupRereadConfig(void)
Definition: startup.c:138
unsigned int uint32
Definition: c.h:441
volatile sig_atomic_t LogMemoryContextPending
Definition: globals.c:38
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:37
bool IsUnderPostmaster
Definition: globals.c:112
void proc_exit(int code)
Definition: ipc.c:104
exit(1)
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1041
#define PostmasterIsAlive()
Definition: pmsignal.h:102
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:453

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

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

◆ has_startup_progress_timeout_expired()

bool has_startup_progress_timeout_expired ( long *  secs,
int *  usecs 
)

Definition at line 343 of file startup.c.

344 {
345  long seconds;
346  int useconds;
348 
349  /* No timeout has occurred. */
351  return false;
352 
353  /* Calculate the elapsed time. */
356 
357  *secs = seconds;
358  *usecs = useconds;
360 
361  return true;
362 }
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1656
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544

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

◆ IsPromoteSignaled()

bool IsPromoteSignaled ( void  )

Definition at line 296 of file startup.c.

297 {
298  return promote_signaled;
299 }
static volatile sig_atomic_t promote_signaled
Definition: startup.c:55

References promote_signaled.

Referenced by CheckForStandbyTrigger().

◆ PostRestoreCommand()

void PostRestoreCommand ( void  )

Definition at line 290 of file startup.c.

291 {
292  in_restore_command = false;
293 }
static volatile sig_atomic_t in_restore_command
Definition: startup.c:61

References in_restore_command.

Referenced by RestoreArchivedFile().

◆ PreRestoreCommand()

void PreRestoreCommand ( void  )

Definition at line 276 of file startup.c.

277 {
278  /*
279  * Set in_restore_command to tell the signal handler that we should exit
280  * right away on SIGTERM. We know that we're at a safe point to do that.
281  * Check if we had already received the signal, so that we don't miss a
282  * shutdown request received just before this.
283  */
284  in_restore_command = true;
285  if (shutdown_requested)
286  proc_exit(1);
287 }

References in_restore_command, proc_exit(), and shutdown_requested.

Referenced by RestoreArchivedFile().

◆ ResetPromoteSignaled()

void ResetPromoteSignaled ( void  )

Definition at line 302 of file startup.c.

303 {
304  promote_signaled = false;
305 }

References promote_signaled.

Referenced by CheckForStandbyTrigger().

◆ startup_progress_timeout_handler()

void startup_progress_timeout_handler ( void  )

Definition at line 311 of file startup.c.

312 {
314 }

References startup_progress_timer_expired.

Referenced by StartupXLOG().

◆ StartupProcessMain()

void StartupProcessMain ( void  )

Definition at line 229 of file startup.c.

230 {
231  /* Arrange to clean up at startup process exit */
233 
234  /*
235  * Properly accept or ignore signals the postmaster might send us.
236  */
237  pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
238  pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
239  pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
240  /* SIGQUIT handler was already set up by InitPostmasterChild */
241  InitializeTimeouts(); /* establishes SIGALRM handler */
245 
246  /*
247  * Reset some signals that are accepted by postmaster but not here
248  */
250 
251  /*
252  * Register timeouts needed for standby mode
253  */
257 
258  /*
259  * Unblock signals (they were blocked when the postmaster forked us)
260  */
262 
263  /*
264  * Do what we came for.
265  */
266  StartupXLOG();
267 
268  /*
269  * Exit normally. Exit code 0 tells postmaster that we completed recovery
270  * successfully.
271  */
272  proc_exit(0);
273 }
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void StartupProcExit(int code, Datum arg)
Definition: startup.c:216
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:118
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:106
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:94
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:642
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:180
void StandbyTimeoutHandler(void)
Definition: standby.c:904
void StandbyLockTimeoutHandler(void)
Definition: standby.c:916
void StandbyDeadLockHandler(void)
Definition: standby.c:893
void InitializeTimeouts(void)
Definition: timeout.c:461
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:496
@ 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:177
#define SIGHUP
Definition: win32_port.h:167
#define SIG_DFL
Definition: win32_port.h:162
#define SIGPIPE
Definition: win32_port.h:172
#define SIGUSR1
Definition: win32_port.h:179
#define SIGUSR2
Definition: win32_port.h:180
#define SIG_IGN
Definition: win32_port.h:164
void StartupXLOG(void)
Definition: xlog.c:6680

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().

◆ StartupProcExit()

static void StartupProcExit ( int  code,
Datum  arg 
)
static

Definition at line 216 of file startup.c.

217 {
218  /* Shutdown the recovery environment */
221 }
void ShutdownRecoveryTransactionEnvironment(void)
Definition: standby.c:138
HotStandbyState standbyState
Definition: xlogutils.c:55
@ 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 118 of file startup.c.

119 {
120  int save_errno = errno;
121 
122  if (in_restore_command)
123  proc_exit(1);
124  else
125  shutdown_requested = true;
126  WakeupRecovery();
127 
128  errno = save_errno;
129 }
void WakeupRecovery(void)
Definition: xlog.c:13222

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

Referenced by StartupProcessMain().

◆ StartupProcSigHupHandler()

static void StartupProcSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 106 of file startup.c.

107 {
108  int save_errno = errno;
109 
110  got_SIGHUP = true;
111  WakeupRecovery();
112 
113  errno = save_errno;
114 }

References got_SIGHUP, and WakeupRecovery().

Referenced by StartupProcessMain().

◆ StartupProcTriggerHandler()

static void StartupProcTriggerHandler ( SIGNAL_ARGS  )
static

Definition at line 94 of file startup.c.

95 {
96  int save_errno = errno;
97 
98  promote_signaled = true;
100 
101  errno = save_errno;
102 }

References promote_signaled, and WakeupRecovery().

Referenced by StartupProcessMain().

◆ StartupRereadConfig()

static void StartupRereadConfig ( void  )
static

Definition at line 138 of file startup.c.

139 {
140  char *conninfo = pstrdup(PrimaryConnInfo);
141  char *slotname = pstrdup(PrimarySlotName);
142  bool tempSlot = wal_receiver_create_temp_slot;
143  bool conninfoChanged;
144  bool slotnameChanged;
145  bool tempSlotChanged = false;
146 
148 
149  conninfoChanged = strcmp(conninfo, PrimaryConnInfo) != 0;
150  slotnameChanged = strcmp(slotname, PrimarySlotName) != 0;
151 
152  /*
153  * wal_receiver_create_temp_slot is used only when we have no slot
154  * configured. We do not need to track this change if it has no effect.
155  */
156  if (!slotnameChanged && strcmp(PrimarySlotName, "") == 0)
157  tempSlotChanged = tempSlot != wal_receiver_create_temp_slot;
158  pfree(conninfo);
159  pfree(slotname);
160 
161  if (conninfoChanged || slotnameChanged || tempSlotChanged)
163 }
@ PGC_SIGHUP
Definition: guc.h:72
void ProcessConfigFile(GucContext context)
char * pstrdup(const char *in)
Definition: mcxt.c:1299
void pfree(void *pointer)
Definition: mcxt.c:1169
void StartupRequestWalReceiverRestart(void)
Definition: xlog.c:13056
char * PrimarySlotName
Definition: xlog.c:284
bool wal_receiver_create_temp_slot
Definition: xlog.c:286
char * PrimaryConnInfo
Definition: xlog.c:283

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 53 of file startup.c.

Referenced by HandleStartupProcInterrupts(), and StartupProcSigHupHandler().

◆ in_restore_command

volatile sig_atomic_t in_restore_command = false
static

Definition at line 61 of file startup.c.

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

◆ log_startup_progress_interval

int log_startup_progress_interval = 10000

Definition at line 77 of file startup.c.

Referenced by begin_startup_progress_phase().

◆ promote_signaled

volatile sig_atomic_t promote_signaled = false
static

Definition at line 55 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

Definition at line 66 of file startup.c.

Referenced by begin_startup_progress_phase(), and has_startup_progress_timeout_expired().

◆ startup_progress_timer_expired

volatile sig_atomic_t startup_progress_timer_expired = false
static