Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions | Variables

startup.c File Reference

#include "postgres.h"
#include <signal.h>
#include <unistd.h>
#include "access/xlog.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "postmaster/startup.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/pmsignal.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 startupproc_quickdie (SIGNAL_ARGS)
static void StartupProcSigUsr1Handler (SIGNAL_ARGS)
static void StartupProcTriggerHandler (SIGNAL_ARGS)
static void StartupProcSigHupHandler (SIGNAL_ARGS)
static void StartupProcShutdownHandler (SIGNAL_ARGS)
void HandleStartupProcInterrupts (void)
void StartupProcessMain (void)
void PreRestoreCommand (void)
void PostRestoreCommand (void)
bool IsPromoteTriggered (void)
void ResetPromoteTriggered (void)

Variables

static volatile sig_atomic_t got_SIGHUP = false
static volatile sig_atomic_t shutdown_requested = false
static volatile sig_atomic_t promote_triggered = false
static volatile sig_atomic_t in_restore_command = false

Function Documentation

void HandleStartupProcInterrupts ( void   ) 

Definition at line 147 of file startup.c.

References got_SIGHUP, IsUnderPostmaster, PGC_SIGHUP, PostmasterIsAlive(), proc_exit(), ProcessConfigFile(), and shutdown_requested.

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

{
    /*
     * Check if we were requested to re-read config file.
     */
    if (got_SIGHUP)
    {
        got_SIGHUP = false;
        ProcessConfigFile(PGC_SIGHUP);
    }

    /*
     * Check if we were requested to exit without finishing recovery.
     */
    if (shutdown_requested)
        proc_exit(1);

    /*
     * Emergency bailout if postmaster has died.  This is to avoid the
     * necessity for manual cleanup of all postmaster children.
     */
    if (IsUnderPostmaster && !PostmasterIsAlive())
        exit(1);
}

bool IsPromoteTriggered ( void   ) 

Definition at line 254 of file startup.c.

References promote_triggered.

Referenced by CheckForStandbyTrigger().

{
    return promote_triggered;
}

void PostRestoreCommand ( void   ) 

Definition at line 248 of file startup.c.

References in_restore_command.

Referenced by RestoreArchivedFile().

{
    in_restore_command = false;
}

void PreRestoreCommand ( void   ) 

Definition at line 234 of file startup.c.

References in_restore_command, proc_exit(), and shutdown_requested.

Referenced by RestoreArchivedFile().

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

void ResetPromoteTriggered ( void   ) 

Definition at line 260 of file startup.c.

References promote_triggered.

Referenced by CheckForStandbyTrigger().

{
    promote_triggered = false;
}

static void startupproc_quickdie ( SIGNAL_ARGS   )  [static]

Definition at line 69 of file startup.c.

References BlockSig, on_exit_reset(), and PG_SETMASK.

Referenced by StartupProcessMain().

{
    PG_SETMASK(&BlockSig);

    /*
     * We DO NOT want to run proc_exit() callbacks -- we're here because
     * shared memory may be corrupted, so we don't want to try to clean up our
     * transaction.  Just nail the windows shut and get out of town.  Now that
     * there's an atexit callback to prevent third-party code from breaking
     * things by calling exit() directly, we have to reset the callbacks
     * explicitly to make this work as intended.
     */
    on_exit_reset();

    /*
     * Note we do exit(2) not exit(0).  This is to force the postmaster into a
     * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
     * backend.  This is necessary precisely because we don't clean up our
     * shared memory state.  (The "dead man switch" mechanism in pmsignal.c
     * should ensure the postmaster sees this as a crash, too, but no harm in
     * being doubly sure.)
     */
    exit(2);
}

void StartupProcessMain ( void   ) 

Definition at line 178 of file startup.c.

References elog, FATAL, InitializeTimeouts(), PG_SETMASK, pqsignal(), proc_exit(), RegisterTimeout(), SIG_DFL, SIG_IGN, SIGCHLD, SIGCONT, SIGHUP, SIGPIPE, SIGQUIT, SIGTTIN, SIGTTOU, SIGUSR1, SIGUSR2, SIGWINCH, STANDBY_DEADLOCK_TIMEOUT, STANDBY_TIMEOUT, StandbyDeadLockHandler(), StandbyTimeoutHandler(), startupproc_quickdie(), StartupProcShutdownHandler(), StartupProcSigHupHandler(), StartupProcSigUsr1Handler(), StartupProcTriggerHandler(), StartupXLOG(), and UnBlockSig.

