PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
startup.c File Reference
#include "postgres.h"
#include "access/xlog.h"
#include "access/xlogrecovery.h"
#include "access/xlogutils.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "postmaster/auxprocess.h"
#include "postmaster/startup.h"
#include "storage/ipc.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 ProcessStartupProcInterrupts (void)
 
void StartupProcessMain (const void *startup_data, size_t startup_data_len)
 
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 46 of file startup.c.

Function Documentation

◆ begin_startup_progress_phase()

void begin_startup_progress_phase ( void  )

Definition at line 347 of file startup.c.

348{
349 /* Feature is disabled. */
351 return;
352
355}
void disable_startup_progress_timeout(void)
Definition: startup.c:313
int log_startup_progress_interval
Definition: startup.c:76
void enable_startup_progress_timeout(void)
Definition: startup.c:327

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

314{
315 /* Feature is disabled. */
317 return;
318
321}
static volatile sig_atomic_t startup_progress_timer_expired
Definition: startup.c:71
void disable_timeout(TimeoutId id, bool keep_indicator)
Definition: timeout.c:685
@ STARTUP_PROGRESS_TIMEOUT
Definition: timeout.h:38

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

328{
329 TimestampTz fin_time;
330
331 /* Feature is disabled. */
333 return;
334
340}
static TimestampTz startup_progress_phase_start_time
Definition: startup.c:65
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
int64 TimestampTz
Definition: timestamp.h:39
void enable_timeout_every(TimeoutId id, TimestampTz fin_time, int delay_ms)
Definition: timeout.c:584
#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().

◆ has_startup_progress_timeout_expired()

bool has_startup_progress_timeout_expired ( long *  secs,
int *  usecs 
)

Definition at line 363 of file startup.c.

364{
365 long seconds;
366 int useconds;
368
369 /* No timeout has occurred. */
371 return false;
372
373 /* Calculate the elapsed time. */
376
377 *secs = seconds;
378 *usecs = useconds;
380
381 return true;
382}
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1721
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609

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

◆ IsPromoteSignaled()

bool IsPromoteSignaled ( void  )

Definition at line 292 of file startup.c.

293{
294 return promote_signaled;
295}
static volatile sig_atomic_t promote_signaled
Definition: startup.c:54

References promote_signaled.

Referenced by CheckForStandbyTrigger().

◆ PostRestoreCommand()

void PostRestoreCommand ( void  )

Definition at line 286 of file startup.c.

287{
288 in_restore_command = false;
289}
static volatile sig_atomic_t in_restore_command
Definition: startup.c:60

References in_restore_command.

Referenced by RestoreArchivedFile().

◆ PreRestoreCommand()

void PreRestoreCommand ( void  )

Definition at line 272 of file startup.c.

273{
274 /*
275 * Set in_restore_command to tell the signal handler that we should exit
276 * right away on SIGTERM. We know that we're at a safe point to do that.
277 * Check if we had already received the signal, so that we don't miss a
278 * shutdown request received just before this.
279 */
280 in_restore_command = true;
282 proc_exit(1);
283}
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:53
void proc_exit(int code)
Definition: ipc.c:104

References in_restore_command, proc_exit(), and shutdown_requested.

Referenced by RestoreArchivedFile().

◆ ProcessStartupProcInterrupts()

void ProcessStartupProcInterrupts ( void  )

Definition at line 154 of file startup.c.

