PostgreSQL Source Code  git master
startup.c File Reference
#include "postgres.h"
#include "access/xlog.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 45 of file startup.c.

Referenced by HandleStartupProcInterrupts().

Function Documentation

◆ HandleStartupProcInterrupts()

void HandleStartupProcInterrupts ( void  )

Definition at line 149 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().

150 {
151 #ifdef POSTMASTER_POLL_RATE_LIMIT
152  static uint32 postmaster_poll_count = 0;
153 #endif
154 
155  /*
156  * Process any requests or signals received recently.
157  */
158  if (got_SIGHUP)
159  {
160  got_SIGHUP = false;
162  }
163 
164  /*
165  * Check if we were requested to exit without finishing recovery.
166  */
167  if (shutdown_requested)
168  proc_exit(1);
169 
170  /*
171  * Emergency bailout if postmaster has died. This is to avoid the
172  * necessity for manual cleanup of all postmaster children. Do this less
173  * frequently on systems for which we don't have signals to make that
174  * cheap.
175  */
176  if (IsUnderPostmaster &&
178  postmaster_poll_count++ % POSTMASTER_POLL_RATE_LIMIT == 0 &&
179 #endif
181  exit(1);
182 
183  /* Process barrier events */
186 }
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:453
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:52
void proc_exit(int code)
Definition: ipc.c:104
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:51
#define POSTMASTER_POLL_RATE_LIMIT
Definition: startup.c:45
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:120
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:37

◆ IsPromoteSignaled()

bool IsPromoteSignaled ( void  )

Definition at line 274 of file startup.c.

References promote_signaled.

Referenced by CheckForStandbyTrigger().

275 {
276  return promote_signaled;
277 }
static volatile sig_atomic_t promote_signaled
Definition: startup.c:53

◆ PostRestoreCommand()

void PostRestoreCommand ( void  )

Definition at line 268 of file startup.c.

References in_restore_command.

Referenced by RestoreArchivedFile().

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

◆ PreRestoreCommand()

void PreRestoreCommand ( void  )

Definition at line 254 of file startup.c.

References in_restore_command, proc_exit(), and shutdown_requested.

Referenced by RestoreArchivedFile().

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

◆ ResetPromoteSignaled()

void ResetPromoteSignaled ( void  )

Definition at line 280 of file startup.c.

References promote_signaled.

Referenced by CheckForStandbyTrigger().

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

◆ StartupProcessMain()

void StartupProcessMain ( void  )

Definition at line 207 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().

208 {
209  /* Arrange to clean up at startup process exit */
211 
212  /*
213  * Properly accept or ignore signals the postmaster might send us.
214  */
215  pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
216  pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
217  pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
218  /* SIGQUIT handler was already set up by InitPostmasterChild */
219  InitializeTimeouts(); /* establishes SIGALRM handler */
223 
224  /*
225  * Reset some signals that are accepted by postmaster but not here
226  */
228 
229  /*
230  * Register timeouts needed for standby mode
231  */
235 
236  /*
237  * Unblock signals (they were blocked when the postmaster forked us)
238  */
240 
241  /*
242  * Do what we came for.
243  */
244  StartupXLOG();
245 
246  /*
247  * Exit normally. Exit code 0 tells postmaster that we completed recovery
248  * successfully.
249  */
250  proc_exit(0);
251 }
void InitializeTimeouts(void)
Definition: timeout.c:435
void StandbyTimeoutHandler(void)
Definition: standby.c:904
#define SIGUSR1
Definition: win32_port.h:171
#define SIGCHLD
Definition: win32_port.h:169
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
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:88
#define SIGHUP
Definition: win32_port.h:159
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:6450
#define SIG_IGN
Definition: win32_port.h:156
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:154
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:76
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:100
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:642
static void StartupProcExit(int code, Datum arg)
Definition: startup.c:194

◆ StartupProcExit()

static void StartupProcExit ( int  code,
Datum  arg 
)
static

Definition at line 194 of file startup.c.

References ShutdownRecoveryTransactionEnvironment(), STANDBY_DISABLED, and standbyState.

Referenced by StartupProcessMain().

195 {
196  /* Shutdown the recovery environment */
199 }
void ShutdownRecoveryTransactionEnvironment(void)
Definition: standby.c:138
HotStandbyState standbyState
Definition: xlog.c:212

◆ StartupProcShutdownHandler()

static void StartupProcShutdownHandler ( SIGNAL_ARGS  )
static

Definition at line 100 of file startup.c.

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

Referenced by StartupProcessMain().

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

◆ StartupProcSigHupHandler()

static void StartupProcSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 88 of file startup.c.

References got_SIGHUP, and WakeupRecovery().

Referenced by StartupProcessMain().

89 {
90  int save_errno = errno;
91 
92  got_SIGHUP = true;
94 
95  errno = save_errno;
96 }
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:51
void WakeupRecovery(void)
Definition: xlog.c:12935

◆ StartupProcTriggerHandler()

static void StartupProcTriggerHandler ( SIGNAL_ARGS  )
static

Definition at line 76 of file startup.c.

References promote_signaled, and WakeupRecovery().

Referenced by StartupProcessMain().

77 {
78  int save_errno = errno;
79 
80  promote_signaled = true;
82 
83  errno = save_errno;
84 }
void WakeupRecovery(void)
Definition: xlog.c:12935
static volatile sig_atomic_t promote_signaled
Definition: startup.c:53

◆ StartupRereadConfig()

static void StartupRereadConfig ( void  )
static

Definition at line 120 of file startup.c.

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

Referenced by HandleStartupProcInterrupts().

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

Variable Documentation

◆ got_SIGHUP

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 51 of file startup.c.

Referenced by HandleStartupProcInterrupts(), and StartupProcSigHupHandler().

◆ in_restore_command

volatile sig_atomic_t in_restore_command = false
static

Definition at line 59 of file startup.c.

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

◆ promote_signaled

volatile sig_atomic_t promote_signaled = false
static

Definition at line 53 of file startup.c.

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

◆ shutdown_requested

volatile sig_atomic_t shutdown_requested = false
static