LCOV - code coverage report
Current view: top level - src/backend/postmaster - postmaster.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 913 1183 77.2 %
Date: 2025-04-24 12:15:10 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        2832 : btmask(BackendType t)
     150             : {
     151        2832 :     BackendTypeMask mask = {.mask = 1 << t};
     152             : 
     153        2832 :     return mask;
     154             : }
     155             : 
     156             : static inline BackendTypeMask
     157       28472 : btmask_add_n(BackendTypeMask mask, int nargs, BackendType *t)
     158             : {
     159      125576 :     for (int i = 0; i < nargs; i++)
     160       97104 :         mask.mask |= 1 << t[i];
     161       28472 :     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        8042 : btmask_del(BackendTypeMask mask, BackendType t)
     172             : {
     173        8042 :     mask.mask &= ~(1 << t);
     174        8042 :     return mask;
     175             : }
     176             : 
     177             : static inline BackendTypeMask
     178        4760 : btmask_all_except_n(int nargs, BackendType *t)
     179             : {
     180        4760 :     BackendTypeMask mask = BTYPE_MASK_ALL;
     181             : 
     182       12802 :     for (int i = 0; i < nargs; i++)
     183        8042 :         mask = btmask_del(mask, t[i]);
     184        4760 :     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      228096 : btmask_contains(BackendTypeMask mask, BackendType t)
     195             : {
     196      228096 :     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        1704 : PostmasterMain(int argc, char *argv[])
     494             : {
     495             :     int         opt;
     496             :     int         status;
     497        1704 :     char       *userDoption = NULL;
     498        1704 :     bool        listen_addr_saved = false;
     499        1704 :     char       *output_config_variable = NULL;
     500             : 
     501        1704 :     InitProcessGlobals();
     502             : 
     503        1704 :     PostmasterPid = MyProcPid;
     504             : 
     505        1704 :     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        1704 :     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        1704 :     PostmasterContext = AllocSetContextCreate(TopMemoryContext,
     531             :                                               "Postmaster",
     532             :                                               ALLOCSET_DEFAULT_SIZES);
     533        1704 :     MemoryContextSwitchTo(PostmasterContext);
     534             : 
     535             :     /* Initialize paths to installation files */
     536        1704 :     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        1704 :     pqinitmask();
     548        1704 :     sigprocmask(SIG_SETMASK, &BlockSig, NULL);
     549             : 
     550        1704 :     pqsignal(SIGHUP, handle_pm_reload_request_signal);
     551        1704 :     pqsignal(SIGINT, handle_pm_shutdown_request_signal);
     552        1704 :     pqsignal(SIGQUIT, handle_pm_shutdown_request_signal);
     553        1704 :     pqsignal(SIGTERM, handle_pm_shutdown_request_signal);
     554        1704 :     pqsignal(SIGALRM, SIG_IGN); /* ignored */
     555        1704 :     pqsignal(SIGPIPE, SIG_IGN); /* ignored */
     556        1704 :     pqsignal(SIGUSR1, handle_pm_pmsignal_signal);
     557        1704 :     pqsignal(SIGUSR2, dummy_handler);   /* unused, reserve for children */
     558        1704 :     pqsignal(SIGCHLD, handle_pm_child_exit_signal);
     559             : 
     560             :     /* This may configure SIGURG, depending on platform. */
     561        1704 :     InitializeWaitEventSupport();
     562        1704 :     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        1704 :     pqsignal(SIGTTIN, SIG_IGN); /* ignored */
     573             : #endif
     574             : #ifdef SIGTTOU
     575        1704 :     pqsignal(SIGTTOU, SIG_IGN); /* ignored */
     576             : #endif
     577             : 
     578             :     /* ignore SIGXFSZ, so that ulimit violations work like disk full */
     579             : #ifdef SIGXFSZ
     580        1704 :     pqsignal(SIGXFSZ, SIG_IGN); /* ignored */
     581             : #endif
     582             : 
     583             :     /* Begin accepting signals. */
     584        1704 :     sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
     585             : 
     586             :     /*
     587             :      * Options setup
     588             :      */
     589        1704 :     InitializeGUCOptions();
     590             : 
     591        1704 :     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        6280 :     while ((opt = getopt(argc, argv, "B:bC:c:D:d:EeFf:h:ijk:lN:OPp:r:S:sTt:W:-:")) != -1)
     599             :     {
     600        4580 :         switch (opt)
     601             :         {
     602           0 :             case 'B':
     603           0 :                 SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
     604           0 :                 break;
     605             : 
     606          88 :             case 'b':
     607             :                 /* Undocumented flag used for binary upgrades */
     608          88 :                 IsBinaryUpgrade = true;
     609          88 :                 break;
     610             : 
     611           6 :             case 'C':
     612           6 :                 output_config_variable = strdup(optarg);
     613           6 :                 break;
     614             : 
     615        1380 :             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        1380 :                 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        2310 :                     ParseLongOption(optarg, &name, &value);
     635        2310 :                     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        2308 :                     SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
     650        2306 :                     pfree(name);
     651        2306 :                     pfree(value);
     652        2306 :                     break;
     653             :                 }
     654             : 
     655        1700 :             case 'D':
     656        1700 :                 userDoption = strdup(optarg);
     657        1700 :                 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         182 :             case 'F':
     672         182 :                 SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
     673         182 :                 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         182 :             case 'k':
     697         182 :                 SetConfigOption("unix_socket_directories", optarg, PGC_POSTMASTER, PGC_S_ARGV);
     698         182 :                 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         112 :             case 'p':
     717         112 :                 SetConfigOption("port", optarg, PGC_POSTMASTER, PGC_S_ARGV);
     718         112 :                 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        1700 :     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        1700 :     if (!SelectConfigFiles(userDoption, progname))
     786           0 :         ExitPostmaster(2);
     787             : 
     788        1698 :     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        1696 :     checkDataDir();
     830             : 
     831             :     /* Check that pg_control exists */
     832        1696 :     checkControlFile();
     833             : 
     834             :     /* And switch working directory into it */
     835        1696 :     ChangeToDataDir();
     836             : 
     837             :     /*
     838             :      * Check for invalid combinations of GUC settings.
     839             :      */
     840        1696 :     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        1696 :     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        1696 :     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        1696 :     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        1696 :     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        1696 :     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        1696 :     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         168 :         for (p = environ; *p; ++p)
     890         164 :             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        1696 :     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        1694 :     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        1694 :     ApplyLauncherRegister();
     928             : 
     929             :     /*
     930             :      * process any libraries that should be preloaded at postmaster start
     931             :      */
     932        1694 :     process_shared_preload_libraries();
     933             : 
     934             :     /*
     935             :      * Initialize SSL library, if specified.
     936             :      */
     937             : #ifdef USE_SSL
     938        1694 :     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        1684 :     InitializeMaxBackends();
     951        1684 :     InitPostmasterChildSlots();
     952             : 
     953             :     /*
     954             :      * Calculate the size of the PGPROC fast-path lock arrays.
     955             :      */
     956        1684 :     InitializeFastPathLocks();
     957             : 
     958             :     /*
     959             :      * Give preloaded libraries a chance to request additional shared memory.
     960             :      */
     961        1684 :     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        1684 :     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        1684 :     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        1684 :     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        1682 :     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        1680 :     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        1680 :     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        1680 :     RemovePromoteSignalFiles();
    1061             : 
    1062             :     /* Do the same for logrotate signal file */
    1063        1680 :     RemoveLogrotateSignalFiles();
    1064             : 
    1065             :     /* Remove any outdated file holding the current log filenames. */
    1066        1680 :     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        1680 :     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        1680 :     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        1680 :     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        1680 :     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        1680 :     ListenSockets = palloc(MAXLISTEN * sizeof(pgsocket));
    1112        1680 :     on_proc_exit(CloseServerPorts, 0);
    1113             : 
    1114        1680 :     if (ListenAddresses)
    1115             :     {
    1116             :         char       *rawstring;
    1117             :         List       *elemlist;
    1118             :         ListCell   *l;
    1119        1680 :         int         success = 0;
    1120             : 
    1121             :         /* Need a modifiable copy of ListenAddresses */
    1122        1680 :         rawstring = pstrdup(ListenAddresses);
    1123             : 
    1124             :         /* Parse string into list of hostnames */
    1125        1680 :         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        1738 :         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        1680 :         if (!success && elemlist != NIL)
    1170           0 :             ereport(FATAL,
    1171             :                     (errmsg("could not create any TCP/IP sockets")));
    1172             : 
    1173        1680 :         list_free(elemlist);
    1174        1680 :         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        1680 :     if (Unix_socket_directories)
    1217             :     {
    1218             :         char       *rawstring;
    1219             :         List       *elemlist;
    1220             :         ListCell   *l;
    1221        1680 :         int         success = 0;
    1222             : 
    1223             :         /* Need a modifiable copy of Unix_socket_directories */
    1224        1680 :         rawstring = pstrdup(Unix_socket_directories);
    1225             : 
    1226             :         /* Parse string into list of directories */
    1227        1680 :         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        3358 :         foreach(l, elemlist)
    1237             :         {
    1238        1678 :             char       *socketdir = (char *) lfirst(l);
    1239             : 
    1240        1678 :             status = ListenServerPort(AF_UNIX, NULL,
    1241        1678 :                                       (unsigned short) PostPortNumber,
    1242             :                                       socketdir,
    1243             :                                       ListenSockets,
    1244             :                                       &NumListenSockets,
    1245             :                                       MAXLISTEN);
    1246             : 
    1247        1678 :             if (status == STATUS_OK)
    1248             :             {
    1249        1678 :                 success++;
    1250             :                 /* record the first successful Unix socket in lockfile */
    1251        1678 :                 if (success == 1)
    1252        1678 :                     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        1680 :         if (!success && elemlist != NIL)
    1261           0 :             ereport(FATAL,
    1262             :                     (errmsg("could not create any Unix-domain sockets")));
    1263             : 
    1264        1680 :         list_free_deep(elemlist);
    1265        1680 :         pfree(rawstring);
    1266             :     }
    1267             : 
    1268             :     /*
    1269             :      * check that we have some socket to listen on
    1270             :      */
    1271        1680 :     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        1680 :     if (!listen_addr_saved)
    1281        1622 :         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        1680 :     if (!CreateOptsFile(argc, argv, my_exec_path))
    1288           0 :         ExitPostmaster(1);
    1289             : 
    1290             :     /*
    1291             :      * Write the external PID file if requested
    1292             :      */
    1293        1680 :     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        1680 :     RemovePgTempFiles();
    1319             : 
    1320             :     /*
    1321             :      * Initialize the autovacuum subsystem (again, no process start yet)
    1322             :      */
    1323        1680 :     autovac_init();
    1324             : 
    1325             :     /*
    1326             :      * Load configuration files for client authentication.
    1327             :      */
    1328        1680 :     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        1680 :     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        1680 :     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        1680 :     AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STARTING);
    1378             : 
    1379        1680 :     UpdatePMState(PM_STARTUP);
    1380             : 
    1381             :     /* Make sure we can perform I/O while starting up. */
    1382        1680 :     maybe_adjust_io_workers();
    1383             : 
    1384             :     /* Start bgwriter and checkpointer so they can help with recovery */
    1385        1680 :     if (CheckpointerPMChild == NULL)
    1386        1680 :         CheckpointerPMChild = StartChildProcess(B_CHECKPOINTER);
    1387        1680 :     if (BgWriterPMChild == NULL)
    1388        1680 :         BgWriterPMChild = StartChildProcess(B_BG_WRITER);
    1389             : 
    1390             :     /*
    1391             :      * We're ready to rock and roll...
    1392             :      */
    1393        1680 :     StartupPMChild = StartChildProcess(B_STARTUP);
    1394             :     Assert(StartupPMChild != NULL);
    1395        1680 :     StartupStatus = STARTUP_RUNNING;
    1396             : 
    1397             :     /* Some workers may be scheduled to start now */
    1398        1680 :     maybe_start_bgworkers();
    1399             : 
    1400        1680 :     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        1674 : 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        3406 :     for (i = 0; i < NumListenSockets; i++)
    1426             :     {
    1427        1732 :         if (closesocket(ListenSockets[i]) != 0)
    1428           0 :             elog(LOG, "could not close listen socket: %m");
    1429             :     }
    1430        1674 :     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        1674 :     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        1674 : }
    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        1704 : getInstallationPaths(const char *argv0)
    1462             : {
    1463             :     DIR        *pdir;
    1464             : 
    1465             :     /* Locate the postgres executable itself */
    1466        1704 :     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        1704 :     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        1704 :     pdir = AllocateDir(pkglib_path);
    1493        1704 :     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        1704 :     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        1704 : }
    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        1696 : checkControlFile(void)
    1516             : {
    1517             :     char        path[MAXPGPATH];
    1518             :     FILE       *fp;
    1519             : 
    1520        1696 :     snprintf(path, sizeof(path), "%s/%s", DataDir, XLOG_CONTROL_FILE);
    1521             : 
    1522        1696 :     fp = AllocateFile(path, PG_BINARY_R);
    1523        1696 :     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        1696 :     FreeFile(fp);
    1532        1696 : }
    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      240768 : DetermineSleepTime(void)
    1545             : {
    1546      240768 :     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      240768 :     if (Shutdown > NoShutdown ||
    1553      228944 :         (!StartWorkerNeeded && !HaveCrashedWorker))
    1554             :     {
    1555      240768 :         if (AbortStartTime != 0)
    1556             :         {
    1557             :             int         seconds;
    1558             : 
    1559             :             /* time left to abort; clamp to 0 in case it already expired */
    1560        2586 :             seconds = SIGKILL_CHILDREN_AFTER_SECS -
    1561        2586 :                 (time(NULL) - AbortStartTime);
    1562             : 
    1563        2586 :             return Max(seconds * 1000, 0);
    1564             :         }
    1565             :         else
    1566      238182 :             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        3372 : ConfigurePostmasterWaitSet(bool accept_connections)
    1630             : {
    1631        3372 :     if (pm_wait_set)
    1632        1692 :         FreeWaitEventSet(pm_wait_set);
    1633        3372 :     pm_wait_set = NULL;
    1634             : 
    1635        5062 :     pm_wait_set = CreateWaitEventSet(NULL,
    1636        1690 :                                      accept_connections ? (1 + NumListenSockets) : 1);
    1637        3372 :     AddWaitEventToSet(pm_wait_set, WL_LATCH_SET, PGINVALID_SOCKET, MyLatch,
    1638             :                       NULL);
    1639             : 
    1640        3372 :     if (accept_connections)
    1641             :     {
    1642        3438 :         for (int i = 0; i < NumListenSockets; i++)
    1643        1748 :             AddWaitEventToSet(pm_wait_set, WL_SOCKET_ACCEPT, ListenSockets[i],
    1644             :                               NULL, NULL);
    1645             :     }
    1646        3372 : }
    1647             : 
    1648             : /*
    1649             :  * Main idle loop of postmaster
    1650             :  */
    1651             : static int
    1652        1680 : ServerLoop(void)
    1653             : {
    1654             :     time_t      last_lockfile_recheck_time,
    1655             :                 last_touch_time;
    1656             :     WaitEvent   events[MAXLISTEN];
    1657             :     int         nevents;
    1658             : 
    1659        1680 :     ConfigurePostmasterWaitSet(true);
    1660        1680 :     last_lockfile_recheck_time = last_touch_time = time(NULL);
    1661             : 
    1662             :     for (;;)
    1663      239088 :     {
    1664             :         time_t      now;
    1665             : 
    1666      240768 :         nevents = WaitEventSetWait(pm_wait_set,
    1667      240768 :                                    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      480774 :         for (int i = 0; i < nevents; i++)
    1677             :         {
    1678      241682 :             if (events[i].events & WL_LATCH_SET)
    1679      215830 :                 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      241682 :             if (pending_pm_shutdown_request)
    1689        1666 :                 process_pm_shutdown_request();
    1690      241682 :             if (pending_pm_reload_request)
    1691         266 :                 process_pm_reload_request();
    1692      241682 :             if (pending_pm_child_exit)
    1693       41988 :                 process_pm_child_exit();
    1694      240008 :             if (pending_pm_pmsignal)
    1695      172424 :                 process_pm_pmsignal();
    1696             : 
    1697      240008 :             if (events[i].events & WL_SOCKET_ACCEPT)
    1698             :             {
    1699             :                 ClientSocket s;
    1700             : 
    1701       25852 :                 if (AcceptConnection(events[i].fd, &s) == STATUS_OK)
    1702       25852 :                     BackendStartup(&s);
    1703             : 
    1704             :                 /* We no longer need the open socket in this process */
    1705       25850 :                 if (s.sock != PGINVALID_SOCKET)
    1706             :                 {
    1707       25850 :                     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      239092 :         LaunchMissingBackgroundProcesses();
    1718             : 
    1719             :         /* If we need to signal the autovacuum launcher, do so now */
    1720      239088 :         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      239088 :         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      239088 :         if ((Shutdown >= ImmediateShutdown || FatalError) &&
    1756        2596 :             AbortStartTime != 0 &&
    1757        2586 :             (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      239088 :         if (now - last_lockfile_recheck_time >= 1 * SECS_PER_MINUTE)
    1780             :         {
    1781          32 :             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          32 :             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      239088 :         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       28228 : canAcceptConnections(BackendType backend_type)
    1812             : {
    1813       28228 :     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       28228 :     if (pmState != PM_RUN && pmState != PM_HOT_STANDBY)
    1823             :     {
    1824         286 :         if (Shutdown > NoShutdown)
    1825          54 :             return CAC_SHUTDOWN;    /* shutdown is pending */
    1826         232 :         else if (!FatalError && pmState == PM_STARTUP)
    1827         222 :             return CAC_STARTUP; /* normal startup */
    1828          10 :         else if (!FatalError && pmState == PM_RECOVERY)
    1829          10 :             return CAC_NOTHOTSTANDBY;   /* not yet ready for hot standby */
    1830             :         else
    1831           0 :             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       27942 :     if (!connsAllowed && backend_type == B_BACKEND)
    1839           0 :         return CAC_SHUTDOWN;    /* shutdown is pending */
    1840             : 
    1841       27942 :     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       40968 : ClosePostmasterPorts(bool am_syslogger)
    1856             : {
    1857             :     /* Release resources held by the postmaster's WaitEventSet. */
    1858       40968 :     if (pm_wait_set)
    1859             :     {
    1860       34372 :         FreeWaitEventSetAfterFork(pm_wait_set);
    1861       34372 :         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       40968 :     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       40968 :     postmaster_alive_fds[POSTMASTER_FD_OWN] = -1;
    1876             :     /* Notify fd.c that we released one pipe FD. */
    1877       40968 :     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       40968 :     if (ListenSockets)
    1889             :     {
    1890       82996 :         for (int i = 0; i < NumListenSockets; i++)
    1891             :         {
    1892       42030 :             if (closesocket(ListenSockets[i]) != 0)
    1893           0 :                 elog(LOG, "could not close listen socket: %m");
    1894             :         }
    1895       40966 :         pfree(ListenSockets);
    1896             :     }
    1897       40968 :     NumListenSockets = 0;
    1898       40968 :     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       40968 :     if (!am_syslogger)
    1906             :     {
    1907             : #ifndef WIN32
    1908       40966 :         if (syslogPipe[0] >= 0)
    1909          34 :             close(syslogPipe[0]);
    1910       40966 :         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       40968 : }
    1924             : 
    1925             : 
    1926             : /*
    1927             :  * InitProcessGlobals -- set MyStartTime[stamp], random seeds
    1928             :  *
    1929             :  * Called early in the postmaster and every backend.
    1930             :  */
    1931             : void
    1932       43136 : InitProcessGlobals(void)
    1933             : {
    1934       43136 :     MyStartTimestamp = GetCurrentTimestamp();
    1935       43136 :     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       43136 :     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       43136 :     srandom(pg_prng_uint32(&pg_global_prng_state));
    1966             : #endif
    1967       43136 : }
    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      172928 : handle_pm_pmsignal_signal(SIGNAL_ARGS)
    1975             : {
    1976      172928 :     pending_pm_pmsignal = true;
    1977      172928 :     SetLatch(MyLatch);
    1978      172928 : }
    1979             : 
    1980             : /*
    1981             :  * pg_ctl uses SIGHUP to request a reload of the configuration files.
    1982             :  */
    1983             : static void
    1984         266 : handle_pm_reload_request_signal(SIGNAL_ARGS)
    1985             : {
    1986         266 :     pending_pm_reload_request = true;
    1987         266 :     SetLatch(MyLatch);
    1988         266 : }
    1989             : 
    1990             : /*
    1991             :  * Re-read config files, and tell children to do same.
    1992             :  */
    1993             : static void
    1994         266 : process_pm_reload_request(void)
    1995             : {
    1996         266 :     pending_pm_reload_request = false;
    1997             : 
    1998         266 :     ereport(DEBUG2,
    1999             :             (errmsg_internal("postmaster received reload request signal")));
    2000             : 
    2001         266 :     if (Shutdown <= SmartShutdown)
    2002             :     {
    2003         266 :         ereport(LOG,
    2004             :                 (errmsg("received SIGHUP, reloading configuration files")));
    2005         266 :         ProcessConfigFile(PGC_SIGHUP);
    2006         266 :         SignalChildren(SIGHUP, btmask_all_except(B_DEAD_END_BACKEND));
    2007             : 
    2008             :         /* Reload authentication config files too */
    2009         266 :         if (!load_hba())
    2010           0 :             ereport(LOG,
    2011             :             /* translator: %s is a configuration file */
    2012             :                     (errmsg("%s was not reloaded", HbaFileName)));
    2013             : 
    2014         266 :         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         266 :         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         264 :             secure_destroy();
    2031         264 :             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         266 : }
    2041             : 
    2042             : /*
    2043             :  * pg_ctl uses SIGTERM, SIGINT and SIGQUIT to request different types of
    2044             :  * shutdown.
    2045             :  */
    2046             : static void
    2047        1666 : handle_pm_shutdown_request_signal(SIGNAL_ARGS)
    2048             : {
    2049        1666 :     switch (postgres_signal_arg)
    2050             :     {
    2051          76 :         case SIGTERM:
    2052             :             /* smart is implied if the other two flags aren't set */
    2053          76 :             pending_pm_shutdown_request = true;
    2054          76 :             break;
    2055         922 :         case SIGINT:
    2056         922 :             pending_pm_fast_shutdown_request = true;
    2057         922 :             pending_pm_shutdown_request = true;
    2058         922 :             break;
    2059         668 :         case SIGQUIT:
    2060         668 :             pending_pm_immediate_shutdown_request = true;
    2061         668 :             pending_pm_shutdown_request = true;
    2062         668 :             break;
    2063             :     }
    2064        1666 :     SetLatch(MyLatch);
    2065        1666 : }
    2066             : 
    2067             : /*
    2068             :  * Process shutdown request.
    2069             :  */
    2070             : static void
    2071        1666 : process_pm_shutdown_request(void)
    2072             : {
    2073             :     int         mode;
    2074             : 
    2075        1666 :     ereport(DEBUG2,
    2076             :             (errmsg_internal("postmaster received shutdown request signal")));
    2077             : 
    2078        1666 :     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        1666 :     if (pending_pm_immediate_shutdown_request)
    2086             :     {
    2087         668 :         pending_pm_immediate_shutdown_request = false;
    2088         668 :         pending_pm_fast_shutdown_request = false;
    2089         668 :         mode = ImmediateShutdown;
    2090             :     }
    2091         998 :     else if (pending_pm_fast_shutdown_request)
    2092             :     {
    2093         922 :         pending_pm_fast_shutdown_request = false;
    2094         922 :         mode = FastShutdown;
    2095             :     }
    2096             :     else
    2097          76 :         mode = SmartShutdown;
    2098             : 
    2099        1666 :     switch (mode)
    2100             :     {
    2101          76 :         case SmartShutdown:
    2102             : 
    2103             :             /*
    2104             :              * Smart Shutdown:
    2105             :              *
    2106             :              * Wait for children to end their work, then shut down.
    2107             :              */
    2108          76 :             if (Shutdown >= SmartShutdown)
    2109           0 :                 break;
    2110          76 :             Shutdown = SmartShutdown;
    2111          76 :             ereport(LOG,
    2112             :                     (errmsg("received smart shutdown request")));
    2113             : 
    2114             :             /* Report status */
    2115          76 :             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          76 :             if (pmState == PM_RUN || pmState == PM_HOT_STANDBY)
    2126          76 :                 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          76 :             PostmasterStateMachine();
    2139          76 :             break;
    2140             : 
    2141         922 :         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         922 :             if (Shutdown >= FastShutdown)
    2150           0 :                 break;
    2151         922 :             Shutdown = FastShutdown;
    2152         922 :             ereport(LOG,
    2153             :                     (errmsg("received fast shutdown request")));
    2154             : 
    2155             :             /* Report status */
    2156         922 :             AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STOPPING);
    2157             : #ifdef USE_SYSTEMD
    2158             :             sd_notify(0, "STOPPING=1");
    2159             : #endif
    2160             : 
    2161         922 :             if (pmState == PM_STARTUP || pmState == PM_RECOVERY)
    2162             :             {
    2163             :                 /* Just shut down background processes silently */
    2164           0 :                 UpdatePMState(PM_STOP_BACKENDS);
    2165             :             }
    2166         922 :             else if (pmState == PM_RUN ||
    2167         108 :                      pmState == PM_HOT_STANDBY)
    2168             :             {
    2169             :                 /* Report that we're about to zap live client sessions */
    2170         922 :                 ereport(LOG,
    2171             :                         (errmsg("aborting any active transactions")));
    2172         922 :                 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         922 :             PostmasterStateMachine();
    2180         922 :             break;
    2181             : 
    2182         668 :         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         668 :             if (Shutdown >= ImmediateShutdown)
    2192           0 :                 break;
    2193         668 :             Shutdown = ImmediateShutdown;
    2194         668 :             ereport(LOG,
    2195             :                     (errmsg("received immediate shutdown request")));
    2196             : 
    2197             :             /* Report status */
    2198         668 :             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         668 :             SetQuitSignalReason(PMQUIT_FOR_STOP);
    2206         668 :             TerminateChildren(SIGQUIT);
    2207         668 :             UpdatePMState(PM_WAIT_BACKENDS);
    2208             : 
    2209             :             /* set stopwatch for them to die */
    2210         668 :             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         668 :             PostmasterStateMachine();
    2217         668 :             break;
    2218             :     }
    2219        1666 : }
    2220             : 
    2221             : static void
    2222       42438 : handle_pm_child_exit_signal(SIGNAL_ARGS)
    2223             : {
    2224       42438 :     pending_pm_child_exit = true;
    2225       42438 :     SetLatch(MyLatch);
    2226       42438 : }
    2227             : 
    2228             : /*
    2229             :  * Cleanup after a child process dies.
    2230             :  */
    2231             : static void
    2232       41988 : process_pm_child_exit(void)
    2233             : {
    2234             :     int         pid;            /* process id of dead child process */
    2235             :     int         exitstatus;     /* its exit status */
    2236             : 
    2237       41988 :     pending_pm_child_exit = false;
    2238             : 
    2239       41988 :     ereport(DEBUG4,
    2240             :             (errmsg_internal("reaping dead processes")));
    2241             : 
    2242       88756 :     while ((pid = waitpid(-1, &exitstatus, WNOHANG)) > 0)
    2243             :     {
    2244             :         PMChild    *pmchild;
    2245             : 
    2246             :         /*
    2247             :          * Check if this child was a startup process.
    2248             :          */
    2249       46770 :         if (StartupPMChild && pid == StartupPMChild->pid)
    2250             :         {
    2251        1690 :             ReleasePostmasterChildSlot(StartupPMChild);
    2252        1690 :             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        1690 :             if (Shutdown > NoShutdown &&
    2259         198 :                 (EXIT_STATUS_0(exitstatus) || EXIT_STATUS_1(exitstatus)))
    2260             :             {
    2261         108 :                 StartupStatus = STARTUP_NOT_RUNNING;
    2262         108 :                 UpdatePMState(PM_WAIT_BACKENDS);
    2263             :                 /* PostmasterStateMachine logic does the rest */
    2264         108 :                 continue;
    2265             :             }
    2266             : 
    2267        1582 :             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        1582 :             if (pmState == PM_STARTUP &&
    2285        1198 :                 StartupStatus != STARTUP_SIGNALED &&
    2286        1198 :                 !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        1580 :             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        1484 :             StartupStatus = STARTUP_NOT_RUNNING;
    2332        1484 :             FatalError = false;
    2333        1484 :             AbortStartTime = 0;
    2334        1484 :             ReachedNormalRunning = true;
    2335        1484 :             UpdatePMState(PM_RUN);
    2336        1484 :             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        1484 :             StartWorkerNeeded = true;
    2344             : 
    2345             :             /* at this point we are really open for business */
    2346        1484 :             ereport(LOG,
    2347             :                     (errmsg("database system is ready to accept connections")));
    2348             : 
    2349             :             /* Report status */
    2350        1484 :             AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_READY);
    2351             : #ifdef USE_SYSTEMD
    2352             :             sd_notify(0, "READY=1");
    2353             : #endif
    2354             : 
    2355        1484 :             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       45080 :         if (BgWriterPMChild && pid == BgWriterPMChild->pid)
    2364             :         {
    2365        1682 :             ReleasePostmasterChildSlot(BgWriterPMChild);
    2366        1682 :             BgWriterPMChild = NULL;
    2367        1682 :             if (!EXIT_STATUS_0(exitstatus))
    2368         684 :                 HandleChildCrash(pid, exitstatus,
    2369         684 :                                  _("background writer process"));
    2370        1682 :             continue;
    2371             :         }
    2372             : 
    2373             :         /*
    2374             :          * Was it the checkpointer?
    2375             :          */
    2376       43398 :         if (CheckpointerPMChild && pid == CheckpointerPMChild->pid)
    2377             :         {
    2378        1682 :             ReleasePostmasterChildSlot(CheckpointerPMChild);
    2379        1682 :             CheckpointerPMChild = NULL;
    2380        1682 :             if (EXIT_STATUS_0(exitstatus) && pmState == PM_WAIT_CHECKPOINTER)
    2381         998 :             {
    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         998 :                 UpdatePMState(PM_WAIT_DEAD_END);
    2392         998 :                 ConfigurePostmasterWaitSet(false);
    2393         998 :                 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         684 :                 HandleChildCrash(pid, exitstatus,
    2402         684 :                                  _("checkpointer process"));
    2403             :             }
    2404             : 
    2405        1682 :             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       41716 :         if (WalWriterPMChild && pid == WalWriterPMChild->pid)
    2414             :         {
    2415        1478 :             ReleasePostmasterChildSlot(WalWriterPMChild);
    2416        1478 :             WalWriterPMChild = NULL;
    2417        1478 :             if (!EXIT_STATUS_0(exitstatus))
    2418         588 :                 HandleChildCrash(pid, exitstatus,
    2419         588 :                                  _("WAL writer process"));
    2420        1478 :             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       40238 :         if (WalReceiverPMChild && pid == WalReceiverPMChild->pid)
    2430             :         {
    2431         482 :             ReleasePostmasterChildSlot(WalReceiverPMChild);
    2432         482 :             WalReceiverPMChild = NULL;
    2433         482 :             if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
    2434          32 :                 HandleChildCrash(pid, exitstatus,
    2435          32 :                                  _("WAL receiver process"));
    2436         482 :             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       39756 :         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       39718 :         if (AutoVacLauncherPMChild && pid == AutoVacLauncherPMChild->pid)
    2461             :         {
    2462        1242 :             ReleasePostmasterChildSlot(AutoVacLauncherPMChild);
    2463        1242 :             AutoVacLauncherPMChild = NULL;
    2464        1242 :             if (!EXIT_STATUS_0(exitstatus))
    2465         500 :                 HandleChildCrash(pid, exitstatus,
    2466         500 :                                  _("autovacuum launcher process"));
    2467        1242 :             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       38476 :         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       38366 :         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       38366 :         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       38358 :         if (maybe_reap_io_worker(pid))
    2521             :         {
    2522        5112 :             if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
    2523        2052 :                 HandleChildCrash(pid, exitstatus, _("io worker"));
    2524             : 
    2525        5112 :             maybe_adjust_io_workers();
    2526        5112 :             continue;
    2527             :         }
    2528             : 
    2529             :         /*
    2530             :          * Was it a backend or a background worker?
    2531             :          */
    2532       33246 :         pmchild = FindPostmasterChildByPid(pid);
    2533       33246 :         if (pmchild)
    2534             :         {
    2535       33246 :             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       41986 :     PostmasterStateMachine();
    2556       40314 : }
    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       33246 : CleanupBackend(PMChild *bp,
    2566             :                int exitstatus)  /* child's exit status. */
    2567             : {
    2568             :     char        namebuf[MAXPGPATH];
    2569             :     const char *procname;
    2570       33246 :     bool        crashed = false;
    2571       33246 :     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       33246 :     if (bp->bkend_type == B_BG_WORKER)
    2579             :     {
    2580        5022 :         snprintf(namebuf, MAXPGPATH, _("background worker \"%s\""),
    2581        5022 :                  bp->rw->rw_worker.bgw_type);
    2582        5022 :         procname = namebuf;
    2583             :     }
    2584             :     else
    2585       28224 :         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       33246 :     if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
    2594        1114 :         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       33246 :     bp_pid = bp->pid;
    2620       33246 :     bp_bgworker_notify = bp->bgworker_notify;
    2621       33246 :     bp_bkend_type = bp->bkend_type;
    2622       33246 :     rw = bp->rw;
    2623       33246 :     if (!ReleasePostmasterChildSlot(bp))
    2624             :     {
    2625             :         /*
    2626             :          * Uh-oh, the child failed to clean itself up.  Treat as a crash after
    2627             :          * all.
    2628             :          */
    2629         732 :         crashed = true;
    2630             :     }
    2631       33246 :     bp = NULL;
    2632             : 
    2633       33246 :     if (crashed)
    2634             :     {
    2635        1114 :         HandleChildCrash(bp_pid, exitstatus, procname);
    2636        1114 :         return;
    2637             :     }
    2638             : 
    2639             :     /*
    2640             :      * This backend may have been slated to receive SIGUSR1 when some
    2641             :      * background worker started or stopped.  Cancel those notifications, as
    2642             :      * we don't want to signal PIDs that are not PostgreSQL backends.  This
    2643             :      * gets skipped in the (probably very common) case where the backend has
    2644             :      * never requested any such notifications.
    2645             :      */
    2646       32132 :     if (bp_bgworker_notify)
    2647         486 :         BackgroundWorkerStopNotifications(bp_pid);
    2648             : 
    2649             :     /*
    2650             :      * If it was a background worker, also update its RegisteredBgWorker
    2651             :      * entry.
    2652             :      */
    2653       32132 :     if (bp_bkend_type == B_BG_WORKER)
    2654             :     {
    2655        4402 :         if (!EXIT_STATUS_0(exitstatus))
    2656             :         {
    2657             :             /* Record timestamp, so we know when to restart the worker. */
    2658        1206 :             rw->rw_crashed_at = GetCurrentTimestamp();
    2659             :         }
    2660             :         else
    2661             :         {
    2662             :             /* Zero exit status means terminate */
    2663        3196 :             rw->rw_crashed_at = 0;
    2664        3196 :             rw->rw_terminate = true;
    2665             :         }
    2666             : 
    2667        4402 :         rw->rw_pid = 0;
    2668        4402 :         ReportBackgroundWorkerExit(rw); /* report child death */
    2669             : 
    2670        4402 :         if (!logged)
    2671             :         {
    2672        4402 :             LogChildExit(EXIT_STATUS_0(exitstatus) ? DEBUG1 : LOG,
    2673             :                          procname, bp_pid, exitstatus);
    2674        4402 :             logged = true;
    2675             :         }
    2676             : 
    2677             :         /* have it be restarted */
    2678        4402 :         HaveCrashedWorker = true;
    2679             :     }
    2680             : 
    2681       32132 :     if (!logged)
    2682       27730 :         LogChildExit(DEBUG2, procname, bp_pid, exitstatus);
    2683             : }
    2684             : 
    2685             : /*
    2686             :  * Transition into FatalError state, in response to something bad having
    2687             :  * happened. Commonly the caller will have logged the reason for entering
    2688             :  * FatalError state.
    2689             :  *
    2690             :  * This should only be called when not already in FatalError or
    2691             :  * ImmediateShutdown state.
    2692             :  */
    2693             : static void
    2694          16 : HandleFatalError(QuitSignalReason reason, bool consider_sigabrt)
    2695             : {
    2696             :     int         sigtosend;
    2697             : 
    2698             :     Assert(!FatalError);
    2699             :     Assert(Shutdown != ImmediateShutdown);
    2700             : 
    2701          16 :     SetQuitSignalReason(reason);
    2702             : 
    2703          16 :     if (consider_sigabrt && send_abort_for_crash)
    2704           0 :         sigtosend = SIGABRT;
    2705             :     else
    2706          16 :         sigtosend = SIGQUIT;
    2707             : 
    2708             :     /*
    2709             :      * Signal all other child processes to exit.
    2710             :      *
    2711             :      * We could exclude dead-end children here, but at least when sending
    2712             :      * SIGABRT it seems better to include them.
    2713             :      */
    2714          16 :     TerminateChildren(sigtosend);
    2715             : 
    2716          16 :     FatalError = true;
    2717             : 
    2718             :     /*
    2719             :      * Choose the appropriate new state to react to the fatal error. Unless we
    2720             :      * were already in the process of shutting down, we go through
    2721             :      * PM_WAIT_BACKENDS. For errors during the shutdown sequence, we directly
    2722             :      * switch to PM_WAIT_DEAD_END.
    2723             :      */
    2724          16 :     switch (pmState)
    2725             :     {
    2726           0 :         case PM_INIT:
    2727             :             /* shouldn't have any children */
    2728             :             Assert(false);
    2729           0 :             break;
    2730           0 :         case PM_STARTUP:
    2731             :             /* should have been handled in process_pm_child_exit */
    2732             :             Assert(false);
    2733           0 :             break;
    2734             : 
    2735             :             /* wait for children to die */
    2736          16 :         case PM_RECOVERY:
    2737             :         case PM_HOT_STANDBY:
    2738             :         case PM_RUN:
    2739             :         case PM_STOP_BACKENDS:
    2740          16 :             UpdatePMState(PM_WAIT_BACKENDS);
    2741          16 :             break;
    2742             : 
    2743           0 :         case PM_WAIT_BACKENDS:
    2744             :             /* there might be more backends to wait for */
    2745           0 :             break;
    2746             : 
    2747           0 :         case PM_WAIT_XLOG_SHUTDOWN:
    2748             :         case PM_WAIT_XLOG_ARCHIVAL:
    2749             :         case PM_WAIT_CHECKPOINTER:
    2750             :         case PM_WAIT_IO_WORKERS:
    2751             : 
    2752             :             /*
    2753             :              * NB: Similar code exists in PostmasterStateMachine()'s handling
    2754             :              * of FatalError in PM_STOP_BACKENDS/PM_WAIT_BACKENDS states.
    2755             :              */
    2756           0 :             ConfigurePostmasterWaitSet(false);
    2757           0 :             UpdatePMState(PM_WAIT_DEAD_END);
    2758           0 :             break;
    2759             : 
    2760           0 :         case PM_WAIT_DEAD_END:
    2761             :         case PM_NO_CHILDREN:
    2762           0 :             break;
    2763             :     }
    2764             : 
    2765             :     /*
    2766             :      * .. and if this doesn't happen quickly enough, now the clock is ticking
    2767             :      * for us to kill them without mercy.
    2768             :      */
    2769          16 :     if (AbortStartTime == 0)
    2770          16 :         AbortStartTime = time(NULL);
    2771          16 : }
    2772             : 
    2773             : /*
    2774             :  * HandleChildCrash -- cleanup after failed backend, bgwriter, checkpointer,
    2775             :  * walwriter, autovacuum, archiver, slot sync worker, or background worker.
    2776             :  *
    2777             :  * The objectives here are to clean up our local state about the child
    2778             :  * process, and to signal all other remaining children to quickdie.
    2779             :  *
    2780             :  * The caller has already released its PMChild slot.
    2781             :  */
    2782             : static void
    2783        5866 : HandleChildCrash(int pid, int exitstatus, const char *procname)
    2784             : {
    2785             :     /*
    2786             :      * We only log messages and send signals if this is the first process
    2787             :      * crash and we're not doing an immediate shutdown; otherwise, we're only
    2788             :      * here to update postmaster's idea of live processes.  If we have already
    2789             :      * signaled children, nonzero exit status is to be expected, so don't
    2790             :      * clutter log.
    2791             :      */
    2792        5866 :     if (FatalError || Shutdown == ImmediateShutdown)
    2793        5850 :         return;
    2794             : 
    2795          16 :     LogChildExit(LOG, procname, pid, exitstatus);
    2796          16 :     ereport(LOG,
    2797             :             (errmsg("terminating any other active server processes")));
    2798             : 
    2799             :     /*
    2800             :      * Switch into error state. The crashed process has already been removed
    2801             :      * from ActiveChildList.
    2802             :      */
    2803          16 :     HandleFatalError(PMQUIT_FOR_CRASH, true);
    2804             : }
    2805             : 
    2806             : /*
    2807             :  * Log the death of a child process.
    2808             :  */
    2809             : static void
    2810       32150 : LogChildExit(int lev, const char *procname, int pid, int exitstatus)
    2811             : {
    2812             :     /*
    2813             :      * size of activity_buffer is arbitrary, but set equal to default
    2814             :      * track_activity_query_size
    2815             :      */
    2816             :     char        activity_buffer[1024];
    2817       32150 :     const char *activity = NULL;
    2818             : 
    2819       32150 :     if (!EXIT_STATUS_0(exitstatus))
    2820        1810 :         activity = pgstat_get_crashed_backend_activity(pid,
    2821             :                                                        activity_buffer,
    2822             :                                                        sizeof(activity_buffer));
    2823             : 
    2824       32150 :     if (WIFEXITED(exitstatus))
    2825       32142 :         ereport(lev,
    2826             : 
    2827             :         /*------
    2828             :           translator: %s is a noun phrase describing a child process, such as
    2829             :           "server process" */
    2830             :                 (errmsg("%s (PID %d) exited with exit code %d",
    2831             :                         procname, pid, WEXITSTATUS(exitstatus)),
    2832             :                  activity ? errdetail("Failed process was running: %s", activity) : 0));
    2833           8 :     else if (WIFSIGNALED(exitstatus))
    2834             :     {
    2835             : #if defined(WIN32)
    2836             :         ereport(lev,
    2837             : 
    2838             :         /*------
    2839             :           translator: %s is a noun phrase describing a child process, such as
    2840             :           "server process" */
    2841             :                 (errmsg("%s (PID %d) was terminated by exception 0x%X",
    2842             :                         procname, pid, WTERMSIG(exitstatus)),
    2843             :                  errhint("See C include file \"ntstatus.h\" for a description of the hexadecimal value."),
    2844             :                  activity ? errdetail("Failed process was running: %s", activity) : 0));
    2845             : #else
    2846           8 :         ereport(lev,
    2847             : 
    2848             :         /*------
    2849             :           translator: %s is a noun phrase describing a child process, such as
    2850             :           "server process" */
    2851             :                 (errmsg("%s (PID %d) was terminated by signal %d: %s",
    2852             :                         procname, pid, WTERMSIG(exitstatus),
    2853             :                         pg_strsignal(WTERMSIG(exitstatus))),
    2854             :                  activity ? errdetail("Failed process was running: %s", activity) : 0));
    2855             : #endif
    2856             :     }
    2857             :     else
    2858           0 :         ereport(lev,
    2859             : 
    2860             :         /*------
    2861             :           translator: %s is a noun phrase describing a child process, such as
    2862             :           "server process" */
    2863             :                 (errmsg("%s (PID %d) exited with unrecognized status %d",
    2864             :                         procname, pid, exitstatus),
    2865             :                  activity ? errdetail("Failed process was running: %s", activity) : 0));
    2866       32150 : }
    2867             : 
    2868             : /*
    2869             :  * Advance the postmaster's state machine and take actions as appropriate
    2870             :  *
    2871             :  * This is common code for process_pm_shutdown_request(),
    2872             :  * process_pm_child_exit() and process_pm_pmsignal(), which process the signals
    2873             :  * that might mean we need to change state.
    2874             :  */
    2875             : static void
    2876       46958 : PostmasterStateMachine(void)
    2877             : {
    2878             :     /* If we're doing a smart shutdown, try to advance that state. */
    2879       46958 :     if (pmState == PM_RUN || pmState == PM_HOT_STANDBY)
    2880             :     {
    2881       33546 :         if (!connsAllowed)
    2882             :         {
    2883             :             /*
    2884             :              * This state ends when we have no normal client backends running.
    2885             :              * Then we're ready to stop other children.
    2886             :              */
    2887         152 :             if (CountChildren(btmask(B_BACKEND)) == 0)
    2888          76 :                 UpdatePMState(PM_STOP_BACKENDS);
    2889             :         }
    2890             :     }
    2891             : 
    2892             :     /*
    2893             :      * In the PM_WAIT_BACKENDS state, wait for all the regular backends and
    2894             :      * processes like autovacuum and background workers that are comparable to
    2895             :      * backends to exit.
    2896             :      *
    2897             :      * PM_STOP_BACKENDS is a transient state that means the same as
    2898             :      * PM_WAIT_BACKENDS, but we signal the processes first, before waiting for
    2899             :      * them.  Treating it as a distinct pmState allows us to share this code
    2900             :      * across multiple shutdown code paths.
    2901             :      */
    2902       46958 :     if (pmState == PM_STOP_BACKENDS || pmState == PM_WAIT_BACKENDS)
    2903             :     {
    2904        8392 :         BackendTypeMask targetMask = BTYPE_MASK_NONE;
    2905             : 
    2906             :         /*
    2907             :          * PM_WAIT_BACKENDS state ends when we have no regular backends, no
    2908             :          * autovac launcher or workers, and no bgworkers (including
    2909             :          * unconnected ones).
    2910             :          */
    2911        8392 :         targetMask = btmask_add(targetMask,
    2912             :                                 B_BACKEND,
    2913             :                                 B_AUTOVAC_LAUNCHER,
    2914             :                                 B_AUTOVAC_WORKER,
    2915             :                                 B_BG_WORKER);
    2916             : 
    2917             :         /*
    2918             :          * No walwriter, bgwriter, slot sync worker, or WAL summarizer either.
    2919             :          */
    2920        8392 :         targetMask = btmask_add(targetMask,
    2921             :                                 B_WAL_WRITER,
    2922             :                                 B_BG_WRITER,
    2923             :                                 B_SLOTSYNC_WORKER,
    2924             :                                 B_WAL_SUMMARIZER);
    2925             : 
    2926             :         /* If we're in recovery, also stop startup and walreceiver procs */
    2927        8392 :         targetMask = btmask_add(targetMask,
    2928             :                                 B_STARTUP,
    2929             :                                 B_WAL_RECEIVER);
    2930             : 
    2931             :         /*
    2932             :          * If we are doing crash recovery or an immediate shutdown then we
    2933             :          * expect archiver, checkpointer, io workers and walsender to exit as
    2934             :          * well, otherwise not.
    2935             :          */
    2936        8392 :         if (FatalError || Shutdown >= ImmediateShutdown)
    2937        3296 :             targetMask = btmask_add(targetMask,
    2938             :                                     B_CHECKPOINTER,
    2939             :                                     B_ARCHIVER,
    2940             :                                     B_IO_WORKER,
    2941             :                                     B_WAL_SENDER);
    2942             : 
    2943             :         /*
    2944             :          * Normally archiver, checkpointer, IO workers and walsenders will
    2945             :          * continue running; they will be terminated later after writing the
    2946             :          * checkpoint record.  We also let dead-end children to keep running
    2947             :          * for now.  The syslogger process exits last.
    2948             :          *
    2949             :          * This assertion checks that we have covered all backend types,
    2950             :          * either by including them in targetMask, or by noting here that they
    2951             :          * are allowed to continue running.
    2952             :          */
    2953             : #ifdef USE_ASSERT_CHECKING
    2954             :         {
    2955             :             BackendTypeMask remainMask = BTYPE_MASK_NONE;
    2956             : 
    2957             :             remainMask = btmask_add(remainMask,
    2958             :                                     B_DEAD_END_BACKEND,
    2959             :                                     B_LOGGER);
    2960             : 
    2961             :             /*
    2962             :              * Archiver, checkpointer, IO workers, and walsender may or may
    2963             :              * not be in targetMask already.
    2964             :              */
    2965             :             remainMask = btmask_add(remainMask,
    2966             :                                     B_ARCHIVER,
    2967             :                                     B_CHECKPOINTER,
    2968             :                                     B_IO_WORKER,
    2969             :                                     B_WAL_SENDER);
    2970             : 
    2971             :             /* these are not real postmaster children */
    2972             :             remainMask = btmask_add(remainMask,
    2973             :                                     B_INVALID,
    2974             :                                     B_STANDALONE_BACKEND);
    2975             : 
    2976             :             /* All types should be included in targetMask or remainMask */
    2977             :             Assert((remainMask.mask | targetMask.mask) == BTYPE_MASK_ALL.mask);
    2978             :         }
    2979             : #endif
    2980             : 
    2981             :         /* If we had not yet signaled the processes to exit, do so now */
    2982        8392 :         if (pmState == PM_STOP_BACKENDS)
    2983             :         {
    2984             :             /*
    2985             :              * Forget any pending requests for background workers, since we're
    2986             :              * no longer willing to launch any new workers.  (If additional
    2987             :              * requests arrive, BackgroundWorkerStateChange will reject them.)
    2988             :              */
    2989         998 :             ForgetUnstartedBackgroundWorkers();
    2990             : 
    2991         998 :             SignalChildren(SIGTERM, targetMask);
    2992             : 
    2993         998 :             UpdatePMState(PM_WAIT_BACKENDS);
    2994             :         }
    2995             : 
    2996             :         /* Are any of the target processes still running? */
    2997        8392 :         if (CountChildren(targetMask) == 0)
    2998             :         {
    2999        1682 :             if (Shutdown >= ImmediateShutdown || FatalError)
    3000             :             {
    3001             :                 /*
    3002             :                  * Stop any dead-end children and stop creating new ones.
    3003             :                  *
    3004             :                  * NB: Similar code exists in HandleFatalError(), when the
    3005             :                  * error happens in pmState > PM_WAIT_BACKENDS.
    3006             :                  */
    3007         684 :                 UpdatePMState(PM_WAIT_DEAD_END);
    3008         684 :                 ConfigurePostmasterWaitSet(false);
    3009         684 :                 SignalChildren(SIGQUIT, btmask(B_DEAD_END_BACKEND));
    3010             : 
    3011             :                 /*
    3012             :                  * We already SIGQUIT'd auxiliary processes (other than
    3013             :                  * logger), if any, when we started immediate shutdown or
    3014             :                  * entered FatalError state.
    3015             :                  */
    3016             :             }
    3017             :             else
    3018             :             {
    3019             :                 /*
    3020             :                  * If we get here, we are proceeding with normal shutdown. All
    3021             :                  * the regular children are gone, and it's time to tell the
    3022             :                  * checkpointer to do a shutdown checkpoint.
    3023             :                  */
    3024             :                 Assert(Shutdown > NoShutdown);
    3025             :                 /* Start the checkpointer if not running */
    3026         998 :                 if (CheckpointerPMChild == NULL)
    3027           0 :                     CheckpointerPMChild = StartChildProcess(B_CHECKPOINTER);
    3028             :                 /* And tell it to write the shutdown checkpoint */
    3029         998 :                 if (CheckpointerPMChild != NULL)
    3030             :                 {
    3031         998 :                     signal_child(CheckpointerPMChild, SIGINT);
    3032         998 :                     UpdatePMState(PM_WAIT_XLOG_SHUTDOWN);
    3033             :                 }
    3034             :                 else
    3035             :                 {
    3036             :                     /*
    3037             :                      * If we failed to fork a checkpointer, just shut down.
    3038             :                      * Any required cleanup will happen at next restart. We
    3039             :                      * set FatalError so that an "abnormal shutdown" message
    3040             :                      * gets logged when we exit.
    3041             :                      *
    3042             :                      * We don't consult send_abort_for_crash here, as it's
    3043             :                      * unlikely that dumping cores would illuminate the reason
    3044             :                      * for checkpointer fork failure.
    3045             :                      *
    3046             :                      * XXX: It may be worth to introduce a different PMQUIT
    3047             :                      * value that signals that the cluster is in a bad state,
    3048             :                      * without a process having crashed. But right now this
    3049             :                      * path is very unlikely to be reached, so it isn't
    3050             :                      * obviously worthwhile adding a distinct error message in
    3051             :                      * quickdie().
    3052             :                      */
    3053           0 :                     HandleFatalError(PMQUIT_FOR_CRASH, false);
    3054             :                 }
    3055             :             }
    3056             :         }
    3057             :     }
    3058             : 
    3059             :     /*
    3060             :      * The state transition from PM_WAIT_XLOG_SHUTDOWN to
    3061             :      * PM_WAIT_XLOG_ARCHIVAL is in process_pm_pmsignal(), in response to
    3062             :      * PMSIGNAL_XLOG_IS_SHUTDOWN.
    3063             :      */
    3064             : 
    3065       46958 :     if (pmState == PM_WAIT_XLOG_ARCHIVAL)
    3066             :     {
    3067             :         /*
    3068             :          * PM_WAIT_XLOG_ARCHIVAL state ends when there are no children other
    3069             :          * than checkpointer, io workers and dead-end children left. There
    3070             :          * shouldn't be any regular backends left by now anyway; what we're
    3071             :          * really waiting for is for walsenders and archiver to exit.
    3072             :          */
    3073        1094 :         if (CountChildren(btmask_all_except(B_CHECKPOINTER, B_IO_WORKER,
    3074             :                                             B_LOGGER, B_DEAD_END_BACKEND)) == 0)
    3075             :         {
    3076         998 :             UpdatePMState(PM_WAIT_IO_WORKERS);
    3077         998 :             SignalChildren(SIGUSR2, btmask(B_IO_WORKER));
    3078             :         }
    3079             :     }
    3080             : 
    3081       46958 :     if (pmState == PM_WAIT_IO_WORKERS)
    3082             :     {
    3083             :         /*
    3084             :          * PM_WAIT_IO_WORKERS state ends when there's only checkpointer and
    3085             :          * dead-end children left.
    3086             :          */
    3087        3900 :         if (io_worker_count == 0)
    3088             :         {
    3089         998 :             UpdatePMState(PM_WAIT_CHECKPOINTER);
    3090             : 
    3091             :             /*
    3092             :              * Now that the processes mentioned above are gone, tell
    3093             :              * checkpointer to shut down too. That allows checkpointer to
    3094             :              * perform some last bits of cleanup without other processes
    3095             :              * interfering.
    3096             :              */
    3097         998 :             if (CheckpointerPMChild != NULL)
    3098         998 :                 signal_child(CheckpointerPMChild, SIGUSR2);
    3099             :         }
    3100             :     }
    3101             : 
    3102             :     /*
    3103             :      * The state transition from PM_WAIT_CHECKPOINTER to PM_WAIT_DEAD_END is
    3104             :      * in process_pm_child_exit().
    3105             :      */
    3106             : 
    3107       46958 :     if (pmState == PM_WAIT_DEAD_END)
    3108             :     {
    3109             :         /*
    3110             :          * PM_WAIT_DEAD_END state ends when all other children are gone except
    3111             :          * for the logger.  During normal shutdown, all that remains are
    3112             :          * dead-end backends, but in FatalError processing we jump straight
    3113             :          * here with more processes remaining.  Note that they have already
    3114             :          * been sent appropriate shutdown signals, either during a normal
    3115             :          * state transition leading up to PM_WAIT_DEAD_END, or during
    3116             :          * FatalError processing.
    3117             :          *
    3118             :          * The reason we wait is to protect against a new postmaster starting
    3119             :          * conflicting subprocesses; this isn't an ironclad protection, but it
    3120             :          * at least helps in the shutdown-and-immediately-restart scenario.
    3121             :          */
    3122        1718 :         if (CountChildren(btmask_all_except(B_LOGGER)) == 0)
    3123             :         {
    3124             :             /* These other guys should be dead already */
    3125             :             Assert(StartupPMChild == NULL);
    3126             :             Assert(WalReceiverPMChild == NULL);
    3127             :             Assert(WalSummarizerPMChild == NULL);
    3128             :             Assert(BgWriterPMChild == NULL);
    3129             :             Assert(CheckpointerPMChild == NULL);
    3130             :             Assert(WalWriterPMChild == NULL);
    3131             :             Assert(AutoVacLauncherPMChild == NULL);
    3132             :             Assert(SlotSyncWorkerPMChild == NULL);
    3133             :             /* syslogger is not considered here */
    3134        1682 :             UpdatePMState(PM_NO_CHILDREN);
    3135             :         }
    3136             :     }
    3137             : 
    3138             :     /*
    3139             :      * If we've been told to shut down, we exit as soon as there are no
    3140             :      * remaining children.  If there was a crash, cleanup will occur at the
    3141             :      * next startup.  (Before PostgreSQL 8.3, we tried to recover from the
    3142             :      * crash before exiting, but that seems unwise if we are quitting because
    3143             :      * we got SIGTERM from init --- there may well not be time for recovery
    3144             :      * before init decides to SIGKILL us.)
    3145             :      *
    3146             :      * Note that the syslogger continues to run.  It will exit when it sees
    3147             :      * EOF on its input pipe, which happens when there are no more upstream
    3148             :      * processes.
    3149             :      */
    3150       46958 :     if (Shutdown > NoShutdown && pmState == PM_NO_CHILDREN)
    3151             :     {
    3152        1666 :         if (FatalError)
    3153             :         {
    3154           0 :             ereport(LOG, (errmsg("abnormal database system shutdown")));
    3155           0 :             ExitPostmaster(1);
    3156             :         }
    3157             :         else
    3158             :         {
    3159             :             /*
    3160             :              * Normal exit from the postmaster is here.  We don't need to log
    3161             :              * anything here, since the UnlinkLockFiles proc_exit callback
    3162             :              * will do so, and that should be the last user-visible action.
    3163             :              */
    3164        1666 :             ExitPostmaster(0);
    3165             :         }
    3166             :     }
    3167             : 
    3168             :     /*
    3169             :      * If the startup process failed, or the user does not want an automatic
    3170             :      * restart after backend crashes, wait for all non-syslogger children to
    3171             :      * exit, and then exit postmaster.  We don't try to reinitialize when the
    3172             :      * startup process fails, because more than likely it will just fail again
    3173             :      * and we will keep trying forever.
    3174             :      */
    3175       45292 :     if (pmState == PM_NO_CHILDREN)
    3176             :     {
    3177          16 :         if (StartupStatus == STARTUP_CRASHED)
    3178             :         {
    3179           6 :             ereport(LOG,
    3180             :                     (errmsg("shutting down due to startup process failure")));
    3181           6 :             ExitPostmaster(1);
    3182             :         }
    3183          10 :         if (!restart_after_crash)
    3184             :         {
    3185           0 :             ereport(LOG,
    3186             :                     (errmsg("shutting down because \"restart_after_crash\" is off")));
    3187           0 :             ExitPostmaster(1);
    3188             :         }
    3189             :     }
    3190             : 
    3191             :     /*
    3192             :      * If we need to recover from a crash, wait for all non-syslogger children
    3193             :      * to exit, then reset shmem and start the startup process.
    3194             :      */
    3195       45286 :     if (FatalError && pmState == PM_NO_CHILDREN)
    3196             :     {
    3197          10 :         ereport(LOG,
    3198             :                 (errmsg("all server processes terminated; reinitializing")));
    3199             : 
    3200             :         /* remove leftover temporary files after a crash */
    3201          10 :         if (remove_temp_files_after_crash)
    3202           8 :             RemovePgTempFiles();
    3203             : 
    3204             :         /* allow background workers to immediately restart */
    3205          10 :         ResetBackgroundWorkerCrashTimes();
    3206             : 
    3207          10 :         shmem_exit(1);
    3208             : 
    3209             :         /* re-read control file into local memory */
    3210          10 :         LocalProcessControlFile(true);
    3211             : 
    3212             :         /* re-create shared memory and semaphores */
    3213          10 :         CreateSharedMemoryAndSemaphores();
    3214             : 
    3215          10 :         UpdatePMState(PM_STARTUP);
    3216             : 
    3217             :         /* Make sure we can perform I/O while starting up. */
    3218          10 :         maybe_adjust_io_workers();
    3219             : 
    3220          10 :         StartupPMChild = StartChildProcess(B_STARTUP);
    3221             :         Assert(StartupPMChild != NULL);
    3222          10 :         StartupStatus = STARTUP_RUNNING;
    3223             :         /* crash recovery started, reset SIGKILL flag */
    3224          10 :         AbortStartTime = 0;
    3225             : 
    3226             :         /* start accepting server socket connection events again */
    3227          10 :         ConfigurePostmasterWaitSet(true);
    3228             :     }
    3229       45286 : }
    3230             : 
    3231             : static const char *
    3232        1940 : pmstate_name(PMState state)
    3233             : {
    3234             : #define PM_TOSTR_CASE(sym) case sym: return #sym
    3235        1940 :     switch (state)
    3236             :     {
    3237         108 :             PM_TOSTR_CASE(PM_INIT);
    3238         216 :             PM_TOSTR_CASE(PM_STARTUP);
    3239          32 :             PM_TOSTR_CASE(PM_RECOVERY);
    3240          24 :             PM_TOSTR_CASE(PM_HOT_STANDBY);
    3241         202 :             PM_TOSTR_CASE(PM_RUN);
    3242         160 :             PM_TOSTR_CASE(PM_STOP_BACKENDS);
    3243         228 :             PM_TOSTR_CASE(PM_WAIT_BACKENDS);
    3244         160 :             PM_TOSTR_CASE(PM_WAIT_XLOG_SHUTDOWN);
    3245         160 :             PM_TOSTR_CASE(PM_WAIT_XLOG_ARCHIVAL);
    3246         160 :             PM_TOSTR_CASE(PM_WAIT_IO_WORKERS);
    3247         220 :             PM_TOSTR_CASE(PM_WAIT_DEAD_END);
    3248         160 :             PM_TOSTR_CASE(PM_WAIT_CHECKPOINTER);
    3249         110 :             PM_TOSTR_CASE(PM_NO_CHILDREN);
    3250             :     }
    3251             : #undef PM_TOSTR_CASE
    3252             : 
    3253           0 :     pg_unreachable();
    3254             :     return "";                    /* silence compiler */
    3255             : }
    3256             : 
    3257             : /*
    3258             :  * Simple wrapper for updating pmState. The main reason to have this wrapper
    3259             :  * is that it makes it easy to log all state transitions.
    3260             :  */
    3261             : static void
    3262       14108 : UpdatePMState(PMState newState)
    3263             : {
    3264       14108 :     elog(DEBUG1, "updating PMState from %s to %s",
    3265             :          pmstate_name(pmState), pmstate_name(newState));
    3266       14108 :     pmState = newState;
    3267       14108 : }
    3268             : 
    3269             : /*
    3270             :  * Launch background processes after state change, or relaunch after an
    3271             :  * existing process has exited.
    3272             :  *
    3273             :  * Check the current pmState and the status of any background processes.  If
    3274             :  * there are any background processes missing that should be running in the
    3275             :  * current state, but are not, launch them.
    3276             :  */
    3277             : static void
    3278      239092 : LaunchMissingBackgroundProcesses(void)
    3279             : {
    3280             :     /* Syslogger is active in all states */
    3281      239092 :     if (SysLoggerPMChild == NULL && Logging_collector)
    3282           0 :         StartSysLogger();
    3283             : 
    3284             :     /*
    3285             :      * The number of configured workers might have changed, or a prior start
    3286             :      * of a worker might have failed. Check if we need to start/stop any
    3287             :      * workers.
    3288             :      *
    3289             :      * A config file change will always lead to this function being called, so
    3290             :      * we always will process the config change in a timely manner.
    3291             :      */
    3292      239092 :     maybe_adjust_io_workers();
    3293             : 
    3294             :     /*
    3295             :      * The checkpointer and the background writer are active from the start,
    3296             :      * until shutdown is initiated.
    3297             :      *
    3298             :      * (If the checkpointer is not running when we enter the
    3299             :      * PM_WAIT_XLOG_SHUTDOWN state, it is launched one more time to perform
    3300             :      * the shutdown checkpoint.  That's done in PostmasterStateMachine(), not
    3301             :      * here.)
    3302             :      */
    3303      239092 :     if (pmState == PM_RUN || pmState == PM_RECOVERY ||
    3304       15444 :         pmState == PM_HOT_STANDBY || pmState == PM_STARTUP)
    3305             :     {
    3306      227300 :         if (CheckpointerPMChild == NULL)
    3307          10 :             CheckpointerPMChild = StartChildProcess(B_CHECKPOINTER);
    3308      227300 :         if (BgWriterPMChild == NULL)
    3309          10 :             BgWriterPMChild = StartChildProcess(B_BG_WRITER);
    3310             :     }
    3311             : 
    3312             :     /*
    3313             :      * WAL writer is needed only in normal operation (else we cannot be
    3314             :      * writing any new WAL).
    3315             :      */
    3316      239092 :     if (WalWriterPMChild == NULL && pmState == PM_RUN)
    3317        1484 :         WalWriterPMChild = StartChildProcess(B_WAL_WRITER);
    3318             : 
    3319             :     /*
    3320             :      * We don't want autovacuum to run in binary upgrade mode because
    3321             :      * autovacuum might update relfrozenxid for empty tables before the
    3322             :      * physical files are put in place.
    3323             :      */
    3324      257274 :     if (!IsBinaryUpgrade && AutoVacLauncherPMChild == NULL &&
    3325       25526 :         (AutoVacuumingActive() || start_autovac_launcher) &&
    3326       10838 :         pmState == PM_RUN)
    3327             :     {
    3328        1248 :         AutoVacLauncherPMChild = StartChildProcess(B_AUTOVAC_LAUNCHER);
    3329        1246 :         if (AutoVacLauncherPMChild != NULL)
    3330        1246 :             start_autovac_launcher = false; /* signal processed */
    3331             :     }
    3332             : 
    3333             :     /*
    3334             :      * If WAL archiving is enabled always, we are allowed to start archiver
    3335             :      * even during recovery.
    3336             :      */
    3337      239090 :     if (PgArchPMChild == NULL &&
    3338      236966 :         ((XLogArchivingActive() && pmState == PM_RUN) ||
    3339      236966 :          (XLogArchivingAlways() && (pmState == PM_RECOVERY || pmState == PM_HOT_STANDBY))) &&
    3340         104 :         PgArchCanRestart())
    3341         104 :         PgArchPMChild = StartChildProcess(B_ARCHIVER);
    3342             : 
    3343             :     /*
    3344             :      * If we need to start a slot sync worker, try to do that now
    3345             :      *
    3346             :      * We allow to start the slot sync worker when we are on a hot standby,
    3347             :      * fast or immediate shutdown is not in progress, slot sync parameters are
    3348             :      * configured correctly, and it is the first time of worker's launch, or
    3349             :      * enough time has passed since the worker was launched last.
    3350             :      */
    3351      239090 :     if (SlotSyncWorkerPMChild == NULL && pmState == PM_HOT_STANDBY &&
    3352        3384 :         Shutdown <= SmartShutdown && sync_replication_slots &&
    3353          22 :         ValidateSlotSyncParams(LOG) && SlotSyncWorkerCanRestart())
    3354           8 :         SlotSyncWorkerPMChild = StartChildProcess(B_SLOTSYNC_WORKER);
    3355             : 
    3356             :     /*
    3357             :      * If we need to start a WAL receiver, try to do that now
    3358             :      *
    3359             :      * Note: if a walreceiver process is already running, it might seem that
    3360             :      * we should clear WalReceiverRequested.  However, there's a race
    3361             :      * condition if the walreceiver terminates and the startup process
    3362             :      * immediately requests a new one: it's quite possible to get the signal
    3363             :      * for the request before reaping the dead walreceiver process.  Better to
    3364             :      * risk launching an extra walreceiver than to miss launching one we need.
    3365             :      * (The walreceiver code has logic to recognize that it should go away if
    3366             :      * not needed.)
    3367             :      */
    3368      239090 :     if (WalReceiverRequested)
    3369             :     {
    3370         662 :         if (WalReceiverPMChild == NULL &&
    3371         488 :             (pmState == PM_STARTUP || pmState == PM_RECOVERY ||
    3372         486 :              pmState == PM_HOT_STANDBY) &&
    3373         482 :             Shutdown <= SmartShutdown)
    3374             :         {
    3375         482 :             WalReceiverPMChild = StartChildProcess(B_WAL_RECEIVER);
    3376         482 :             if (WalReceiverPMChild != 0)
    3377         482 :                 WalReceiverRequested = false;
    3378             :             /* else leave the flag set, so we'll try again later */
    3379             :         }
    3380             :     }
    3381             : 
    3382             :     /* If we need to start a WAL summarizer, try to do that now */
    3383      239090 :     if (summarize_wal && WalSummarizerPMChild == NULL &&
    3384         132 :         (pmState == PM_RUN || pmState == PM_HOT_STANDBY) &&
    3385          38 :         Shutdown <= SmartShutdown)
    3386          38 :         WalSummarizerPMChild = StartChildProcess(B_WAL_SUMMARIZER);
    3387             : 
    3388             :     /* Get other worker processes running, if needed */
    3389      239090 :     if (StartWorkerNeeded || HaveCrashedWorker)
    3390       12576 :         maybe_start_bgworkers();
    3391      239088 : }
    3392             : 
    3393             : /*
    3394             :  * Return string representation of signal.
    3395             :  *
    3396             :  * Because this is only implemented for signals we already rely on in this
    3397             :  * file we don't need to deal with unimplemented or same-numeric-value signals
    3398             :  * (as we'd e.g. have to for EWOULDBLOCK / EAGAIN).
    3399             :  */
    3400             : static const char *
    3401          32 : pm_signame(int signal)
    3402             : {
    3403             : #define PM_TOSTR_CASE(sym) case sym: return #sym
    3404          32 :     switch (signal)
    3405             :     {
    3406           0 :             PM_TOSTR_CASE(SIGABRT);
    3407           0 :             PM_TOSTR_CASE(SIGCHLD);
    3408           0 :             PM_TOSTR_CASE(SIGHUP);
    3409           4 :             PM_TOSTR_CASE(SIGINT);
    3410           0 :             PM_TOSTR_CASE(SIGKILL);
    3411           0 :             PM_TOSTR_CASE(SIGQUIT);
    3412          18 :             PM_TOSTR_CASE(SIGTERM);
    3413           0 :             PM_TOSTR_CASE(SIGUSR1);
    3414          10 :             PM_TOSTR_CASE(SIGUSR2);
    3415           0 :         default:
    3416             :             /* all signals sent by postmaster should be listed here */
    3417             :             Assert(false);
    3418           0 :             return "(unknown)";
    3419             :     }
    3420             : #undef PM_TOSTR_CASE
    3421             : 
    3422             :     return "";                    /* silence compiler */
    3423             : }
    3424             : 
    3425             : /*
    3426             :  * Send a signal to a postmaster child process
    3427             :  *
    3428             :  * On systems that have setsid(), each child process sets itself up as a
    3429             :  * process group leader.  For signals that are generally interpreted in the
    3430             :  * appropriate fashion, we signal the entire process group not just the
    3431             :  * direct child process.  This allows us to, for example, SIGQUIT a blocked
    3432             :  * archive_recovery script, or SIGINT a script being run by a backend via
    3433             :  * system().
    3434             :  *
    3435             :  * There is a race condition for recently-forked children: they might not
    3436             :  * have executed setsid() yet.  So we signal the child directly as well as
    3437             :  * the group.  We assume such a child will handle the signal before trying
    3438             :  * to spawn any grandchild processes.  We also assume that signaling the
    3439             :  * child twice will not cause any problems.
    3440             :  */
    3441             : static void
    3442       17886 : signal_child(PMChild *pmchild, int signal)
    3443             : {
    3444       17886 :     pid_t       pid = pmchild->pid;
    3445             : 
    3446       17886 :     ereport(DEBUG3,
    3447             :             (errmsg_internal("sending signal %d/%s to %s process with pid %d",
    3448             :                              signal, pm_signame(signal),
    3449             :                              GetBackendTypeDesc(pmchild->bkend_type),
    3450             :                              (int) pmchild->pid)));
    3451             : 
    3452       17886 :     if (kill(pid, signal) < 0)
    3453           0 :         elog(DEBUG3, "kill(%ld,%d) failed: %m", (long) pid, signal);
    3454             : #ifdef HAVE_SETSID
    3455       17886 :     switch (signal)
    3456             :     {
    3457       11210 :         case SIGINT:
    3458             :         case SIGTERM:
    3459             :         case SIGQUIT:
    3460             :         case SIGKILL:
    3461             :         case SIGABRT:
    3462       11210 :             if (kill(-pid, signal) < 0)
    3463          92 :                 elog(DEBUG3, "kill(%ld,%d) failed: %m", (long) (-pid), signal);
    3464       11210 :             break;
    3465        6676 :         default:
    3466        6676 :             break;
    3467             :     }
    3468             : #endif
    3469       17886 : }
    3470             : 
    3471             : /*
    3472             :  * Send a signal to the targeted children.
    3473             :  */
    3474             : static bool
    3475        5626 : SignalChildren(int signal, BackendTypeMask targetMask)
    3476             : {
    3477             :     dlist_iter  iter;
    3478        5626 :     bool        signaled = false;
    3479             : 
    3480       30638 :     dlist_foreach(iter, &ActiveChildList)
    3481             :     {
    3482       25012 :         PMChild    *bp = dlist_container(PMChild, elem, iter.cur);
    3483             : 
    3484             :         /*
    3485             :          * If we need to distinguish between B_BACKEND and B_WAL_SENDER, check
    3486             :          * if any B_BACKEND backends have recently announced that they are
    3487             :          * actually WAL senders.
    3488             :          */
    3489       25012 :         if (btmask_contains(targetMask, B_WAL_SENDER) != btmask_contains(targetMask, B_BACKEND) &&
    3490       12558 :             bp->bkend_type == B_BACKEND)
    3491             :         {
    3492         668 :             if (IsPostmasterChildWalSender(bp->child_slot))
    3493          74 :                 bp->bkend_type = B_WAL_SENDER;
    3494             :         }
    3495             : 
    3496       25012 :         if (!btmask_contains(targetMask, bp->bkend_type))
    3497        9238 :             continue;
    3498             : 
    3499       15774 :         signal_child(bp, signal);
    3500       15774 :         signaled = true;
    3501             :     }
    3502        5626 :     return signaled;
    3503             : }
    3504             : 
    3505             : /*
    3506             :  * Send a termination signal to children.  This considers all of our children
    3507             :  * processes, except syslogger.
    3508             :  */
    3509             : static void
    3510         684 : TerminateChildren(int signal)
    3511             : {
    3512         684 :     SignalChildren(signal, btmask_all_except(B_LOGGER));
    3513         684 :     if (StartupPMChild != NULL)
    3514             :     {
    3515          90 :         if (signal == SIGQUIT || signal == SIGKILL || signal == SIGABRT)
    3516          90 :             StartupStatus = STARTUP_SIGNALED;
    3517             :     }
    3518         684 : }
    3519             : 
    3520             : /*
    3521             :  * BackendStartup -- start backend process
    3522             :  *
    3523             :  * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise.
    3524             :  *
    3525             :  * Note: if you change this code, also consider StartAutovacuumWorker and
    3526             :  * StartBackgroundWorker.
    3527             :  */
    3528             : static int
    3529       25852 : BackendStartup(ClientSocket *client_sock)
    3530             : {
    3531       25852 :     PMChild    *bn = NULL;
    3532             :     pid_t       pid;
    3533             :     BackendStartupData startup_data;
    3534             :     CAC_state   cac;
    3535             : 
    3536             :     /*
    3537             :      * Capture time that Postmaster got a socket from accept (for logging
    3538             :      * connection establishment and setup total duration).
    3539             :      */
    3540       25852 :     startup_data.socket_created = GetCurrentTimestamp();
    3541             : 
    3542             :     /*
    3543             :      * Allocate and assign the child slot.  Note we must do this before
    3544             :      * forking, so that we can handle failures (out of memory or child-process
    3545             :      * slots) cleanly.
    3546             :      */
    3547       25852 :     cac = canAcceptConnections(B_BACKEND);
    3548       25852 :     if (cac == CAC_OK)
    3549             :     {
    3550             :         /* Can change later to B_WAL_SENDER */
    3551       25566 :         bn = AssignPostmasterChildSlot(B_BACKEND);
    3552       25566 :         if (!bn)
    3553             :         {
    3554             :             /*
    3555             :              * Too many regular child processes; launch a dead-end child
    3556             :              * process instead.
    3557             :              */
    3558          56 :             cac = CAC_TOOMANY;
    3559             :         }
    3560             :     }
    3561       25852 :     if (!bn)
    3562             :     {
    3563         342 :         bn = AllocDeadEndChild();
    3564         342 :         if (!bn)
    3565             :         {
    3566           0 :             ereport(LOG,
    3567             :                     (errcode(ERRCODE_OUT_OF_MEMORY),
    3568             :                      errmsg("out of memory")));
    3569           0 :             return STATUS_ERROR;
    3570             :         }
    3571             :     }
    3572             : 
    3573             :     /* Pass down canAcceptConnections state */
    3574       25852 :     startup_data.canAcceptConnections = cac;
    3575       25852 :     bn->rw = NULL;
    3576             : 
    3577             :     /* Hasn't asked to be notified about any bgworkers yet */
    3578       25852 :     bn->bgworker_notify = false;
    3579             : 
    3580       25852 :     pid = postmaster_child_launch(bn->bkend_type, bn->child_slot,
    3581             :                                   &startup_data, sizeof(startup_data),
    3582             :                                   client_sock);
    3583       25850 :     if (pid < 0)
    3584             :     {
    3585             :         /* in parent, fork failed */
    3586           0 :         int         save_errno = errno;
    3587             : 
    3588           0 :         (void) ReleasePostmasterChildSlot(bn);
    3589           0 :         errno = save_errno;
    3590           0 :         ereport(LOG,
    3591             :                 (errmsg("could not fork new process for connection: %m")));
    3592           0 :         report_fork_failure_to_client(client_sock, save_errno);
    3593           0 :         return STATUS_ERROR;
    3594             :     }
    3595             : 
    3596             :     /* in parent, successful fork */
    3597       25850 :     ereport(DEBUG2,
    3598             :             (errmsg_internal("forked new %s, pid=%d socket=%d",
    3599             :                              GetBackendTypeDesc(bn->bkend_type),
    3600             :                              (int) pid, (int) client_sock->sock)));
    3601             : 
    3602             :     /*
    3603             :      * Everything's been successful, it's safe to add this backend to our list
    3604             :      * of backends.
    3605             :      */
    3606       25850 :     bn->pid = pid;
    3607       25850 :     return STATUS_OK;
    3608             : }
    3609             : 
    3610             : /*
    3611             :  * Try to report backend fork() failure to client before we close the
    3612             :  * connection.  Since we do not care to risk blocking the postmaster on
    3613             :  * this connection, we set the connection to non-blocking and try only once.
    3614             :  *
    3615             :  * This is grungy special-purpose code; we cannot use backend libpq since
    3616             :  * it's not up and running.
    3617             :  */
    3618             : static void
    3619           0 : report_fork_failure_to_client(ClientSocket *client_sock, int errnum)
    3620             : {
    3621             :     char        buffer[1000];
    3622             :     int         rc;
    3623             : 
    3624             :     /* Format the error message packet (always V2 protocol) */
    3625           0 :     snprintf(buffer, sizeof(buffer), "E%s%s\n",
    3626             :              _("could not fork new process for connection: "),
    3627             :              strerror(errnum));
    3628             : 
    3629             :     /* Set port to non-blocking.  Don't do send() if this fails */
    3630           0 :     if (!pg_set_noblock(client_sock->sock))
    3631           0 :         return;
    3632             : 
    3633             :     /* We'll retry after EINTR, but ignore all other failures */
    3634             :     do
    3635             :     {
    3636           0 :         rc = send(client_sock->sock, buffer, strlen(buffer) + 1, 0);
    3637           0 :     } while (rc < 0 && errno == EINTR);
    3638             : }
    3639             : 
    3640             : /*
    3641             :  * ExitPostmaster -- cleanup
    3642             :  *
    3643             :  * Do NOT call exit() directly --- always go through here!
    3644             :  */
    3645             : static void
    3646        1678 : ExitPostmaster(int status)
    3647             : {
    3648             : #ifdef HAVE_PTHREAD_IS_THREADED_NP
    3649             : 
    3650             :     /*
    3651             :      * There is no known cause for a postmaster to become multithreaded after
    3652             :      * startup.  However, we might reach here via an error exit before
    3653             :      * reaching the test in PostmasterMain, so provide the same hint as there.
    3654             :      * This message uses LOG level, because an unclean shutdown at this point
    3655             :      * would usually not look much different from a clean shutdown.
    3656             :      */
    3657             :     if (pthread_is_threaded_np() != 0)
    3658             :         ereport(LOG,
    3659             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    3660             :                  errmsg("postmaster became multithreaded"),
    3661             :                  errhint("Set the LC_ALL environment variable to a valid locale.")));
    3662             : #endif
    3663             : 
    3664             :     /* should cleanup shared memory and kill all backends */
    3665             : 
    3666             :     /*
    3667             :      * Not sure of the semantics here.  When the Postmaster dies, should the
    3668             :      * backends all be killed? probably not.
    3669             :      *
    3670             :      * MUST     -- vadim 05-10-1999
    3671             :      */
    3672             : 
    3673        1678 :     proc_exit(status);
    3674             : }
    3675             : 
    3676             : /*
    3677             :  * Handle pmsignal conditions representing requests from backends,
    3678             :  * and check for promote and logrotate requests from pg_ctl.
    3679             :  */
    3680             : static void
    3681      172424 : process_pm_pmsignal(void)
    3682             : {
    3683      172424 :     bool        request_state_update = false;
    3684             : 
    3685      172424 :     pending_pm_pmsignal = false;
    3686             : 
    3687      172424 :     ereport(DEBUG2,
    3688             :             (errmsg_internal("postmaster received pmsignal signal")));
    3689             : 
    3690             :     /*
    3691             :      * RECOVERY_STARTED and BEGIN_HOT_STANDBY signals are ignored in
    3692             :      * unexpected states. If the startup process quickly starts up, completes
    3693             :      * recovery, exits, we might process the death of the startup process
    3694             :      * first. We don't want to go back to recovery in that case.
    3695             :      */
    3696      172424 :     if (CheckPostmasterSignal(PMSIGNAL_RECOVERY_STARTED) &&
    3697         492 :         pmState == PM_STARTUP && Shutdown == NoShutdown)
    3698             :     {
    3699             :         /* WAL redo has started. We're out of reinitialization. */
    3700         492 :         FatalError = false;
    3701         492 :         AbortStartTime = 0;
    3702         492 :         reachedConsistency = false;
    3703             : 
    3704             :         /*
    3705             :          * Start the archiver if we're responsible for (re-)archiving received
    3706             :          * files.
    3707             :          */
    3708             :         Assert(PgArchPMChild == NULL);
    3709         492 :         if (XLogArchivingAlways())
    3710           6 :             PgArchPMChild = StartChildProcess(B_ARCHIVER);
    3711             : 
    3712             :         /*
    3713             :          * If we aren't planning to enter hot standby mode later, treat
    3714             :          * RECOVERY_STARTED as meaning we're out of startup, and report status
    3715             :          * accordingly.
    3716             :          */
    3717         492 :         if (!EnableHotStandby)
    3718             :         {
    3719           4 :             AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STANDBY);
    3720             : #ifdef USE_SYSTEMD
    3721             :             sd_notify(0, "READY=1");
    3722             : #endif
    3723             :         }
    3724             : 
    3725         492 :         UpdatePMState(PM_RECOVERY);
    3726             :     }
    3727             : 
    3728      172424 :     if (CheckPostmasterSignal(PMSIGNAL_RECOVERY_CONSISTENT) &&
    3729         314 :         pmState == PM_RECOVERY && Shutdown == NoShutdown)
    3730             :     {
    3731         314 :         reachedConsistency = true;
    3732             :     }
    3733             : 
    3734      172424 :     if (CheckPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY) &&
    3735         298 :         (pmState == PM_RECOVERY && Shutdown == NoShutdown))
    3736             :     {
    3737         298 :         ereport(LOG,
    3738             :                 (errmsg("database system is ready to accept read-only connections")));
    3739             : 
    3740             :         /* Report status */
    3741         298 :         AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_READY);
    3742             : #ifdef USE_SYSTEMD
    3743             :         sd_notify(0, "READY=1");
    3744             : #endif
    3745             : 
    3746         298 :         UpdatePMState(PM_HOT_STANDBY);
    3747         298 :         connsAllowed = true;
    3748             : 
    3749             :         /* Some workers may be scheduled to start now */
    3750         298 :         StartWorkerNeeded = true;
    3751             :     }
    3752             : 
    3753             :     /* Process background worker state changes. */
    3754      172424 :     if (CheckPostmasterSignal(PMSIGNAL_BACKGROUND_WORKER_CHANGE))
    3755             :     {
    3756             :         /* Accept new worker requests only if not stopping. */
    3757        2150 :         BackgroundWorkerStateChange(pmState < PM_STOP_BACKENDS);
    3758        2150 :         StartWorkerNeeded = true;
    3759             :     }
    3760             : 
    3761             :     /* Tell syslogger to rotate logfile if requested */
    3762      172424 :     if (SysLoggerPMChild != NULL)
    3763             :     {
    3764           4 :         if (CheckLogrotateSignal())
    3765             :         {
    3766           2 :             signal_child(SysLoggerPMChild, SIGUSR1);
    3767           2 :             RemoveLogrotateSignalFiles();
    3768             :         }
    3769           2 :         else if (CheckPostmasterSignal(PMSIGNAL_ROTATE_LOGFILE))
    3770             :         {
    3771           0 :             signal_child(SysLoggerPMChild, SIGUSR1);
    3772             :         }
    3773             :     }
    3774             : 
    3775      172424 :     if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER) &&
    3776      163540 :         Shutdown <= SmartShutdown && pmState < PM_STOP_BACKENDS)
    3777             :     {
    3778             :         /*
    3779             :          * Start one iteration of the autovacuum daemon, even if autovacuuming
    3780             :          * is nominally not enabled.  This is so we can have an active defense
    3781             :          * against transaction ID wraparound.  We set a flag for the main loop
    3782             :          * to do it rather than trying to do it here --- this is because the
    3783             :          * autovac process itself may send the signal, and we want to handle
    3784             :          * that by launching another iteration as soon as the current one
    3785             :          * completes.
    3786             :          */
    3787      163540 :         start_autovac_launcher = true;
    3788             :     }
    3789             : 
    3790      172424 :     if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER) &&
    3791        2376 :         Shutdown <= SmartShutdown && pmState < PM_STOP_BACKENDS)
    3792             :     {
    3793             :         /* The autovacuum launcher wants us to start a worker process. */
    3794        2376 :         StartAutovacuumWorker();
    3795             :     }
    3796             : 
    3797      172424 :     if (CheckPostmasterSignal(PMSIGNAL_START_WALRECEIVER))
    3798             :     {
    3799             :         /* Startup Process wants us to start the walreceiver process. */
    3800         484 :         WalReceiverRequested = true;
    3801             :     }
    3802             : 
    3803      172424 :     if (CheckPostmasterSignal(PMSIGNAL_XLOG_IS_SHUTDOWN))
    3804             :     {
    3805             :         /* Checkpointer completed the shutdown checkpoint */
    3806         998 :         if (pmState == PM_WAIT_XLOG_SHUTDOWN)
    3807             :         {
    3808             :             /*
    3809             :              * If we have an archiver subprocess, tell it to do a last archive
    3810             :              * cycle and quit. Likewise, if we have walsender processes, tell
    3811             :              * them to send any remaining WAL and quit.
    3812             :              */
    3813             :             Assert(Shutdown > NoShutdown);
    3814             : 
    3815             :             /* Waken archiver for the last time */
    3816         998 :             if (PgArchPMChild != NULL)
    3817          30 :                 signal_child(PgArchPMChild, SIGUSR2);
    3818             : 
    3819             :             /*
    3820             :              * Waken walsenders for the last time. No regular backends should
    3821             :              * be around anymore.
    3822             :              */
    3823         998 :             SignalChildren(SIGUSR2, btmask(B_WAL_SENDER));
    3824             : 
    3825         998 :             UpdatePMState(PM_WAIT_XLOG_ARCHIVAL);
    3826             :         }
    3827           0 :         else if (!FatalError && Shutdown != ImmediateShutdown)
    3828             :         {
    3829             :             /*
    3830             :              * Checkpointer only ought to perform the shutdown checkpoint
    3831             :              * during shutdown.  If somehow checkpointer did so in another
    3832             :              * situation, we have no choice but to crash-restart.
    3833             :              *
    3834             :              * It's possible however that we get PMSIGNAL_XLOG_IS_SHUTDOWN
    3835             :              * outside of PM_WAIT_XLOG_SHUTDOWN if an orderly shutdown was
    3836             :              * "interrupted" by a crash or an immediate shutdown.
    3837             :              */
    3838           0 :             ereport(LOG,
    3839             :                     (errmsg("WAL was shut down unexpectedly")));
    3840             : 
    3841             :             /*
    3842             :              * Doesn't seem likely to help to take send_abort_for_crash into
    3843             :              * account here.
    3844             :              */
    3845           0 :             HandleFatalError(PMQUIT_FOR_CRASH, false);
    3846             :         }
    3847             : 
    3848             :         /*
    3849             :          * Need to run PostmasterStateMachine() to check if we already can go
    3850             :          * to the next state.
    3851             :          */
    3852         998 :         request_state_update = true;
    3853             :     }
    3854             : 
    3855             :     /*
    3856             :      * Try to advance postmaster's state machine, if a child requests it.
    3857             :      */
    3858      172424 :     if (CheckPostmasterSignal(PMSIGNAL_ADVANCE_STATE_MACHINE))
    3859             :     {
    3860        2308 :         request_state_update = true;
    3861             :     }
    3862             : 
    3863             :     /*
    3864             :      * Be careful about the order of this action relative to this function's
    3865             :      * other actions.  Generally, this should be after other actions, in case
    3866             :      * they have effects PostmasterStateMachine would need to know about.
    3867             :      * However, we should do it before the CheckPromoteSignal step, which
    3868             :      * cannot have any (immediate) effect on the state machine, but does
    3869             :      * depend on what state we're in now.
    3870             :      */
    3871      172424 :     if (request_state_update)
    3872             :     {
    3873        3306 :         PostmasterStateMachine();
    3874             :     }
    3875             : 
    3876      172424 :     if (StartupPMChild != NULL &&
    3877        1470 :         (pmState == PM_STARTUP || pmState == PM_RECOVERY ||
    3878        2420 :          pmState == PM_HOT_STANDBY) &&
    3879        1470 :         CheckPromoteSignal())
    3880             :     {
    3881             :         /*
    3882             :          * Tell startup process to finish recovery.
    3883             :          *
    3884             :          * Leave the promote signal file in place and let the Startup process
    3885             :          * do the unlink.
    3886             :          */
    3887          84 :         signal_child(StartupPMChild, SIGUSR2);
    3888             :     }
    3889      172424 : }
    3890             : 
    3891             : /*
    3892             :  * Dummy signal handler
    3893             :  *
    3894             :  * We use this for signals that we don't actually use in the postmaster,
    3895             :  * but we do use in backends.  If we were to SIG_IGN such signals in the
    3896             :  * postmaster, then a newly started backend might drop a signal that arrives
    3897             :  * before it's able to reconfigure its signal processing.  (See notes in
    3898             :  * tcop/postgres.c.)
    3899             :  */
    3900             : static void
    3901           0 : dummy_handler(SIGNAL_ARGS)
    3902             : {
    3903           0 : }
    3904             : 
    3905             : /*
    3906             :  * Count up number of child processes of specified types.
    3907             :  */
    3908             : static int
    3909       11356 : CountChildren(BackendTypeMask targetMask)
    3910             : {
    3911             :     dlist_iter  iter;
    3912       11356 :     int         cnt = 0;
    3913             : 
    3914       62376 :     dlist_foreach(iter, &ActiveChildList)
    3915             :     {
    3916       51020 :         PMChild    *bp = dlist_container(PMChild, elem, iter.cur);
    3917             : 
    3918             :         /*
    3919             :          * If we need to distinguish between B_BACKEND and B_WAL_SENDER, check
    3920             :          * if any B_BACKEND backends have recently announced that they are
    3921             :          * actually WAL senders.
    3922             :          */
    3923       51020 :         if (btmask_contains(targetMask, B_WAL_SENDER) != btmask_contains(targetMask, B_BACKEND) &&
    3924       33690 :             bp->bkend_type == B_BACKEND)
    3925             :         {
    3926        1026 :             if (IsPostmasterChildWalSender(bp->child_slot))
    3927           0 :                 bp->bkend_type = B_WAL_SENDER;
    3928             :         }
    3929             : 
    3930       51020 :         if (!btmask_contains(targetMask, bp->bkend_type))
    3931       26756 :             continue;
    3932             : 
    3933       24264 :         ereport(DEBUG4,
    3934             :                 (errmsg_internal("%s process %d is still running",
    3935             :                                  GetBackendTypeDesc(bp->bkend_type), (int) bp->pid)));
    3936             : 
    3937       24264 :         cnt++;
    3938             :     }
    3939       11356 :     return cnt;
    3940             : }
    3941             : 
    3942             : 
    3943             : /*
    3944             :  * StartChildProcess -- start an auxiliary process for the postmaster
    3945             :  *
    3946             :  * "type" determines what kind of child will be started.  All child types
    3947             :  * initially go to AuxiliaryProcessMain, which will handle common setup.
    3948             :  *
    3949             :  * Return value of StartChildProcess is subprocess' PMChild entry, or NULL on
    3950             :  * failure.
    3951             :  */
    3952             : static PMChild *
    3953       15952 : StartChildProcess(BackendType type)
    3954             : {
    3955             :     PMChild    *pmchild;
    3956             :     pid_t       pid;
    3957             : 
    3958       15952 :     pmchild = AssignPostmasterChildSlot(type);
    3959       15952 :     if (!pmchild)
    3960             :     {
    3961           0 :         if (type == B_AUTOVAC_WORKER)
    3962           0 :             ereport(LOG,
    3963             :                     (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
    3964             :                      errmsg("no slot available for new autovacuum worker process")));
    3965             :         else
    3966             :         {
    3967             :             /* shouldn't happen because we allocate enough slots */
    3968           0 :             elog(LOG, "no postmaster child slot available for aux process");
    3969             :         }
    3970           0 :         return NULL;
    3971             :     }
    3972             : 
    3973       15952 :     pid = postmaster_child_launch(type, pmchild->child_slot, NULL, 0, NULL);
    3974       15950 :     if (pid < 0)
    3975             :     {
    3976             :         /* in parent, fork failed */
    3977           0 :         ReleasePostmasterChildSlot(pmchild);
    3978           0 :         ereport(LOG,
    3979             :                 (errmsg("could not fork \"%s\" process: %m", PostmasterChildName(type))));
    3980             : 
    3981             :         /*
    3982             :          * fork failure is fatal during startup, but there's no need to choke
    3983             :          * immediately if starting other child types fails.
    3984             :          */
    3985           0 :         if (type == B_STARTUP)
    3986           0 :             ExitPostmaster(1);
    3987           0 :         return NULL;
    3988             :     }
    3989             : 
    3990             :     /* in parent, successful fork */
    3991       15950 :     pmchild->pid = pid;
    3992       15950 :     return pmchild;
    3993             : }
    3994             : 
    3995             : /*
    3996             :  * StartSysLogger -- start the syslogger process
    3997             :  */
    3998             : void
    3999           2 : StartSysLogger(void)
    4000             : {
    4001             :     Assert(SysLoggerPMChild == NULL);
    4002             : 
    4003           2 :     SysLoggerPMChild = AssignPostmasterChildSlot(B_LOGGER);
    4004           2 :     if (!SysLoggerPMChild)
    4005           0 :         elog(PANIC, "no postmaster child slot available for syslogger");
    4006           2 :     SysLoggerPMChild->pid = SysLogger_Start(SysLoggerPMChild->child_slot);
    4007           2 :     if (SysLoggerPMChild->pid == 0)
    4008             :     {
    4009           0 :         ReleasePostmasterChildSlot(SysLoggerPMChild);
    4010           0 :         SysLoggerPMChild = NULL;
    4011             :     }
    4012           2 : }
    4013             : 
    4014             : /*
    4015             :  * StartAutovacuumWorker
    4016             :  *      Start an autovac worker process.
    4017             :  *
    4018             :  * This function is here because it enters the resulting PID into the
    4019             :  * postmaster's private backends list.
    4020             :  *
    4021             :  * NB -- this code very roughly matches BackendStartup.
    4022             :  */
    4023             : static void
    4024        2376 : StartAutovacuumWorker(void)
    4025             : {
    4026             :     PMChild    *bn;
    4027             : 
    4028             :     /*
    4029             :      * If not in condition to run a process, don't try, but handle it like a
    4030             :      * fork failure.  This does not normally happen, since the signal is only
    4031             :      * supposed to be sent by autovacuum launcher when it's OK to do it, but
    4032             :      * we have to check to avoid race-condition problems during DB state
    4033             :      * changes.
    4034             :      */
    4035        2376 :     if (canAcceptConnections(B_AUTOVAC_WORKER) == CAC_OK)
    4036             :     {
    4037        2376 :         bn = StartChildProcess(B_AUTOVAC_WORKER);
    4038        2376 :         if (bn)
    4039             :         {
    4040        2376 :             bn->bgworker_notify = false;
    4041        2376 :             bn->rw = NULL;
    4042        2376 :             return;
    4043             :         }
    4044             :         else
    4045             :         {
    4046             :             /*
    4047             :              * fork failed, fall through to report -- actual error message was
    4048             :              * logged by StartChildProcess
    4049             :              */
    4050             :         }
    4051             :     }
    4052             : 
    4053             :     /*
    4054             :      * Report the failure to the launcher, if it's running.  (If it's not, we
    4055             :      * might not even be connected to shared memory, so don't try to call
    4056             :      * AutoVacWorkerFailed.)  Note that we also need to signal it so that it
    4057             :      * responds to the condition, but we don't do that here, instead waiting
    4058             :      * for ServerLoop to do it.  This way we avoid a ping-pong signaling in
    4059             :      * quick succession between the autovac launcher and postmaster in case
    4060             :      * things get ugly.
    4061             :      */
    4062           0 :     if (AutoVacLauncherPMChild != NULL)
    4063             :     {
    4064           0 :         AutoVacWorkerFailed();
    4065           0 :         avlauncher_needs_signal = true;
    4066             :     }
    4067             : }
    4068             : 
    4069             : 
    4070             : /*
    4071             :  * Create the opts file
    4072             :  */
    4073             : static bool
    4074        1680 : CreateOptsFile(int argc, char *argv[], char *fullprogname)
    4075             : {
    4076             :     FILE       *fp;
    4077             :     int         i;
    4078             : 
    4079             : #define OPTS_FILE   "postmaster.opts"
    4080             : 
    4081        1680 :     if ((fp = fopen(OPTS_FILE, "w")) == NULL)
    4082             :     {
    4083           0 :         ereport(LOG,
    4084             :                 (errcode_for_file_access(),
    4085             :                  errmsg("could not create file \"%s\": %m", OPTS_FILE)));
    4086           0 :         return false;
    4087             :     }
    4088             : 
    4089        1680 :     fprintf(fp, "%s", fullprogname);
    4090        9114 :     for (i = 1; i < argc; i++)
    4091        7434 :         fprintf(fp, " \"%s\"", argv[i]);
    4092        1680 :     fputs("\n", fp);
    4093             : 
    4094        1680 :     if (fclose(fp))
    4095             :     {
    4096           0 :         ereport(LOG,
    4097             :                 (errcode_for_file_access(),
    4098             :                  errmsg("could not write file \"%s\": %m", OPTS_FILE)));
    4099           0 :         return false;
    4100             :     }
    4101             : 
    4102        1680 :     return true;
    4103             : }
    4104             : 
    4105             : 
    4106             : /*
    4107             :  * Start a new bgworker.
    4108             :  * Starting time conditions must have been checked already.
    4109             :  *
    4110             :  * Returns true on success, false on failure.
    4111             :  * In either case, update the RegisteredBgWorker's state appropriately.
    4112             :  *
    4113             :  * NB -- this code very roughly matches BackendStartup.
    4114             :  */
    4115             : static bool
    4116        5026 : StartBackgroundWorker(RegisteredBgWorker *rw)
    4117             : {
    4118             :     PMChild    *bn;
    4119             :     pid_t       worker_pid;
    4120             : 
    4121             :     Assert(rw->rw_pid == 0);
    4122             : 
    4123             :     /*
    4124             :      * Allocate and assign the child slot.  Note we must do this before
    4125             :      * forking, so that we can handle failures (out of memory or child-process
    4126             :      * slots) cleanly.
    4127             :      *
    4128             :      * Treat failure as though the worker had crashed.  That way, the
    4129             :      * postmaster will wait a bit before attempting to start it again; if we
    4130             :      * tried again right away, most likely we'd find ourselves hitting the
    4131             :      * same resource-exhaustion condition.
    4132             :      */
    4133        5026 :     bn = AssignPostmasterChildSlot(B_BG_WORKER);
    4134        5026 :     if (bn == NULL)
    4135             :     {
    4136           0 :         ereport(LOG,
    4137             :                 (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
    4138             :                  errmsg("no slot available for new background worker process")));
    4139           0 :         rw->rw_crashed_at = GetCurrentTimestamp();
    4140           0 :         return false;
    4141             :     }
    4142        5026 :     bn->rw = rw;
    4143        5026 :     bn->bkend_type = B_BG_WORKER;
    4144        5026 :     bn->bgworker_notify = false;
    4145             : 
    4146        5026 :     ereport(DEBUG1,
    4147             :             (errmsg_internal("starting background worker process \"%s\"",
    4148             :                              rw->rw_worker.bgw_name)));
    4149             : 
    4150        5026 :     worker_pid = postmaster_child_launch(B_BG_WORKER, bn->child_slot,
    4151        5026 :                                          &rw->rw_worker, sizeof(BackgroundWorker), NULL);
    4152        5024 :     if (worker_pid == -1)
    4153             :     {
    4154             :         /* in postmaster, fork failed ... */
    4155           0 :         ereport(LOG,
    4156             :                 (errmsg("could not fork background worker process: %m")));
    4157             :         /* undo what AssignPostmasterChildSlot did */
    4158           0 :         ReleasePostmasterChildSlot(bn);
    4159             : 
    4160             :         /* mark entry as crashed, so we'll try again later */
    4161           0 :         rw->rw_crashed_at = GetCurrentTimestamp();
    4162           0 :         return false;
    4163             :     }
    4164             : 
    4165             :     /* in postmaster, fork successful ... */
    4166        5024 :     rw->rw_pid = worker_pid;
    4167        5024 :     bn->pid = rw->rw_pid;
    4168        5024 :     ReportBackgroundWorkerPID(rw);
    4169        5024 :     return true;
    4170             : }
    4171             : 
    4172             : /*
    4173             :  * Does the current postmaster state require starting a worker with the
    4174             :  * specified start_time?
    4175             :  */
    4176             : static bool
    4177        6908 : bgworker_should_start_now(BgWorkerStartTime start_time)
    4178             : {
    4179        6908 :     switch (pmState)
    4180             :     {
    4181           2 :         case PM_NO_CHILDREN:
    4182             :         case PM_WAIT_CHECKPOINTER:
    4183             :         case PM_WAIT_DEAD_END:
    4184             :         case PM_WAIT_XLOG_ARCHIVAL:
    4185             :         case PM_WAIT_XLOG_SHUTDOWN:
    4186             :         case PM_WAIT_IO_WORKERS:
    4187             :         case PM_WAIT_BACKENDS:
    4188             :         case PM_STOP_BACKENDS:
    4189           2 :             break;
    4190             : 
    4191        5026 :         case PM_RUN:
    4192        5026 :             if (start_time == BgWorkerStart_RecoveryFinished)
    4193        2270 :                 return true;
    4194             :             /* fall through */
    4195             : 
    4196             :         case PM_HOT_STANDBY:
    4197        3046 :             if (start_time == BgWorkerStart_ConsistentState)
    4198        2756 :                 return true;
    4199             :             /* fall through */
    4200             : 
    4201             :         case PM_RECOVERY:
    4202             :         case PM_STARTUP:
    4203             :         case PM_INIT:
    4204        1880 :             if (start_time == BgWorkerStart_PostmasterStart)
    4205           0 :                 return true;
    4206             :             /* fall through */
    4207             :     }
    4208             : 
    4209        1882 :     return false;
    4210             : }
    4211             : 
    4212             : /*
    4213             :  * If the time is right, start background worker(s).
    4214             :  *
    4215             :  * As a side effect, the bgworker control variables are set or reset
    4216             :  * depending on whether more workers may need to be started.
    4217             :  *
    4218             :  * We limit the number of workers started per call, to avoid consuming the
    4219             :  * postmaster's attention for too long when many such requests are pending.
    4220             :  * As long as StartWorkerNeeded is true, ServerLoop will not block and will
    4221             :  * call this function again after dealing with any other issues.
    4222             :  */
    4223             : static void
    4224       14256 : maybe_start_bgworkers(void)
    4225             : {
    4226             : #define MAX_BGWORKERS_TO_LAUNCH 100
    4227       14256 :     int         num_launched = 0;
    4228       14256 :     TimestampTz now = 0;
    4229             :     dlist_mutable_iter iter;
    4230             : 
    4231             :     /*
    4232             :      * During crash recovery, we have no need to be called until the state
    4233             :      * transition out of recovery.
    4234             :      */
    4235       14256 :     if (FatalError)
    4236             :     {
    4237           0 :         StartWorkerNeeded = false;
    4238           0 :         HaveCrashedWorker = false;
    4239           0 :         return;
    4240             :     }
    4241             : 
    4242             :     /* Don't need to be called again unless we find a reason for it below */
    4243       14256 :     StartWorkerNeeded = false;
    4244       14256 :     HaveCrashedWorker = false;
    4245             : 
    4246       36590 :     dlist_foreach_modify(iter, &BackgroundWorkerList)
    4247             :     {
    4248             :         RegisteredBgWorker *rw;
    4249             : 
    4250       22336 :         rw = dlist_container(RegisteredBgWorker, rw_lnode, iter.cur);
    4251             : 
    4252             :         /* ignore if already running */
    4253       22336 :         if (rw->rw_pid != 0)
    4254       10054 :             continue;
    4255             : 
    4256             :         /* if marked for death, clean up and remove from list */
    4257       12282 :         if (rw->rw_terminate)
    4258             :         {
    4259           0 :             ForgetBackgroundWorker(rw);
    4260           0 :             continue;
    4261             :         }
    4262             : 
    4263             :         /*
    4264             :          * If this worker has crashed previously, maybe it needs to be
    4265             :          * restarted (unless on registration it specified it doesn't want to
    4266             :          * be restarted at all).  Check how long ago did a crash last happen.
    4267             :          * If the last crash is too recent, don't start it right away; let it
    4268             :          * be restarted once enough time has passed.
    4269             :          */
    4270       12282 :         if (rw->rw_crashed_at != 0)
    4271             :         {
    4272        5376 :             if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART)
    4273             :             {
    4274             :                 int         notify_pid;
    4275             : 
    4276           0 :                 notify_pid = rw->rw_worker.bgw_notify_pid;
    4277             : 
    4278           0 :                 ForgetBackgroundWorker(rw);
    4279             : 
    4280             :                 /* Report worker is gone now. */
    4281           0 :                 if (notify_pid != 0)
    4282           0 :                     kill(notify_pid, SIGUSR1);
    4283             : 
    4284           0 :                 continue;
    4285             :             }
    4286             : 
    4287             :             /* read system time only when needed */
    4288        5376 :             if (now == 0)
    4289        5376 :                 now = GetCurrentTimestamp();
    4290             : 
    4291        5376 :             if (!TimestampDifferenceExceeds(rw->rw_crashed_at, now,
    4292        5376 :                                             rw->rw_worker.bgw_restart_time * 1000))
    4293             :             {
    4294             :                 /* Set flag to remember that we have workers to start later */
    4295        5374 :                 HaveCrashedWorker = true;
    4296        5374 :                 continue;
    4297             :             }
    4298             :         }
    4299             : 
    4300        6908 :         if (bgworker_should_start_now(rw->rw_worker.bgw_start_time))
    4301             :         {
    4302             :             /* reset crash time before trying to start worker */
    4303        5026 :             rw->rw_crashed_at = 0;
    4304             : 
    4305             :             /*
    4306             :              * Try to start the worker.
    4307             :              *
    4308             :              * On failure, give up processing workers for now, but set
    4309             :              * StartWorkerNeeded so we'll come back here on the next iteration
    4310             :              * of ServerLoop to try again.  (We don't want to wait, because
    4311             :              * there might be additional ready-to-run workers.)  We could set
    4312             :              * HaveCrashedWorker as well, since this worker is now marked
    4313             :              * crashed, but there's no need because the next run of this
    4314             :              * function will do that.
    4315             :              */
    4316        5026 :             if (!StartBackgroundWorker(rw))
    4317             :             {
    4318           0 :                 StartWorkerNeeded = true;
    4319           0 :                 return;
    4320             :             }
    4321             : 
    4322             :             /*
    4323             :              * If we've launched as many workers as allowed, quit, but have
    4324             :              * ServerLoop call us again to look for additional ready-to-run
    4325             :              * workers.  There might not be any, but we'll find out the next
    4326             :              * time we run.
    4327             :              */
    4328        5024 :             if (++num_launched >= MAX_BGWORKERS_TO_LAUNCH)
    4329             :             {
    4330           0 :                 StartWorkerNeeded = true;
    4331           0 :                 return;
    4332             :             }
    4333             :         }
    4334             :     }
    4335             : }
    4336             : 
    4337             : static bool
    4338       38358 : maybe_reap_io_worker(int pid)
    4339             : {
    4340     1108334 :     for (int id = 0; id < MAX_IO_WORKERS; ++id)
    4341             :     {
    4342     1075088 :         if (io_worker_children[id] &&
    4343      106436 :             io_worker_children[id]->pid == pid)
    4344             :         {
    4345        5112 :             ReleasePostmasterChildSlot(io_worker_children[id]);
    4346             : 
    4347        5112 :             --io_worker_count;
    4348        5112 :             io_worker_children[id] = NULL;
    4349        5112 :             return true;
    4350             :         }
    4351             :     }
    4352       33246 :     return false;
    4353             : }
    4354             : 
    4355             : /*
    4356             :  * Start or stop IO workers, to close the gap between the number of running
    4357             :  * workers and the number of configured workers.  Used to respond to change of
    4358             :  * the io_workers GUC (by increasing and decreasing the number of workers), as
    4359             :  * well as workers terminating in response to errors (by starting
    4360             :  * "replacement" workers).
    4361             :  */
    4362             : static void
    4363      245894 : maybe_adjust_io_workers(void)
    4364             : {
    4365      245894 :     if (!pgaio_workers_enabled())
    4366         108 :         return;
    4367             : 
    4368             :     /*
    4369             :      * If we're in final shutting down state, then we're just waiting for all
    4370             :      * processes to exit.
    4371             :      */
    4372      245786 :     if (pmState >= PM_WAIT_IO_WORKERS)
    4373        6998 :         return;
    4374             : 
    4375             :     /* Don't start new workers during an immediate shutdown either. */
    4376      238788 :     if (Shutdown >= ImmediateShutdown)
    4377        4546 :         return;
    4378             : 
    4379             :     /*
    4380             :      * Don't start new workers if we're in the shutdown phase of a crash
    4381             :      * restart. But we *do* need to start if we're already starting up again.
    4382             :      */
    4383      234242 :     if (FatalError && pmState >= PM_STOP_BACKENDS)
    4384          92 :         return;
    4385             : 
    4386             :     Assert(pmState < PM_WAIT_IO_WORKERS);
    4387             : 
    4388             :     /* Not enough running? */
    4389      239286 :     while (io_worker_count < io_workers)
    4390             :     {
    4391             :         PMChild    *child;
    4392             :         int         id;
    4393             : 
    4394             :         /* find unused entry in io_worker_children array */
    4395       11264 :         for (id = 0; id < MAX_IO_WORKERS; ++id)
    4396             :         {
    4397       11264 :             if (io_worker_children[id] == NULL)
    4398        5136 :                 break;
    4399             :         }
    4400        5136 :         if (id == MAX_IO_WORKERS)
    4401           0 :             elog(ERROR, "could not find a free IO worker ID");
    4402             : 
    4403             :         /* Try to launch one. */
    4404        5136 :         child = StartChildProcess(B_IO_WORKER);
    4405        5136 :         if (child != NULL)
    4406             :         {
    4407        5136 :             io_worker_children[id] = child;
    4408        5136 :             ++io_worker_count;
    4409             :         }
    4410             :         else
    4411           0 :             break;              /* try again next time */
    4412             :     }
    4413             : 
    4414             :     /* Too many running? */
    4415      234150 :     if (io_worker_count > io_workers)
    4416             :     {
    4417             :         /* ask the IO worker in the highest slot to exit */
    4418        1158 :         for (int id = MAX_IO_WORKERS - 1; id >= 0; --id)
    4419             :         {
    4420        1158 :             if (io_worker_children[id] != NULL)
    4421             :             {
    4422         102 :                 kill(io_worker_children[id]->pid, SIGUSR2);
    4423         102 :                 break;
    4424             :             }
    4425             :         }
    4426             :     }
    4427             : }
    4428             : 
    4429             : 
    4430             : /*
    4431             :  * When a backend asks to be notified about worker state changes, we
    4432             :  * set a flag in its backend entry.  The background worker machinery needs
    4433             :  * to know when such backends exit.
    4434             :  */
    4435             : bool
    4436        3638 : PostmasterMarkPIDForWorkerNotify(int pid)
    4437             : {
    4438             :     dlist_iter  iter;
    4439             :     PMChild    *bp;
    4440             : 
    4441        8424 :     dlist_foreach(iter, &ActiveChildList)
    4442             :     {
    4443        8424 :         bp = dlist_container(PMChild, elem, iter.cur);
    4444        8424 :         if (bp->pid == pid)
    4445             :         {
    4446        3638 :             bp->bgworker_notify = true;
    4447        3638 :             return true;
    4448             :         }
    4449             :     }
    4450           0 :     return false;
    4451             : }
    4452             : 
    4453             : #ifdef WIN32
    4454             : 
    4455             : /*
    4456             :  * Subset implementation of waitpid() for Windows.  We assume pid is -1
    4457             :  * (that is, check all child processes) and options is WNOHANG (don't wait).
    4458             :  */
    4459             : static pid_t
    4460             : waitpid(pid_t pid, int *exitstatus, int options)
    4461             : {
    4462             :     win32_deadchild_waitinfo *childinfo;
    4463             :     DWORD       exitcode;
    4464             :     DWORD       dwd;
    4465             :     ULONG_PTR   key;
    4466             :     OVERLAPPED *ovl;
    4467             : 
    4468             :     /* Try to consume one win32_deadchild_waitinfo from the queue. */
    4469             :     if (!GetQueuedCompletionStatus(win32ChildQueue, &dwd, &key, &ovl, 0))
    4470             :     {
    4471             :         errno = EAGAIN;
    4472             :         return -1;
    4473             :     }
    4474             : 
    4475             :     childinfo = (win32_deadchild_waitinfo *) key;
    4476             :     pid = childinfo->procId;
    4477             : 
    4478             :     /*
    4479             :      * Remove handle from wait - required even though it's set to wait only
    4480             :      * once
    4481             :      */
    4482             :     UnregisterWaitEx(childinfo->waitHandle, NULL);
    4483             : 
    4484             :     if (!GetExitCodeProcess(childinfo->procHandle, &exitcode))
    4485             :     {
    4486             :         /*
    4487             :          * Should never happen. Inform user and set a fixed exitcode.
    4488             :          */
    4489             :         write_stderr("could not read exit code for process\n");
    4490             :         exitcode = 255;
    4491             :     }
    4492             :     *exitstatus = exitcode;
    4493             : 
    4494             :     /*
    4495             :      * Close the process handle.  Only after this point can the PID can be
    4496             :      * recycled by the kernel.
    4497             :      */
    4498             :     CloseHandle(childinfo->procHandle);
    4499             : 
    4500             :     /*
    4501             :      * Free struct that was allocated before the call to
    4502             :      * RegisterWaitForSingleObject()
    4503             :      */
    4504             :     pfree(childinfo);
    4505             : 
    4506             :     return pid;
    4507             : }
    4508             : 
    4509             : /*
    4510             :  * Note! Code below executes on a thread pool! All operations must
    4511             :  * be thread safe! Note that elog() and friends must *not* be used.
    4512             :  */
    4513             : static void WINAPI
    4514             : pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
    4515             : {
    4516             :     /* Should never happen, since we use INFINITE as timeout value. */
    4517             :     if (TimerOrWaitFired)
    4518             :         return;
    4519             : 
    4520             :     /*
    4521             :      * Post the win32_deadchild_waitinfo object for waitpid() to deal with. If
    4522             :      * that fails, we leak the object, but we also leak a whole process and
    4523             :      * get into an unrecoverable state, so there's not much point in worrying
    4524             :      * about that.  We'd like to panic, but we can't use that infrastructure
    4525             :      * from this thread.
    4526             :      */
    4527             :     if (!PostQueuedCompletionStatus(win32ChildQueue,
    4528             :                                     0,
    4529             :                                     (ULONG_PTR) lpParameter,
    4530             :                                     NULL))
    4531             :         write_stderr("could not post child completion status\n");
    4532             : 
    4533             :     /* Queue SIGCHLD signal. */
    4534             :     pg_queue_signal(SIGCHLD);
    4535             : }
    4536             : 
    4537             : /*
    4538             :  * Queue a waiter to signal when this child dies.  The wait will be handled
    4539             :  * automatically by an operating system thread pool.  The memory and the
    4540             :  * process handle will be freed by a later call to waitpid().
    4541             :  */
    4542             : void
    4543             : pgwin32_register_deadchild_callback(HANDLE procHandle, DWORD procId)
    4544             : {
    4545             :     win32_deadchild_waitinfo *childinfo;
    4546             : 
    4547             :     childinfo = palloc(sizeof(win32_deadchild_waitinfo));
    4548             :     childinfo->procHandle = procHandle;
    4549             :     childinfo->procId = procId;
    4550             : 
    4551             :     if (!RegisterWaitForSingleObject(&childinfo->waitHandle,
    4552             :                                      procHandle,
    4553             :                                      pgwin32_deadchild_callback,
    4554             :                                      childinfo,
    4555             :                                      INFINITE,
    4556             :                                      WT_EXECUTEONLYONCE | WT_EXECUTEINWAITTHREAD))
    4557             :         ereport(FATAL,
    4558             :                 (errmsg_internal("could not register process for wait: error code %lu",
    4559             :                                  GetLastError())));
    4560             : }
    4561             : 
    4562             : #endif                          /* WIN32 */
    4563             : 
    4564             : /*
    4565             :  * Initialize one and only handle for monitoring postmaster death.
    4566             :  *
    4567             :  * Called once in the postmaster, so that child processes can subsequently
    4568             :  * monitor if their parent is dead.
    4569             :  */
    4570             : static void
    4571        1680 : InitPostmasterDeathWatchHandle(void)
    4572             : {
    4573             : #ifndef WIN32
    4574             : 
    4575             :     /*
    4576             :      * Create a pipe. Postmaster holds the write end of the pipe open
    4577             :      * (POSTMASTER_FD_OWN), and children hold the read end. Children can pass
    4578             :      * the read file descriptor to select() to wake up in case postmaster
    4579             :      * dies, or check for postmaster death with a (read() == 0). Children must
    4580             :      * close the write end as soon as possible after forking, because EOF
    4581             :      * won't be signaled in the read end until all processes have closed the
    4582             :      * write fd. That is taken care of in ClosePostmasterPorts().
    4583             :      */
    4584             :     Assert(MyProcPid == PostmasterPid);
    4585        1680 :     if (pipe(postmaster_alive_fds) < 0)
    4586           0 :         ereport(FATAL,
    4587             :                 (errcode_for_file_access(),
    4588             :                  errmsg_internal("could not create pipe to monitor postmaster death: %m")));
    4589             : 
    4590             :     /* Notify fd.c that we've eaten two FDs for the pipe. */
    4591        1680 :     ReserveExternalFD();
    4592        1680 :     ReserveExternalFD();
    4593             : 
    4594             :     /*
    4595             :      * Set O_NONBLOCK to allow testing for the fd's presence with a read()
    4596             :      * call.
    4597             :      */
    4598        1680 :     if (fcntl(postmaster_alive_fds[POSTMASTER_FD_WATCH], F_SETFL, O_NONBLOCK) == -1)
    4599           0 :         ereport(FATAL,
    4600             :                 (errcode_for_socket_access(),
    4601             :                  errmsg_internal("could not set postmaster death monitoring pipe to nonblocking mode: %m")));
    4602             : #else
    4603             : 
    4604             :     /*
    4605             :      * On Windows, we use a process handle for the same purpose.
    4606             :      */
    4607             :     if (DuplicateHandle(GetCurrentProcess(),
    4608             :                         GetCurrentProcess(),
    4609             :                         GetCurrentProcess(),
    4610             :                         &PostmasterHandle,
    4611             :                         0,
    4612             :                         TRUE,
    4613             :                         DUPLICATE_SAME_ACCESS) == 0)
    4614             :         ereport(FATAL,
    4615             :                 (errmsg_internal("could not duplicate postmaster handle: error code %lu",
    4616             :                                  GetLastError())));
    4617             : #endif                          /* WIN32 */
    4618        1680 : }

Generated by: LCOV version 1.14