155{
156#ifdef POSTMASTER_POLL_RATE_LIMIT
157 static uint32 postmaster_poll_count = 0;
158#endif
159
160 /*
161 * Process any requests or signals received recently.
162 */
163 if (got_SIGHUP)
164 {
165 got_SIGHUP = false;
167 }
168
169 /*
170 * Check if we were requested to exit without finishing recovery.
171 */
173 proc_exit(1);
174
175 /*
176 * Emergency bailout if postmaster has died. This is to avoid the
177 * necessity for manual cleanup of all postmaster children. Do this less
178 * frequently on systems for which we don't have signals to make that
179 * cheap.
180 */
181 if (IsUnderPostmaster &&
183 postmaster_poll_count++ % POSTMASTER_POLL_RATE_LIMIT == 0 &&
184#endif
186 exit(1);
187
188 /* Process barrier events */
191
192 /* Perform logging of memory contexts of this process */
195
196 /* Publish memory contexts of this process */
199}
#define POSTMASTER_POLL_RATE_LIMIT
Definition: startup.c:46
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:52
static void StartupRereadConfig(void)
Definition: startup.c:125
uint32_t uint32
Definition: c.h:502
volatile sig_atomic_t LogMemoryContextPending
Definition: globals.c:41
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:40
bool IsUnderPostmaster
Definition: globals.c:121
volatile sig_atomic_t PublishMemoryContextPending
Definition: globals.c:42
void ProcessGetMemoryContextInterrupt(void)
Definition: mcxt.c:1436
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1384
#define PostmasterIsAlive()
Definition: pmsignal.h:107
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:498

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

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

◆ ResetPromoteSignaled()

void ResetPromoteSignaled ( void  )

Definition at line 298 of file startup.c.

299{
300 promote_signaled = false;
301}

References promote_signaled.

Referenced by CheckForStandbyTrigger().

◆ startup_progress_timeout_handler()

void startup_progress_timeout_handler ( void  )

Definition at line 307 of file startup.c.

308{
310}

References startup_progress_timer_expired.

Referenced by StartupXLOG().

◆ StartupProcessMain()

void StartupProcessMain ( const void *  startup_data,
size_t  startup_data_len 
)

Definition at line 220 of file startup.c.

221{
222 Assert(startup_data_len == 0);
223
226
227 /* Arrange to clean up at startup process exit */
229
230 /*
231 * Properly accept or ignore signals the postmaster might send us.
232 */
233 pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
234 pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
235 pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
236 /* SIGQUIT handler was already set up by InitPostmasterChild */
237 InitializeTimeouts(); /* establishes SIGALRM handler */
238 pqsignal(SIGPIPE, SIG_IGN);
241
242 /*
243 * Reset some signals that are accepted by postmaster but not here
244 */
245 pqsignal(SIGCHLD, SIG_DFL);
246
247 /*
248 * Register timeouts needed for standby mode
249 */
253
254 /*
255 * Unblock signals (they were blocked when the postmaster forked us)
256 */
257 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
258
259 /*
260 * Do what we came for.
261 */
262 StartupXLOG();
263
264 /*
265 * Exit normally. Exit code 0 tells postmaster that we completed recovery
266 * successfully.
267 */
268 proc_exit(0);
269}
void AuxiliaryProcessMainCommon(void)
Definition: auxprocess.c:39
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void StartupProcExit(int code, Datum arg)
Definition: startup.c:207
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:109
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:101
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:93
Assert(PointerIsAligned(start, uint64))
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
@ B_STARTUP
Definition: miscadmin.h:365
BackendType MyBackendType
Definition: miscinit.c:64
#define pqsignal
Definition: port.h:531
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:673
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:470
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:505
@ 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:168
#define SIGHUP
Definition: win32_port.h:158
#define SIGPIPE
Definition: win32_port.h:163
#define SIGUSR1
Definition: win32_port.h:170
#define SIGUSR2
Definition: win32_port.h:171
void StartupXLOG(void)
Definition: xlog.c:5614

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

◆ StartupProcExit()

static void StartupProcExit ( int  code,
Datum  arg 
)
static

Definition at line 207 of file startup.c.

208{
209 /* Shutdown the recovery environment */
212}
void ShutdownRecoveryTransactionEnvironment(void)
Definition: standby.c:161
HotStandbyState standbyState
Definition: xlogutils.c:53
@ STANDBY_DISABLED
Definition: xlogutils.h:52

References ShutdownRecoveryTransactionEnvironment(), STANDBY_DISABLED, and standbyState.

Referenced by StartupProcessMain().

◆ StartupProcShutdownHandler()

static void StartupProcShutdownHandler ( SIGNAL_ARGS  )
static

Definition at line 109 of file startup.c.

110{
112 proc_exit(1);
113 else
114 shutdown_requested = true;
116}
void WakeupRecovery(void)

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

Referenced by StartupProcessMain().

◆ StartupProcSigHupHandler()

static void StartupProcSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 101 of file startup.c.

102{
103 got_SIGHUP = true;
105}

References got_SIGHUP, and WakeupRecovery().

Referenced by StartupProcessMain().

◆ StartupProcTriggerHandler()

static void StartupProcTriggerHandler ( SIGNAL_ARGS  )
static

Definition at line 93 of file startup.c.

94{
95 promote_signaled = true;
97}

References promote_signaled, and WakeupRecovery().

Referenced by StartupProcessMain().

◆ StartupRereadConfig()

static void StartupRereadConfig ( void  )
static

Definition at line 125 of file startup.c.

126{
127 char *conninfo = pstrdup(PrimaryConnInfo);
128 char *slotname = pstrdup(PrimarySlotName);
129 bool tempSlot = wal_receiver_create_temp_slot;
130 bool conninfoChanged;
131 bool slotnameChanged;
132 bool tempSlotChanged = false;
133
135
136 conninfoChanged = strcmp(conninfo, PrimaryConnInfo) != 0;
137 slotnameChanged = strcmp(slotname, PrimarySlotName) != 0;
138
139 /*
140 * wal_receiver_create_temp_slot is used only when we have no slot
141 * configured. We do not need to track this change if it has no effect.
142 */
143 if (!slotnameChanged && strcmp(PrimarySlotName, "") == 0)
144 tempSlotChanged = tempSlot != wal_receiver_create_temp_slot;
145 pfree(conninfo);
146 pfree(slotname);
147
148 if (conninfoChanged || slotnameChanged || tempSlotChanged)
150}
void ProcessConfigFile(GucContext context)
Definition: guc-file.l:120
@ PGC_SIGHUP
Definition: guc.h:75
char * pstrdup(const char *in)
Definition: mcxt.c:2325
void pfree(void *pointer)
Definition: mcxt.c:2150
void StartupRequestWalReceiverRestart(void)
char * PrimarySlotName
Definition: xlogrecovery.c:98
bool wal_receiver_create_temp_slot
Definition: xlogrecovery.c:99
char * PrimaryConnInfo
Definition: xlogrecovery.c:97

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

Referenced by ProcessStartupProcInterrupts().

Variable Documentation

◆ got_SIGHUP

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 52 of file startup.c.

Referenced by ProcessStartupProcInterrupts(), 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().

◆ log_startup_progress_interval

int log_startup_progress_interval = 10000

◆ 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

◆ 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