Referenced by AuxiliaryProcessMain().

{
    /*
     * If possible, make this process a group leader, so that the postmaster
     * can signal any child processes too.
     */
#ifdef HAVE_SETSID
    if (setsid() < 0)
        elog(FATAL, "setsid() failed: %m");
#endif

    /*
     * Properly accept or ignore signals the postmaster might send us.
     */
    pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
    pqsignal(SIGINT, SIG_IGN);  /* ignore query cancel */
    pqsignal(SIGTERM, StartupProcShutdownHandler);      /* request shutdown */
    pqsignal(SIGQUIT, startupproc_quickdie);    /* hard crash time */
    InitializeTimeouts();       /* establishes SIGALRM handler */
    pqsignal(SIGPIPE, SIG_IGN);
    pqsignal(SIGUSR1, StartupProcSigUsr1Handler);
    pqsignal(SIGUSR2, StartupProcTriggerHandler);

    /*
     * Reset some signals that are accepted by postmaster but not here
     */
    pqsignal(SIGCHLD, SIG_DFL);
    pqsignal(SIGTTIN, SIG_DFL);
    pqsignal(SIGTTOU, SIG_DFL);
    pqsignal(SIGCONT, SIG_DFL);
    pqsignal(SIGWINCH, SIG_DFL);

    /*
     * Register timeouts needed for standby mode
     */
    RegisterTimeout(STANDBY_DEADLOCK_TIMEOUT, StandbyDeadLockHandler);
    RegisterTimeout(STANDBY_TIMEOUT, StandbyTimeoutHandler);

    /*
     * Unblock signals (they were blocked when the postmaster forked us)
     */
    PG_SETMASK(&UnBlockSig);

    /*
     * Do what we came for.
     */
    StartupXLOG();

    /*
     * Exit normally. Exit code 0 tells postmaster that we completed recovery
     * successfully.
     */
    proc_exit(0);
}

static void StartupProcShutdownHandler ( SIGNAL_ARGS   )  [static]

Definition at line 132 of file startup.c.

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

Referenced by StartupProcessMain().

{
    int         save_errno = errno;

    if (in_restore_command)
        proc_exit(1);
    else
        shutdown_requested = true;
    WakeupRecovery();

    errno = save_errno;
}

static void StartupProcSigHupHandler ( SIGNAL_ARGS   )  [static]

Definition at line 120 of file startup.c.

References got_SIGHUP, and WakeupRecovery().

Referenced by StartupProcessMain().

{
    int         save_errno = errno;

    got_SIGHUP = true;
    WakeupRecovery();

    errno = save_errno;
}

static void StartupProcSigUsr1Handler ( SIGNAL_ARGS   )  [static]

Definition at line 97 of file startup.c.

References latch_sigusr1_handler().

Referenced by StartupProcessMain().

{
    int         save_errno = errno;

    latch_sigusr1_handler();

    errno = save_errno;
}

static void StartupProcTriggerHandler ( SIGNAL_ARGS   )  [static]

Definition at line 108 of file startup.c.

References promote_triggered, and WakeupRecovery().

Referenced by StartupProcessMain().

{
    int         save_errno = errno;

    promote_triggered = true;
    WakeupRecovery();

    errno = save_errno;
}


Variable Documentation

volatile sig_atomic_t got_SIGHUP = false [static]

Definition at line 40 of file startup.c.

Referenced by HandleStartupProcInterrupts(), and StartupProcSigHupHandler().

volatile sig_atomic_t in_restore_command = false [static]

Definition at line 48 of file startup.c.

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

volatile sig_atomic_t promote_triggered = false [static]

Definition at line 42 of file startup.c.

Referenced by IsPromoteTriggered(), ResetPromoteTriggered(), and StartupProcTriggerHandler().

volatile sig_atomic_t shutdown_requested = false [static]