LCOV - code coverage report
Current view: top level - src/backend/postmaster - postmaster.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 914 1184 77.2 %
Date: 2025-08-09 08:18:06 Functions: 49 52 94.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * postmaster.c
       4             :  *    This program acts as a clearing house for requests to the
       5             :  *    POSTGRES system.  Frontend programs connect to the Postmaster,
       6             :  *    and postmaster forks a new backend process to handle the
       7             :  *    connection.
       8             :  *
       9             :  *    The postmaster also manages system-wide operations such as
      10             :  *    startup and shutdown. The postmaster itself doesn't do those
      11             :  *    operations, mind you --- it just forks off a subprocess to do them
      12             :  *    at the right times.  It also takes care of resetting the system
      13             :  *    if a backend crashes.
      14             :  *
      15             :  *    The postmaster process creates the shared memory and semaphore
      16             :  *    pools during startup, but as a rule does not touch them itself.
      17             :  *    In particular, it is not a member of the PGPROC array of backends
      18             :  *    and so it cannot participate in lock-manager operations.  Keeping
      19             :  *    the postmaster away from shared memory operations makes it simpler
      20             :  *    and more reliable.  The postmaster is almost always able to recover
      21             :  *    from crashes of individual backends by resetting shared memory;
      22             :  *    if it did much with shared memory then it would be prone to crashing
      23             :  *    along with the backends.
      24             :  *
      25             :  *    When a request message is received, we now fork() immediately.
      26             :  *    The child process performs authentication of the request, and
      27             :  *    then becomes a backend if successful.  This allows the auth code
      28             :  *    to be written in a simple single-threaded style (as opposed to the
      29             :  *    crufty "poor man's multitasking" code that used to be needed).
      30             :  *    More importantly, it ensures that blockages in non-multithreaded
      31             :  *    libraries like SSL or PAM cannot cause denial of service to other
      32             :  *    clients.
      33             :  *
      34             :  *
      35             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
      36             :  * Portions Copyright (c) 1994, Regents of the University of California
      37             :  *
      38             :  *
      39             :  * IDENTIFICATION
      40             :  *    src/backend/postmaster/postmaster.c
      41             :  *
      42             :  * NOTES
      43             :  *
      44             :  * Initialization:
      45             :  *      The Postmaster sets up shared memory data structures
      46             :  *      for the backends.
      47             :  *
      48             :  * Synchronization:
      49             :  *      The Postmaster shares memory with the backends but should avoid
      50             :  *      touching shared memory, so as not to become stuck if a crashing
      51             :  *      backend screws up locks or shared memory.  Likewise, the Postmaster
      52             :  *      should never block on messages from frontend clients.
      53             :  *
      54             :  * Garbage Collection:
      55             :  *      The Postmaster cleans up after backends if they have an emergency
      56             :  *      exit and/or core dump.
      57             :  *
      58             :  * Error Reporting:
      59             :  *      Use write_stderr() only for reporting "interactive" errors
      60             :  *      (essentially, bogus arguments on the command line).  Once the
      61             :  *      postmaster is launched, use ereport().
      62             :  *
      63             :  *-------------------------------------------------------------------------
      64             :  */
      65             : 
      66             : #include "postgres.h"
      67             : 
      68             : #include <unistd.h>
      69             : #include <signal.h>
      70             : #include <time.h>
      71             : #include <sys/wait.h>
      72             : #include <ctype.h>
      73             : #include <sys/stat.h>
      74             : #include <sys/socket.h>
      75             : #include <fcntl.h>
      76             : #include <sys/param.h>
      77             : #include <netdb.h>
      78             : #include <limits.h>
      79             : 
      80             : #ifdef USE_BONJOUR
      81             : #include <dns_sd.h>
      82             : #endif
      83             : 
      84             : #ifdef USE_SYSTEMD
      85             : #include <systemd/sd-daemon.h>
      86             : #endif
      87             : 
      88             : #ifdef HAVE_PTHREAD_IS_THREADED_NP
      89             : #include <pthread.h>
      90             : #endif
      91             : 
      92             : #include "access/xlog.h"
      93             : #include "access/xlog_internal.h"
      94             : #include "access/xlogrecovery.h"
      95             : #include "common/file_perm.h"
      96             : #include "common/pg_prng.h"
      97             : #include "lib/ilist.h"
      98             : #include "libpq/libpq.h"
      99             : #include "libpq/pqsignal.h"
     100             : #include "pg_getopt.h"
     101             : #include "pgstat.h"
     102             : #include "port/pg_bswap.h"
     103             : #include "postmaster/autovacuum.h"
     104             : #include "postmaster/bgworker_internals.h"
     105             : #include "postmaster/pgarch.h"
     106             : #include "postmaster/postmaster.h"
     107             : #include "postmaster/syslogger.h"
     108             : #include "postmaster/walsummarizer.h"
     109             : #include "replication/logicallauncher.h"
     110             : #include "replication/slotsync.h"
     111             : #include "replication/walsender.h"
     112             : #include "storage/aio_subsys.h"
     113             : #include "storage/fd.h"
     114             : #include "storage/io_worker.h"
     115             : #include "storage/ipc.h"
     116             : #include "storage/pmsignal.h"
     117             : #include "storage/proc.h"
     118             : #include "tcop/backend_startup.h"
     119             : #include "tcop/tcopprot.h"
     120             : #include "utils/datetime.h"
     121             : #include "utils/memutils.h"
     122             : #include "utils/pidfile.h"
     123             : #include "utils/timestamp.h"
     124             : #include "utils/varlena.h"
     125             : 
     126             : #ifdef EXEC_BACKEND
     127             : #include "common/file_utils.h"
     128             : #include "storage/pg_shmem.h"
     129             : #endif
     130             : 
     131             : 
     132             : /*
     133             :  * CountChildren and SignalChildren take a bitmask argument to represent
     134             :  * BackendTypes to count or signal.  Define a separate type and functions to
     135             :  * work with the bitmasks, to avoid accidentally passing a plain BackendType
     136             :  * in place of a bitmask or vice versa.
     137             :  */
     138             : typedef struct
     139             : {
     140             :     uint32      mask;
     141             : } BackendTypeMask;
     142             : 
     143             : StaticAssertDecl(BACKEND_NUM_TYPES < 32, "too many backend types for uint32");
     144             : 
     145             : static const BackendTypeMask BTYPE_MASK_ALL = {(1 << BACKEND_NUM_TYPES) - 1};
     146             : static const BackendTypeMask BTYPE_MASK_NONE = {0};
     147             : 
     148             : static inline BackendTypeMask
     149        2962 : btmask(BackendType t)
     150             : {
     151        2962 :     BackendTypeMask mask = {.mask = 1 << t};
     152             : 
     153        2962 :     return mask;
     154             : }
     155             : 
     156             : static inline BackendTypeMask
     157       30462 : btmask_add_n(BackendTypeMask mask, int nargs, BackendType *t)
     158             : {
     159      134038 :     for (int i = 0; i < nargs; i++)
     160      103576 :         mask.mask |= 1 << t[i];
     161       30462 :     return mask;
     162             : }
     163             : 
     164             : #define btmask_add(mask, ...) \
     165             :     btmask_add_n(mask, \
     166             :         lengthof(((BackendType[]){__VA_ARGS__})), \
     167             :         (BackendType[]){__VA_ARGS__} \
     168             :     )
     169             : 
     170             : static inline BackendTypeMask
     171        8268 : btmask_del(BackendTypeMask mask, BackendType t)
     172             : {
     173        8268 :     mask.mask &= ~(1 << t);
     174        8268 :     return mask;
     175             : }
     176             : 
     177             : static inline BackendTypeMask
     178        4872 : btmask_all_except_n(int nargs, BackendType *t)
     179             : {
     180        4872 :     BackendTypeMask mask = BTYPE_MASK_ALL;
     181             : 
     182       13140 :     for (int i = 0; i < nargs; i++)
     183        8268 :         mask = btmask_del(mask, t[i]);
     184        4872 :     return mask;
     185             : }
     186             : 
     187             : #define btmask_all_except(...) \
     188             :     btmask_all_except_n( \
     189             :         lengthof(((BackendType[]){__VA_ARGS__})), \
     190             :         (BackendType[]){__VA_ARGS__} \
     191             :     )
     192             : 
     193             : static inline bool
     194      263004 : btmask_contains(BackendTypeMask mask, BackendType t)
     195             : {
     196      263004 :     return (mask.mask & (1 << t)) != 0;
     197             : }
     198             : 
     199             : 
     200             : BackgroundWorker *MyBgworkerEntry = NULL;
     201             : 
     202             : /* The socket number we are listening for connections on */
     203             : int         PostPortNumber = DEF_PGPORT;
     204             : 
     205             : /* The directory names for Unix socket(s) */
     206             : char       *Unix_socket_directories;
     207             : 
     208             : /* The TCP listen address(es) */
     209             : char       *ListenAddresses;
     210             : 
     211             : /*
     212             :  * SuperuserReservedConnections is the number of backends reserved for
     213             :  * superuser use, and ReservedConnections is the number of backends reserved
     214             :  * for use by roles with privileges of the pg_use_reserved_connections
     215             :  * predefined role.  These are taken out of the pool of MaxConnections backend
     216             :  * slots, so the number of backend slots available for roles that are neither
     217             :  * superuser nor have privileges of pg_use_reserved_connections is
     218             :  * (MaxConnections - SuperuserReservedConnections - ReservedConnections).
     219             :  *
     220             :  * If the number of remaining slots is less than or equal to
     221             :  * SuperuserReservedConnections, only superusers can make new connections.  If
     222             :  * the number of remaining slots is greater than SuperuserReservedConnections
     223             :  * but less than or equal to
     224             :  * (SuperuserReservedConnections + ReservedConnections), only superusers and
     225             :  * roles with privileges of pg_use_reserved_connections can make new
     226             :  * connections.  Note that pre-existing superuser and
     227             :  * pg_use_reserved_connections connections don't count against the limits.
     228             :  */
     229             : int         SuperuserReservedConnections;
     230             : int         ReservedConnections;
     231             : 
     232             : /* The socket(s) we're listening to. */
     233             : #define MAXLISTEN   64
     234             : static int  NumListenSockets = 0;
     235             : static pgsocket *ListenSockets = NULL;
     236             : 
     237             : /* still more option variables */
     238             : bool        EnableSSL = false;
     239             : 
     240             : int         PreAuthDelay = 0;
     241             : int         AuthenticationTimeout = 60;
     242             : 
     243             : bool        log_hostname;       /* for ps display and logging */
     244             : 
     245             : bool        enable_bonjour = false;
     246             : char       *bonjour_name;
     247             : bool        restart_after_crash = true;
     248             : bool        remove_temp_files_after_crash = true;
     249             : 
     250             : /*
     251             :  * When terminating child processes after fatal errors, like a crash of a
     252             :  * child process, we normally send SIGQUIT -- and most other comments in this
     253             :  * file are written on the assumption that we do -- but developers might
     254             :  * prefer to use SIGABRT to collect per-child core dumps.
     255             :  */
     256             : bool        send_abort_for_crash = false;
     257             : bool        send_abort_for_kill = false;
     258             : 
     259             : /* special child processes; NULL when not running */
     260             : static PMChild *StartupPMChild = NULL,
     261             :            *BgWriterPMChild = NULL,
     262             :            *CheckpointerPMChild = NULL,
     263             :            *WalWriterPMChild = NULL,
     264             :            *WalReceiverPMChild = NULL,
     265             :            *WalSummarizerPMChild = NULL,
     266             :            *AutoVacLauncherPMChild = NULL,
     267             :            *PgArchPMChild = NULL,
     268             :            *SysLoggerPMChild = NULL,
     269             :            *SlotSyncWorkerPMChild = NULL;
     270             : 
     271             : /* Startup process's status */
     272             : typedef enum
     273             : {
     274             :     STARTUP_NOT_RUNNING,
     275             :     STARTUP_RUNNING,
     276             :     STARTUP_SIGNALED,           /* we sent it a SIGQUIT or SIGKILL */
     277             :     STARTUP_CRASHED,
     278             : } StartupStatusEnum;
     279             : 
     280             : static StartupStatusEnum StartupStatus = STARTUP_NOT_RUNNING;
     281             : 
     282             : /* Startup/shutdown state */
     283             : #define         NoShutdown      0
     284             : #define         SmartShutdown   1
     285             : #define         FastShutdown    2
     286             : #define         ImmediateShutdown   3
     287             : 
     288             : static int  Shutdown = NoShutdown;
     289             : 
     290             : static bool FatalError = false; /* T if recovering from backend crash */
     291             : 
     292             : /*
     293             :  * We use a simple state machine to control startup, shutdown, and
     294             :  * crash recovery (which is rather like shutdown followed by startup).
     295             :  *
     296             :  * After doing all the postmaster initialization work, we enter PM_STARTUP
     297             :  * state and the startup process is launched. The startup process begins by
     298             :  * reading the control file and other preliminary initialization steps.
     299             :  * In a normal startup, or after crash recovery, the startup process exits
     300             :  * with exit code 0 and we switch to PM_RUN state.  However, archive recovery
     301             :  * is handled specially since it takes much longer and we would like to support
     302             :  * hot standby during archive recovery.
     303             :  *
     304             :  * When the startup process is ready to start archive recovery, it signals the
     305             :  * postmaster, and we switch to PM_RECOVERY state. The background writer and
     306             :  * checkpointer are launched, while the startup process continues applying WAL.
     307             :  * If Hot Standby is enabled, then, after reaching a consistent point in WAL
     308             :  * redo, startup process signals us again, and we switch to PM_HOT_STANDBY
     309             :  * state and begin accepting connections to perform read-only queries.  When
     310             :  * archive recovery is finished, the startup process exits with exit code 0
     311             :  * and we switch to PM_RUN state.
     312             :  *
     313             :  * Normal child backends can only be launched when we are in PM_RUN or
     314             :  * PM_HOT_STANDBY state.  (connsAllowed can also restrict launching.)
     315             :  * In other states we handle connection requests by launching "dead-end"
     316             :  * child processes, which will simply send the client an error message and
     317             :  * quit.  (We track these in the ActiveChildList so that we can know when they
     318             :  * are all gone; this is important because they're still connected to shared
     319             :  * memory, and would interfere with an attempt to destroy the shmem segment,
     320             :  * possibly leading to SHMALL failure when we try to make a new one.)
     321             :  * In PM_WAIT_DEAD_END state we are waiting for all the dead-end children
     322             :  * to drain out of the system, and therefore stop accepting connection
     323             :  * requests at all until the last existing child has quit (which hopefully
     324             :  * will not be very long).
     325             :  *
     326             :  * Notice that this state variable does not distinguish *why* we entered
     327             :  * states later than PM_RUN --- Shutdown and FatalError must be consulted
     328             :  * to find that out.  FatalError is never true in PM_RECOVERY, PM_HOT_STANDBY,
     329             :  * or PM_RUN states, nor in PM_WAIT_XLOG_SHUTDOWN states (because we don't
     330             :  * enter those states when trying to recover from a crash).  It can be true in
     331             :  * PM_STARTUP state, because we don't clear it until we've successfully
     332             :  * started WAL redo.
     333             :  */
     334             : typedef enum
     335             : {
     336             :     PM_INIT,                    /* postmaster starting */
     337             :     PM_STARTUP,                 /* waiting for startup subprocess */
     338             :     PM_RECOVERY,                /* in archive recovery mode */
     339             :     PM_HOT_STANDBY,             /* in hot standby mode */
     340             :     PM_RUN,                     /* normal "database is alive" state */
     341             :     PM_STOP_BACKENDS,           /* need to stop remaining backends */
     342             :     PM_WAIT_BACKENDS,           /* waiting for live backends to exit */
     343             :     PM_WAIT_XLOG_SHUTDOWN,      /* waiting for checkpointer to do shutdown
     344             :                                  * ckpt */
     345             :     PM_WAIT_XLOG_ARCHIVAL,      /* waiting for archiver and walsenders to
     346             :                                  * finish */
     347             :     PM_WAIT_IO_WORKERS,         /* waiting for io workers to exit */
     348             :     PM_WAIT_CHECKPOINTER,       /* waiting for checkpointer to shut down */
     349             :     PM_WAIT_DEAD_END,           /* waiting for dead-end children to exit */
     350             :     PM_NO_CHILDREN,             /* all important children have exited */
     351             : } PMState;
     352             : 
     353             : static PMState pmState = PM_INIT;
     354             : 
     355             : /*
     356             :  * While performing a "smart shutdown", we restrict new connections but stay
     357             :  * in PM_RUN or PM_HOT_STANDBY state until all the client backends are gone.
     358             :  * connsAllowed is a sub-state indicator showing the active restriction.
     359             :  * It is of no interest unless pmState is PM_RUN or PM_HOT_STANDBY.
     360             :  */
     361             : static bool connsAllowed = true;
     362             : 
     363             : /* Start time of SIGKILL timeout during immediate shutdown or child crash */
     364             : /* Zero means timeout is not running */
     365             : static time_t AbortStartTime = 0;
     366             : 
     367             : /* Length of said timeout */
     368             : #define SIGKILL_CHILDREN_AFTER_SECS     5
     369             : 
     370             : static bool ReachedNormalRunning = false;   /* T if we've reached PM_RUN */
     371             : 
     372             : bool        ClientAuthInProgress = false;   /* T during new-client
     373             :                                              * authentication */
     374             : 
     375             : bool        redirection_done = false;   /* stderr redirected for syslogger? */
     376             : 
     377             : /* received START_AUTOVAC_LAUNCHER signal */
     378             : static bool start_autovac_launcher = false;
     379             : 
     380             : /* the launcher needs to be signaled to communicate some condition */
     381             : static bool avlauncher_needs_signal = false;
     382             : 
     383             : /* received START_WALRECEIVER signal */
     384             : static bool WalReceiverRequested = false;
     385             : 
     386             : /* set when there's a worker that needs to be started up */
     387             : static bool StartWorkerNeeded = true;
     388             : static bool HaveCrashedWorker = false;
     389             : 
     390             : /* set when signals arrive */
     391             : static volatile sig_atomic_t pending_pm_pmsignal;
     392             : static volatile sig_atomic_t pending_pm_child_exit;
     393             : static volatile sig_atomic_t pending_pm_reload_request;
     394             : static volatile sig_atomic_t pending_pm_shutdown_request;
     395             : static volatile sig_atomic_t pending_pm_fast_shutdown_request;
     396             : static volatile sig_atomic_t pending_pm_immediate_shutdown_request;
     397             : 
     398             : /* event multiplexing object */
     399             : static WaitEventSet *pm_wait_set;
     400             : 
     401             : #ifdef USE_SSL
     402             : /* Set when and if SSL has been initialized properly */
     403             : bool        LoadedSSL = false;
     404             : #endif
     405             : 
     406             : #ifdef USE_BONJOUR
     407             : static DNSServiceRef bonjour_sdref = NULL;
     408             : #endif
     409             : 
     410             : /* State for IO worker management. */
     411             : static int  io_worker_count = 0;
     412             : static PMChild *io_worker_children[MAX_IO_WORKERS];
     413             : 
     414             : /*
     415             :  * postmaster.c - function prototypes
     416             :  */
     417             : static void CloseServerPorts(int status, Datum arg);
     418             : static void unlink_external_pid_file(int status, Datum arg);
     419             : static void getInstallationPaths(const char *argv0);
     420             : static void checkControlFile(void);
     421             : static void handle_pm_pmsignal_signal(SIGNAL_ARGS);
     422             : static void handle_pm_child_exit_signal(SIGNAL_ARGS);
     423             : static void handle_pm_reload_request_signal(SIGNAL_ARGS);
     424             : static void handle_pm_shutdown_request_signal(SIGNAL_ARGS);
     425             : static void process_pm_pmsignal(void);
     426             : static void process_pm_child_exit(void);
     427             : static void process_pm_reload_request(void);
     428             : static void process_pm_shutdown_request(void);
     429             : static void dummy_handler(SIGNAL_ARGS);
     430             : static void CleanupBackend(PMChild *bp, int exitstatus);
     431             : static void HandleChildCrash(int pid, int exitstatus, const char *procname);
     432             : static void LogChildExit(int lev, const char *procname,
     433             :                          int pid, int exitstatus);
     434             : static void PostmasterStateMachine(void);
     435             : static void UpdatePMState(PMState newState);
     436             : 
     437             : pg_noreturn static void ExitPostmaster(int status);
     438             : static int  ServerLoop(void);
     439             : static int  BackendStartup(ClientSocket *client_sock);
     440             : static void report_fork_failure_to_client(ClientSocket *client_sock, int errnum);
     441             : static CAC_state canAcceptConnections(BackendType backend_type);
     442             : static void signal_child(PMChild *pmchild, int signal);
     443             : static bool SignalChildren(int signal, BackendTypeMask targetMask);
     444             : static void TerminateChildren(int signal);
     445             : static int  CountChildren(BackendTypeMask targetMask);
     446             : static void LaunchMissingBackgroundProcesses(void);
     447             : static void maybe_start_bgworkers(void);
     448             : static bool maybe_reap_io_worker(int pid);
     449             : static void maybe_adjust_io_workers(void);
     450             : static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
     451             : static PMChild *StartChildProcess(BackendType type);
     452             : static void StartSysLogger(void);
     453             : static void StartAutovacuumWorker(void);
     454             : static bool StartBackgroundWorker(RegisteredBgWorker *rw);
     455             : static void InitPostmasterDeathWatchHandle(void);
     456             : 
     457             : #ifdef WIN32
     458             : #define WNOHANG 0               /* ignored, so any integer value will do */
     459             : 
     460             : static pid_t waitpid(pid_t pid, int *exitstatus, int options);
     461             : static void WINAPI pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired);
     462             : 
     463             : static HANDLE win32ChildQueue;
     464             : 
     465             : typedef struct
     466             : {
     467             :     HANDLE      waitHandle;
     468             :     HANDLE      procHandle;
     469             :     DWORD       procId;
     470             : } win32_deadchild_waitinfo;
     471             : #endif                          /* WIN32 */
     472             : 
     473             : /* Macros to check exit status of a child process */
     474             : #define EXIT_STATUS_0(st)  ((st) == 0)
     475             : #define EXIT_STATUS_1(st)  (WIFEXITED(st) && WEXITSTATUS(st) == 1)
     476             : #define EXIT_STATUS_3(st)  (WIFEXITED(st) && WEXITSTATUS(st) == 3)
     477             : 
     478             : #ifndef WIN32
     479             : /*
     480             :  * File descriptors for pipe used to monitor if postmaster is alive.
     481             :  * First is POSTMASTER_FD_WATCH, second is POSTMASTER_FD_OWN.
     482             :  */
     483             : int         postmaster_alive_fds[2] = {-1, -1};
     484             : #else
     485             : /* Process handle of postmaster used for the same purpose on Windows */
     486             : HANDLE      PostmasterHandle;
     487             : #endif
     488             : 
     489             : /*
     490             :  * Postmaster main entry point
     491             :  */
     492             : void
     493        1728 : PostmasterMain(int argc, char *argv[])
     494             : {
     495             :     int         opt;
     496             :     int         status;
     497        1728 :     char       *userDoption = NULL;
     498        1728 :     bool        listen_addr_saved = false;
     499        1728 :     char       *output_config_variable = NULL;
     500             : 
     501        1728 :     InitProcessGlobals();
     502             : 
     503        1728 :     PostmasterPid = MyProcPid;
     504             : 
     505        1728 :     IsPostmasterEnvironment = true;
     506             : 
     507             :     /*
     508             :      * Start our win32 signal implementation
     509             :      */
     510             : #ifdef WIN32
     511             :     pgwin32_signal_initialize();
     512             : #endif
     513             : 
     514             :     /*
     515             :      * We should not be creating any files or directories before we check the
     516             :      * data directory (see checkDataDir()), but just in case set the umask to
     517             :      * the most restrictive (owner-only) permissions.
     518             :      *
     519             :      * checkDataDir() will reset the umask based on the data directory
     520             :      * permissions.
     521             :      */
     522        1728 :     umask(PG_MODE_MASK_OWNER);
     523             : 
     524             :     /*
     525             :      * By default, palloc() requests in the postmaster will be allocated in
     526             :      * the PostmasterContext, which is space that can be recycled by backends.
     527             :      * Allocated data that needs to be available to backends should be
     528             :      * allocated in TopMemoryContext.
     529             :      */
     530        1728 :     PostmasterContext = AllocSetContextCreate(TopMemoryContext,
     531             :                                               "Postmaster",
     532             :                                               ALLOCSET_DEFAULT_SIZES);
     533        1728 :     MemoryContextSwitchTo(PostmasterContext);
     534             : 
     535             :     /* Initialize paths to installation files */
     536        1728 :     getInstallationPaths(argv[0]);
     537             : 
     538             :     /*
     539             :      * Set up signal handlers for the postmaster process.
     540             :      *
     541             :      * CAUTION: when changing this list, check for side-effects on the signal
     542             :      * handling setup of child processes.  See tcop/postgres.c,
     543             :      * bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/walwriter.c,
     544             :      * postmaster/autovacuum.c, postmaster/pgarch.c, postmaster/syslogger.c,
     545             :      * postmaster/bgworker.c and postmaster/checkpointer.c.
     546             :      */
     547        1728 :     pqinitmask();
     548        1728 :     sigprocmask(SIG_SETMASK, &BlockSig, NULL);
     549             : 
     550        1728 :     pqsignal(SIGHUP, handle_pm_reload_request_signal);
     551        1728 :     pqsignal(SIGINT, handle_pm_shutdown_request_signal);
     552        1728 :     pqsignal(SIGQUIT, handle_pm_shutdown_request_signal);
     553        1728 :     pqsignal(SIGTERM, handle_pm_shutdown_request_signal);
     554        1728 :     pqsignal(SIGALRM, SIG_IGN); /* ignored */
     555        1728 :     pqsignal(SIGPIPE, SIG_IGN); /* ignored */
     556        1728 :     pqsignal(SIGUSR1, handle_pm_pmsignal_signal);
     557        1728 :     pqsignal(SIGUSR2, dummy_handler);   /* unused, reserve for children */
     558        1728 :     pqsignal(SIGCHLD, handle_pm_child_exit_signal);
     559             : 
     560             :     /* This may configure SIGURG, depending on platform. */
     561        1728 :     InitializeWaitEventSupport();
     562        1728 :     InitProcessLocalLatch();
     563             : 
     564             :     /*
     565             :      * No other place in Postgres should touch SIGTTIN/SIGTTOU handling.  We
     566             :      * ignore those signals in a postmaster environment, so that there is no
     567             :      * risk of a child process freezing up due to writing to stderr.  But for
     568             :      * a standalone backend, their default handling is reasonable.  Hence, all
     569             :      * child processes should just allow the inherited settings to stand.
     570             :      */
     571             : #ifdef SIGTTIN
     572        1728 :     pqsignal(SIGTTIN, SIG_IGN); /* ignored */
     573             : #endif
     574             : #ifdef SIGTTOU
     575        1728 :     pqsignal(SIGTTOU, SIG_IGN); /* ignored */
     576             : #endif
     577             : 
     578             :     /* ignore SIGXFSZ, so that ulimit violations work like disk full */
     579             : #ifdef SIGXFSZ
     580        1728 :     pqsignal(SIGXFSZ, SIG_IGN); /* ignored */
     581             : #endif
     582             : 
     583             :     /* Begin accepting signals. */
     584        1728 :     sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
     585             : 
     586             :     /*
     587             :      * Options setup
     588             :      */
     589        1728 :     InitializeGUCOptions();
     590             : 
     591        1728 :     opterr = 1;
     592             : 
     593             :     /*
     594             :      * Parse command-line options.  CAUTION: keep this in sync with
     595             :      * tcop/postgres.c (the option sets should not conflict) and with the
     596             :      * common help() function in main/main.c.
     597             :      */
     598        6218 :     while ((opt = getopt(argc, argv, "B:bC:c:D:d:EeFf:h:ijk:lN:OPp:r:S:sTt:W:-:")) != -1)
     599             :     {
     600        4494 :         switch (opt)
     601             :         {
     602           0 :             case 'B':
     603           0 :                 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
     604           0 :                 break;
     605             : 
     606          94 :             case 'b':
     607             :                 /* Undocumented flag used for binary upgrades */
     608          94 :                 IsBinaryUpgrade = true;
     609          94 :                 break;
     610             : 
     611           6 :             case 'C':
     612           6 :                 output_config_variable = strdup(optarg);
     613           6 :                 break;
     614             : 
     615        1386 :             case '-':
     616             : 
     617             :                 /*
     618             :                  * Error if the user misplaced a special must-be-first option
     619             :                  * for dispatching to a subprogram.  parse_dispatch_option()
     620             :                  * returns DISPATCH_POSTMASTER if it doesn't find a match, so
     621             :                  * error for anything else.
     622             :                  */
     623        1386 :                 if (parse_dispatch_option(optarg) != DISPATCH_POSTMASTER)
     624           0 :                     ereport(ERROR,
     625             :                             (errcode(ERRCODE_SYNTAX_ERROR),
     626             :                              errmsg("--%s must be first argument", optarg)));
     627             : 
     628             :                 /* FALLTHROUGH */
     629             :             case 'c':
     630             :                 {
     631             :                     char       *name,
     632             :                                *value;
     633             : 
     634        2176 :                     ParseLongOption(optarg, &name, &value);
     635        2176 :                     if (!value)
     636             :                     {
     637           2 :                         if (opt == '-')
     638           2 :                             ereport(ERROR,
     639             :                                     (errcode(ERRCODE_SYNTAX_ERROR),
     640             :                                      errmsg("--%s requires a value",
     641             :                                             optarg)));
     642             :                         else
     643           0 :                             ereport(ERROR,
     644             :                                     (errcode(ERRCODE_SYNTAX_ERROR),
     645             :                                      errmsg("-c %s requires a value",
     646             :                                             optarg)));
     647             :                     }
     648             : 
     649        2174 :                     SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
     650        2172 :                     pfree(name);
     651        2172 :                     pfree(value);
     652        2172 :                     break;
     653             :                 }
     654             : 
     655        1724 :             case 'D':
     656        1724 :                 userDoption = strdup(optarg);
     657        1724 :                 break;
     658             : 
     659           0 :             case 'd':
     660           0 :                 set_debug_options(atoi(optarg), PGC_POSTMASTER, PGC_S_ARGV);
     661           0 :                 break;
     662             : 
     663           0 :             case 'E':
     664           0 :                 SetConfigOption("log_statement", "all", PGC_POSTMASTER, PGC_S_ARGV);
     665           0 :                 break;
     666             : 
     667           0 :             case 'e':
     668           0 :                 SetConfigOption("datestyle", "euro", PGC_POSTMASTER, PGC_S_ARGV);
     669           0 :                 break;
     670             : 
     671         188 :             case 'F':
     672         188 :                 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
     673         188 :                 break;
     674             : 
     675           0 :             case 'f':
     676           0 :                 if (!set_plan_disabling_options(optarg, PGC_POSTMASTER, PGC_S_ARGV))
     677             :                 {
     678           0 :                     write_stderr("%s: invalid argument for option -f: \"%s\"\n",
     679             :                                  progname, optarg);
     680           0 :                     ExitPostmaster(1);
     681             :                 }
     682           0 :                 break;
     683             : 
     684           0 :             case 'h':
     685           0 :                 SetConfigOption("listen_addresses", optarg, PGC_POSTMASTER, PGC_S_ARGV);
     686           0 :                 break;
     687             : 
     688           0 :             case 'i':
     689           0 :                 SetConfigOption("listen_addresses", "*", PGC_POSTMASTER, PGC_S_ARGV);
     690           0 :                 break;
     691             : 
     692           0 :             case 'j':
     693             :                 /* only used by interactive backend */
     694           0 :                 break;
     695             : 
     696         188 :             case 'k':
     697         188 :                 SetConfigOption("unix_socket_directories", optarg, PGC_POSTMASTER, PGC_S_ARGV);
     698         188 :                 break;
     699             : 
     700           0 :             case 'l':
     701           0 :                 SetConfigOption("ssl", "true", PGC_POSTMASTER, PGC_S_ARGV);
     702           0 :                 break;
     703             : 
     704           0 :             case 'N':
     705           0 :                 SetConfigOption("max_connections", optarg, PGC_POSTMASTER, PGC_S_ARGV);
     706           0 :                 break;
     707             : 
     708           0 :             case 'O':
     709           0 :                 SetConfigOption("allow_system_table_mods", "true", PGC_POSTMASTER, PGC_S_ARGV);
     710           0 :                 break;
     711             : 
     712           0 :             case 'P':
     713           0 :                 SetConfigOption("ignore_system_indexes", "true", PGC_POSTMASTER, PGC_S_ARGV);
     714           0 :                 break;
     715             : 
     716         118 :             case 'p':
     717         118 :                 SetConfigOption("port", optarg, PGC_POSTMASTER, PGC_S_ARGV);
     718         118 :                 break;
     719             : 
     720           0 :             case 'r':
     721             :                 /* only used by single-user backend */
     722           0 :                 break;
     723             : 
     724           0 :             case 'S':
     725           0 :                 SetConfigOption("work_mem", optarg, PGC_POSTMASTER, PGC_S_ARGV);
     726           0 :                 break;
     727             : 
     728           0 :             case 's':
     729           0 :                 SetConfigOption("log_statement_stats", "true", PGC_POSTMASTER, PGC_S_ARGV);
     730           0 :                 break;
     731             : 
     732           0 :             case 'T':
     733             : 
     734             :                 /*
     735             :                  * This option used to be defined as sending SIGSTOP after a
     736             :                  * backend crash, but sending SIGABRT seems more useful.
     737             :                  */
     738           0 :                 SetConfigOption("send_abort_for_crash", "true", PGC_POSTMASTER, PGC_S_ARGV);
     739           0 :                 break;
     740             : 
     741           0 :             case 't':
     742             :                 {
     743           0 :                     const char *tmp = get_stats_option_name(optarg);
     744             : 
     745           0 :                     if (tmp)
     746             :                     {
     747           0 :                         SetConfigOption(tmp, "true", PGC_POSTMASTER, PGC_S_ARGV);
     748             :                     }
     749             :                     else
     750             :                     {
     751           0 :                         write_stderr("%s: invalid argument for option -t: \"%s\"\n",
     752             :                                      progname, optarg);
     753           0 :                         ExitPostmaster(1);
     754             :                     }
     755           0 :                     break;
     756             :                 }
     757             : 
     758           0 :             case 'W':
     759           0 :                 SetConfigOption("post_auth_delay", optarg, PGC_POSTMASTER, PGC_S_ARGV);
     760           0 :                 break;
     761             : 
     762           0 :             default:
     763           0 :                 write_stderr("Try \"%s --help\" for more information.\n",
     764             :                              progname);
     765           0 :                 ExitPostmaster(1);
     766             :         }
     767             :     }
     768             : 
     769             :     /*
     770             :      * Postmaster accepts no non-option switch arguments.
     771             :      */
     772        1724 :     if (optind < argc)
     773             :     {
     774           0 :         write_stderr("%s: invalid argument: \"%s\"\n",
     775           0 :                      progname, argv[optind]);
     776           0 :         write_stderr("Try \"%s --help\" for more information.\n",
     777             :                      progname);
     778           0 :         ExitPostmaster(1);
     779             :     }
     780             : 
     781             :     /*
     782             :      * Locate the proper configuration files and data directory, and read
     783             :      * postgresql.conf for the first time.
     784             :      */
     785        1724 :     if (!SelectConfigFiles(userDoption, progname))
     786           0 :         ExitPostmaster(2);
     787             : 
     788        1716 :     if (output_config_variable != NULL)
     789             :     {
     790             :         /*
     791             :          * If this is a runtime-computed GUC, it hasn't yet been initialized,
     792             :          * and the present value is not useful.  However, this is a convenient
     793             :          * place to print the value for most GUCs because it is safe to run
     794             :          * postmaster startup to this point even if the server is already
     795             :          * running.  For the handful of runtime-computed GUCs that we cannot
     796             :          * provide meaningful values for yet, we wait until later in
     797             :          * postmaster startup to print the value.  We won't be able to use -C
     798             :          * on running servers for those GUCs, but using this option now would
     799             :          * lead to incorrect results for them.
     800             :          */
     801           4 :         int         flags = GetConfigOptionFlags(output_config_variable, true);
     802             : 
     803           4 :         if ((flags & GUC_RUNTIME_COMPUTED) == 0)
     804             :         {
     805             :             /*
     806             :              * "-C guc" was specified, so print GUC's value and exit.  No
     807             :              * extra permission check is needed because the user is reading
     808             :              * inside the data dir.
     809             :              */
     810           2 :             const char *config_val = GetConfigOption(output_config_variable,
     811             :                                                      false, false);
     812             : 
     813           2 :             puts(config_val ? config_val : "");
     814           2 :             ExitPostmaster(0);
     815             :         }
     816             : 
     817             :         /*
     818             :          * A runtime-computed GUC will be printed later on.  As we initialize
     819             :          * a server startup sequence, silence any log messages that may show
     820             :          * up in the output generated.  FATAL and more severe messages are
     821             :          * useful to show, even if one would only expect at least PANIC.  LOG
     822             :          * entries are hidden.
     823             :          */
     824           2 :         SetConfigOption("log_min_messages", "FATAL", PGC_SUSET,
     825             :                         PGC_S_OVERRIDE);
     826             :     }
     827             : 
     828             :     /* Verify that DataDir looks reasonable */
     829        1714 :     checkDataDir();
     830             : 
     831             :     /* Check that pg_control exists */
     832        1714 :     checkControlFile();
     833             : 
     834             :     /* And switch working directory into it */
     835        1714 :     ChangeToDataDir();
     836             : 
     837             :     /*
     838             :      * Check for invalid combinations of GUC settings.
     839             :      */
     840        1714 :     if (SuperuserReservedConnections + ReservedConnections >= MaxConnections)
     841             :     {
     842           0 :         write_stderr("%s: \"superuser_reserved_connections\" (%d) plus \"reserved_connections\" (%d) must be less than \"max_connections\" (%d)\n",
     843             :                      progname,
     844             :                      SuperuserReservedConnections, ReservedConnections,
     845             :                      MaxConnections);
     846           0 :         ExitPostmaster(1);
     847             :     }
     848        1714 :     if (XLogArchiveMode > ARCHIVE_MODE_OFF && wal_level == WAL_LEVEL_MINIMAL)
     849           0 :         ereport(ERROR,
     850             :                 (errmsg("WAL archival cannot be enabled when \"wal_level\" is \"minimal\"")));
     851        1714 :     if (max_wal_senders > 0 && wal_level == WAL_LEVEL_MINIMAL)
     852           0 :         ereport(ERROR,
     853             :                 (errmsg("WAL streaming (\"max_wal_senders\" > 0) requires \"wal_level\" to be \"replica\" or \"logical\"")));
     854        1714 :     if (summarize_wal && wal_level == WAL_LEVEL_MINIMAL)
     855           0 :         ereport(ERROR,
     856             :                 (errmsg("WAL cannot be summarized when \"wal_level\" is \"minimal\"")));
     857             : 
     858             :     /*
     859             :      * Other one-time internal sanity checks can go here, if they are fast.
     860             :      * (Put any slow processing further down, after postmaster.pid creation.)
     861             :      */
     862        1714 :     if (!CheckDateTokenTables())
     863             :     {
     864           0 :         write_stderr("%s: invalid datetoken tables, please fix\n", progname);
     865           0 :         ExitPostmaster(1);
     866             :     }
     867             : 
     868             :     /*
     869             :      * Now that we are done processing the postmaster arguments, reset
     870             :      * getopt(3) library so that it will work correctly in subprocesses.
     871             :      */
     872        1714 :     optind = 1;
     873             : #ifdef HAVE_INT_OPTRESET
     874             :     optreset = 1;               /* some systems need this too */
     875             : #endif
     876             : 
     877             :     /* For debugging: display postmaster environment */
     878        1714 :     if (message_level_is_interesting(DEBUG3))
     879             :     {
     880             : #if !defined(WIN32) || defined(_MSC_VER)
     881             :         extern char **environ;
     882             : #endif
     883             :         char      **p;
     884             :         StringInfoData si;
     885             : 
     886           4 :         initStringInfo(&si);
     887             : 
     888           4 :         appendStringInfoString(&si, "initial environment dump:");
     889         172 :         for (p = environ; *p; ++p)
     890         168 :             appendStringInfo(&si, "\n%s", *p);
     891             : 
     892           4 :         ereport(DEBUG3, errmsg_internal("%s", si.data));
     893           4 :         pfree(si.data);
     894             :     }
     895             : 
     896             :     /*
     897             :      * Create lockfile for data directory.
     898             :      *
     899             :      * We want to do this before we try to grab the input sockets, because the
     900             :      * data directory interlock is more reliable than the socket-file
     901             :      * interlock (thanks to whoever decided to put socket files in /tmp :-().
     902             :      * For the same reason, it's best to grab the TCP socket(s) before the
     903             :      * Unix socket(s).
     904             :      *
     905             :      * Also note that this internally sets up the on_proc_exit function that
     906             :      * is responsible for removing both data directory and socket lockfiles;
     907             :      * so it must happen before opening sockets so that at exit, the socket
     908             :      * lockfiles go away after CloseServerPorts runs.
     909             :      */
     910        1714 :     CreateDataDirLockFile(true);
     911             : 
     912             :     /*
     913             :      * Read the control file (for error checking and config info).
     914             :      *
     915             :      * Since we verify the control file's CRC, this has a useful side effect
     916             :      * on machines where we need a run-time test for CRC support instructions.
     917             :      * The postmaster will do the test once at startup, and then its child
     918             :      * processes will inherit the correct function pointer and not need to
     919             :      * repeat the test.
     920             :      */
     921        1712 :     LocalProcessControlFile(false);
     922             : 
     923             :     /*
     924             :      * Register the apply launcher.  It's probably a good idea to call this
     925             :      * before any modules had a chance to take the background worker slots.
     926             :      */
     927        1712 :     ApplyLauncherRegister();
     928             : 
     929             :     /*
     930             :      * process any libraries that should be preloaded at postmaster start
     931             :      */
     932        1712 :     process_shared_preload_libraries();
     933             : 
     934             :     /*
     935             :      * Initialize SSL library, if specified.
     936             :      */
     937             : #ifdef USE_SSL
     938        1712 :     if (EnableSSL)
     939             :     {
     940          62 :         (void) secure_initialize(true);
     941          52 :         LoadedSSL = true;
     942             :     }
     943             : #endif
     944             : 
     945             :     /*
     946             :      * Now that loadable modules have had their chance to alter any GUCs,
     947             :      * calculate MaxBackends and initialize the machinery to track child
     948             :      * processes.
     949             :      */
     950        1702 :     InitializeMaxBackends();
     951        1702 :     InitPostmasterChildSlots();
     952             : 
     953             :     /*
     954             :      * Calculate the size of the PGPROC fast-path lock arrays.
     955             :      */
     956        1702 :     InitializeFastPathLocks();
     957             : 
     958             :     /*
     959             :      * Give preloaded libraries a chance to request additional shared memory.
     960             :      */
     961        1702 :     process_shmem_requests();
     962             : 
     963             :     /*
     964             :      * Now that loadable modules have had their chance to request additional
     965             :      * shared memory, determine the value of any runtime-computed GUCs that
     966             :      * depend on the amount of shared memory required.
     967             :      */
     968        1702 :     InitializeShmemGUCs();
     969             : 
     970             :     /*
     971             :      * Now that modules have been loaded, we can process any custom resource
     972             :      * managers specified in the wal_consistency_checking GUC.
     973             :      */
     974        1702 :     InitializeWalConsistencyChecking();
     975             : 
     976             :     /*
     977             :      * If -C was specified with a runtime-computed GUC, we held off printing
     978             :      * the value earlier, as the GUC was not yet initialized.  We handle -C
     979             :      * for most GUCs before we lock the data directory so that the option may
     980             :      * be used on a running server.  However, a handful of GUCs are runtime-
     981             :      * computed and do not have meaningful values until after locking the data
     982             :      * directory, and we cannot safely calculate their values earlier on a
     983             :      * running server.  At this point, such GUCs should be properly
     984             :      * initialized, and we haven't yet set up shared memory, so this is a good
     985             :      * time to handle the -C option for these special GUCs.
     986             :      */
     987        1702 :     if (output_config_variable != NULL)
     988             :     {
     989           2 :         const char *config_val = GetConfigOption(output_config_variable,
     990             :                                                  false, false);
     991             : 
     992           2 :         puts(config_val ? config_val : "");
     993           2 :         ExitPostmaster(0);
     994             :     }
     995             : 
     996             :     /*
     997             :      * Set up shared memory and semaphores.
     998             :      *
     999             :      * Note: if using SysV shmem and/or semas, each postmaster startup will
    1000             :      * normally choose the same IPC keys.  This helps ensure that we will
    1001             :      * clean up dead IPC objects if the postmaster crashes and is restarted.
    1002             :      */
    1003        1700 :     CreateSharedMemoryAndSemaphores();
    1004             : 
    1005             :     /*
    1006             :      * Estimate number of openable files.  This must happen after setting up
    1007             :      * semaphores, because on some platforms semaphores count as open files.
    1008             :      */
    1009        1698 :     set_max_safe_fds();
    1010             : 
    1011             :     /*
    1012             :      * Initialize pipe (or process handle on Windows) that allows children to
    1013             :      * wake up from sleep on postmaster death.
    1014             :      */
    1015        1698 :     InitPostmasterDeathWatchHandle();
    1016             : 
    1017             : #ifdef WIN32
    1018             : 
    1019             :     /*
    1020             :      * Initialize I/O completion port used to deliver list of dead children.
    1021             :      */
    1022             :     win32ChildQueue = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
    1023             :     if (win32ChildQueue == NULL)
    1024             :         ereport(FATAL,
    1025             :                 (errmsg("could not create I/O completion port for child queue")));
    1026             : #endif
    1027             : 
    1028             : #ifdef EXEC_BACKEND
    1029             :     /* Write out nondefault GUC settings for child processes to use */
    1030             :     write_nondefault_variables(PGC_POSTMASTER);
    1031             : 
    1032             :     /*
    1033             :      * Clean out the temp directory used to transmit parameters to child
    1034             :      * processes (see internal_forkexec).  We must do this before launching
    1035             :      * any child processes, else we have a race condition: we could remove a
    1036             :      * parameter file before the child can read it.  It should be safe to do
    1037             :      * so now, because we verified earlier that there are no conflicting
    1038             :      * Postgres processes in this data directory.
    1039             :      */
    1040             :     RemovePgTempFilesInDir(PG_TEMP_FILES_DIR, true, false);
    1041             : #endif
    1042             : 
    1043             :     /*
    1044             :      * Forcibly remove the files signaling a standby promotion request.
    1045             :      * Otherwise, the existence of those files triggers a promotion too early,
    1046             :      * whether a user wants that or not.
    1047             :      *
    1048             :      * This removal of files is usually unnecessary because they can exist
    1049             :      * only during a few moments during a standby promotion. However there is
    1050             :      * a race condition: if pg_ctl promote is executed and creates the files
    1051             :      * during a promotion, the files can stay around even after the server is
    1052             :      * brought up to be the primary.  Then, if a new standby starts by using
    1053             :      * the backup taken from the new primary, the files can exist at server
    1054             :      * startup and must be removed in order to avoid an unexpected promotion.
    1055             :      *
    1056             :      * Note that promotion signal files need to be removed before the startup
    1057             :      * process is invoked. Because, after that, they can be used by
    1058             :      * postmaster's SIGUSR1 signal handler.
    1059             :      */
    1060        1698 :     RemovePromoteSignalFiles();
    1061             : 
    1062             :     /* Do the same for logrotate signal file */
    1063        1698 :     RemoveLogrotateSignalFiles();
    1064             : 
    1065             :     /* Remove any outdated file holding the current log filenames. */
    1066        1698 :     if (unlink(LOG_METAINFO_DATAFILE) < 0 && errno != ENOENT)
    1067           0 :         ereport(LOG,
    1068             :                 (errcode_for_file_access(),
    1069             :                  errmsg("could not remove file \"%s\": %m",
    1070             :                         LOG_METAINFO_DATAFILE)));
    1071             : 
    1072             :     /*
    1073             :      * If enabled, start up syslogger collection subprocess
    1074             :      */
    1075        1698 :     if (Logging_collector)
    1076           2 :         StartSysLogger();
    1077             : 
    1078             :     /*
    1079             :      * Reset whereToSendOutput from DestDebug (its starting state) to
    1080             :      * DestNone. This stops ereport from sending log messages to stderr unless
    1081             :      * Log_destination permits.  We don't do this until the postmaster is
    1082             :      * fully launched, since startup failures may as well be reported to
    1083             :      * stderr.
    1084             :      *
    1085             :      * If we are in fact disabling logging to stderr, first emit a log message
    1086             :      * saying so, to provide a breadcrumb trail for users who may not remember
    1087             :      * that their logging is configured to go somewhere else.
    1088             :      */
    1089        1698 :     if (!(Log_destination & LOG_DESTINATION_STDERR))
    1090           0 :         ereport(LOG,
    1091             :                 (errmsg("ending log output to stderr"),
    1092             :                  errhint("Future log output will go to log destination \"%s\".",
    1093             :                          Log_destination_string)));
    1094             : 
    1095        1698 :     whereToSendOutput = DestNone;
    1096             : 
    1097             :     /*
    1098             :      * Report server startup in log.  While we could emit this much earlier,
    1099             :      * it seems best to do so after starting the log collector, if we intend
    1100             :      * to use one.
    1101             :      */
    1102        1698 :     ereport(LOG,
    1103             :             (errmsg("starting %s", PG_VERSION_STR)));
    1104             : 
    1105             :     /*
    1106             :      * Establish input sockets.
    1107             :      *
    1108             :      * First set up an on_proc_exit function that's charged with closing the
    1109             :      * sockets again at postmaster shutdown.
    1110             :      */
    1111        1698 :     ListenSockets = palloc(MAXLISTEN * sizeof(pgsocket));
    1112        1698 :     on_proc_exit(CloseServerPorts, 0);
    1113             : 
    1114        1698 :     if (ListenAddresses)
    1115             :     {
    1116             :         char       *rawstring;
    1117             :         List       *elemlist;
    1118             :         ListCell   *l;
    1119        1698 :         int         success = 0;
    1120             : 
    1121             :         /* Need a modifiable copy of ListenAddresses */
    1122        1698 :         rawstring = pstrdup(ListenAddresses);
    1123             : 
    1124             :         /* Parse string into list of hostnames */
    1125        1698 :         if (!SplitGUCList(rawstring, ',', &elemlist))
    1126             :         {
    1127             :             /* syntax error in list */
    1128           0 :             ereport(FATAL,
    1129             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1130             :                      errmsg("invalid list syntax in parameter \"%s\"",
    1131             :                             "listen_addresses")));
    1132             :         }
    1133             : 
    1134        1756 :         foreach(l, elemlist)
    1135             :         {
    1136          58 :             char       *curhost = (char *) lfirst(l);
    1137             : 
    1138          58 :             if (strcmp(curhost, "*") == 0)
    1139           0 :                 status = ListenServerPort(AF_UNSPEC, NULL,
    1140           0 :                                           (unsigned short) PostPortNumber,
    1141             :                                           NULL,
    1142             :                                           ListenSockets,
    1143             :                                           &NumListenSockets,
    1144             :                                           MAXLISTEN);
    1145             :             else
    1146          58 :                 status = ListenServerPort(AF_UNSPEC, curhost,
    1147          58 :                                           (unsigned short) PostPortNumber,
    1148             :                                           NULL,
    1149             :                                           ListenSockets,
    1150             :                                           &NumListenSockets,
    1151             :                                           MAXLISTEN);
    1152             : 
    1153          58 :             if (status == STATUS_OK)
    1154             :             {
    1155          58 :                 success++;
    1156             :                 /* record the first successful host addr in lockfile */
    1157          58 :                 if (!listen_addr_saved)
    1158             :                 {
    1159          58 :                     AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR, curhost);
    1160          58 :                     listen_addr_saved = true;
    1161             :                 }
    1162             :             }
    1163             :             else
    1164           0 :                 ereport(WARNING,
    1165             :                         (errmsg("could not create listen socket for \"%s\"",
    1166             :                                 curhost)));
    1167             :         }
    1168             : 
    1169        1698 :         if (!success && elemlist != NIL)
    1170           0 :             ereport(FATAL,
    1171             :                     (errmsg("could not create any TCP/IP sockets")));
    1172             : 
    1173        1698 :         list_free(elemlist);
    1174        1698 :         pfree(rawstring);
    1175             :     }
    1176             : 
    1177             : #ifdef USE_BONJOUR
    1178             :     /* Register for Bonjour only if we opened TCP socket(s) */
    1179             :     if (enable_bonjour && NumListenSockets > 0)
    1180             :     {
    1181             :         DNSServiceErrorType err;
    1182             : 
    1183             :         /*
    1184             :          * We pass 0 for interface_index, which will result in registering on
    1185             :          * all "applicable" interfaces.  It's not entirely clear from the
    1186             :          * DNS-SD docs whether this would be appropriate if we have bound to
    1187             :          * just a subset of the available network interfaces.
    1188             :          */
    1189             :         err = DNSServiceRegister(&bonjour_sdref,
    1190             :                                  0,
    1191             :                                  0,
    1192             :                                  bonjour_name,
    1193             :                                  "_postgresql._tcp.",
    1194             :                                  NULL,
    1195             :                                  NULL,
    1196             :                                  pg_hton16(PostPortNumber),
    1197             :                                  0,
    1198             :                                  NULL,
    1199             :                                  NULL,
    1200             :                                  NULL);
    1201             :         if (err != kDNSServiceErr_NoError)
    1202             :             ereport(LOG,
    1203             :                     (errmsg("DNSServiceRegister() failed: error code %ld",
    1204             :                             (long) err)));
    1205             : 
    1206             :         /*
    1207             :          * We don't bother to read the mDNS daemon's reply, and we expect that
    1208             :          * it will automatically terminate our registration when the socket is
    1209             :          * closed at postmaster termination.  So there's nothing more to be
    1210             :          * done here.  However, the bonjour_sdref is kept around so that
    1211             :          * forked children can close their copies of the socket.
    1212             :          */
    1213             :     }
    1214             : #endif
    1215             : 
    1216        1698 :     if (Unix_socket_directories)
    1217             :     {
    1218             :         char       *rawstring;
    1219             :         List       *elemlist;
    1220             :         ListCell   *l;
    1221        1698 :         int         success = 0;
    1222             : 
    1223             :         /* Need a modifiable copy of Unix_socket_directories */
    1224        1698 :         rawstring = pstrdup(Unix_socket_directories);
    1225             : 
    1226             :         /* Parse string into list of directories */
    1227        1698 :         if (!SplitDirectoriesString(rawstring, ',', &elemlist))
    1228             :         {
    1229             :             /* syntax error in list */
    1230           0 :             ereport(FATAL,
    1231             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    1232             :                      errmsg("invalid list syntax in parameter \"%s\"",
    1233             :                             "unix_socket_directories")));
    1234             :         }
    1235             : 
    1236        3394 :         foreach(l, elemlist)
    1237             :         {
    1238        1696 :             char       *socketdir = (char *) lfirst(l);
    1239             : 
    1240        1696 :             status = ListenServerPort(AF_UNIX, NULL,
    1241        1696 :                                       (unsigned short) PostPortNumber,
    1242             :                                       socketdir,
    1243             :                                       ListenSockets,
    1244             :                                       &NumListenSockets,
    1245             :                                       MAXLISTEN);
    1246             : 
    1247        1696 :             if (status == STATUS_OK)
    1248             :             {
    1249        1696 :                 success++;
    1250             :                 /* record the first successful Unix socket in lockfile */
    1251        1696 :                 if (success == 1)
    1252        1696 :                     AddToDataDirLockFile(LOCK_FILE_LINE_SOCKET_DIR, socketdir);
    1253             :             }
    1254             :             else
    1255           0 :                 ereport(WARNING,
    1256             :                         (errmsg("could not create Unix-domain socket in directory \"%s\"",
    1257             :                                 socketdir)));
    1258             :         }
    1259             : 
    1260        1698 :         if (!success && elemlist != NIL)
    1261           0 :             ereport(FATAL,
    1262             :                     (errmsg("could not create any Unix-domain sockets")));
    1263             : 
    1264        1698 :         list_free_deep(elemlist);
    1265        1698 :         pfree(rawstring);
    1266             :     }
    1267             : 
    1268             :     /*
    1269             :      * check that we have some socket to listen on
    1270             :      */
    1271        1698 :     if (NumListenSockets == 0)
    1272           0 :         ereport(FATAL,
    1273             :                 (errmsg("no socket created for listening")));
    1274             : 
    1275             :     /*
    1276             :      * If no valid TCP ports, write an empty line for listen address,
    1277             :      * indicating the Unix socket must be used.  Note that this line is not
    1278             :      * added to the lock file until there is a socket backing it.
    1279             :      */
    1280        1698 :     if (!listen_addr_saved)
    1281        1640 :         AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR, "");
    1282             : 
    1283             :     /*
    1284             :      * Record postmaster options.  We delay this till now to avoid recording
    1285             :      * bogus options (eg, unusable port number).
    1286             :      */
    1287        1698 :     if (!CreateOptsFile(argc, argv, my_exec_path))
    1288           0 :         ExitPostmaster(1);
    1289             : 
    1290             :     /*
    1291             :      * Write the external PID file if requested
    1292             :      */
    1293        1698 :     if (external_pid_file)
    1294             :     {
    1295           0 :         FILE       *fpidfile = fopen(external_pid_file, "w");
    1296             : 
    1297           0 :         if (fpidfile)
    1298             :         {
    1299           0 :             fprintf(fpidfile, "%d\n", MyProcPid);
    1300           0 :             fclose(fpidfile);
    1301             : 
    1302             :             /* Make PID file world readable */
    1303           0 :             if (chmod(external_pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) != 0)
    1304           0 :                 write_stderr("%s: could not change permissions of external PID file \"%s\": %m\n",
    1305             :                              progname, external_pid_file);
    1306             :         }
    1307             :         else
    1308           0 :             write_stderr("%s: could not write external PID file \"%s\": %m\n",
    1309             :                          progname, external_pid_file);
    1310             : 
    1311           0 :         on_proc_exit(unlink_external_pid_file, 0);
    1312             :     }
    1313             : 
    1314             :     /*
    1315             :      * Remove old temporary files.  At this point there can be no other
    1316             :      * Postgres processes running in this directory, so this should be safe.
    1317             :      */
    1318        1698 :     RemovePgTempFiles();
    1319             : 
    1320             :     /*
    1321             :      * Initialize the autovacuum subsystem (again, no process start yet)
    1322             :      */
    1323        1698 :     autovac_init();
    1324             : 
    1325             :     /*
    1326             :      * Load configuration files for client authentication.
    1327             :      */
    1328        1698 :     if (!load_hba())
    1329             :     {
    1330             :         /*
    1331             :          * It makes no sense to continue if we fail to load the HBA file,
    1332             :          * since there is no way to connect to the database in this case.
    1333             :          */
    1334           0 :         ereport(FATAL,
    1335             :         /* translator: %s is a configuration file */
    1336             :                 (errmsg("could not load %s", HbaFileName)));
    1337             :     }
    1338        1698 :     if (!load_ident())
    1339             :     {
    1340             :         /*
    1341             :          * We can start up without the IDENT file, although it means that you
    1342             :          * cannot log in using any of the authentication methods that need a
    1343             :          * user name mapping. load_ident() already logged the details of error
    1344             :          * to the log.
    1345             :          */
    1346             :     }
    1347             : 
    1348             : #ifdef HAVE_PTHREAD_IS_THREADED_NP
    1349             : 
    1350             :     /*
    1351             :      * On macOS, libintl replaces setlocale() with a version that calls
    1352             :      * CFLocaleCopyCurrent() when its second argument is "" and every relevant
    1353             :      * environment variable is unset or empty.  CFLocaleCopyCurrent() makes
    1354             :      * the process multithreaded.  The postmaster calls sigprocmask() and
    1355             :      * calls fork() without an immediate exec(), both of which have undefined
    1356             :      * behavior in a multithreaded program.  A multithreaded postmaster is the
    1357             :      * normal case on Windows, which offers neither fork() nor sigprocmask().
    1358             :      * Currently, macOS is the only platform having pthread_is_threaded_np(),
    1359             :      * so we need not worry whether this HINT is appropriate elsewhere.
    1360             :      */
    1361             :     if (pthread_is_threaded_np() != 0)
    1362             :         ereport(FATAL,
    1363             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1364             :                  errmsg("postmaster became multithreaded during startup"),
    1365             :                  errhint("Set the LC_ALL environment variable to a valid locale.")));
    1366             : #endif
    1367             : 
    1368             :     /*
    1369             :      * Remember postmaster startup time
    1370             :      */
    1371        1698 :     PgStartTime = GetCurrentTimestamp();
    1372             : 
    1373             :     /*
    1374             :      * Report postmaster status in the postmaster.pid file, to allow pg_ctl to
    1375             :      * see what's happening.
    1376             :      */
    1377        1698 :     AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STARTING);
    1378             : 
    1379        1698 :     UpdatePMState(PM_STARTUP);
    1380             : 
    1381             :     /* Make sure we can perform I/O while starting up. */
    1382        1698 :     maybe_adjust_io_workers();
    1383             : 
    1384             :     /* Start bgwriter and checkpointer so they can help with recovery */
    1385        1698 :     if (CheckpointerPMChild == NULL)
    1386        1698 :         CheckpointerPMChild = StartChildProcess(B_CHECKPOINTER);
    1387        1698 :     if (BgWriterPMChild == NULL)
    1388        1698 :         BgWriterPMChild = StartChildProcess(B_BG_WRITER);
    1389             : 
    1390             :     /*
    1391             :      * We're ready to rock and roll...
    1392             :      */
    1393        1698 :     StartupPMChild = StartChildProcess(B_STARTUP);
    1394             :     Assert(StartupPMChild != NULL);
    1395        1698 :     StartupStatus = STARTUP_RUNNING;
    1396             : 
    1397             :     /* Some workers may be scheduled to start now */
    1398        1698 :     maybe_start_bgworkers();
    1399             : 
    1400        1698 :     status = ServerLoop();
    1401             : 
    1402             :     /*
    1403             :      * ServerLoop probably shouldn't ever return, but if it does, close down.
    1404             :      */
    1405           0 :     ExitPostmaster(status != STATUS_OK);
    1406             : 
    1407             :     abort();                    /* not reached */
    1408             : }
    1409             : 
    1410             : 
    1411             : /*
    1412             :  * on_proc_exit callback to close server's listen sockets
    1413             :  */
    1414             : static void
    1415        1698 : CloseServerPorts(int status, Datum arg)
    1416             : {
    1417             :     int         i;
    1418             : 
    1419             :     /*
    1420             :      * First, explicitly close all the socket FDs.  We used to just let this
    1421             :      * happen implicitly at postmaster exit, but it's better to close them
    1422             :      * before we remove the postmaster.pid lockfile; otherwise there's a race
    1423             :      * condition if a new postmaster wants to re-use the TCP port number.
    1424             :      */
    1425        3454 :     for (i = 0; i < NumListenSockets; i++)
    1426             :     {
    1427        1756 :         if (closesocket(ListenSockets[i]) != 0)
    1428           0 :             elog(LOG, "could not close listen socket: %m");
    1429             :     }
    1430        1698 :     NumListenSockets = 0;
    1431             : 
    1432             :     /*
    1433             :      * Next, remove any filesystem entries for Unix sockets.  To avoid race
    1434             :      * conditions against incoming postmasters, this must happen after closing
    1435             :      * the sockets and before removing lock files.
    1436             :      */
    1437        1698 :     RemoveSocketFiles();
    1438             : 
    1439             :     /*
    1440             :      * We don't do anything about socket lock files here; those will be
    1441             :      * removed in a later on_proc_exit callback.
    1442             :      */
    1443        1698 : }
    1444             : 
    1445             : /*
    1446             :  * on_proc_exit callback to delete external_pid_file
    1447             :  */
    1448             : static void
    1449           0 : unlink_external_pid_file(int status, Datum arg)
    1450             : {
    1451           0 :     if (external_pid_file)
    1452           0 :         unlink(external_pid_file);
    1453           0 : }
    1454             : 
    1455             : 
    1456             : /*
    1457             :  * Compute and check the directory paths to files that are part of the
    1458             :  * installation (as deduced from the postgres executable's own location)
    1459             :  */
    1460             : static void
    1461        1728 : getInstallationPaths(const char *argv0)
    1462             : {
    1463             :     DIR        *pdir;
    1464             : 
    1465             :     /* Locate the postgres executable itself */
    1466        1728 :     if (find_my_exec(argv0, my_exec_path) < 0)
    1467           0 :         ereport(FATAL,
    1468             :                 (errmsg("%s: could not locate my own executable path", argv0)));
    1469             : 
    1470             : #ifdef EXEC_BACKEND
    1471             :     /* Locate executable backend before we change working directory */
    1472             :     if (find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
    1473             :                         postgres_exec_path) < 0)
    1474             :         ereport(FATAL,
    1475             :                 (errmsg("%s: could not locate matching postgres executable",
    1476             :                         argv0)));
    1477             : #endif
    1478             : 
    1479             :     /*
    1480             :      * Locate the pkglib directory --- this has to be set early in case we try
    1481             :      * to load any modules from it in response to postgresql.conf entries.
    1482             :      */
    1483        1728 :     get_pkglib_path(my_exec_path, pkglib_path);
    1484             : 
    1485             :     /*
    1486             :      * Verify that there's a readable directory there; otherwise the Postgres
    1487             :      * installation is incomplete or corrupt.  (A typical cause of this
    1488             :      * failure is that the postgres executable has been moved or hardlinked to
    1489             :      * some directory that's not a sibling of the installation lib/
    1490             :      * directory.)
    1491             :      */
    1492        1728 :     pdir = AllocateDir(pkglib_path);
    1493        1728 :     if (pdir == NULL)
    1494           0 :         ereport(ERROR,
    1495             :                 (errcode_for_file_access(),
    1496             :                  errmsg("could not open directory \"%s\": %m",
    1497             :                         pkglib_path),
    1498             :                  errhint("This may indicate an incomplete PostgreSQL installation, or that the file \"%s\" has been moved away from its proper location.",
    1499             :                          my_exec_path)));
    1500        1728 :     FreeDir(pdir);
    1501             : 
    1502             :     /*
    1503             :      * It's not worth checking the share/ directory.  If the lib/ directory is
    1504             :      * there, then share/ probably is too.
    1505             :      */
    1506        1728 : }
    1507             : 
    1508             : /*
    1509             :  * Check that pg_control exists in the correct location in the data directory.
    1510             :  *
    1511             :  * No attempt is made to validate the contents of pg_control here.  This is
    1512             :  * just a sanity check to see if we are looking at a real data directory.
    1513             :  */
    1514             : static void
    1515        1714 : checkControlFile(void)
    1516             : {
    1517             :     char        path[MAXPGPATH];
    1518             :     FILE       *fp;
    1519             : 
    1520        1714 :     snprintf(path, sizeof(path), "%s/%s", DataDir, XLOG_CONTROL_FILE);
    1521             : 
    1522        1714 :     fp = AllocateFile(path, PG_BINARY_R);
    1523        1714 :     if (fp == NULL)
    1524             :     {
    1525           0 :         write_stderr("%s: could not find the database system\n"
    1526             :                      "Expected to find it in the directory \"%s\",\n"
    1527             :                      "but could not open file \"%s\": %m\n",
    1528             :                      progname, DataDir, path);
    1529           0 :         ExitPostmaster(2);
    1530             :     }
    1531        1714 :     FreeFile(fp);
    1532        1714 : }
    1533             : 
    1534             : /*
    1535             :  * Determine how long should we let ServerLoop sleep, in milliseconds.
    1536             :  *
    1537             :  * In normal conditions we wait at most one minute, to ensure that the other
    1538             :  * background tasks handled by ServerLoop get done even when no requests are
    1539             :  * arriving.  However, if there are background workers waiting to be started,
    1540             :  * we don't actually sleep so that they are quickly serviced.  Other exception
    1541             :  * cases are as shown in the code.
    1542             :  */
    1543             : static int
    1544      269778 : DetermineSleepTime(void)
    1545             : {
    1546      269778 :     TimestampTz next_wakeup = 0;
    1547             : 
    1548             :     /*
    1549             :      * Normal case: either there are no background workers at all, or we're in
    1550             :      * a shutdown sequence (during which we ignore bgworkers altogether).
    1551             :      */
    1552      269778 :     if (Shutdown > NoShutdown ||
    1553      256774 :         (!StartWorkerNeeded && !HaveCrashedWorker))
    1554             :     {
    1555      269778 :         if (AbortStartTime != 0)
    1556             :         {
    1557             :             int         seconds;
    1558             : 
    1559             :             /* time left to abort; clamp to 0 in case it already expired */
    1560        2378 :             seconds = SIGKILL_CHILDREN_AFTER_SECS -
    1561        2378 :                 (time(NULL) - AbortStartTime);
    1562             : 
    1563        2378 :             return Max(seconds * 1000, 0);
    1564             :         }
    1565             :         else
    1566      267400 :             return 60 * 1000;
    1567             :     }
    1568             : 
    1569           0 :     if (StartWorkerNeeded)
    1570           0 :         return 0;
    1571             : 
    1572           0 :     if (HaveCrashedWorker)
    1573             :     {
    1574             :         dlist_mutable_iter iter;
    1575             : 
    1576             :         /*
    1577             :          * When there are crashed bgworkers, we sleep just long enough that
    1578             :          * they are restarted when they request to be.  Scan the list to
    1579             :          * determine the minimum of all wakeup times according to most recent
    1580             :          * crash time and requested restart interval.
    1581             :          */
    1582           0 :         dlist_foreach_modify(iter, &BackgroundWorkerList)
    1583             :         {
    1584             :             RegisteredBgWorker *rw;
    1585             :             TimestampTz this_wakeup;
    1586             : 
    1587           0 :             rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
    1588             : 
    1589           0 :             if (rw->rw_crashed_at == 0)
    1590           0 :                 continue;
    1591             : 
    1592           0 :             if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART
    1593           0 :                 || rw->rw_terminate)
    1594             :             {
    1595           0 :                 ForgetBackgroundWorker(rw);
    1596           0 :                 continue;
    1597             :             }
    1598             : 
    1599           0 :             this_wakeup = TimestampTzPlusMilliseconds(rw->rw_crashed_at,
    1600             :                                                       1000L * rw->rw_worker.bgw_restart_time);
    1601           0 :             if (next_wakeup == 0 || this_wakeup < next_wakeup)
    1602           0 :                 next_wakeup = this_wakeup;
    1603             :         }
    1604             :     }
    1605             : 
    1606           0 :     if (next_wakeup != 0)
    1607             :     {
    1608             :         int         ms;
    1609             : 
    1610             :         /* result of TimestampDifferenceMilliseconds is in [0, INT_MAX] */
    1611           0 :         ms = (int) TimestampDifferenceMilliseconds(GetCurrentTimestamp(),
    1612             :                                                    next_wakeup);
    1613           0 :         return Min(60 * 1000, ms);
    1614             :     }
    1615             : 
    1616           0 :     return 60 * 1000;
    1617             : }
    1618             : 
    1619             : /*
    1620             :  * Activate or deactivate notifications of server socket events.  Since we
    1621             :  * don't currently have a way to remove events from an existing WaitEventSet,
    1622             :  * we'll just destroy and recreate the whole thing.  This is called during
    1623             :  * shutdown so we can wait for backends to exit without accepting new
    1624             :  * connections, and during crash reinitialization when we need to start
    1625             :  * listening for new connections again.  The WaitEventSet will be freed in fork
    1626             :  * children by ClosePostmasterPorts().
    1627             :  */
    1628             : static void
    1629        3414 : ConfigurePostmasterWaitSet(bool accept_connections)
    1630             : {
    1631        3414 :     if (pm_wait_set)
    1632        1716 :         FreeWaitEventSet(pm_wait_set);
    1633        3414 :     pm_wait_set = NULL;
    1634             : 
    1635        5122 :     pm_wait_set = CreateWaitEventSet(NULL,
    1636        1708 :                                      accept_connections ? (1 + NumListenSockets) : 1);
    1637        3414 :     AddWaitEventToSet(pm_wait_set, WL_LATCH_SET, PGINVALID_SOCKET, MyLatch,
    1638             :                       NULL);
    1639             : 
    1640        3414 :     if (accept_connections)
    1641             :     {
    1642        3474 :         for (int i = 0; i < NumListenSockets; i++)
    1643        1766 :             AddWaitEventToSet(pm_wait_set, WL_SOCKET_ACCEPT, ListenSockets[i],
    1644             :                               NULL, NULL);
    1645             :     }
    1646        3414 : }
    1647             : 
    1648             : /*
    1649             :  * Main idle loop of postmaster
    1650             :  */
    1651             : static int
    1652        1698 : ServerLoop(void)
    1653             : {
    1654             :     time_t      last_lockfile_recheck_time,
    1655             :                 last_touch_time;
    1656             :     WaitEvent   events[MAXLISTEN];
    1657             :     int         nevents;
    1658             : 
    1659        1698 :     ConfigurePostmasterWaitSet(true);
    1660        1698 :     last_lockfile_recheck_time = last_touch_time = time(NULL);
    1661             : 
    1662             :     for (;;)
    1663      268080 :     {
    1664             :         time_t      now;
    1665             : 
    1666      269778 :         nevents = WaitEventSetWait(pm_wait_set,
    1667      269778 :                                    DetermineSleepTime(),
    1668             :                                    events,
    1669             :                                    lengthof(events),
    1670             :                                    0 /* postmaster posts no wait_events */ );
    1671             : 
    1672             :         /*
    1673             :          * Latch set by signal handler, or new connection pending on any of
    1674             :          * our sockets? If the latter, fork a child process to deal with it.
    1675             :          */
    1676      537852 :         for (int i = 0; i < nevents; i++)
    1677             :         {
    1678      269772 :             if (events[i].events & WL_LATCH_SET)
    1679      242904 :                 ResetLatch(MyLatch);
    1680             : 
    1681             :             /*
    1682             :              * The following requests are handled unconditionally, even if we
    1683             :              * didn't see WL_LATCH_SET.  This gives high priority to shutdown
    1684             :              * and reload requests where the latch happens to appear later in
    1685             :              * events[] or will be reported by a later call to
    1686             :              * WaitEventSetWait().
    1687             :              */
    1688      269772 :             if (pending_pm_shutdown_request)
    1689        1690 :                 process_pm_shutdown_request();
    1690      269772 :             if (pending_pm_reload_request)
    1691         270 :                 process_pm_reload_request();
    1692      269772 :             if (pending_pm_child_exit)
    1693       44952 :                 process_pm_child_exit();
    1694      268074 :             if (pending_pm_pmsignal)
    1695      196042 :                 process_pm_pmsignal();
    1696             : 
    1697      268074 :             if (events[i].events & WL_SOCKET_ACCEPT)
    1698             :             {
    1699             :                 ClientSocket s;
    1700             : 
    1701       26868 :                 if (AcceptConnection(events[i].fd, &s) == STATUS_OK)
    1702       26868 :                     BackendStartup(&s);
    1703             : 
    1704             :                 /* We no longer need the open socket in this process */
    1705       26868 :                 if (s.sock != PGINVALID_SOCKET)
    1706             :                 {
    1707       26868 :                     if (closesocket(s.sock) != 0)
    1708           0 :                         elog(LOG, "could not close client socket: %m");
    1709             :                 }
    1710             :             }
    1711             :         }
    1712             : 
    1713             :         /*
    1714             :          * If we need to launch any background processes after changing state
    1715             :          * or because some exited, do so now.
    1716             :          */
    1717      268080 :         LaunchMissingBackgroundProcesses();
    1718             : 
    1719             :         /* If we need to signal the autovacuum launcher, do so now */
    1720      268080 :         if (avlauncher_needs_signal)
    1721             :         {
    1722           0 :             avlauncher_needs_signal = false;
    1723           0 :             if (AutoVacLauncherPMChild != NULL)
    1724           0 :                 signal_child(AutoVacLauncherPMChild, SIGUSR2);
    1725             :         }
    1726             : 
    1727             : #ifdef HAVE_PTHREAD_IS_THREADED_NP
    1728             : 
    1729             :         /*
    1730             :          * With assertions enabled, check regularly for appearance of
    1731             :          * additional threads.  All builds check at start and exit.
    1732             :          */
    1733             :         Assert(pthread_is_threaded_np() == 0);
    1734             : #endif
    1735             : 
    1736             :         /*
    1737             :          * Lastly, check to see if it's time to do some things that we don't
    1738             :          * want to do every single time through the loop, because they're a
    1739             :          * bit expensive.  Note that there's up to a minute of slop in when
    1740             :          * these tasks will be performed, since DetermineSleepTime() will let
    1741             :          * us sleep at most that long; except for SIGKILL timeout which has
    1742             :          * special-case logic there.
    1743             :          */
    1744      268080 :         now = time(NULL);
    1745             : 
    1746             :         /*
    1747             :          * If we already sent SIGQUIT to children and they are slow to shut
    1748             :          * down, it's time to send them SIGKILL (or SIGABRT if requested).
    1749             :          * This doesn't happen normally, but under certain conditions backends
    1750             :          * can get stuck while shutting down.  This is a last measure to get
    1751             :          * them unwedged.
    1752             :          *
    1753             :          * Note we also do this during recovery from a process crash.
    1754             :          */
    1755      268080 :         if ((Shutdown >= ImmediateShutdown || FatalError) &&
    1756        2396 :             AbortStartTime != 0 &&
    1757        2378 :             (now - AbortStartTime) >= SIGKILL_CHILDREN_AFTER_SECS)
    1758             :         {
    1759             :             /* We were gentle with them before. Not anymore */
    1760           0 :             ereport(LOG,
    1761             :             /* translator: %s is SIGKILL or SIGABRT */
    1762             :                     (errmsg("issuing %s to recalcitrant children",
    1763             :                             send_abort_for_kill ? "SIGABRT" : "SIGKILL")));
    1764           0 :             TerminateChildren(send_abort_for_kill ? SIGABRT : SIGKILL);
    1765             :             /* reset flag so we don't SIGKILL again */
    1766           0 :             AbortStartTime = 0;
    1767             :         }
    1768             : 
    1769             :         /*
    1770             :          * Once a minute, verify that postmaster.pid hasn't been removed or
    1771             :          * overwritten.  If it has, we force a shutdown.  This avoids having
    1772             :          * postmasters and child processes hanging around after their database
    1773             :          * is gone, and maybe causing problems if a new database cluster is
    1774             :          * created in the same place.  It also provides some protection
    1775             :          * against a DBA foolishly removing postmaster.pid and manually
    1776             :          * starting a new postmaster.  Data corruption is likely to ensue from
    1777             :          * that anyway, but we can minimize the damage by aborting ASAP.
    1778             :          */
    1779      268080 :         if (now - last_lockfile_recheck_time >= 1 * SECS_PER_MINUTE)
    1780             :         {
    1781          56 :             if (!RecheckDataDirLockFile())
    1782             :             {
    1783           0 :                 ereport(LOG,
    1784             :                         (errmsg("performing immediate shutdown because data directory lock file is invalid")));
    1785           0 :                 kill(MyProcPid, SIGQUIT);
    1786             :             }
    1787          56 :             last_lockfile_recheck_time = now;
    1788             :         }
    1789             : 
    1790             :         /*
    1791             :          * Touch Unix socket and lock files every 58 minutes, to ensure that
    1792             :          * they are not removed by overzealous /tmp-cleaning tasks.  We assume
    1793             :          * no one runs cleaners with cutoff times of less than an hour ...
    1794             :          */
    1795      268080 :         if (now - last_touch_time >= 58 * SECS_PER_MINUTE)
    1796             :         {
    1797           0 :             TouchSocketFiles();
    1798           0 :             TouchSocketLockFiles();
    1799           0 :             last_touch_time = now;
    1800             :         }
    1801             :     }
    1802             : }
    1803             : 
    1804             : /*
    1805             :  * canAcceptConnections --- check to see if database state allows connections
    1806             :  * of the specified type.  backend_type can be B_BACKEND or B_AUTOVAC_WORKER.
    1807             :  * (Note that we don't yet know whether a normal B_BACKEND connection might
    1808             :  * turn into a walsender.)
    1809             :  */
    1810             : static CAC_state
    1811       30310 : canAcceptConnections(BackendType backend_type)
    1812             : {
    1813       30310 :     CAC_state   result = CAC_OK;
    1814             : 
    1815             :     Assert(backend_type == B_BACKEND || backend_type == B_AUTOVAC_WORKER);
    1816             : 
    1817             :     /*
    1818             :      * Can't start backends when in startup/shutdown/inconsistent recovery
    1819             :      * state.  We treat autovac workers the same as user backends for this
    1820             :      * purpose.
    1821             :      */
    1822       30310 :     if (pmState != PM_RUN && pmState != PM_HOT_STANDBY)
    1823             :     {
    1824         724 :         if (Shutdown > NoShutdown)
    1825         106 :             return CAC_SHUTDOWN;    /* shutdown is pending */
    1826         618 :         else if (!FatalError && pmState == PM_STARTUP)
    1827         586 :             return CAC_STARTUP; /* normal startup */
    1828          32 :         else if (!FatalError && pmState == PM_RECOVERY)
    1829          24 :             return CAC_NOTHOTSTANDBY;   /* not yet ready for hot standby */
    1830             :         else
    1831           8 :             return CAC_RECOVERY;    /* else must be crash recovery */
    1832             :     }
    1833             : 
    1834             :     /*
    1835             :      * "Smart shutdown" restrictions are applied only to normal connections,
    1836             :      * not to autovac workers.
    1837             :      */
    1838       29586 :     if (!connsAllowed && backend_type == B_BACKEND)
    1839           0 :         return CAC_SHUTDOWN;    /* shutdown is pending */
    1840             : 
    1841       29586 :     return result;
    1842             : }
    1843             : 
    1844             : /*
    1845             :  * ClosePostmasterPorts -- close all the postmaster's open sockets
    1846             :  *
    1847             :  * This is called during child process startup to release file descriptors
    1848             :  * that are not needed by that child process.  The postmaster still has
    1849             :  * them open, of course.
    1850             :  *
    1851             :  * Note: we pass am_syslogger as a boolean because we don't want to set
    1852             :  * the global variable yet when this is called.
    1853             :  */
    1854             : void
    1855       43228 : ClosePostmasterPorts(bool am_syslogger)
    1856             : {
    1857             :     /* Release resources held by the postmaster's WaitEventSet. */
    1858       43228 :     if (pm_wait_set)
    1859             :     {
    1860       36448 :         FreeWaitEventSetAfterFork(pm_wait_set);
    1861       36448 :         pm_wait_set = NULL;
    1862             :     }
    1863             : 
    1864             : #ifndef WIN32
    1865             : 
    1866             :     /*
    1867             :      * Close the write end of postmaster death watch pipe. It's important to
    1868             :      * do this as early as possible, so that if postmaster dies, others won't
    1869             :      * think that it's still running because we're holding the pipe open.
    1870             :      */
    1871       43228 :     if (close(postmaster_alive_fds[POSTMASTER_FD_OWN]) != 0)
    1872           0 :         ereport(FATAL,
    1873             :                 (errcode_for_file_access(),
    1874             :                  errmsg_internal("could not close postmaster death monitoring pipe in child process: %m")));
    1875       43228 :     postmaster_alive_fds[POSTMASTER_FD_OWN] = -1;
    1876             :     /* Notify fd.c that we released one pipe FD. */
    1877       43228 :     ReleaseExternalFD();
    1878             : #endif
    1879             : 
    1880             :     /*
    1881             :      * Close the postmaster's listen sockets.  These aren't tracked by fd.c,
    1882             :      * so we don't call ReleaseExternalFD() here.
    1883             :      *
    1884             :      * The listen sockets are marked as FD_CLOEXEC, so this isn't needed in
    1885             :      * EXEC_BACKEND mode.
    1886             :      */
    1887             : #ifndef EXEC_BACKEND
    1888       43228 :     if (ListenSockets)
    1889             :     {
    1890       87514 :         for (int i = 0; i < NumListenSockets; i++)
    1891             :         {
    1892       44288 :             if (closesocket(ListenSockets[i]) != 0)
    1893           0 :                 elog(LOG, "could not close listen socket: %m");
    1894             :         }
    1895       43226 :         pfree(ListenSockets);
    1896             :     }
    1897       43228 :     NumListenSockets = 0;
    1898       43228 :     ListenSockets = NULL;
    1899             : #endif
    1900             : 
    1901             :     /*
    1902             :      * If using syslogger, close the read side of the pipe.  We don't bother
    1903             :      * tracking this in fd.c, either.
    1904             :      */
    1905       43228 :     if (!am_syslogger)
    1906             :     {
    1907             : #ifndef WIN32
    1908       43226 :         if (syslogPipe[0] >= 0)
    1909          34 :             close(syslogPipe[0]);
    1910       43226 :         syslogPipe[0] = -1;
    1911             : #else
    1912             :         if (syslogPipe[0])
    1913             :             CloseHandle(syslogPipe[0]);
    1914             :         syslogPipe[0] = 0;
    1915             : #endif
    1916             :     }
    1917             : 
    1918             : #ifdef USE_BONJOUR
    1919             :     /* If using Bonjour, close the connection to the mDNS daemon */
    1920             :     if (bonjour_sdref)
    1921             :         close(DNSServiceRefSockFD(bonjour_sdref));
    1922             : #endif
    1923       43228 : }
    1924             : 
    1925             : 
    1926             : /*
    1927             :  * InitProcessGlobals -- set MyStartTime[stamp], random seeds
    1928             :  *
    1929             :  * Called early in the postmaster and every backend.
    1930             :  */
    1931             : void
    1932       45428 : InitProcessGlobals(void)
    1933             : {
    1934       45428 :     MyStartTimestamp = GetCurrentTimestamp();
    1935       45428 :     MyStartTime = timestamptz_to_time_t(MyStartTimestamp);
    1936             : 
    1937             :     /*
    1938             :      * Set a different global seed in every process.  We want something
    1939             :      * unpredictable, so if possible, use high-quality random bits for the
    1940             :      * seed.  Otherwise, fall back to a seed based on timestamp and PID.
    1941             :      */
    1942       45428 :     if (unlikely(!pg_prng_strong_seed(&pg_global_prng_state)))
    1943             :     {
    1944             :         uint64      rseed;
    1945             : 
    1946             :         /*
    1947             :          * Since PIDs and timestamps tend to change more frequently in their
    1948             :          * least significant bits, shift the timestamp left to allow a larger
    1949             :          * total number of seeds in a given time period.  Since that would
    1950             :          * leave only 20 bits of the timestamp that cycle every ~1 second,
    1951             :          * also mix in some higher bits.
    1952             :          */
    1953           0 :         rseed = ((uint64) MyProcPid) ^
    1954           0 :             ((uint64) MyStartTimestamp << 12) ^
    1955           0 :             ((uint64) MyStartTimestamp >> 20);
    1956             : 
    1957           0 :         pg_prng_seed(&pg_global_prng_state, rseed);
    1958             :     }
    1959             : 
    1960             :     /*
    1961             :      * Also make sure that we've set a good seed for random(3).  Use of that
    1962             :      * is deprecated in core Postgres, but extensions might use it.
    1963             :      */
    1964             : #ifndef WIN32
    1965       45428 :     srandom(pg_prng_uint32(&pg_global_prng_state));
    1966             : #endif
    1967       45428 : }
    1968             : 
    1969             : /*
    1970             :  * Child processes use SIGUSR1 to notify us of 'pmsignals'.  pg_ctl uses
    1971             :  * SIGUSR1 to ask postmaster to check for logrotate and promote files.
    1972             :  */
    1973             : static void
    1974      196568 : handle_pm_pmsignal_signal(SIGNAL_ARGS)
    1975             : {
    1976      196568 :     pending_pm_pmsignal = true;
    1977      196568 :     SetLatch(MyLatch);
    1978      196568 : }
    1979             : 
    1980             : /*
    1981             :  * pg_ctl uses SIGHUP to request a reload of the configuration files.
    1982             :  */
    1983             : static void
    1984         270 : handle_pm_reload_request_signal(SIGNAL_ARGS)
    1985             : {
    1986         270 :     pending_pm_reload_request = true;
    1987         270 :     SetLatch(MyLatch);
    1988         270 : }
    1989             : 
    1990             : /*
    1991             :  * Re-read config files, and tell children to do same.
    1992             :  */
    1993             : static void
    1994         270 : process_pm_reload_request(void)
    1995             : {
    1996         270 :     pending_pm_reload_request = false;
    1997             : 
    1998         270 :     ereport(DEBUG2,
    1999             :             (errmsg_internal("postmaster received reload request signal")));
    2000             : 
    2001         270 :     if (Shutdown <= SmartShutdown)
    2002             :     {
    2003         270 :         ereport(LOG,
    2004             :                 (errmsg("received SIGHUP, reloading configuration files")));
    2005         270 :         ProcessConfigFile(PGC_SIGHUP);
    2006         270 :         SignalChildren(SIGHUP, btmask_all_except(B_DEAD_END_BACKEND));
    2007             : 
    2008             :         /* Reload authentication config files too */
    2009         270 :         if (!load_hba())
    2010           0 :             ereport(LOG,
    2011             :             /* translator: %s is a configuration file */
    2012             :                     (errmsg("%s was not reloaded", HbaFileName)));
    2013             : 
    2014         270 :         if (!load_ident())
    2015           0 :             ereport(LOG,
    2016             :                     (errmsg("%s was not reloaded", IdentFileName)));
    2017             : 
    2018             : #ifdef USE_SSL
    2019             :         /* Reload SSL configuration as well */
    2020         270 :         if (EnableSSL)
    2021             :         {
    2022           2 :             if (secure_initialize(false) == 0)
    2023           2 :                 LoadedSSL = true;
    2024             :             else
    2025           0 :                 ereport(LOG,
    2026             :                         (errmsg("SSL configuration was not reloaded")));
    2027             :         }
    2028             :         else
    2029             :         {
    2030         268 :             secure_destroy();
    2031         268 :             LoadedSSL = false;
    2032             :         }
    2033             : #endif
    2034             : 
    2035             : #ifdef EXEC_BACKEND
    2036             :         /* Update the starting-point file for future children */
    2037             :         write_nondefault_variables(PGC_SIGHUP);
    2038             : #endif
    2039             :     }
    2040         270 : }
    2041             : 
    2042             : /*
    2043             :  * pg_ctl uses SIGTERM, SIGINT and SIGQUIT to request different types of
    2044             :  * shutdown.
    2045             :  */
    2046             : static void
    2047        1690 : handle_pm_shutdown_request_signal(SIGNAL_ARGS)
    2048             : {
    2049        1690 :     switch (postgres_signal_arg)
    2050             :     {
    2051          80 :         case SIGTERM:
    2052             :             /* smart is implied if the other two flags aren't set */
    2053          80 :             pending_pm_shutdown_request = true;
    2054          80 :             break;
    2055         950 :         case SIGINT:
    2056         950 :             pending_pm_fast_shutdown_request = true;
    2057         950 :             pending_pm_shutdown_request = true;
    2058         950 :             break;
    2059         660 :         case SIGQUIT:
    2060         660 :             pending_pm_immediate_shutdown_request = true;
    2061         660 :             pending_pm_shutdown_request = true;
    2062         660 :             break;
    2063             :     }
    2064        1690 :     SetLatch(MyLatch);
    2065        1690 : }
    2066             : 
    2067             : /*
    2068             :  * Process shutdown request.
    2069             :  */
    2070             : static void
    2071        1690 : process_pm_shutdown_request(void)
    2072             : {
    2073             :     int         mode;
    2074             : 
    2075        1690 :     ereport(DEBUG2,
    2076             :             (errmsg_internal("postmaster received shutdown request signal")));
    2077             : 
    2078        1690 :     pending_pm_shutdown_request = false;
    2079             : 
    2080             :     /*
    2081             :      * If more than one shutdown request signal arrived since the last server
    2082             :      * loop, take the one that is the most immediate.  That matches the
    2083             :      * priority that would apply if we processed them one by one in any order.
    2084             :      */
    2085        1690 :     if (pending_pm_immediate_shutdown_request)
    2086             :     {
    2087         660 :         pending_pm_immediate_shutdown_request = false;
    2088         660 :         pending_pm_fast_shutdown_request = false;
    2089         660 :         mode = ImmediateShutdown;
    2090             :     }
    2091        1030 :     else if (pending_pm_fast_shutdown_request)
    2092             :     {
    2093         950 :         pending_pm_fast_shutdown_request = false;
    2094         950 :         mode = FastShutdown;
    2095             :     }
    2096             :     else
    2097          80 :         mode = SmartShutdown;
    2098             : 
    2099        1690 :     switch (mode)
    2100             :     {
    2101          80 :         case SmartShutdown:
    2102             : 
    2103             :             /*
    2104             :              * Smart Shutdown:
    2105             :              *
    2106             :              * Wait for children to end their work, then shut down.
    2107             :              */
    2108          80 :             if (Shutdown >= SmartShutdown)
    2109           0 :                 break;
    2110          80 :             Shutdown = SmartShutdown;
    2111          80 :             ereport(LOG,
    2112             :                     (errmsg("received smart shutdown request")));
    2113             : 
    2114             :             /* Report status */
    2115          80 :             AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STOPPING);
    2116             : #ifdef USE_SYSTEMD
    2117             :             sd_notify(0, "STOPPING=1");
    2118             : #endif
    2119             : 
    2120             :             /*
    2121             :              * If we reached normal running, we go straight to waiting for
    2122             :              * client backends to exit.  If already in PM_STOP_BACKENDS or a
    2123             :              * later state, do not change it.
    2124             :              */
    2125          80 :             if (pmState == PM_RUN || pmState == PM_HOT_STANDBY)
    2126          80 :                 connsAllowed = false;
    2127           0 :             else if (pmState == PM_STARTUP || pmState == PM_RECOVERY)
    2128             :             {
    2129             :                 /* There should be no clients, so proceed to stop children */
    2130           0 :                 UpdatePMState(PM_STOP_BACKENDS);
    2131             :             }
    2132             : 
    2133             :             /*
    2134             :              * Now wait for online backup mode to end and backends to exit. If
    2135             :              * that is already the case, PostmasterStateMachine will take the
    2136             :              * next step.
    2137             :              */
    2138          80 :             PostmasterStateMachine();
    2139          80 :             break;
    2140             : 
    2141         950 :         case FastShutdown:
    2142             : 
    2143             :             /*
    2144             :              * Fast Shutdown:
    2145             :              *
    2146             :              * Abort all children with SIGTERM (rollback active transactions
    2147             :              * and exit) and shut down when they are gone.
    2148             :              */
    2149         950 :             if (Shutdown >= FastShutdown)
    2150           0 :                 break;
    2151         950 :             Shutdown = FastShutdown;
    2152         950 :             ereport(LOG,
    2153             :                     (errmsg("received fast shutdown request")));
    2154             : 
    2155             :             /* Report status */
    2156         950 :             AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STOPPING);
    2157             : #ifdef USE_SYSTEMD
    2158             :             sd_notify(0, "STOPPING=1");
    2159             : #endif
    2160             : 
    2161         950 :             if (pmState == PM_STARTUP || pmState == PM_RECOVERY)
    2162             :             {
    2163             :                 /* Just shut down background processes silently */
    2164           0 :                 UpdatePMState(PM_STOP_BACKENDS);
    2165             :             }
    2166         950 :             else if (pmState == PM_RUN ||
    2167         116 :                      pmState == PM_HOT_STANDBY)
    2168             :             {
    2169             :                 /* Report that we're about to zap live client sessions */
    2170         950 :                 ereport(LOG,
    2171             :                         (errmsg("aborting any active transactions")));
    2172         950 :                 UpdatePMState(PM_STOP_BACKENDS);
    2173             :             }
    2174             : 
    2175             :             /*
    2176             :              * PostmasterStateMachine will issue any necessary signals, or
    2177             :              * take the next step if no child processes need to be killed.
    2178             :              */
    2179         950 :             PostmasterStateMachine();
    2180         950 :             break;
    2181             : 
    2182         660 :         case ImmediateShutdown:
    2183             : 
    2184             :             /*
    2185             :              * Immediate Shutdown:
    2186             :              *
    2187             :              * abort all children with SIGQUIT, wait for them to exit,
    2188             :              * terminate remaining ones with SIGKILL, then exit without
    2189             :              * attempt to properly shut down the data base system.
    2190             :              */
    2191         660 :             if (Shutdown >= ImmediateShutdown)
    2192           0 :                 break;
    2193         660 :             Shutdown = ImmediateShutdown;
    2194         660 :             ereport(LOG,
    2195             :                     (errmsg("received immediate shutdown request")));
    2196             : 
    2197             :             /* Report status */
    2198         660 :             AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STOPPING);
    2199             : #ifdef USE_SYSTEMD
    2200             :             sd_notify(0, "STOPPING=1");
    2201             : #endif
    2202             : 
    2203             :             /* tell children to shut down ASAP */
    2204             :             /* (note we don't apply send_abort_for_crash here) */
    2205         660 :             SetQuitSignalReason(PMQUIT_FOR_STOP);
    2206         660 :             TerminateChildren(SIGQUIT);
    2207         660 :             UpdatePMState(PM_WAIT_BACKENDS);
    2208             : 
    2209             :             /* set stopwatch for them to die */
    2210         660 :             AbortStartTime = time(NULL);
    2211             : 
    2212             :             /*
    2213             :              * Now wait for backends to exit.  If there are none,
    2214             :              * PostmasterStateMachine will take the next step.
    2215             :              */
    2216         660 :             PostmasterStateMachine();
    2217         660 :             break;
    2218             :     }
    2219        1690 : }
    2220             : 
    2221             : static void
    2222       45034 : handle_pm_child_exit_signal(SIGNAL_ARGS)
    2223             : {
    2224       45034 :     pending_pm_child_exit = true;
    2225       45034 :     SetLatch(MyLatch);
    2226       45034 : }
    2227             : 
    2228             : /*
    2229             :  * Cleanup after a child process dies.
    2230             :  */
    2231             : static void
    2232       44952 : process_pm_child_exit(void)
    2233             : {
    2234             :     int         pid;            /* process id of dead child process */
    2235             :     int         exitstatus;     /* its exit status */
    2236             : 
    2237       44952 :     pending_pm_child_exit = false;
    2238             : 
    2239       44952 :     ereport(DEBUG4,
    2240             :             (errmsg_internal("reaping dead processes")));
    2241             : 
    2242       94108 :     while ((pid = waitpid(-1, &exitstatus, WNOHANG)) > 0)
    2243             :     {
    2244             :         PMChild    *pmchild;
    2245             : 
    2246             :         /*
    2247             :          * Check if this child was a startup process.
    2248             :          */
    2249       49158 :         if (StartupPMChild && pid == StartupPMChild->pid)
    2250             :         {
    2251        1708 :             ReleasePostmasterChildSlot(StartupPMChild);
    2252        1708 :             StartupPMChild = NULL;
    2253             : 
    2254             :             /*
    2255             :              * Startup process exited in response to a shutdown request (or it
    2256             :              * completed normally regardless of the shutdown request).
    2257             :              */
    2258        1708 :             if (Shutdown > NoShutdown &&
    2259         206 :                 (EXIT_STATUS_0(exitstatus) || EXIT_STATUS_1(exitstatus)))
    2260             :             {
    2261         116 :                 StartupStatus = STARTUP_NOT_RUNNING;
    2262         116 :                 UpdatePMState(PM_WAIT_BACKENDS);
    2263             :                 /* PostmasterStateMachine logic does the rest */
    2264         116 :                 continue;
    2265             :             }
    2266             : 
    2267        1592 :             if (EXIT_STATUS_3(exitstatus))
    2268             :             {
    2269           0 :                 ereport(LOG,
    2270             :                         (errmsg("shutdown at recovery target")));
    2271           0 :                 StartupStatus = STARTUP_NOT_RUNNING;
    2272           0 :                 Shutdown = Max(Shutdown, SmartShutdown);
    2273           0 :                 TerminateChildren(SIGTERM);
    2274           0 :                 UpdatePMState(PM_WAIT_BACKENDS);
    2275             :                 /* PostmasterStateMachine logic does the rest */
    2276           0 :                 continue;
    2277             :             }
    2278             : 
    2279             :             /*
    2280             :              * Unexpected exit of startup process (including FATAL exit)
    2281             :              * during PM_STARTUP is treated as catastrophic. There are no
    2282             :              * other processes running yet, so we can just exit.
    2283             :              */
    2284        1592 :             if (pmState == PM_STARTUP &&
    2285        1212 :                 StartupStatus != STARTUP_SIGNALED &&
    2286        1212 :                 !EXIT_STATUS_0(exitstatus))
    2287             :             {
    2288           2 :                 LogChildExit(LOG, _("startup process"),
    2289             :                              pid, exitstatus);
    2290           2 :                 ereport(LOG,
    2291             :                         (errmsg("aborting startup due to startup process failure")));
    2292           2 :                 ExitPostmaster(1);
    2293             :             }
    2294             : 
    2295             :             /*
    2296             :              * After PM_STARTUP, any unexpected exit (including FATAL exit) of
    2297             :              * the startup process is catastrophic, so kill other children,
    2298             :              * and set StartupStatus so we don't try to reinitialize after
    2299             :              * they're gone.  Exception: if StartupStatus is STARTUP_SIGNALED,
    2300             :              * then we previously sent the startup process a SIGQUIT; so
    2301             :              * that's probably the reason it died, and we do want to try to
    2302             :              * restart in that case.
    2303             :              *
    2304             :              * This stanza also handles the case where we sent a SIGQUIT
    2305             :              * during PM_STARTUP due to some dead-end child crashing: in that
    2306             :              * situation, if the startup process dies on the SIGQUIT, we need
    2307             :              * to transition to PM_WAIT_BACKENDS state which will allow
    2308             :              * PostmasterStateMachine to restart the startup process.  (On the
    2309             :              * other hand, the startup process might complete normally, if we
    2310             :              * were too late with the SIGQUIT.  In that case we'll fall
    2311             :              * through and commence normal operations.)
    2312             :              */
    2313        1590 :             if (!EXIT_STATUS_0(exitstatus))
    2314             :             {
    2315          96 :                 if (StartupStatus == STARTUP_SIGNALED)
    2316             :                 {
    2317          90 :                     StartupStatus = STARTUP_NOT_RUNNING;
    2318          90 :                     if (pmState == PM_STARTUP)
    2319           0 :                         UpdatePMState(PM_WAIT_BACKENDS);
    2320             :                 }
    2321             :                 else
    2322           6 :                     StartupStatus = STARTUP_CRASHED;
    2323          96 :                 HandleChildCrash(pid, exitstatus,
    2324          96 :                                  _("startup process"));
    2325          96 :                 continue;
    2326             :             }
    2327             : 
    2328             :             /*
    2329             :              * Startup succeeded, commence normal operations
    2330             :              */
    2331        1494 :             StartupStatus = STARTUP_NOT_RUNNING;
    2332        1494 :             FatalError = false;
    2333        1494 :             AbortStartTime = 0;
    2334        1494 :             ReachedNormalRunning = true;
    2335        1494 :             UpdatePMState(PM_RUN);
    2336        1494 :             connsAllowed = true;
    2337             : 
    2338             :             /*
    2339             :              * At the next iteration of the postmaster's main loop, we will
    2340             :              * crank up the background tasks like the autovacuum launcher and
    2341             :              * background workers that were not started earlier already.
    2342             :              */
    2343        1494 :             StartWorkerNeeded = true;
    2344             : 
    2345             :             /* at this point we are really open for business */
    2346        1494 :             ereport(LOG,
    2347             :                     (errmsg("database system is ready to accept connections")));
    2348             : 
    2349             :             /* Report status */
    2350        1494 :             AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_READY);
    2351             : #ifdef USE_SYSTEMD
    2352             :             sd_notify(0, "READY=1");
    2353             : #endif
    2354             : 
    2355        1494 :             continue;
    2356             :         }
    2357             : 
    2358             :         /*
    2359             :          * Was it the bgwriter?  Normal exit can be ignored; we'll start a new
    2360             :          * one at the next iteration of the postmaster's main loop, if
    2361             :          * necessary.  Any other exit condition is treated as a crash.
    2362             :          */
    2363       47450 :         if (BgWriterPMChild && pid == BgWriterPMChild->pid)
    2364             :         {
    2365        1706 :             ReleasePostmasterChildSlot(BgWriterPMChild);
    2366        1706 :             BgWriterPMChild = NULL;
    2367        1706 :             if (!EXIT_STATUS_0(exitstatus))
    2368         676 :                 HandleChildCrash(pid, exitstatus,
    2369         676 :                                  _("background writer process"));
    2370        1706 :             continue;
    2371             :         }
    2372             : 
    2373             :         /*
    2374             :          * Was it the checkpointer?
    2375             :          */
    2376       45744 :         if (CheckpointerPMChild && pid == CheckpointerPMChild->pid)
    2377             :         {
    2378        1706 :             ReleasePostmasterChildSlot(CheckpointerPMChild);
    2379        1706 :             CheckpointerPMChild = NULL;
    2380        1706 :             if (EXIT_STATUS_0(exitstatus) && pmState == PM_WAIT_CHECKPOINTER)
    2381        1030 :             {
    2382             :                 /*
    2383             :                  * OK, we saw normal exit of the checkpointer after it's been
    2384             :                  * told to shut down.  We know checkpointer wrote a shutdown
    2385             :                  * checkpoint, otherwise we'd still be in
    2386             :                  * PM_WAIT_XLOG_SHUTDOWN state.
    2387             :                  *
    2388             :                  * At this point only dead-end children and logger should be
    2389             :                  * left.
    2390             :                  */
    2391        1030 :                 UpdatePMState(PM_WAIT_DEAD_END);
    2392        1030 :                 ConfigurePostmasterWaitSet(false);
    2393        1030 :                 SignalChildren(SIGTERM, btmask_all_except(B_LOGGER));
    2394             :             }
    2395             :             else
    2396             :             {
    2397             :                 /*
    2398             :                  * Any unexpected exit of the checkpointer (including FATAL
    2399             :                  * exit) is treated as a crash.
    2400             :                  */
    2401         676 :                 HandleChildCrash(pid, exitstatus,
    2402         676 :                                  _("checkpointer process"));
    2403             :             }
    2404             : 
    2405        1706 :             continue;
    2406             :         }
    2407             : 
    2408             :         /*
    2409             :          * Was it the wal writer?  Normal exit can be ignored; we'll start a
    2410             :          * new one at the next iteration of the postmaster's main loop, if
    2411             :          * necessary.  Any other exit condition is treated as a crash.
    2412             :          */
    2413       44038 :         if (WalWriterPMChild && pid == WalWriterPMChild->pid)
    2414             :         {
    2415        1494 :             ReleasePostmasterChildSlot(WalWriterPMChild);
    2416        1494 :             WalWriterPMChild = NULL;
    2417        1494 :             if (!EXIT_STATUS_0(exitstatus))
    2418         580 :                 HandleChildCrash(pid, exitstatus,
    2419         580 :                                  _("WAL writer process"));
    2420        1494 :             continue;
    2421             :         }
    2422             : 
    2423             :         /*
    2424             :          * Was it the wal receiver?  If exit status is zero (normal) or one
    2425             :          * (FATAL exit), we assume everything is all right just like normal
    2426             :          * backends.  (If we need a new wal receiver, we'll start one at the
    2427             :          * next iteration of the postmaster's main loop.)
    2428             :          */
    2429       42544 :         if (WalReceiverPMChild && pid == WalReceiverPMChild->pid)
    2430             :         {
    2431         516 :             ReleasePostmasterChildSlot(WalReceiverPMChild);
    2432         516 :             WalReceiverPMChild = NULL;
    2433         516 :             if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
    2434          60 :                 HandleChildCrash(pid, exitstatus,
    2435          60 :                                  _("WAL receiver process"));
    2436         516 :             continue;
    2437             :         }
    2438             : 
    2439             :         /*
    2440             :          * Was it the wal summarizer? Normal exit can be ignored; we'll start
    2441             :          * a new one at the next iteration of the postmaster's main loop, if
    2442             :          * necessary.  Any other exit condition is treated as a crash.
    2443             :          */
    2444       42028 :         if (WalSummarizerPMChild && pid == WalSummarizerPMChild->pid)
    2445             :         {
    2446          38 :             ReleasePostmasterChildSlot(WalSummarizerPMChild);
    2447          38 :             WalSummarizerPMChild = NULL;
    2448          38 :             if (!EXIT_STATUS_0(exitstatus))
    2449          36 :                 HandleChildCrash(pid, exitstatus,
    2450          36 :                                  _("WAL summarizer process"));
    2451          38 :             continue;
    2452             :         }
    2453             : 
    2454             :         /*
    2455             :          * Was it the autovacuum launcher?  Normal exit can be ignored; we'll
    2456             :          * start a new one at the next iteration of the postmaster's main
    2457             :          * loop, if necessary.  Any other exit condition is treated as a
    2458             :          * crash.
    2459             :          */
    2460       41990 :         if (AutoVacLauncherPMChild && pid == AutoVacLauncherPMChild->pid)
    2461             :         {
    2462        1240 :             ReleasePostmasterChildSlot(AutoVacLauncherPMChild);
    2463        1240 :             AutoVacLauncherPMChild = NULL;
    2464        1240 :             if (!EXIT_STATUS_0(exitstatus))
    2465         490 :                 HandleChildCrash(pid, exitstatus,
    2466         490 :                                  _("autovacuum launcher process"));
    2467        1240 :             continue;
    2468             :         }
    2469             : 
    2470             :         /*
    2471             :          * Was it the archiver?  If exit status is zero (normal) or one (FATAL
    2472             :          * exit), we assume everything is all right just like normal backends
    2473             :          * and just try to start a new one on the next cycle of the
    2474             :          * postmaster's main loop, to retry archiving remaining files.
    2475             :          */
    2476       40750 :         if (PgArchPMChild && pid == PgArchPMChild->pid)
    2477             :         {
    2478         110 :             ReleasePostmasterChildSlot(PgArchPMChild);
    2479         110 :             PgArchPMChild = NULL;
    2480         110 :             if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
    2481          80 :                 HandleChildCrash(pid, exitstatus,
    2482          80 :                                  _("archiver process"));
    2483         110 :             continue;
    2484             :         }
    2485             : 
    2486             :         /* Was it the system logger?  If so, try to start a new one */
    2487       40640 :         if (SysLoggerPMChild && pid == SysLoggerPMChild->pid)
    2488             :         {
    2489           0 :             ReleasePostmasterChildSlot(SysLoggerPMChild);
    2490           0 :             SysLoggerPMChild = NULL;
    2491             : 
    2492             :             /* for safety's sake, launch new logger *first* */
    2493           0 :             if (Logging_collector)
    2494           0 :                 StartSysLogger();
    2495             : 
    2496           0 :             if (!EXIT_STATUS_0(exitstatus))
    2497           0 :                 LogChildExit(LOG, _("system logger process"),
    2498             :                              pid, exitstatus);
    2499           0 :             continue;
    2500             :         }
    2501             : 
    2502             :         /*
    2503             :          * Was it the slot sync worker? Normal exit or FATAL exit can be
    2504             :          * ignored (FATAL can be caused by libpqwalreceiver on receiving
    2505             :          * shutdown request by the startup process during promotion); we'll
    2506             :          * start a new one at the next iteration of the postmaster's main
    2507             :          * loop, if necessary. Any other exit condition is treated as a crash.
    2508             :          */
    2509       40640 :         if (SlotSyncWorkerPMChild && pid == SlotSyncWorkerPMChild->pid)
    2510             :         {
    2511           8 :             ReleasePostmasterChildSlot(SlotSyncWorkerPMChild);
    2512           8 :             SlotSyncWorkerPMChild = NULL;
    2513           8 :             if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
    2514           0 :                 HandleChildCrash(pid, exitstatus,
    2515           0 :                                  _("slot sync worker process"));
    2516           8 :             continue;
    2517             :         }
    2518             : 
    2519             :         /* Was it an IO worker? */
    2520       40632 :         if (maybe_reap_io_worker(pid))
    2521             :         {
    2522        5194 :             if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
    2523        2028 :                 HandleChildCrash(pid, exitstatus, _("io worker"));
    2524             : 
    2525        5194 :             maybe_adjust_io_workers();
    2526        5194 :             continue;
    2527             :         }
    2528             : 
    2529             :         /*
    2530             :          * Was it a backend or a background worker?
    2531             :          */
    2532       35438 :         pmchild = FindPostmasterChildByPid(pid);
    2533       35438 :         if (pmchild)
    2534             :         {
    2535       35438 :             CleanupBackend(pmchild, exitstatus);
    2536             :         }
    2537             : 
    2538             :         /*
    2539             :          * We don't know anything about this child process.  That's highly
    2540             :          * unexpected, as we do track all the child processes that we fork.
    2541             :          */
    2542             :         else
    2543             :         {
    2544           0 :             if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
    2545           0 :                 HandleChildCrash(pid, exitstatus, _("untracked child process"));
    2546             :             else
    2547           0 :                 LogChildExit(LOG, _("untracked child process"), pid, exitstatus);
    2548             :         }
    2549             :     }                           /* loop over pending child-death reports */
    2550             : 
    2551             :     /*
    2552             :      * After cleaning out the SIGCHLD queue, see if we have any state changes
    2553             :      * or actions to make.
    2554             :      */
    2555       44950 :     PostmasterStateMachine();
    2556       43254 : }
    2557             : 
    2558             : /*
    2559             :  * CleanupBackend -- cleanup after terminated backend or background worker.
    2560             :  *
    2561             :  * Remove all local state associated with the child process and release its
    2562             :  * PMChild slot.
    2563             :  */
    2564             : static void
    2565       35438 : CleanupBackend(PMChild *bp,
    2566             :                int exitstatus)  /* child's exit status. */
    2567             : {
    2568             :     char        namebuf[MAXPGPATH];
    2569             :     const char *procname;
    2570       35438 :     bool        crashed = false;
    2571       35438 :     bool        logged = false;
    2572             :     pid_t       bp_pid;
    2573             :     bool        bp_bgworker_notify;
    2574             :     BackendType bp_bkend_type;
    2575             :     RegisteredBgWorker *rw;
    2576             : 
    2577             :     /* Construct a process name for the log message */
    2578       35438 :     if (bp->bkend_type == B_BG_WORKER)
    2579             :     {
    2580        5128 :         snprintf(namebuf, MAXPGPATH, _("background worker \"%s\""),
    2581        5128 :                  bp->rw->rw_worker.bgw_type);
    2582        5128 :         procname = namebuf;
    2583             :     }
    2584             :     else
    2585       30310 :         procname = _(GetBackendTypeDesc(bp->bkend_type));
    2586             : 
    2587             :     /*
    2588             :      * If a backend dies in an ugly way then we must signal all other backends
    2589             :      * to quickdie.  If exit status is zero (normal) or one (FATAL exit), we
    2590             :      * assume everything is all right and proceed to remove the backend from
    2591             :      * the active child list.
    2592             :      */
    2593       35438 :     if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
    2594        1616 :         crashed = true;
    2595             : 
    2596             : #ifdef WIN32
    2597             : 
    2598             :     /*
    2599             :      * On win32, also treat ERROR_WAIT_NO_CHILDREN (128) as nonfatal case,
    2600             :      * since that sometimes happens under load when the process fails to start
    2601             :      * properly (long before it starts using shared memory). Microsoft reports
    2602             :      * it is related to mutex failure:
    2603             :      * http://archives.postgresql.org/pgsql-hackers/2010-09/msg00790.php
    2604             :      */
    2605             :     if (exitstatus == ERROR_WAIT_NO_CHILDREN)
    2606             :     {
    2607             :         LogChildExit(LOG, procname, bp->pid, exitstatus);
    2608             :         logged = true;
    2609             :         crashed = false;
    2610             :     }
    2611             : #endif
    2612             : 
    2613             :     /*
    2614             :      * Release the PMChild entry.
    2615             :      *
    2616             :      * If the process attached to shared memory, this also checks that it
    2617             :      * detached cleanly.
    2618             :      */
    2619       35438 :     bp_pid = bp->pid;
    2620       35438 :     bp_bgworker_notify = bp->bgworker_notify;
    2621       35438 :     bp_bkend_type = bp->bkend_type;
    2622       35438 :     rw = bp->rw;
    2623       35438 :     if (!ReleasePostmasterChildSlot(bp))
    2624             :     {
    2625             :         /*
    2626             :          * Uh-oh, the child failed to clean itself up.  Treat as a crash after
    2627             :          * all.
    2628             :          */
    2629         740 :         crashed = true;
    2630             :     }
    2631       35438 :     bp = NULL;
    2632             : 
    2633             :     /*
    2634             :      * In a crash case, exit immediately without resetting background worker
    2635             :      * state. However, if restart_after_crash is enabled, the background
    2636             :      * worker state (e.g., rw_pid) still needs be reset so the worker can
    2637             :      * restart after crash recovery. This reset is handled in
    2638             :      * ResetBackgroundWorkerCrashTimes(), not here.
    2639             :      */
    2640       35438 :     if (crashed)
    2641             :     {
    2642        1616 :         HandleChildCrash(bp_pid, exitstatus, procname);
    2643        1616 :         return;
    2644             :     }
    2645             : 
    2646             :     /*
    2647             :      * This backend may have been slated to receive SIGUSR1 when some
    2648             :      * background worker started or stopped.  Cancel those notifications, as
    2649             :      * we don't want to signal PIDs that are not PostgreSQL backends.  This
    2650             :      * gets skipped in the (probably very common) case where the backend has
    2651             :      * never requested any such notifications.
    2652             :      */
    2653       33822 :     if (bp_bgworker_notify)
    2654         482 :         BackgroundWorkerStopNotifications(bp_pid);
    2655             : 
    2656             :     /*
    2657             :      * If it was a background worker, also update its RegisteredBgWorker
    2658             :      * entry.
    2659             :      */
    2660       33822 :     if (bp_bkend_type == B_BG_WORKER)
    2661             :     {
    2662        4470 :         if (!EXIT_STATUS_0(exitstatus))
    2663             :         {
    2664             :             /* Record timestamp, so we know when to restart the worker. */
    2665        1260 :             rw->rw_crashed_at = GetCurrentTimestamp();
    2666             :         }
    2667             :         else
    2668             :         {
    2669             :             /* Zero exit status means terminate */
    2670        3210 :             rw->rw_crashed_at = 0;
    2671        3210 :             rw->rw_terminate = true;
    2672             :         }
    2673             : 
    2674        4470 :         rw->rw_pid = 0;
    2675        4470 :         ReportBackgroundWorkerExit(rw); /* report child death */
    2676             : 
    2677        4470 :         if (!logged)
    2678             :         {
    2679        4470 :             LogChildExit(EXIT_STATUS_0(exitstatus) ? DEBUG1 : LOG,
    2680             :                          procname, bp_pid, exitstatus);
    2681        4470 :             logged = true;
    2682             :         }
    2683             : 
    2684             :         /* have it be restarted */
    2685        4470 :         HaveCrashedWorker = true;
    2686             :     }
    2687             : 
    2688       33822 :     if (!logged)
    2689       29352 :         LogChildExit(DEBUG2, procname, bp_pid, exitstatus);
    2690             : }
    2691             : 
    2692             : /*
    2693             :  * Transition into FatalError state, in response to something bad having
    2694             :  * happened. Commonly the caller will have logged the reason for entering
    2695             :  * FatalError state.
    2696             :  *
    2697             :  * This should only be called when not already in FatalError or
    2698             :  * ImmediateShutdown state.
    2699             :  */
    2700             : static void
    2701          16 : HandleFatalError(QuitSignalReason reason, bool consider_sigabrt)
    2702             : {
    2703             :     int         sigtosend;
    2704             : 
    2705             :     Assert(!FatalError);
    2706             :     Assert(Shutdown != ImmediateShutdown);
    2707             : 
    2708          16 :     SetQuitSignalReason(reason);
    2709             : 
    2710          16 :     if (consider_sigabrt && send_abort_for_crash)
    2711           0 :         sigtosend = SIGABRT;
    2712             :     else
    2713          16 :         sigtosend = SIGQUIT;
    2714             : 
    2715             :     /*
    2716             :      * Signal all other child processes to exit.
    2717             :      *
    2718             :      * We could exclude dead-end children here, but at least when sending
    2719             :      * SIGABRT it seems better to include them.
    2720             :      */
    2721          16 :     TerminateChildren(sigtosend);
    2722             : 
    2723          16 :     FatalError = true;
    2724             : 
    2725             :     /*
    2726             :      * Choose the appropriate new state to react to the fatal error. Unless we
    2727             :      * were already in the process of shutting down, we go through
    2728             :      * PM_WAIT_BACKENDS. For errors during the shutdown sequence, we directly
    2729             :      * switch to PM_WAIT_DEAD_END.
    2730             :      */
    2731          16 :     switch (pmState)
    2732             :     {
    2733           0 :         case PM_INIT:
    2734             :             /* shouldn't have any children */
    2735             :             Assert(false);
    2736           0 :             break;
    2737           0 :         case PM_STARTUP:
    2738             :             /* should have been handled in process_pm_child_exit */
    2739             :             Assert(false);
    2740           0 :             break;
    2741             : 
    2742             :             /* wait for children to die */
    2743          16 :         case PM_RECOVERY:
    2744             :         case PM_HOT_STANDBY:
    2745             :         case PM_RUN:
    2746             :         case PM_STOP_BACKENDS:
    2747          16 :             UpdatePMState(PM_WAIT_BACKENDS);
    2748          16 :             break;
    2749             : 
    2750           0 :         case PM_WAIT_BACKENDS:
    2751             :             /* there might be more backends to wait for */
    2752           0 :             break;
    2753             : 
    2754           0 :         case PM_WAIT_XLOG_SHUTDOWN:
    2755             :         case PM_WAIT_XLOG_ARCHIVAL:
    2756             :         case PM_WAIT_CHECKPOINTER:
    2757             :         case PM_WAIT_IO_WORKERS:
    2758             : 
    2759             :             /*
    2760             :              * NB: Similar code exists in PostmasterStateMachine()'s handling
    2761             :              * of FatalError in PM_STOP_BACKENDS/PM_WAIT_BACKENDS states.
    2762             :              */
    2763           0 :             ConfigurePostmasterWaitSet(false);
    2764           0 :             UpdatePMState(PM_WAIT_DEAD_END);
    2765           0 :             break;
    2766             : 
    2767           0 :         case PM_WAIT_DEAD_END:
    2768             :         case PM_NO_CHILDREN:
    2769           0 :             break;
    2770             :     }
    2771             : 
    2772             :     /*
    2773             :      * .. and if this doesn't happen quickly enough, now the clock is ticking
    2774             :      * for us to kill them without mercy.
    2775             :      */
    2776          16 :     if (AbortStartTime == 0)
    2777          16 :         AbortStartTime = time(NULL);
    2778          16 : }
    2779             : 
    2780             : /*
    2781             :  * HandleChildCrash -- cleanup after failed backend, bgwriter, checkpointer,
    2782             :  * walwriter, autovacuum, archiver, slot sync worker, or background worker.
    2783             :  *
    2784             :  * The objectives here are to clean up our local state about the child
    2785             :  * process, and to signal all other remaining children to quickdie.
    2786             :  *
    2787             :  * The caller has already released its PMChild slot.
    2788             :  */
    2789             : static void
    2790        6338 : HandleChildCrash(int pid, int exitstatus, const char *procname)
    2791             : {
    2792             :     /*
    2793             :      * We only log messages and send signals if this is the first process
    2794             :      * crash and we're not doing an immediate shutdown; otherwise, we're only
    2795             :      * here to update postmaster's idea of live processes.  If we have already
    2796             :      * signaled children, nonzero exit status is to be expected, so don't
    2797             :      * clutter log.
    2798             :      */
    2799        6338 :     if (FatalError || Shutdown == ImmediateShutdown)
    2800        6322 :         return;
    2801             : 
    2802          16 :     LogChildExit(LOG, procname, pid, exitstatus);
    2803          16 :     ereport(LOG,
    2804             :             (errmsg("terminating any other active server processes")));
    2805             : 
    2806             :     /*
    2807             :      * Switch into error state. The crashed process has already been removed
    2808             :      * from ActiveChildList.
    2809             :      */
    2810          16 :     HandleFatalError(PMQUIT_FOR_CRASH, true);
    2811             : }
    2812             : 
    2813             : /*
    2814             :  * Log the death of a child process.
    2815             :  */
    2816             : static void
    2817       33840 : LogChildExit(int lev, const char *procname, int pid, int exitstatus)
    2818             : {
    2819             :     /*
    2820             :      * size of activity_buffer is arbitrary, but set equal to default
    2821             :      * track_activity_query_size
    2822             :      */
    2823             :     char        activity_buffer[1024];
    2824       33840 :     const char *activity = NULL;
    2825             : 
    2826       33840 :     if (!EXIT_STATUS_0(exitstatus))
    2827        2302 :         activity = pgstat_get_crashed_backend_activity(pid,
    2828             :                                                        activity_buffer,
    2829             :                                                        sizeof(activity_buffer));
    2830             : 
    2831       33840 :     if (WIFEXITED(exitstatus))
    2832       33832 :         ereport(lev,
    2833             : 
    2834             :         /*------
    2835             :           translator: %s is a noun phrase describing a child process, such as
    2836             :           "server process" */
    2837             :                 (errmsg("%s (PID %d) exited with exit code %d",
    2838             :                         procname, pid, WEXITSTATUS(exitstatus)),
    2839             :                  activity ? errdetail("Failed process was running: %s", activity) : 0));
    2840           8 :     else if (WIFSIGNALED(exitstatus))
    2841             :     {
    2842             : #if defined(WIN32)
    2843             :         ereport(lev,
    2844             : 
    2845             :         /*------
    2846             :           translator: %s is a noun phrase describing a child process, such as
    2847             :           "server process" */
    2848             :                 (errmsg("%s (PID %d) was terminated by exception 0x%X",
    2849             :                         procname, pid, WTERMSIG(exitstatus)),
    2850             :                  errhint("See C include file \"ntstatus.h\" for a description of the hexadecimal value."),
    2851             :                  activity ? errdetail("Failed process was running: %s", activity) : 0));
    2852             : #else
    2853           8 :         ereport(lev,
    2854             : 
    2855             :         /*------
    2856             :           translator: %s is a noun phrase describing a child process, such as
    2857             :           "server process" */
    2858             :                 (errmsg("%s (PID %d) was terminated by signal %d: %s",
    2859             :                         procname, pid, WTERMSIG(exitstatus),
    2860             :                         pg_strsignal(WTERMSIG(exitstatus))),
    2861             :                  activity ? errdetail("Failed process was running: %s", activity) : 0));
    2862             : #endif
    2863             :     }
    2864             :     else
    2865           0 :         ereport(lev,
    2866             : 
    2867             :         /*------
    2868             :           translator: %s is a noun phrase describing a child process, such as
    2869             :           "server process" */
    2870             :                 (errmsg("%s (PID %d) exited with unrecognized status %d",
    2871             :                         procname, pid, exitstatus),
    2872             :                  activity ? errdetail("Failed process was running: %s", activity) : 0));
    2873       33840 : }
    2874             : 
    2875             : /*
    2876             :  * Advance the postmaster's state machine and take actions as appropriate
    2877             :  *
    2878             :  * This is common code for process_pm_shutdown_request(),
    2879             :  * process_pm_child_exit() and process_pm_pmsignal(), which process the signals
    2880             :  * that might mean we need to change state.
    2881             :  */
    2882             : static void
    2883       50070 : PostmasterStateMachine(void)
    2884             : {
    2885             :     /* If we're doing a smart shutdown, try to advance that state. */
    2886       50070 :     if (pmState == PM_RUN || pmState == PM_HOT_STANDBY)
    2887             :     {
    2888       35574 :         if (!connsAllowed)
    2889             :         {
    2890             :             /*
    2891             :              * This state ends when we have no normal client backends running.
    2892             :              * Then we're ready to stop other children.
    2893             :              */
    2894         226 :             if (CountChildren(btmask(B_BACKEND)) == 0)
    2895          80 :                 UpdatePMState(PM_STOP_BACKENDS);
    2896             :         }
    2897             :     }
    2898             : 
    2899             :     /*
    2900             :      * In the PM_WAIT_BACKENDS state, wait for all the regular backends and
    2901             :      * processes like autovacuum and background workers that are comparable to
    2902             :      * backends to exit.
    2903             :      *
    2904             :      * PM_STOP_BACKENDS is a transient state that means the same as
    2905             :      * PM_WAIT_BACKENDS, but we signal the processes first, before waiting for
    2906             :      * them.  Treating it as a distinct pmState allows us to share this code
    2907             :      * across multiple shutdown code paths.
    2908             :      */
    2909       50070 :     if (pmState == PM_STOP_BACKENDS || pmState == PM_WAIT_BACKENDS)
    2910             :     {
    2911        9136 :         BackendTypeMask targetMask = BTYPE_MASK_NONE;
    2912             : 
    2913             :         /*
    2914             :          * PM_WAIT_BACKENDS state ends when we have no regular backends, no
    2915             :          * autovac launcher or workers, and no bgworkers (including
    2916             :          * unconnected ones).
    2917             :          */
    2918        9136 :         targetMask = btmask_add(targetMask,
    2919             :                                 B_BACKEND,
    2920             :                                 B_AUTOVAC_LAUNCHER,
    2921             :                                 B_AUTOVAC_WORKER,
    2922             :                                 B_BG_WORKER);
    2923             : 
    2924             :         /*
    2925             :          * No walwriter, bgwriter, slot sync worker, or WAL summarizer either.
    2926             :          */
    2927        9136 :         targetMask = btmask_add(targetMask,
    2928             :                                 B_WAL_WRITER,
    2929             :                                 B_BG_WRITER,
    2930             :                                 B_SLOTSYNC_WORKER,
    2931             :                                 B_WAL_SUMMARIZER);
    2932             : 
    2933             :         /* If we're in recovery, also stop startup and walreceiver procs */
    2934        9136 :         targetMask = btmask_add(targetMask,
    2935             :                                 B_STARTUP,
    2936             :                                 B_WAL_RECEIVER);
    2937             : 
    2938             :         /*
    2939             :          * If we are doing crash recovery or an immediate shutdown then we
    2940             :          * expect archiver, checkpointer, io workers and walsender to exit as
    2941             :          * well, otherwise not.
    2942             :          */
    2943        9136 :         if (FatalError || Shutdown >= ImmediateShutdown)
    2944        3054 :             targetMask = btmask_add(targetMask,
    2945             :                                     B_CHECKPOINTER,
    2946             :                                     B_ARCHIVER,
    2947             :                                     B_IO_WORKER,
    2948             :                                     B_WAL_SENDER);
    2949             : 
    2950             :         /*
    2951             :          * Normally archiver, checkpointer, IO workers and walsenders will
    2952             :          * continue running; they will be terminated later after writing the
    2953             :          * checkpoint record.  We also let dead-end children to keep running
    2954             :          * for now.  The syslogger process exits last.
    2955             :          *
    2956             :          * This assertion checks that we have covered all backend types,
    2957             :          * either by including them in targetMask, or by noting here that they
    2958             :          * are allowed to continue running.
    2959             :          */
    2960             : #ifdef USE_ASSERT_CHECKING
    2961             :         {
    2962             :             BackendTypeMask remainMask = BTYPE_MASK_NONE;
    2963             : 
    2964             :             remainMask = btmask_add(remainMask,
    2965             :                                     B_DEAD_END_BACKEND,
    2966             :                                     B_LOGGER);
    2967             : 
    2968             :             /*
    2969             :              * Archiver, checkpointer, IO workers, and walsender may or may
    2970             :              * not be in targetMask already.
    2971             :              */
    2972             :             remainMask = btmask_add(remainMask,
    2973             :                                     B_ARCHIVER,
    2974             :                                     B_CHECKPOINTER,
    2975             :                                     B_IO_WORKER,
    2976             :                                     B_WAL_SENDER);
    2977             : 
    2978             :             /* these are not real postmaster children */
    2979             :             remainMask = btmask_add(remainMask,
    2980             :                                     B_INVALID,
    2981             :                                     B_STANDALONE_BACKEND);
    2982             : 
    2983             :             /* All types should be included in targetMask or remainMask */
    2984             :             Assert((remainMask.mask | targetMask.mask) == BTYPE_MASK_ALL.mask);
    2985             :         }
    2986             : #endif
    2987             : 
    2988             :         /* If we had not yet signaled the processes to exit, do so now */
    2989        9136 :         if (pmState == PM_STOP_BACKENDS)
    2990             :         {
    2991             :             /*
    2992             :              * Forget any pending requests for background workers, since we're
    2993             :              * no longer willing to launch any new workers.  (If additional
    2994             :              * requests arrive, BackgroundWorkerStateChange will reject them.)
    2995             :              */
    2996        1030 :             ForgetUnstartedBackgroundWorkers();
    2997             : 
    2998        1030 :             SignalChildren(SIGTERM, targetMask);
    2999             : 
    3000        1030 :             UpdatePMState(PM_WAIT_BACKENDS);
    3001             :         }
    3002             : 
    3003             :         /* Are any of the target processes still running? */
    3004        9136 :         if (CountChildren(targetMask) == 0)
    3005             :         {
    3006        1706 :             if (Shutdown >= ImmediateShutdown || FatalError)
    3007             :             {
    3008             :                 /*
    3009             :                  * Stop any dead-end children and stop creating new ones.
    3010             :                  *
    3011             :                  * NB: Similar code exists in HandleFatalError(), when the
    3012             :                  * error happens in pmState > PM_WAIT_BACKENDS.
    3013             :                  */
    3014         676 :                 UpdatePMState(PM_WAIT_DEAD_END);
    3015         676 :                 ConfigurePostmasterWaitSet(false);
    3016         676 :                 SignalChildren(SIGQUIT, btmask(B_DEAD_END_BACKEND));
    3017             : 
    3018             :                 /*
    3019             :                  * We already SIGQUIT'd auxiliary processes (other than
    3020             :                  * logger), if any, when we started immediate shutdown or
    3021             :                  * entered FatalError state.
    3022             :                  */
    3023             :             }
    3024             :             else
    3025             :             {
    3026             :                 /*
    3027             :                  * If we get here, we are proceeding with normal shutdown. All
    3028             :                  * the regular children are gone, and it's time to tell the
    3029             :                  * checkpointer to do a shutdown checkpoint.
    3030             :                  */
    3031             :                 Assert(Shutdown > NoShutdown);
    3032             :                 /* Start the checkpointer if not running */
    3033        1030 :                 if (CheckpointerPMChild == NULL)
    3034           0 :                     CheckpointerPMChild = StartChildProcess(B_CHECKPOINTER);
    3035             :                 /* And tell it to write the shutdown checkpoint */
    3036        1030 :                 if (CheckpointerPMChild != NULL)
    3037             :                 {
    3038        1030 :                     signal_child(CheckpointerPMChild, SIGINT);
    3039        1030 :                     UpdatePMState(PM_WAIT_XLOG_SHUTDOWN);
    3040             :                 }
    3041             :                 else
    3042             :                 {
    3043             :                     /*
    3044             :                      * If we failed to fork a checkpointer, just shut down.
    3045             :                      * Any required cleanup will happen at next restart. We
    3046             :                      * set FatalError so that an "abnormal shutdown" message
    3047             :                      * gets logged when we exit.
    3048             :                      *
    3049             :                      * We don't consult send_abort_for_crash here, as it's
    3050             :                      * unlikely that dumping cores would illuminate the reason
    3051             :                      * for checkpointer fork failure.
    3052             :                      *
    3053             :                      * XXX: It may be worth to introduce a different PMQUIT
    3054             :                      * value that signals that the cluster is in a bad state,
    3055             :                      * without a process having crashed. But right now this
    3056             :                      * path is very unlikely to be reached, so it isn't
    3057             :                      * obviously worthwhile adding a distinct error message in
    3058             :                      * quickdie().
    3059             :                      */
    3060           0 :                     HandleFatalError(PMQUIT_FOR_CRASH, false);
    3061             :                 }
    3062             :             }
    3063             :         }
    3064             :     }
    3065             : 
    3066             :     /*
    3067             :      * The state transition from PM_WAIT_XLOG_SHUTDOWN to
    3068             :      * PM_WAIT_XLOG_ARCHIVAL is in process_pm_pmsignal(), in response to
    3069             :      * PMSIGNAL_XLOG_IS_SHUTDOWN.
    3070             :      */
    3071             : 
    3072       50070 :     if (pmState == PM_WAIT_XLOG_ARCHIVAL)
    3073             :     {
    3074             :         /*
    3075             :          * PM_WAIT_XLOG_ARCHIVAL state ends when there are no children other
    3076             :          * than checkpointer, io workers and dead-end children left. There
    3077             :          * shouldn't be any regular backends left by now anyway; what we're
    3078             :          * really waiting for is for walsenders and archiver to exit.
    3079             :          */
    3080        1132 :         if (CountChildren(btmask_all_except(B_CHECKPOINTER, B_IO_WORKER,
    3081             :                                             B_LOGGER, B_DEAD_END_BACKEND)) == 0)
    3082             :         {
    3083        1030 :             UpdatePMState(PM_WAIT_IO_WORKERS);
    3084        1030 :             SignalChildren(SIGUSR2, btmask(B_IO_WORKER));
    3085             :         }
    3086             :     }
    3087             : 
    3088       50070 :     if (pmState == PM_WAIT_IO_WORKERS)
    3089             :     {
    3090             :         /*
    3091             :          * PM_WAIT_IO_WORKERS state ends when there's only checkpointer and
    3092             :          * dead-end children left.
    3093             :          */
    3094        4118 :         if (io_worker_count == 0)
    3095             :         {
    3096        1030 :             UpdatePMState(PM_WAIT_CHECKPOINTER);
    3097             : 
    3098             :             /*
    3099             :              * Now that the processes mentioned above are gone, tell
    3100             :              * checkpointer to shut down too. That allows checkpointer to
    3101             :              * perform some last bits of cleanup without other processes
    3102             :              * interfering.
    3103             :              */
    3104        1030 :             if (CheckpointerPMChild != NULL)
    3105        1030 :                 signal_child(CheckpointerPMChild, SIGUSR2);
    3106             :         }
    3107             :     }
    3108             : 
    3109             :     /*
    3110             :      * The state transition from PM_WAIT_CHECKPOINTER to PM_WAIT_DEAD_END is
    3111             :      * in process_pm_child_exit().
    3112             :      */
    3113             : 
    3114       50070 :     if (pmState == PM_WAIT_DEAD_END)
    3115             :     {
    3116             :         /*
    3117             :          * PM_WAIT_DEAD_END state ends when all other children are gone except
    3118             :          * for the logger.  During normal shutdown, all that remains are
    3119             :          * dead-end backends, but in FatalError processing we jump straight
    3120             :          * here with more processes remaining.  Note that they have already
    3121             :          * been sent appropriate shutdown signals, either during a normal
    3122             :          * state transition leading up to PM_WAIT_DEAD_END, or during
    3123             :          * FatalError processing.
    3124             :          *
    3125             :          * The reason we wait is to protect against a new postmaster starting
    3126             :          * conflicting subprocesses; this isn't an ironclad protection, but it
    3127             :          * at least helps in the shutdown-and-immediately-restart scenario.
    3128             :          */
    3129        1764 :         if (CountChildren(btmask_all_except(B_LOGGER)) == 0)
    3130             :         {
    3131             :             /* These other guys should be dead already */
    3132             :             Assert(StartupPMChild == NULL);
    3133             :             Assert(WalReceiverPMChild == NULL);
    3134             :             Assert(WalSummarizerPMChild == NULL);
    3135             :             Assert(BgWriterPMChild == NULL);
    3136             :             Assert(CheckpointerPMChild == NULL);
    3137             :             Assert(WalWriterPMChild == NULL);
    3138             :             Assert(AutoVacLauncherPMChild == NULL);
    3139             :             Assert(SlotSyncWorkerPMChild == NULL);
    3140             :             /* syslogger is not considered here */
    3141        1706 :             UpdatePMState(PM_NO_CHILDREN);
    3142             :         }
    3143             :     }
    3144             : 
    3145             :     /*
    3146             :      * If we've been told to shut down, we exit as soon as there are no
    3147             :      * remaining children.  If there was a crash, cleanup will occur at the
    3148             :      * next startup.  (Before PostgreSQL 8.3, we tried to recover from the
    3149             :      * crash before exiting, but that seems unwise if we are quitting because
    3150             :      * we got SIGTERM from init --- there may well not be time for recovery
    3151             :      * before init decides to SIGKILL us.)
    3152             :      *
    3153             :      * Note that the syslogger continues to run.  It will exit when it sees
    3154             :      * EOF on its input pipe, which happens when there are no more upstream
    3155             :      * processes.
    3156             :      */
    3157       50070 :     if (Shutdown > NoShutdown && pmState == PM_NO_CHILDREN)
    3158             :     {
    3159        1690 :         if (FatalError)
    3160             :         {
    3161           0 :             ereport(LOG, (errmsg("abnormal database system shutdown")));
    3162           0 :             ExitPostmaster(1);
    3163             :         }
    3164             :         else
    3165             :         {
    3166             :             /*
    3167             :              * Normal exit from the postmaster is here.  We don't need to log
    3168             :              * anything here, since the UnlinkLockFiles proc_exit callback
    3169             :              * will do so, and that should be the last user-visible action.
    3170             :              */
    3171        1690 :             ExitPostmaster(0);
    3172             :         }
    3173             :     }
    3174             : 
    3175             :     /*
    3176             :      * If the startup process failed, or the user does not want an automatic
    3177             :      * restart after backend crashes, wait for all non-syslogger children to
    3178             :      * exit, and then exit postmaster.  We don't try to reinitialize when the
    3179             :      * startup process fails, because more than likely it will just fail again
    3180             :      * and we will keep trying forever.
    3181             :      */
    3182       48380 :     if (pmState == PM_NO_CHILDREN)
    3183             :     {
    3184          16 :         if (StartupStatus == STARTUP_CRASHED)
    3185             :         {
    3186           6 :             ereport(LOG,
    3187             :                     (errmsg("shutting down due to startup process failure")));
    3188           6 :             ExitPostmaster(1);
    3189             :         }
    3190          10 :         if (!restart_after_crash)
    3191             :         {
    3192           0 :             ereport(LOG,
    3193             :                     (errmsg("shutting down because \"restart_after_crash\" is off")));
    3194           0 :             ExitPostmaster(1);
    3195             :         }
    3196             :     }
    3197             : 
    3198             :     /*
    3199             :      * If we need to recover from a crash, wait for all non-syslogger children
    3200             :      * to exit, then reset shmem and start the startup process.
    3201             :      */
    3202       48374 :     if (FatalError && pmState == PM_NO_CHILDREN)
    3203             :     {
    3204          10 :         ereport(LOG,
    3205             :                 (errmsg("all server processes terminated; reinitializing")));
    3206             : 
    3207             :         /* remove leftover temporary files after a crash */
    3208          10 :         if (remove_temp_files_after_crash)
    3209           8 :             RemovePgTempFiles();
    3210             : 
    3211             :         /* allow background workers to immediately restart */
    3212          10 :         ResetBackgroundWorkerCrashTimes();
    3213             : 
    3214          10 :         shmem_exit(1);
    3215             : 
    3216             :         /* re-read control file into local memory */
    3217          10 :         LocalProcessControlFile(true);
    3218             : 
    3219             :         /* re-create shared memory and semaphores */
    3220          10 :         CreateSharedMemoryAndSemaphores();
    3221             : 
    3222          10 :         UpdatePMState(PM_STARTUP);
    3223             : 
    3224             :         /* Make sure we can perform I/O while starting up. */
    3225          10 :         maybe_adjust_io_workers();
    3226             : 
    3227          10 :         StartupPMChild = StartChildProcess(B_STARTUP);
    3228             :         Assert(StartupPMChild != NULL);
    3229          10 :         StartupStatus = STARTUP_RUNNING;
    3230             :         /* crash recovery started, reset SIGKILL flag */
    3231          10 :         AbortStartTime = 0;
    3232             : 
    3233             :         /* start accepting server socket connection events again */
    3234          10 :         ConfigurePostmasterWaitSet(true);
    3235             :     }
    3236       48374 : }
    3237             : 
    3238             : static const char *
    3239        1960 : pmstate_name(PMState state)
    3240             : {
    3241             : #define PM_TOSTR_CASE(sym) case sym: return #sym
    3242        1960 :     switch (state)
    3243             :     {
    3244         110 :             PM_TOSTR_CASE(PM_INIT);
    3245         220 :             PM_TOSTR_CASE(PM_STARTUP);
    3246          32 :             PM_TOSTR_CASE(PM_RECOVERY);
    3247          24 :             PM_TOSTR_CASE(PM_HOT_STANDBY);
    3248         206 :             PM_TOSTR_CASE(PM_RUN);
    3249         160 :             PM_TOSTR_CASE(PM_STOP_BACKENDS);
    3250         232 :             PM_TOSTR_CASE(PM_WAIT_BACKENDS);
    3251         160 :             PM_TOSTR_CASE(PM_WAIT_XLOG_SHUTDOWN);
    3252         160 :             PM_TOSTR_CASE(PM_WAIT_XLOG_ARCHIVAL);
    3253         160 :             PM_TOSTR_CASE(PM_WAIT_IO_WORKERS);
    3254         224 :             PM_TOSTR_CASE(PM_WAIT_DEAD_END);
    3255         160 :             PM_TOSTR_CASE(PM_WAIT_CHECKPOINTER);
    3256         112 :             PM_TOSTR_CASE(PM_NO_CHILDREN);
    3257             :     }
    3258             : #undef PM_TOSTR_CASE
    3259             : 
    3260           0 :     pg_unreachable();
    3261             :     return "";                    /* silence compiler */
    3262             : }
    3263             : 
    3264             : /*
    3265             :  * Simple wrapper for updating pmState. The main reason to have this wrapper
    3266             :  * is that it makes it easy to log all state transitions.
    3267             :  */
    3268             : static void
    3269       14382 : UpdatePMState(PMState newState)
    3270             : {
    3271       14382 :     elog(DEBUG1, "updating PMState from %s to %s",
    3272             :          pmstate_name(pmState), pmstate_name(newState));
    3273       14382 :     pmState = newState;
    3274       14382 : }
    3275             : 
    3276             : /*
    3277             :  * Launch background processes after state change, or relaunch after an
    3278             :  * existing process has exited.
    3279             :  *
    3280             :  * Check the current pmState and the status of any background processes.  If
    3281             :  * there are any background processes missing that should be running in the
    3282             :  * current state, but are not, launch them.
    3283             :  */
    3284             : static void
    3285      268080 : LaunchMissingBackgroundProcesses(void)
    3286             : {
    3287             :     /* Syslogger is active in all states */
    3288      268080 :     if (SysLoggerPMChild == NULL && Logging_collector)
    3289           0 :         StartSysLogger();
    3290             : 
    3291             :     /*
    3292             :      * The number of configured workers might have changed, or a prior start
    3293             :      * of a worker might have failed. Check if we need to start/stop any
    3294             :      * workers.
    3295             :      *
    3296             :      * A config file change will always lead to this function being called, so
    3297             :      * we always will process the config change in a timely manner.
    3298             :      */
    3299      268080 :     maybe_adjust_io_workers();
    3300             : 
    3301             :     /*
    3302             :      * The checkpointer and the background writer are active from the start,
    3303             :      * until shutdown is initiated.
    3304             :      *
    3305             :      * (If the checkpointer is not running when we enter the
    3306             :      * PM_WAIT_XLOG_SHUTDOWN state, it is launched one more time to perform
    3307             :      * the shutdown checkpoint.  That's done in PostmasterStateMachine(), not
    3308             :      * here.)
    3309             :      */
    3310      268080 :     if (pmState == PM_RUN || pmState == PM_RECOVERY ||
    3311       17112 :         pmState == PM_HOT_STANDBY || pmState == PM_STARTUP)
    3312             :     {
    3313      255196 :         if (CheckpointerPMChild == NULL)
    3314          10 :             CheckpointerPMChild = StartChildProcess(B_CHECKPOINTER);
    3315      255196 :         if (BgWriterPMChild == NULL)
    3316          10 :             BgWriterPMChild = StartChildProcess(B_BG_WRITER);
    3317             :     }
    3318             : 
    3319             :     /*
    3320             :      * WAL writer is needed only in normal operation (else we cannot be
    3321             :      * writing any new WAL).
    3322             :      */
    3323      268080 :     if (WalWriterPMChild == NULL && pmState == PM_RUN)
    3324        1494 :         WalWriterPMChild = StartChildProcess(B_WAL_WRITER);
    3325             : 
    3326             :     /*
    3327             :      * We don't want autovacuum to run in binary upgrade mode because
    3328             :      * autovacuum might update relfrozenxid for empty tables before the
    3329             :      * physical files are put in place.
    3330             :      */
    3331      287666 :     if (!IsBinaryUpgrade && AutoVacLauncherPMChild == NULL &&
    3332       27512 :         (AutoVacuumingActive() || start_autovac_launcher) &&
    3333       11660 :         pmState == PM_RUN)
    3334             :     {
    3335        1240 :         AutoVacLauncherPMChild = StartChildProcess(B_AUTOVAC_LAUNCHER);
    3336        1240 :         if (AutoVacLauncherPMChild != NULL)
    3337        1240 :             start_autovac_launcher = false; /* signal processed */
    3338             :     }
    3339             : 
    3340             :     /*
    3341             :      * If WAL archiving is enabled always, we are allowed to start archiver
    3342             :      * even during recovery.
    3343             :      */
    3344      268080 :     if (PgArchPMChild == NULL &&
    3345      265966 :         ((XLogArchivingActive() && pmState == PM_RUN) ||
    3346      265966 :          (XLogArchivingAlways() && (pmState == PM_RECOVERY || pmState == PM_HOT_STANDBY))) &&
    3347         104 :         PgArchCanRestart())
    3348         104 :         PgArchPMChild = StartChildProcess(B_ARCHIVER);
    3349             : 
    3350             :     /*
    3351             :      * If we need to start a slot sync worker, try to do that now
    3352             :      *
    3353             :      * We allow to start the slot sync worker when we are on a hot standby,
    3354             :      * fast or immediate shutdown is not in progress, slot sync parameters are
    3355             :      * configured correctly, and it is the first time of worker's launch, or
    3356             :      * enough time has passed since the worker was launched last.
    3357             :      */
    3358      268080 :     if (SlotSyncWorkerPMChild == NULL && pmState == PM_HOT_STANDBY &&
    3359        3526 :         Shutdown <= SmartShutdown && sync_replication_slots &&
    3360          26 :         ValidateSlotSyncParams(LOG) && SlotSyncWorkerCanRestart())
    3361           8 :         SlotSyncWorkerPMChild = StartChildProcess(B_SLOTSYNC_WORKER);
    3362             : 
    3363             :     /*
    3364             :      * If we need to start a WAL receiver, try to do that now
    3365             :      *
    3366             :      * Note: if a walreceiver process is already running, it might seem that
    3367             :      * we should clear WalReceiverRequested.  However, there's a race
    3368             :      * condition if the walreceiver terminates and the startup process
    3369             :      * immediately requests a new one: it's quite possible to get the signal
    3370             :      * for the request before reaping the dead walreceiver process.  Better to
    3371             :      * risk launching an extra walreceiver than to miss launching one we need.
    3372             :      * (The walreceiver code has logic to recognize that it should go away if
    3373             :      * not needed.)
    3374             :      */
    3375      268080 :     if (WalReceiverRequested)
    3376             :     {
    3377         912 :         if (WalReceiverPMChild == NULL &&
    3378         560 :             (pmState == PM_STARTUP || pmState == PM_RECOVERY ||
    3379         558 :              pmState == PM_HOT_STANDBY) &&
    3380         516 :             Shutdown <= SmartShutdown)
    3381             :         {
    3382         516 :             WalReceiverPMChild = StartChildProcess(B_WAL_RECEIVER);
    3383         516 :             if (WalReceiverPMChild != 0)
    3384         516 :                 WalReceiverRequested = false;
    3385             :             /* else leave the flag set, so we'll try again later */
    3386             :         }
    3387             :     }
    3388             : 
    3389             :     /* If we need to start a WAL summarizer, try to do that now */
    3390      268080 :     if (summarize_wal && WalSummarizerPMChild == NULL &&
    3391         138 :         (pmState == PM_RUN || pmState == PM_HOT_STANDBY) &&
    3392          38 :         Shutdown <= SmartShutdown)
    3393          38 :         WalSummarizerPMChild = StartChildProcess(B_WAL_SUMMARIZER);
    3394             : 
    3395             :     /* Get other worker processes running, if needed */
    3396      268080 :     if (StartWorkerNeeded || HaveCrashedWorker)
    3397       13532 :         maybe_start_bgworkers();
    3398      268080 : }
    3399             : 
    3400             : /*
    3401             :  * Return string representation of signal.
    3402             :  *
    3403             :  * Because this is only implemented for signals we already rely on in this
    3404             :  * file we don't need to deal with unimplemented or same-numeric-value signals
    3405             :  * (as we'd e.g. have to for EWOULDBLOCK / EAGAIN).
    3406             :  */
    3407             : static const char *
    3408          36 : pm_signame(int signal)
    3409             : {
    3410             : #define PM_TOSTR_CASE(sym) case sym: return #sym
    3411          36 :     switch (signal)
    3412             :     {
    3413           0 :             PM_TOSTR_CASE(SIGABRT);
    3414           0 :             PM_TOSTR_CASE(SIGCHLD);
    3415           0 :             PM_TOSTR_CASE(SIGHUP);
    3416           4 :             PM_TOSTR_CASE(SIGINT);
    3417           0 :             PM_TOSTR_CASE(SIGKILL);
    3418           0 :             PM_TOSTR_CASE(SIGQUIT);
    3419          22 :             PM_TOSTR_CASE(SIGTERM);
    3420           0 :             PM_TOSTR_CASE(SIGUSR1);
    3421          10 :             PM_TOSTR_CASE(SIGUSR2);
    3422           0 :         default:
    3423             :             /* all signals sent by postmaster should be listed here */
    3424             :             Assert(false);
    3425           0 :             return "(unknown)";
    3426             :     }
    3427             : #undef PM_TOSTR_CASE
    3428             : 
    3429             :     return "";                    /* silence compiler */
    3430             : }
    3431             : 
    3432             : /*
    3433             :  * Send a signal to a postmaster child process
    3434             :  *
    3435             :  * On systems that have setsid(), each child process sets itself up as a
    3436             :  * process group leader.  For signals that are generally interpreted in the
    3437             :  * appropriate fashion, we signal the entire process group not just the
    3438             :  * direct child process.  This allows us to, for example, SIGQUIT a blocked
    3439             :  * archive_recovery script, or SIGINT a script being run by a backend via
    3440             :  * system().
    3441             :  *
    3442             :  * There is a race condition for recently-forked children: they might not
    3443             :  * have executed setsid() yet.  So we signal the child directly as well as
    3444             :  * the group.  We assume such a child will handle the signal before trying
    3445             :  * to spawn any grandchild processes.  We also assume that signaling the
    3446             :  * child twice will not cause any problems.
    3447             :  */
    3448             : static void
    3449       19568 : signal_child(PMChild *pmchild, int signal)
    3450             : {
    3451       19568 :     pid_t       pid = pmchild->pid;
    3452             : 
    3453       19568 :     ereport(DEBUG3,
    3454             :             (errmsg_internal("sending signal %d/%s to %s process with pid %d",
    3455             :                              signal, pm_signame(signal),
    3456             :                              GetBackendTypeDesc(pmchild->bkend_type),
    3457             :                              (int) pmchild->pid)));
    3458             : 
    3459       19568 :     if (kill(pid, signal) < 0)
    3460           0 :         elog(DEBUG3, "kill(%ld,%d) failed: %m", (long) pid, signal);
    3461             : #ifdef HAVE_SETSID
    3462       19568 :     switch (signal)
    3463             :     {
    3464       12564 :         case SIGINT:
    3465             :         case SIGTERM:
    3466             :         case SIGQUIT:
    3467             :         case SIGKILL:
    3468             :         case SIGABRT:
    3469       12564 :             if (kill(-pid, signal) < 0)
    3470          12 :                 elog(DEBUG3, "kill(%ld,%d) failed: %m", (long) (-pid), signal);
    3471       12564 :             break;
    3472        7004 :         default:
    3473        7004 :             break;
    3474             :     }
    3475             : #endif
    3476       19568 : }
    3477             : 
    3478             : /*
    3479             :  * Send a signal to the targeted children.
    3480             :  */
    3481             : static bool
    3482        5742 : SignalChildren(int signal, BackendTypeMask targetMask)
    3483             : {
    3484             :     dlist_iter  iter;
    3485        5742 :     bool        signaled = false;
    3486             : 
    3487       32656 :     dlist_foreach(iter, &ActiveChildList)
    3488             :     {
    3489       26914 :         PMChild    *bp = dlist_container(PMChild, elem, iter.cur);
    3490             : 
    3491             :         /*
    3492             :          * If we need to distinguish between B_BACKEND and B_WAL_SENDER, check
    3493             :          * if any B_BACKEND backends have recently announced that they are
    3494             :          * actually WAL senders.
    3495             :          */
    3496       26914 :         if (btmask_contains(targetMask, B_WAL_SENDER) != btmask_contains(targetMask, B_BACKEND) &&
    3497       13652 :             bp->bkend_type == B_BACKEND)
    3498             :         {
    3499        1374 :             if (IsPostmasterChildWalSender(bp->child_slot))
    3500          72 :                 bp->bkend_type = B_WAL_SENDER;
    3501             :         }
    3502             : 
    3503       26914 :         if (!btmask_contains(targetMask, bp->bkend_type))
    3504        9522 :             continue;
    3505             : 
    3506       17392 :         signal_child(bp, signal);
    3507       17392 :         signaled = true;
    3508             :     }
    3509        5742 :     return signaled;
    3510             : }
    3511             : 
    3512             : /*
    3513             :  * Send a termination signal to children.  This considers all of our children
    3514             :  * processes, except syslogger.
    3515             :  */
    3516             : static void
    3517         676 : TerminateChildren(int signal)
    3518             : {
    3519         676 :     SignalChildren(signal, btmask_all_except(B_LOGGER));
    3520         676 :     if (StartupPMChild != NULL)
    3521             :     {
    3522          90 :         if (signal == SIGQUIT || signal == SIGKILL || signal == SIGABRT)
    3523          90 :             StartupStatus = STARTUP_SIGNALED;
    3524             :     }
    3525         676 : }
    3526             : 
    3527             : /*
    3528             :  * BackendStartup -- start backend process
    3529             :  *
    3530             :  * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise.
    3531             :  *
    3532             :  * Note: if you change this code, also consider StartAutovacuumWorker and
    3533             :  * StartBackgroundWorker.
    3534             :  */
    3535             : static int
    3536       26868 : BackendStartup(ClientSocket *client_sock)
    3537             : {
    3538       26868 :     PMChild    *bn = NULL;
    3539             :     pid_t       pid;
    3540             :     BackendStartupData startup_data;
    3541             :     CAC_state   cac;
    3542             : 
    3543             :     /*
    3544             :      * Capture time that Postmaster got a socket from accept (for logging
    3545             :      * connection establishment and setup total duration).
    3546             :      */
    3547       26868 :     startup_data.socket_created = GetCurrentTimestamp();
    3548             : 
    3549             :     /*
    3550             :      * Allocate and assign the child slot.  Note we must do this before
    3551             :      * forking, so that we can handle failures (out of memory or child-process
    3552             :      * slots) cleanly.
    3553             :      */
    3554       26868 :     cac = canAcceptConnections(B_BACKEND);
    3555       26868 :     if (cac == CAC_OK)
    3556             :     {
    3557             :         /* Can change later to B_WAL_SENDER */
    3558       26144 :         bn = AssignPostmasterChildSlot(B_BACKEND);
    3559       26144 :         if (!bn)
    3560             :         {
    3561             :             /*
    3562             :              * Too many regular child processes; launch a dead-end child
    3563             :              * process instead.
    3564             :              */
    3565          56 :             cac = CAC_TOOMANY;
    3566             :         }
    3567             :     }
    3568       26868 :     if (!bn)
    3569             :     {
    3570         780 :         bn = AllocDeadEndChild();
    3571         780 :         if (!bn)
    3572             :         {
    3573           0 :             ereport(LOG,
    3574             :                     (errcode(ERRCODE_OUT_OF_MEMORY),
    3575             :                      errmsg("out of memory")));
    3576           0 :             return STATUS_ERROR;
    3577             :         }
    3578             :     }
    3579             : 
    3580             :     /* Pass down canAcceptConnections state */
    3581       26868 :     startup_data.canAcceptConnections = cac;
    3582       26868 :     bn->rw = NULL;
    3583             : 
    3584             :     /* Hasn't asked to be notified about any bgworkers yet */
    3585       26868 :     bn->bgworker_notify = false;
    3586             : 
    3587       26868 :     pid = postmaster_child_launch(bn->bkend_type, bn->child_slot,
    3588             :                                   &startup_data, sizeof(startup_data),
    3589             :                                   client_sock);
    3590       26868 :     if (pid < 0)
    3591             :     {
    3592             :         /* in parent, fork failed */
    3593           0 :         int         save_errno = errno;
    3594             : 
    3595           0 :         (void) ReleasePostmasterChildSlot(bn);
    3596           0 :         errno = save_errno;
    3597           0 :         ereport(LOG,
    3598             :                 (errmsg("could not fork new process for connection: %m")));
    3599           0 :         report_fork_failure_to_client(client_sock, save_errno);
    3600           0 :         return STATUS_ERROR;
    3601             :     }
    3602             : 
    3603             :     /* in parent, successful fork */
    3604       26868 :     ereport(DEBUG2,
    3605             :             (errmsg_internal("forked new %s, pid=%d socket=%d",
    3606             :                              GetBackendTypeDesc(bn->bkend_type),
    3607             :                              (int) pid, (int) client_sock->sock)));
    3608             : 
    3609             :     /*
    3610             :      * Everything's been successful, it's safe to add this backend to our list
    3611             :      * of backends.
    3612             :      */
    3613       26868 :     bn->pid = pid;
    3614       26868 :     return STATUS_OK;
    3615             : }
    3616             : 
    3617             : /*
    3618             :  * Try to report backend fork() failure to client before we close the
    3619             :  * connection.  Since we do not care to risk blocking the postmaster on
    3620             :  * this connection, we set the connection to non-blocking and try only once.
    3621             :  *
    3622             :  * This is grungy special-purpose code; we cannot use backend libpq since
    3623             :  * it's not up and running.
    3624             :  */
    3625             : static void
    3626           0 : report_fork_failure_to_client(ClientSocket *client_sock, int errnum)
    3627             : {
    3628             :     char        buffer[1000];
    3629             :     int         rc;
    3630             : 
    3631             :     /* Format the error message packet (always V2 protocol) */
    3632           0 :     snprintf(buffer, sizeof(buffer), "E%s%s\n",
    3633             :              _("could not fork new process for connection: "),
    3634             :              strerror(errnum));
    3635             : 
    3636             :     /* Set port to non-blocking.  Don't do send() if this fails */
    3637           0 :     if (!pg_set_noblock(client_sock->sock))
    3638           0 :         return;
    3639             : 
    3640             :     /* We'll retry after EINTR, but ignore all other failures */
    3641             :     do
    3642             :     {
    3643           0 :         rc = send(client_sock->sock, buffer, strlen(buffer) + 1, 0);
    3644           0 :     } while (rc < 0 && errno == EINTR);
    3645             : }
    3646             : 
    3647             : /*
    3648             :  * ExitPostmaster -- cleanup
    3649             :  *
    3650             :  * Do NOT call exit() directly --- always go through here!
    3651             :  */
    3652             : static void
    3653        1702 : ExitPostmaster(int status)
    3654             : {
    3655             : #ifdef HAVE_PTHREAD_IS_THREADED_NP
    3656             : 
    3657             :     /*
    3658             :      * There is no known cause for a postmaster to become multithreaded after
    3659             :      * startup.  However, we might reach here via an error exit before
    3660             :      * reaching the test in PostmasterMain, so provide the same hint as there.
    3661             :      * This message uses LOG level, because an unclean shutdown at this point
    3662             :      * would usually not look much different from a clean shutdown.
    3663             :      */
    3664             :     if (pthread_is_threaded_np() != 0)
    3665             :         ereport(LOG,
    3666             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    3667             :                  errmsg("postmaster became multithreaded"),
    3668             :                  errhint("Set the LC_ALL environment variable to a valid locale.")));
    3669             : #endif
    3670             : 
    3671             :     /* should cleanup shared memory and kill all backends */
    3672             : 
    3673             :     /*
    3674             :      * Not sure of the semantics here.  When the Postmaster dies, should the
    3675             :      * backends all be killed? probably not.
    3676             :      *
    3677             :      * MUST     -- vadim 05-10-1999
    3678             :      */
    3679             : 
    3680        1702 :     proc_exit(status);
    3681             : }
    3682             : 
    3683             : /*
    3684             :  * Handle pmsignal conditions representing requests from backends,
    3685             :  * and check for promote and logrotate requests from pg_ctl.
    3686             :  */
    3687             : static void
    3688      196042 : process_pm_pmsignal(void)
    3689             : {
    3690      196042 :     bool        request_state_update = false;
    3691             : 
    3692      196042 :     pending_pm_pmsignal = false;
    3693             : 
    3694      196042 :     ereport(DEBUG2,
    3695             :             (errmsg_internal("postmaster received pmsignal signal")));
    3696             : 
    3697             :     /*
    3698             :      * RECOVERY_STARTED and BEGIN_HOT_STANDBY signals are ignored in
    3699             :      * unexpected states. If the startup process quickly starts up, completes
    3700             :      * recovery, exits, we might process the death of the startup process
    3701             :      * first. We don't want to go back to recovery in that case.
    3702             :      */
    3703      196042 :     if (CheckPostmasterSignal(PMSIGNAL_RECOVERY_STARTED) &&
    3704         496 :         pmState == PM_STARTUP && Shutdown == NoShutdown)
    3705             :     {
    3706             :         /* WAL redo has started. We're out of reinitialization. */
    3707         496 :         FatalError = false;
    3708         496 :         AbortStartTime = 0;
    3709         496 :         reachedConsistency = false;
    3710             : 
    3711             :         /*
    3712             :          * Start the archiver if we're responsible for (re-)archiving received
    3713             :          * files.
    3714             :          */
    3715             :         Assert(PgArchPMChild == NULL);
    3716         496 :         if (XLogArchivingAlways())
    3717           6 :             PgArchPMChild = StartChildProcess(B_ARCHIVER);
    3718             : 
    3719             :         /*
    3720             :          * If we aren't planning to enter hot standby mode later, treat
    3721             :          * RECOVERY_STARTED as meaning we're out of startup, and report status
    3722             :          * accordingly.
    3723             :          */
    3724         496 :         if (!EnableHotStandby)
    3725             :         {
    3726           4 :             AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STANDBY);
    3727             : #ifdef USE_SYSTEMD
    3728             :             sd_notify(0, "READY=1");
    3729             : #endif
    3730             :         }
    3731             : 
    3732         496 :         UpdatePMState(PM_RECOVERY);
    3733             :     }
    3734             : 
    3735      196042 :     if (CheckPostmasterSignal(PMSIGNAL_RECOVERY_CONSISTENT) &&
    3736         316 :         pmState == PM_RECOVERY && Shutdown == NoShutdown)
    3737             :     {
    3738         316 :         reachedConsistency = true;
    3739             :     }
    3740             : 
    3741      196042 :     if (CheckPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY) &&
    3742         300 :         (pmState == PM_RECOVERY && Shutdown == NoShutdown))
    3743             :     {
    3744         300 :         ereport(LOG,
    3745             :                 (errmsg("database system is ready to accept read-only connections")));
    3746             : 
    3747             :         /* Report status */
    3748         300 :         AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_READY);
    3749             : #ifdef USE_SYSTEMD
    3750             :         sd_notify(0, "READY=1");
    3751             : #endif
    3752             : 
    3753         300 :         UpdatePMState(PM_HOT_STANDBY);
    3754         300 :         connsAllowed = true;
    3755             : 
    3756             :         /* Some workers may be scheduled to start now */
    3757         300 :         StartWorkerNeeded = true;
    3758             :     }
    3759             : 
    3760             :     /* Process background worker state changes. */
    3761      196042 :     if (CheckPostmasterSignal(PMSIGNAL_BACKGROUND_WORKER_CHANGE))
    3762             :     {
    3763             :         /* Accept new worker requests only if not stopping. */
    3764        2474 :         BackgroundWorkerStateChange(pmState < PM_STOP_BACKENDS);
    3765        2474 :         StartWorkerNeeded = true;
    3766             :     }
    3767             : 
    3768             :     /* Tell syslogger to rotate logfile if requested */
    3769      196042 :     if (SysLoggerPMChild != NULL)
    3770             :     {
    3771           4 :         if (CheckLogrotateSignal())
    3772             :         {
    3773           2 :             signal_child(SysLoggerPMChild, SIGUSR1);
    3774           2 :             RemoveLogrotateSignalFiles();
    3775             :         }
    3776           2 :         else if (CheckPostmasterSignal(PMSIGNAL_ROTATE_LOGFILE))
    3777             :         {
    3778           0 :             signal_child(SysLoggerPMChild, SIGUSR1);
    3779             :         }
    3780             :     }
    3781             : 
    3782      196042 :     if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER) &&
    3783      185610 :         Shutdown <= SmartShutdown && pmState < PM_STOP_BACKENDS)
    3784             :     {
    3785             :         /*
    3786             :          * Start one iteration of the autovacuum daemon, even if autovacuuming
    3787             :          * is nominally not enabled.  This is so we can have an active defense
    3788             :          * against transaction ID wraparound.  We set a flag for the main loop
    3789             :          * to do it rather than trying to do it here --- this is because the
    3790             :          * autovac process itself may send the signal, and we want to handle
    3791             :          * that by launching another iteration as soon as the current one
    3792             :          * completes.
    3793             :          */
    3794      185610 :         start_autovac_launcher = true;
    3795             :     }
    3796             : 
    3797      196042 :     if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER) &&
    3798        3442 :         Shutdown <= SmartShutdown && pmState < PM_STOP_BACKENDS)
    3799             :     {
    3800             :         /* The autovacuum launcher wants us to start a worker process. */
    3801        3442 :         StartAutovacuumWorker();
    3802             :     }
    3803             : 
    3804      196042 :     if (CheckPostmasterSignal(PMSIGNAL_START_WALRECEIVER))
    3805             :     {
    3806             :         /* Startup Process wants us to start the walreceiver process. */
    3807         544 :         WalReceiverRequested = true;
    3808             :     }
    3809             : 
    3810      196042 :     if (CheckPostmasterSignal(PMSIGNAL_XLOG_IS_SHUTDOWN))
    3811             :     {
    3812             :         /* Checkpointer completed the shutdown checkpoint */
    3813        1030 :         if (pmState == PM_WAIT_XLOG_SHUTDOWN)
    3814             :         {
    3815             :             /*
    3816             :              * If we have an archiver subprocess, tell it to do a last archive
    3817             :              * cycle and quit. Likewise, if we have walsender processes, tell
    3818             :              * them to send any remaining WAL and quit.
    3819             :              */
    3820             :             Assert(Shutdown > NoShutdown);
    3821             : 
    3822             :             /* Waken archiver for the last time */
    3823        1030 :             if (PgArchPMChild != NULL)
    3824          30 :                 signal_child(PgArchPMChild, SIGUSR2);
    3825             : 
    3826             :             /*
    3827             :              * Waken walsenders for the last time. No regular backends should
    3828             :              * be around anymore.
    3829             :              */
    3830        1030 :             SignalChildren(SIGUSR2, btmask(B_WAL_SENDER));
    3831             : 
    3832        1030 :             UpdatePMState(PM_WAIT_XLOG_ARCHIVAL);
    3833             :         }
    3834           0 :         else if (!FatalError && Shutdown != ImmediateShutdown)
    3835             :         {
    3836             :             /*
    3837             :              * Checkpointer only ought to perform the shutdown checkpoint
    3838             :              * during shutdown.  If somehow checkpointer did so in another
    3839             :              * situation, we have no choice but to crash-restart.
    3840             :              *
    3841             :              * It's possible however that we get PMSIGNAL_XLOG_IS_SHUTDOWN
    3842             :              * outside of PM_WAIT_XLOG_SHUTDOWN if an orderly shutdown was
    3843             :              * "interrupted" by a crash or an immediate shutdown.
    3844             :              */
    3845           0 :             ereport(LOG,
    3846             :                     (errmsg("WAL was shut down unexpectedly")));
    3847             : 
    3848             :             /*
    3849             :              * Doesn't seem likely to help to take send_abort_for_crash into
    3850             :              * account here.
    3851             :              */
    3852           0 :             HandleFatalError(PMQUIT_FOR_CRASH, false);
    3853             :         }
    3854             : 
    3855             :         /*
    3856             :          * Need to run PostmasterStateMachine() to check if we already can go
    3857             :          * to the next state.
    3858             :          */
    3859        1030 :         request_state_update = true;
    3860             :     }
    3861             : 
    3862             :     /*
    3863             :      * Try to advance postmaster's state machine, if a child requests it.
    3864             :      */
    3865      196042 :     if (CheckPostmasterSignal(PMSIGNAL_ADVANCE_STATE_MACHINE))
    3866             :     {
    3867        2400 :         request_state_update = true;
    3868             :     }
    3869             : 
    3870             :     /*
    3871             :      * Be careful about the order of this action relative to this function's
    3872             :      * other actions.  Generally, this should be after other actions, in case
    3873             :      * they have effects PostmasterStateMachine would need to know about.
    3874             :      * However, we should do it before the CheckPromoteSignal step, which
    3875             :      * cannot have any (immediate) effect on the state machine, but does
    3876             :      * depend on what state we're in now.
    3877             :      */
    3878      196042 :     if (request_state_update)
    3879             :     {
    3880        3430 :         PostmasterStateMachine();
    3881             :     }
    3882             : 
    3883      196042 :     if (StartupPMChild != NULL &&
    3884        1620 :         (pmState == PM_STARTUP || pmState == PM_RECOVERY ||
    3885        2652 :          pmState == PM_HOT_STANDBY) &&
    3886        1620 :         CheckPromoteSignal())
    3887             :     {
    3888             :         /*
    3889             :          * Tell startup process to finish recovery.
    3890             :          *
    3891             :          * Leave the promote signal file in place and let the Startup process
    3892             :          * do the unlink.
    3893             :          */
    3894          84 :         signal_child(StartupPMChild, SIGUSR2);
    3895             :     }
    3896      196042 : }
    3897             : 
    3898             : /*
    3899             :  * Dummy signal handler
    3900             :  *
    3901             :  * We use this for signals that we don't actually use in the postmaster,
    3902             :  * but we do use in backends.  If we were to SIG_IGN such signals in the
    3903             :  * postmaster, then a newly started backend might drop a signal that arrives
    3904             :  * before it's able to reconfigure its signal processing.  (See notes in
    3905             :  * tcop/postgres.c.)
    3906             :  */
    3907             : static void
    3908           0 : dummy_handler(SIGNAL_ARGS)
    3909             : {
    3910           0 : }
    3911             : 
    3912             : /*
    3913             :  * Count up number of child processes of specified types.
    3914             :  */
    3915             : static int
    3916       12258 : CountChildren(BackendTypeMask targetMask)
    3917             : {
    3918             :     dlist_iter  iter;
    3919       12258 :     int         cnt = 0;
    3920             : 
    3921       73012 :     dlist_foreach(iter, &ActiveChildList)
    3922             :     {
    3923       60754 :         PMChild    *bp = dlist_container(PMChild, elem, iter.cur);
    3924             : 
    3925             :         /*
    3926             :          * If we need to distinguish between B_BACKEND and B_WAL_SENDER, check
    3927             :          * if any B_BACKEND backends have recently announced that they are
    3928             :          * actually WAL senders.
    3929             :          */
    3930       60754 :         if (btmask_contains(targetMask, B_WAL_SENDER) != btmask_contains(targetMask, B_BACKEND) &&
    3931       43176 :             bp->bkend_type == B_BACKEND)
    3932             :         {
    3933        2984 :             if (IsPostmasterChildWalSender(bp->child_slot))
    3934           0 :                 bp->bkend_type = B_WAL_SENDER;
    3935             :         }
    3936             : 
    3937       60754 :         if (!btmask_contains(targetMask, bp->bkend_type))
    3938       31320 :             continue;
    3939             : 
    3940       29434 :         ereport(DEBUG4,
    3941             :                 (errmsg_internal("%s process %d is still running",
    3942             :                                  GetBackendTypeDesc(bp->bkend_type), (int) bp->pid)));
    3943             : 
    3944       29434 :         cnt++;
    3945             :     }
    3946       12258 :     return cnt;
    3947             : }
    3948             : 
    3949             : 
    3950             : /*
    3951             :  * StartChildProcess -- start an auxiliary process for the postmaster
    3952             :  *
    3953             :  * "type" determines what kind of child will be started.  All child types
    3954             :  * initially go to AuxiliaryProcessMain, which will handle common setup.
    3955             :  *
    3956             :  * Return value of StartChildProcess is subprocess' PMChild entry, or NULL on
    3957             :  * failure.
    3958             :  */
    3959             : static PMChild *
    3960       17172 : StartChildProcess(BackendType type)
    3961             : {
    3962             :     PMChild    *pmchild;
    3963             :     pid_t       pid;
    3964             : 
    3965       17172 :     pmchild = AssignPostmasterChildSlot(type);
    3966       17172 :     if (!pmchild)
    3967             :     {
    3968           0 :         if (type == B_AUTOVAC_WORKER)
    3969           0 :             ereport(LOG,
    3970             :                     (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
    3971             :                      errmsg("no slot available for new autovacuum worker process")));
    3972             :         else
    3973             :         {
    3974             :             /* shouldn't happen because we allocate enough slots */
    3975           0 :             elog(LOG, "no postmaster child slot available for aux process");
    3976             :         }
    3977           0 :         return NULL;
    3978             :     }
    3979             : 
    3980       17172 :     pid = postmaster_child_launch(type, pmchild->child_slot, NULL, 0, NULL);
    3981       17172 :     if (pid < 0)
    3982             :     {
    3983             :         /* in parent, fork failed */
    3984           0 :         ReleasePostmasterChildSlot(pmchild);
    3985           0 :         ereport(LOG,
    3986             :                 (errmsg("could not fork \"%s\" process: %m", PostmasterChildName(type))));
    3987             : 
    3988             :         /*
    3989             :          * fork failure is fatal during startup, but there's no need to choke
    3990             :          * immediately if starting other child types fails.
    3991             :          */
    3992           0 :         if (type == B_STARTUP)
    3993           0 :             ExitPostmaster(1);
    3994           0 :         return NULL;
    3995             :     }
    3996             : 
    3997             :     /* in parent, successful fork */
    3998       17172 :     pmchild->pid = pid;
    3999       17172 :     return pmchild;
    4000             : }
    4001             : 
    4002             : /*
    4003             :  * StartSysLogger -- start the syslogger process
    4004             :  */
    4005             : void
    4006           2 : StartSysLogger(void)
    4007             : {
    4008             :     Assert(SysLoggerPMChild == NULL);
    4009             : 
    4010           2 :     SysLoggerPMChild = AssignPostmasterChildSlot(B_LOGGER);
    4011           2 :     if (!SysLoggerPMChild)
    4012           0 :         elog(PANIC, "no postmaster child slot available for syslogger");
    4013           2 :     SysLoggerPMChild->pid = SysLogger_Start(SysLoggerPMChild->child_slot);
    4014           2 :     if (SysLoggerPMChild->pid == 0)
    4015             :     {
    4016           0 :         ReleasePostmasterChildSlot(SysLoggerPMChild);
    4017           0 :         SysLoggerPMChild = NULL;
    4018             :     }
    4019           2 : }
    4020             : 
    4021             : /*
    4022             :  * StartAutovacuumWorker
    4023             :  *      Start an autovac worker process.
    4024             :  *
    4025             :  * This function is here because it enters the resulting PID into the
    4026             :  * postmaster's private backends list.
    4027             :  *
    4028             :  * NB -- this code very roughly matches BackendStartup.
    4029             :  */
    4030             : static void
    4031        3442 : StartAutovacuumWorker(void)
    4032             : {
    4033             :     PMChild    *bn;
    4034             : 
    4035             :     /*
    4036             :      * If not in condition to run a process, don't try, but handle it like a
    4037             :      * fork failure.  This does not normally happen, since the signal is only
    4038             :      * supposed to be sent by autovacuum launcher when it's OK to do it, but
    4039             :      * we have to check to avoid race-condition problems during DB state
    4040             :      * changes.
    4041             :      */
    4042        3442 :     if (canAcceptConnections(B_AUTOVAC_WORKER) == CAC_OK)
    4043             :     {
    4044        3442 :         bn = StartChildProcess(B_AUTOVAC_WORKER);
    4045        3442 :         if (bn)
    4046             :         {
    4047        3442 :             bn->bgworker_notify = false;
    4048        3442 :             bn->rw = NULL;
    4049        3442 :             return;
    4050             :         }
    4051             :         else
    4052             :         {
    4053             :             /*
    4054             :              * fork failed, fall through to report -- actual error message was
    4055             :              * logged by StartChildProcess
    4056             :              */
    4057             :         }
    4058             :     }
    4059             : 
    4060             :     /*
    4061             :      * Report the failure to the launcher, if it's running.  (If it's not, we
    4062             :      * might not even be connected to shared memory, so don't try to call
    4063             :      * AutoVacWorkerFailed.)  Note that we also need to signal it so that it
    4064             :      * responds to the condition, but we don't do that here, instead waiting
    4065             :      * for ServerLoop to do it.  This way we avoid a ping-pong signaling in
    4066             :      * quick succession between the autovac launcher and postmaster in case
    4067             :      * things get ugly.
    4068             :      */
    4069           0 :     if (AutoVacLauncherPMChild != NULL)
    4070             :     {
    4071           0 :         AutoVacWorkerFailed();
    4072           0 :         avlauncher_needs_signal = true;
    4073             :     }
    4074             : }
    4075             : 
    4076             : 
    4077             : /*
    4078             :  * Create the opts file
    4079             :  */
    4080             : static bool
    4081        1698 : CreateOptsFile(int argc, char *argv[], char *fullprogname)
    4082             : {
    4083             :     FILE       *fp;
    4084             :     int         i;
    4085             : 
    4086             : #define OPTS_FILE   "postmaster.opts"
    4087             : 
    4088        1698 :     if ((fp = fopen(OPTS_FILE, "w")) == NULL)
    4089             :     {
    4090           0 :         ereport(LOG,
    4091             :                 (errcode_for_file_access(),
    4092             :                  errmsg("could not create file \"%s\": %m", OPTS_FILE)));
    4093           0 :         return false;
    4094             :     }
    4095             : 
    4096        1698 :     fprintf(fp, "%s", fullprogname);
    4097        8930 :     for (i = 1; i < argc; i++)
    4098        7232 :         fprintf(fp, " \"%s\"", argv[i]);
    4099        1698 :     fputs("\n", fp);
    4100             : 
    4101        1698 :     if (fclose(fp))
    4102             :     {
    4103           0 :         ereport(LOG,
    4104             :                 (errcode_for_file_access(),
    4105             :                  errmsg("could not write file \"%s\": %m", OPTS_FILE)));
    4106           0 :         return false;
    4107             :     }
    4108             : 
    4109        1698 :     return true;
    4110             : }
    4111             : 
    4112             : 
    4113             : /*
    4114             :  * Start a new bgworker.
    4115             :  * Starting time conditions must have been checked already.
    4116             :  *
    4117             :  * Returns true on success, false on failure.
    4118             :  * In either case, update the RegisteredBgWorker's state appropriately.
    4119             :  *
    4120             :  * NB -- this code very roughly matches BackendStartup.
    4121             :  */
    4122             : static bool
    4123        5128 : StartBackgroundWorker(RegisteredBgWorker *rw)
    4124             : {
    4125             :     PMChild    *bn;
    4126             :     pid_t       worker_pid;
    4127             : 
    4128             :     Assert(rw->rw_pid == 0);
    4129             : 
    4130             :     /*
    4131             :      * Allocate and assign the child slot.  Note we must do this before
    4132             :      * forking, so that we can handle failures (out of memory or child-process
    4133             :      * slots) cleanly.
    4134             :      *
    4135             :      * Treat failure as though the worker had crashed.  That way, the
    4136             :      * postmaster will wait a bit before attempting to start it again; if we
    4137             :      * tried again right away, most likely we'd find ourselves hitting the
    4138             :      * same resource-exhaustion condition.
    4139             :      */
    4140        5128 :     bn = AssignPostmasterChildSlot(B_BG_WORKER);
    4141        5128 :     if (bn == NULL)
    4142             :     {
    4143           0 :         ereport(LOG,
    4144             :                 (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
    4145             :                  errmsg("no slot available for new background worker process")));
    4146           0 :         rw->rw_crashed_at = GetCurrentTimestamp();
    4147           0 :         return false;
    4148             :     }
    4149        5128 :     bn->rw = rw;
    4150        5128 :     bn->bkend_type = B_BG_WORKER;
    4151        5128 :     bn->bgworker_notify = false;
    4152             : 
    4153        5128 :     ereport(DEBUG1,
    4154             :             (errmsg_internal("starting background worker process \"%s\"",
    4155             :                              rw->rw_worker.bgw_name)));
    4156             : 
    4157        5128 :     worker_pid = postmaster_child_launch(B_BG_WORKER, bn->child_slot,
    4158        5128 :                                          &rw->rw_worker, sizeof(BackgroundWorker), NULL);
    4159        5128 :     if (worker_pid == -1)
    4160             :     {
    4161             :         /* in postmaster, fork failed ... */
    4162           0 :         ereport(LOG,
    4163             :                 (errmsg("could not fork background worker process: %m")));
    4164             :         /* undo what AssignPostmasterChildSlot did */
    4165           0 :         ReleasePostmasterChildSlot(bn);
    4166             : 
    4167             :         /* mark entry as crashed, so we'll try again later */
    4168           0 :         rw->rw_crashed_at = GetCurrentTimestamp();
    4169           0 :         return false;
    4170             :     }
    4171             : 
    4172             :     /* in postmaster, fork successful ... */
    4173        5128 :     rw->rw_pid = worker_pid;
    4174        5128 :     bn->pid = rw->rw_pid;
    4175        5128 :     ReportBackgroundWorkerPID(rw);
    4176        5128 :     return true;
    4177             : }
    4178             : 
    4179             : /*
    4180             :  * Does the current postmaster state require starting a worker with the
    4181             :  * specified start_time?
    4182             :  */
    4183             : static bool
    4184        7024 : bgworker_should_start_now(BgWorkerStartTime start_time)
    4185             : {
    4186        7024 :     switch (pmState)
    4187             :     {
    4188           2 :         case PM_NO_CHILDREN:
    4189             :         case PM_WAIT_CHECKPOINTER:
    4190             :         case PM_WAIT_DEAD_END:
    4191             :         case PM_WAIT_XLOG_ARCHIVAL:
    4192             :         case PM_WAIT_XLOG_SHUTDOWN:
    4193             :         case PM_WAIT_IO_WORKERS:
    4194             :         case PM_WAIT_BACKENDS:
    4195             :         case PM_STOP_BACKENDS:
    4196           2 :             break;
    4197             : 
    4198        5128 :         case PM_RUN:
    4199        5128 :             if (start_time == BgWorkerStart_RecoveryFinished)
    4200        2372 :                 return true;
    4201             :             /* fall through */
    4202             : 
    4203             :         case PM_HOT_STANDBY:
    4204        3048 :             if (start_time == BgWorkerStart_ConsistentState)
    4205        2756 :                 return true;
    4206             :             /* fall through */
    4207             : 
    4208             :         case PM_RECOVERY:
    4209             :         case PM_STARTUP:
    4210             :         case PM_INIT:
    4211        1894 :             if (start_time == BgWorkerStart_PostmasterStart)
    4212           0 :                 return true;
    4213             :             /* fall through */
    4214             :     }
    4215             : 
    4216        1896 :     return false;
    4217             : }
    4218             : 
    4219             : /*
    4220             :  * If the time is right, start background worker(s).
    4221             :  *
    4222             :  * As a side effect, the bgworker control variables are set or reset
    4223             :  * depending on whether more workers may need to be started.
    4224             :  *
    4225             :  * We limit the number of workers started per call, to avoid consuming the
    4226             :  * postmaster's attention for too long when many such requests are pending.
    4227             :  * As long as StartWorkerNeeded is true, ServerLoop will not block and will
    4228             :  * call this function again after dealing with any other issues.
    4229             :  */
    4230             : static void
    4231       15230 : maybe_start_bgworkers(void)
    4232             : {
    4233             : #define MAX_BGWORKERS_TO_LAUNCH 100
    4234       15230 :     int         num_launched = 0;
    4235       15230 :     TimestampTz now = 0;
    4236             :     dlist_mutable_iter iter;
    4237             : 
    4238             :     /*
    4239             :      * During crash recovery, we have no need to be called until the state
    4240             :      * transition out of recovery.
    4241             :      */
    4242       15230 :     if (FatalError)
    4243             :     {
    4244           0 :         StartWorkerNeeded = false;
    4245           0 :         HaveCrashedWorker = false;
    4246           0 :         return;
    4247             :     }
    4248             : 
    4249             :     /* Don't need to be called again unless we find a reason for it below */
    4250       15230 :     StartWorkerNeeded = false;
    4251       15230 :     HaveCrashedWorker = false;
    4252             : 
    4253       40526 :     dlist_foreach_modify(iter, &BackgroundWorkerList)
    4254             :     {
    4255             :         RegisteredBgWorker *rw;
    4256             : 
    4257       25296 :         rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
    4258             : 
    4259             :         /* ignore if already running */
    4260       25296 :         if (rw->rw_pid != 0)
    4261       12604 :             continue;
    4262             : 
    4263             :         /* if marked for death, clean up and remove from list */
    4264       12692 :         if (rw->rw_terminate)
    4265             :         {
    4266           0 :             ForgetBackgroundWorker(rw);
    4267           0 :             continue;
    4268             :         }
    4269             : 
    4270             :         /*
    4271             :          * If this worker has crashed previously, maybe it needs to be
    4272             :          * restarted (unless on registration it specified it doesn't want to
    4273             :          * be restarted at all).  Check how long ago did a crash last happen.
    4274             :          * If the last crash is too recent, don't start it right away; let it
    4275             :          * be restarted once enough time has passed.
    4276             :          */
    4277       12692 :         if (rw->rw_crashed_at != 0)
    4278             :         {
    4279        5670 :             if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART)
    4280           0 :             {
    4281             :                 int         notify_pid;
    4282             : 
    4283           0 :                 notify_pid = rw->rw_worker.bgw_notify_pid;
    4284             : 
    4285           0 :                 ForgetBackgroundWorker(rw);
    4286             : 
    4287             :                 /* Report worker is gone now. */
    4288           0 :                 if (notify_pid != 0)
    4289           0 :                     kill(notify_pid, SIGUSR1);
    4290             : 
    4291           0 :                 continue;
    4292             :             }
    4293             : 
    4294             :             /* read system time only when needed */
    4295        5670 :             if (now == 0)
    4296        5670 :                 now = GetCurrentTimestamp();
    4297             : 
    4298        5670 :             if (!TimestampDifferenceExceeds(rw->rw_crashed_at, now,
    4299        5670 :                                             rw->rw_worker.bgw_restart_time * 1000))
    4300             :             {
    4301             :                 /* Set flag to remember that we have workers to start later */
    4302        5668 :                 HaveCrashedWorker = true;
    4303        5668 :                 continue;
    4304             :             }
    4305             :         }
    4306             : 
    4307        7024 :         if (bgworker_should_start_now(rw->rw_worker.bgw_start_time))
    4308             :         {
    4309             :             /* reset crash time before trying to start worker */
    4310        5128 :             rw->rw_crashed_at = 0;
    4311             : 
    4312             :             /*
    4313             :              * Try to start the worker.
    4314             :              *
    4315             :              * On failure, give up processing workers for now, but set
    4316             :              * StartWorkerNeeded so we'll come back here on the next iteration
    4317             :              * of ServerLoop to try again.  (We don't want to wait, because
    4318             :              * there might be additional ready-to-run workers.)  We could set
    4319             :              * HaveCrashedWorker as well, since this worker is now marked
    4320             :              * crashed, but there's no need because the next run of this
    4321             :              * function will do that.
    4322             :              */
    4323        5128 :             if (!StartBackgroundWorker(rw))
    4324             :             {
    4325           0 :                 StartWorkerNeeded = true;
    4326           0 :                 return;
    4327             :             }
    4328             : 
    4329             :             /*
    4330             :              * If we've launched as many workers as allowed, quit, but have
    4331             :              * ServerLoop call us again to look for additional ready-to-run
    4332             :              * workers.  There might not be any, but we'll find out the next
    4333             :              * time we run.
    4334             :              */
    4335        5128 :             if (++num_launched >= MAX_BGWORKERS_TO_LAUNCH)
    4336             :             {
    4337           0 :                 StartWorkerNeeded = true;
    4338           0 :                 return;
    4339             :             }
    4340             :         }
    4341             :     }
    4342             : }
    4343             : 
    4344             : static bool
    4345       40632 : maybe_reap_io_worker(int pid)
    4346             : {
    4347     1180856 :     for (int i = 0; i < MAX_IO_WORKERS; ++i)
    4348             :     {
    4349     1145418 :         if (io_worker_children[i] &&
    4350      111168 :             io_worker_children[i]->pid == pid)
    4351             :         {
    4352        5194 :             ReleasePostmasterChildSlot(io_worker_children[i]);
    4353             : 
    4354        5194 :             --io_worker_count;
    4355        5194 :             io_worker_children[i] = NULL;
    4356        5194 :             return true;
    4357             :         }
    4358             :     }
    4359       35438 :     return false;
    4360             : }
    4361             : 
    4362             : /*
    4363             :  * Start or stop IO workers, to close the gap between the number of running
    4364             :  * workers and the number of configured workers.  Used to respond to change of
    4365             :  * the io_workers GUC (by increasing and decreasing the number of workers), as
    4366             :  * well as workers terminating in response to errors (by starting
    4367             :  * "replacement" workers).
    4368             :  */
    4369             : static void
    4370      274982 : maybe_adjust_io_workers(void)
    4371             : {
    4372      274982 :     if (!pgaio_workers_enabled())
    4373         112 :         return;
    4374             : 
    4375             :     /*
    4376             :      * If we're in final shutting down state, then we're just waiting for all
    4377             :      * processes to exit.
    4378             :      */
    4379      274870 :     if (pmState >= PM_WAIT_IO_WORKERS)
    4380        7408 :         return;
    4381             : 
    4382             :     /* Don't start new workers during an immediate shutdown either. */
    4383      267462 :     if (Shutdown >= ImmediateShutdown)
    4384        4332 :         return;
    4385             : 
    4386             :     /*
    4387             :      * Don't start new workers if we're in the shutdown phase of a crash
    4388             :      * restart. But we *do* need to start if we're already starting up again.
    4389             :      */
    4390      263130 :     if (FatalError && pmState >= PM_STOP_BACKENDS)
    4391          74 :         return;
    4392             : 
    4393             :     Assert(pmState < PM_WAIT_IO_WORKERS);
    4394             : 
    4395             :     /* Not enough running? */
    4396      268256 :     while (io_worker_count < io_workers)
    4397             :     {
    4398             :         PMChild    *child;
    4399             :         int         i;
    4400             : 
    4401             :         /* find unused entry in io_worker_children array */
    4402       11414 :         for (i = 0; i < MAX_IO_WORKERS; ++i)
    4403             :         {
    4404       11414 :             if (io_worker_children[i] == NULL)
    4405        5200 :                 break;
    4406             :         }
    4407        5200 :         if (i == MAX_IO_WORKERS)
    4408           0 :             elog(ERROR, "could not find a free IO worker slot");
    4409             : 
    4410             :         /* Try to launch one. */
    4411        5200 :         child = StartChildProcess(B_IO_WORKER);
    4412        5200 :         if (child != NULL)
    4413             :         {
    4414        5200 :             io_worker_children[i] = child;
    4415        5200 :             ++io_worker_count;
    4416             :         }
    4417             :         else
    4418           0 :             break;              /* try again next time */
    4419             :     }
    4420             : 
    4421             :     /* Too many running? */
    4422      263056 :     if (io_worker_count > io_workers)
    4423             :     {
    4424             :         /* ask the IO worker in the highest slot to exit */
    4425        2768 :         for (int i = MAX_IO_WORKERS - 1; i >= 0; --i)
    4426             :         {
    4427        2768 :             if (io_worker_children[i] != NULL)
    4428             :             {
    4429         184 :                 kill(io_worker_children[i]->pid, SIGUSR2);
    4430         184 :                 break;
    4431             :             }
    4432             :         }
    4433             :     }
    4434             : }
    4435             : 
    4436             : 
    4437             : /*
    4438             :  * When a backend asks to be notified about worker state changes, we
    4439             :  * set a flag in its backend entry.  The background worker machinery needs
    4440             :  * to know when such backends exit.
    4441             :  */
    4442             : bool
    4443        3724 : PostmasterMarkPIDForWorkerNotify(int pid)
    4444             : {
    4445             :     dlist_iter  iter;
    4446             :     PMChild    *bp;
    4447             : 
    4448        9302 :     dlist_foreach(iter, &ActiveChildList)
    4449             :     {
    4450        9302 :         bp = dlist_container(PMChild, elem, iter.cur);
    4451        9302 :         if (bp->pid == pid)
    4452             :         {
    4453        3724 :             bp->bgworker_notify = true;
    4454        3724 :             return true;
    4455             :         }
    4456             :     }
    4457           0 :     return false;
    4458             : }
    4459             : 
    4460             : #ifdef WIN32
    4461             : 
    4462             : /*
    4463             :  * Subset implementation of waitpid() for Windows.  We assume pid is -1
    4464             :  * (that is, check all child processes) and options is WNOHANG (don't wait).
    4465             :  */
    4466             : static pid_t
    4467             : waitpid(pid_t pid, int *exitstatus, int options)
    4468             : {
    4469             :     win32_deadchild_waitinfo *childinfo;
    4470             :     DWORD       exitcode;
    4471             :     DWORD       dwd;
    4472             :     ULONG_PTR   key;
    4473             :     OVERLAPPED *ovl;
    4474             : 
    4475             :     /* Try to consume one win32_deadchild_waitinfo from the queue. */
    4476             :     if (!GetQueuedCompletionStatus(win32ChildQueue, &dwd, &key, &ovl, 0))
    4477             :     {
    4478             :         errno = EAGAIN;
    4479             :         return -1;
    4480             :     }
    4481             : 
    4482             :     childinfo = (win32_deadchild_waitinfo *) key;
    4483             :     pid = childinfo->procId;
    4484             : 
    4485             :     /*
    4486             :      * Remove handle from wait - required even though it's set to wait only
    4487             :      * once
    4488             :      */
    4489             :     UnregisterWaitEx(childinfo->waitHandle, NULL);
    4490             : 
    4491             :     if (!GetExitCodeProcess(childinfo->procHandle, &exitcode))
    4492             :     {
    4493             :         /*
    4494             :          * Should never happen. Inform user and set a fixed exitcode.
    4495             :          */
    4496             :         write_stderr("could not read exit code for process\n");
    4497             :         exitcode = 255;
    4498             :     }
    4499             :     *exitstatus = exitcode;
    4500             : 
    4501             :     /*
    4502             :      * Close the process handle.  Only after this point can the PID can be
    4503             :      * recycled by the kernel.
    4504             :      */
    4505             :     CloseHandle(childinfo->procHandle);
    4506             : 
    4507             :     /*
    4508             :      * Free struct that was allocated before the call to
    4509             :      * RegisterWaitForSingleObject()
    4510             :      */
    4511             :     pfree(childinfo);
    4512             : 
    4513             :     return pid;
    4514             : }
    4515             : 
    4516             : /*
    4517             :  * Note! Code below executes on a thread pool! All operations must
    4518             :  * be thread safe! Note that elog() and friends must *not* be used.
    4519             :  */
    4520             : static void WINAPI
    4521             : pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
    4522             : {
    4523             :     /* Should never happen, since we use INFINITE as timeout value. */
    4524             :     if (TimerOrWaitFired)
    4525             :         return;
    4526             : 
    4527             :     /*
    4528             :      * Post the win32_deadchild_waitinfo object for waitpid() to deal with. If
    4529             :      * that fails, we leak the object, but we also leak a whole process and
    4530             :      * get into an unrecoverable state, so there's not much point in worrying
    4531             :      * about that.  We'd like to panic, but we can't use that infrastructure
    4532             :      * from this thread.
    4533             :      */
    4534             :     if (!PostQueuedCompletionStatus(win32ChildQueue,
    4535             :                                     0,
    4536             :                                     (ULONG_PTR) lpParameter,
    4537             :                                     NULL))
    4538             :         write_stderr("could not post child completion status\n");
    4539             : 
    4540             :     /* Queue SIGCHLD signal. */
    4541             :     pg_queue_signal(SIGCHLD);
    4542             : }
    4543             : 
    4544             : /*
    4545             :  * Queue a waiter to signal when this child dies.  The wait will be handled
    4546             :  * automatically by an operating system thread pool.  The memory and the
    4547             :  * process handle will be freed by a later call to waitpid().
    4548             :  */
    4549             : void
    4550             : pgwin32_register_deadchild_callback(HANDLE procHandle, DWORD procId)
    4551             : {
    4552             :     win32_deadchild_waitinfo *childinfo;
    4553             : 
    4554             :     childinfo = palloc(sizeof(win32_deadchild_waitinfo));
    4555             :     childinfo->procHandle = procHandle;
    4556             :     childinfo->procId = procId;
    4557             : 
    4558             :     if (!RegisterWaitForSingleObject(&childinfo->waitHandle,
    4559             :                                      procHandle,
    4560             :                                      pgwin32_deadchild_callback,
    4561             :                                      childinfo,
    4562             :                                      INFINITE,
    4563             :                                      WT_EXECUTEONLYONCE | WT_EXECUTEINWAITTHREAD))
    4564             :         ereport(FATAL,
    4565             :                 (errmsg_internal("could not register process for wait: error code %lu",
    4566             :                                  GetLastError())));
    4567             : }
    4568             : 
    4569             : #endif                          /* WIN32 */
    4570             : 
    4571             : /*
    4572             :  * Initialize one and only handle for monitoring postmaster death.
    4573             :  *
    4574             :  * Called once in the postmaster, so that child processes can subsequently
    4575             :  * monitor if their parent is dead.
    4576             :  */
    4577             : static void
    4578        1698 : InitPostmasterDeathWatchHandle(void)
    4579             : {
    4580             : #ifndef WIN32
    4581             : 
    4582             :     /*
    4583             :      * Create a pipe. Postmaster holds the write end of the pipe open
    4584             :      * (POSTMASTER_FD_OWN), and children hold the read end. Children can pass
    4585             :      * the read file descriptor to select() to wake up in case postmaster
    4586             :      * dies, or check for postmaster death with a (read() == 0). Children must
    4587             :      * close the write end as soon as possible after forking, because EOF
    4588             :      * won't be signaled in the read end until all processes have closed the
    4589             :      * write fd. That is taken care of in ClosePostmasterPorts().
    4590             :      */
    4591             :     Assert(MyProcPid == PostmasterPid);
    4592        1698 :     if (pipe(postmaster_alive_fds) < 0)
    4593           0 :         ereport(FATAL,
    4594             :                 (errcode_for_file_access(),
    4595             :                  errmsg_internal("could not create pipe to monitor postmaster death: %m")));
    4596             : 
    4597             :     /* Notify fd.c that we've eaten two FDs for the pipe. */
    4598        1698 :     ReserveExternalFD();
    4599        1698 :     ReserveExternalFD();
    4600             : 
    4601             :     /*
    4602             :      * Set O_NONBLOCK to allow testing for the fd's presence with a read()
    4603             :      * call.
    4604             :      */
    4605        1698 :     if (fcntl(postmaster_alive_fds[POSTMASTER_FD_WATCH], F_SETFL, O_NONBLOCK) == -1)
    4606           0 :         ereport(FATAL,
    4607             :                 (errcode_for_socket_access(),
    4608             :                  errmsg_internal("could not set postmaster death monitoring pipe to nonblocking mode: %m")));
    4609             : #else
    4610             : 
    4611             :     /*
    4612             :      * On Windows, we use a process handle for the same purpose.
    4613             :      */
    4614             :     if (DuplicateHandle(GetCurrentProcess(),
    4615             :                         GetCurrentProcess(),
    4616             :                         GetCurrentProcess(),
    4617             :                         &PostmasterHandle,
    4618             :                         0,
    4619             :                         TRUE,
    4620             :                         DUPLICATE_SAME_ACCESS) == 0)
    4621             :         ereport(FATAL,
    4622             :                 (errmsg_internal("could not duplicate postmaster handle: error code %lu",
    4623             :                                  GetLastError())));
    4624             : #endif                          /* WIN32 */
    4625        1698 : }

Generated by: LCOV version 1.16