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.

Functions

static void StartupProcTriggerHandler (SIGNAL_ARGS)
 
static void StartupProcSigHupHandler (SIGNAL_ARGS)
 
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
 

Function Documentation

◆ HandleStartupProcInterrupts()

void HandleStartupProcInterrupts ( void  )

Definition at line 135 of file startup.c.

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

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

136 {
137  /*
138  * Process any requests or signals received recently.
139  */
140  if (got_SIGHUP)
141  {
142  got_SIGHUP = false;
144  }
145 
146  /*
147  * Check if we were requested to exit without finishing recovery.
148  */
149  if (shutdown_requested)
150  proc_exit(1);
151 
152  /*
153  * Emergency bailout if postmaster has died. This is to avoid the
154  * necessity for manual cleanup of all postmaster children.
155  */
157  exit(1);
158 
159  /* Process barrier events */
162 }
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:429
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:41
void proc_exit(int code)
Definition: ipc.c:104
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:40
bool IsUnderPostmaster
Definition: globals.c:109
#define PostmasterIsAlive()
Definition: pmsignal.h:91
static void StartupRereadConfig(void)
Definition: startup.c:106
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:35

◆ IsPromoteSignaled()

bool IsPromoteSignaled ( void  )

Definition at line 234 of file startup.c.

References promote_signaled.

Referenced by CheckForStandbyTrigger().

235 {
236  return promote_signaled;
237 }
static volatile sig_atomic_t promote_signaled
Definition: startup.c:42

◆ PostRestoreCommand()

void PostRestoreCommand ( void  )

Definition at line 228 of file startup.c.

References in_restore_command.

Referenced by RestoreArchivedFile().

229 {
230  in_restore_command = false;
231 }
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48

◆ PreRestoreCommand()

void PreRestoreCommand ( void  )

Definition at line 214 of file startup.c.

References in_restore_command, proc_exit(), and shutdown_requested.

Referenced by RestoreArchivedFile().

215 {
216  /*
217  * Set in_restore_command to tell the signal handler that we should exit
218  * right away on SIGTERM. We know that we're at a safe point to do that.
219  * Check if we had already received the signal, so that we don't miss a
220  * shutdown request received just before this.
221  */
222  in_restore_command = true;
223  if (shutdown_requested)
224  proc_exit(1);
225 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:41
void proc_exit(int code)
Definition: ipc.c:104
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48

◆ ResetPromoteSignaled()

void ResetPromoteSignaled ( void  )

Definition at line 240 of file startup.c.

References promote_signaled.

Referenced by CheckForStandbyTrigger().

241 {
242  promote_signaled = false;
243 }
static volatile sig_atomic_t promote_signaled
Definition: startup.c:42

◆ StartupProcessMain()

void StartupProcessMain ( void  )

Definition at line 170 of file startup.c.

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

Referenced by AuxiliaryProcessMain().

171 {
172  /*
173  * Properly accept or ignore signals the postmaster might send us.
174  */
175  pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
176  pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
177  pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
179  InitializeTimeouts(); /* establishes SIGALRM handler */
183 
184  /*
185  * Reset some signals that are accepted by postmaster but not here
186  */
188 
189  /*
190  * Register timeouts needed for standby mode
191  */
195 
196  /*
197  * Unblock signals (they were blocked when the postmaster forked us)
198  */
200 
201  /*
202  * Do what we came for.
203  */
204  StartupXLOG();
205 
206  /*
207  * Exit normally. Exit code 0 tells postmaster that we completed recovery
208  * successfully.
209  */
210  proc_exit(0);
211 }
void InitializeTimeouts(void)
Definition: timeout.c:346
#define SIGQUIT
Definition: win32_port.h:154
void StandbyTimeoutHandler(void)
Definition: standby.c:607
#define SIGUSR1
Definition: win32_port.h:165
#define SIGCHLD
Definition: win32_port.h:163
void proc_exit(int code)
Definition: ipc.c:104
#define SIGPIPE
Definition: win32_port.h:158
#define SIGUSR2
Definition: win32_port.h:166
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void SignalHandlerForCrashExit(SIGNAL_ARGS)
Definition: interrupt.c:72
static void StartupProcSigHupHandler(SIGNAL_ARGS)
Definition: startup.c:74
#define SIGHUP
Definition: win32_port.h:153
void StandbyLockTimeoutHandler(void)
Definition: standby.c:620
void StandbyDeadLockHandler(void)
Definition: standby.c:596
sigset_t UnBlockSig
Definition: pqsignal.c:22
void StartupXLOG(void)
Definition: xlog.c:6302
#define SIG_IGN
Definition: win32_port.h:150
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler)
Definition: timeout.c:380
#define SIG_DFL
Definition: win32_port.h:148
static void StartupProcTriggerHandler(SIGNAL_ARGS)
Definition: startup.c:62
static void StartupProcShutdownHandler(SIGNAL_ARGS)
Definition: startup.c:86
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:533

◆ StartupProcShutdownHandler()

static void StartupProcShutdownHandler ( SIGNAL_ARGS  )
static

Definition at line 86 of file startup.c.

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

Referenced by StartupProcessMain().

87 {
88  int save_errno = errno;
89 
91  proc_exit(1);
92  else
93  shutdown_requested = true;
95 
96  errno = save_errno;
97 }
static volatile sig_atomic_t shutdown_requested
Definition: startup.c:41
void proc_exit(int code)
Definition: ipc.c:104
void WakeupRecovery(void)
Definition: xlog.c:12660
static volatile sig_atomic_t in_restore_command
Definition: startup.c:48

◆ StartupProcSigHupHandler()

static void StartupProcSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 74 of file startup.c.

References got_SIGHUP, and WakeupRecovery().

Referenced by StartupProcessMain().

75 {
76  int save_errno = errno;
77 
78  got_SIGHUP = true;
80 
81  errno = save_errno;
82 }
static volatile sig_atomic_t got_SIGHUP
Definition: startup.c:40
void WakeupRecovery(void)
Definition: xlog.c:12660

◆ StartupProcTriggerHandler()

static void StartupProcTriggerHandler ( SIGNAL_ARGS  )
static

Definition at line 62 of file startup.c.

References promote_signaled, and WakeupRecovery().

Referenced by StartupProcessMain().

63 {
64  int save_errno = errno;
65 
66  promote_signaled = true;
68 
69  errno = save_errno;
70 }
void WakeupRecovery(void)
Definition: xlog.c:12660
static volatile sig_atomic_t promote_signaled
Definition: startup.c:42

◆ StartupRereadConfig()

static void StartupRereadConfig ( void  )
static

Definition at line 106 of file startup.c.

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

Referenced by HandleStartupProcInterrupts().

107 {
108  char *conninfo = pstrdup(PrimaryConnInfo);
109  char *slotname = pstrdup(PrimarySlotName);
110  bool tempSlot = wal_receiver_create_temp_slot;
111  bool conninfoChanged;
112  bool slotnameChanged;
113  bool tempSlotChanged = false;
114 
116 
117  conninfoChanged = strcmp(conninfo, PrimaryConnInfo) != 0;
118  slotnameChanged = strcmp(slotname, PrimarySlotName) != 0;
119 
120  /*
121  * wal_receiver_create_temp_slot is used only when we have no slot
122  * configured. We do not need to track this change if it has no effect.
123  */
124  if (!slotnameChanged && strcmp(PrimarySlotName, "") == 0)
125  tempSlotChanged = tempSlot != wal_receiver_create_temp_slot;
126  pfree(conninfo);
127  pfree(slotname);
128 
129  if (conninfoChanged || slotnameChanged || tempSlotChanged)
131 }
void ProcessConfigFile(GucContext context)
char * PrimarySlotName
Definition: xlog.c:295
char * pstrdup(const char *in)
Definition: mcxt.c:1186
void pfree(void *pointer)
Definition: mcxt.c:1056
char * PrimaryConnInfo
Definition: xlog.c:294
Definition: guc.h:72
bool wal_receiver_create_temp_slot
Definition: xlog.c:297
void StartupRequestWalReceiverRestart(void)
Definition: xlog.c:12490

Variable Documentation

◆ got_SIGHUP

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 40 of file startup.c.

Referenced by HandleStartupProcInterrupts(), and StartupProcSigHupHandler().

◆ in_restore_command

volatile sig_atomic_t in_restore_command = false
static

Definition at line 48 of file startup.c.

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

◆ promote_signaled

volatile sig_atomic_t promote_signaled = false
static

Definition at line 42 of file startup.c.

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

◆ shutdown_requested

volatile sig_atomic_t shutdown_requested = false
static