1 /*-------------------------------------------------------------------------
2  *
3  * postmaster.c
4  *	  This program acts as a clearing house for requests to the
5  *	  POSTGRES system.  Frontend programs send a startup message
6  *	  to the Postmaster and the postmaster uses the info in the
7  *	  message to setup a backend process.
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-2019, 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 HAVE_SYS_SELECT_H
81 #include <sys/select.h>
82 #endif
83 
84 #ifdef USE_BONJOUR
85 #include <dns_sd.h>
86 #endif
87 
88 #ifdef USE_SYSTEMD
89 #include <systemd/sd-daemon.h>
90 #endif
91 
92 #ifdef HAVE_PTHREAD_IS_THREADED_NP
93 #include <pthread.h>
94 #endif
95 
96 #include "access/transam.h"
97 #include "access/xlog.h"
98 #include "bootstrap/bootstrap.h"
99 #include "catalog/pg_control.h"
100 #include "common/file_perm.h"
101 #include "common/ip.h"
102 #include "common/string.h"
103 #include "lib/ilist.h"
104 #include "libpq/auth.h"
105 #include "libpq/libpq.h"
106 #include "libpq/pqformat.h"
107 #include "libpq/pqsignal.h"
108 #include "miscadmin.h"
109 #include "pg_getopt.h"
110 #include "pgstat.h"
111 #include "port/pg_bswap.h"
112 #include "postmaster/autovacuum.h"
113 #include "postmaster/bgworker_internals.h"
114 #include "postmaster/fork_process.h"
115 #include "postmaster/pgarch.h"
116 #include "postmaster/postmaster.h"
117 #include "postmaster/syslogger.h"
118 #include "replication/logicallauncher.h"
119 #include "replication/walsender.h"
120 #include "storage/fd.h"
121 #include "storage/ipc.h"
122 #include "storage/pg_shmem.h"
123 #include "storage/pmsignal.h"
124 #include "storage/proc.h"
125 #include "tcop/tcopprot.h"
126 #include "utils/builtins.h"
127 #include "utils/datetime.h"
128 #include "utils/memutils.h"
129 #include "utils/pidfile.h"
130 #include "utils/ps_status.h"
131 #include "utils/timeout.h"
132 #include "utils/timestamp.h"
133 #include "utils/varlena.h"
134 
135 #ifdef EXEC_BACKEND
136 #include "storage/spin.h"
137 #endif
138 
139 
140 /*
141  * Possible types of a backend. Beyond being the possible bkend_type values in
142  * struct bkend, these are OR-able request flag bits for SignalSomeChildren()
143  * and CountChildren().
144  */
145 #define BACKEND_TYPE_NORMAL		0x0001	/* normal backend */
146 #define BACKEND_TYPE_AUTOVAC	0x0002	/* autovacuum worker process */
147 #define BACKEND_TYPE_WALSND		0x0004	/* walsender process */
148 #define BACKEND_TYPE_BGWORKER	0x0008	/* bgworker process */
149 #define BACKEND_TYPE_ALL		0x000F	/* OR of all the above */
150 
151 /*
152  * List of active backends (or child processes anyway; we don't actually
153  * know whether a given child has become a backend or is still in the
154  * authorization phase).  This is used mainly to keep track of how many
155  * children we have and send them appropriate signals when necessary.
156  *
157  * "Special" children such as the startup, bgwriter and autovacuum launcher
158  * tasks are not in this list.  Autovacuum worker and walsender are in it.
159  * Also, "dead_end" children are in it: these are children launched just for
160  * the purpose of sending a friendly rejection message to a would-be client.
161  * We must track them because they are attached to shared memory, but we know
162  * they will never become live backends.  dead_end children are not assigned a
163  * PMChildSlot.
164  *
165  * Background workers are in this list, too.
166  */
167 typedef struct bkend
168 {
169 	pid_t		pid;			/* process id of backend */
170 	int32		cancel_key;		/* cancel key for cancels for this backend */
171 	int			child_slot;		/* PMChildSlot for this backend, if any */
172 
173 	/*
174 	 * Flavor of backend or auxiliary process.  Note that BACKEND_TYPE_WALSND
175 	 * backends initially announce themselves as BACKEND_TYPE_NORMAL, so if
176 	 * bkend_type is normal, you should check for a recent transition.
177 	 */
178 	int			bkend_type;
179 	bool		dead_end;		/* is it going to send an error and quit? */
180 	bool		bgworker_notify;	/* gets bgworker start/stop notifications */
181 	dlist_node	elem;			/* list link in BackendList */
182 } Backend;
183 
184 static dlist_head BackendList = DLIST_STATIC_INIT(BackendList);
185 
186 #ifdef EXEC_BACKEND
187 static Backend *ShmemBackendArray;
188 #endif
189 
190 BackgroundWorker *MyBgworkerEntry = NULL;
191 
192 
193 
194 /* The socket number we are listening for connections on */
195 int			PostPortNumber;
196 
197 /* The directory names for Unix socket(s) */
198 char	   *Unix_socket_directories;
199 
200 /* The TCP listen address(es) */
201 char	   *ListenAddresses;
202 
203 /*
204  * ReservedBackends is the number of backends reserved for superuser use.
205  * This number is taken out of the pool size given by MaxConnections so
206  * number of backend slots available to non-superusers is
207  * (MaxConnections - ReservedBackends).  Note what this really means is
208  * "if there are <= ReservedBackends connections available, only superusers
209  * can make new connections" --- pre-existing superuser connections don't
210  * count against the limit.
211  */
212 int			ReservedBackends;
213 
214 /* The socket(s) we're listening to. */
215 #define MAXLISTEN	64
216 static pgsocket ListenSocket[MAXLISTEN];
217 
218 /*
219  * Set by the -o option
220  */
221 static char ExtraOptions[MAXPGPATH];
222 
223 /*
224  * These globals control the behavior of the postmaster in case some
225  * backend dumps core.  Normally, it kills all peers of the dead backend
226  * and reinitializes shared memory.  By specifying -s or -n, we can have
227  * the postmaster stop (rather than kill) peers and not reinitialize
228  * shared data structures.  (Reinit is currently dead code, though.)
229  */
230 static bool Reinit = true;
231 static int	SendStop = false;
232 
233 /* still more option variables */
234 bool		EnableSSL = false;
235 
236 int			PreAuthDelay = 0;
237 int			AuthenticationTimeout = 60;
238 
239 bool		log_hostname;		/* for ps display and logging */
240 bool		Log_connections = false;
241 bool		Db_user_namespace = false;
242 
243 bool		enable_bonjour = false;
244 char	   *bonjour_name;
245 bool		restart_after_crash = true;
246 
247 /* PIDs of special child processes; 0 when not running */
248 static pid_t StartupPID = 0,
249 			BgWriterPID = 0,
250 			CheckpointerPID = 0,
251 			WalWriterPID = 0,
252 			WalReceiverPID = 0,
253 			AutoVacPID = 0,
254 			PgArchPID = 0,
255 			PgStatPID = 0,
256 			SysLoggerPID = 0;
257 
258 /* Startup process's status */
259 typedef enum
260 {
261 	STARTUP_NOT_RUNNING,
262 	STARTUP_RUNNING,
263 	STARTUP_SIGNALED,			/* we sent it a SIGQUIT or SIGKILL */
264 	STARTUP_CRASHED
265 } StartupStatusEnum;
266 
267 static StartupStatusEnum StartupStatus = STARTUP_NOT_RUNNING;
268 
269 /* Startup/shutdown state */
270 #define			NoShutdown		0
271 #define			SmartShutdown	1
272 #define			FastShutdown	2
273 #define			ImmediateShutdown	3
274 
275 static int	Shutdown = NoShutdown;
276 
277 static bool FatalError = false; /* T if recovering from backend crash */
278 
279 /*
280  * We use a simple state machine to control startup, shutdown, and
281  * crash recovery (which is rather like shutdown followed by startup).
282  *
283  * After doing all the postmaster initialization work, we enter PM_STARTUP
284  * state and the startup process is launched. The startup process begins by
285  * reading the control file and other preliminary initialization steps.
286  * In a normal startup, or after crash recovery, the startup process exits
287  * with exit code 0 and we switch to PM_RUN state.  However, archive recovery
288  * is handled specially since it takes much longer and we would like to support
289  * hot standby during archive recovery.
290  *
291  * When the startup process is ready to start archive recovery, it signals the
292  * postmaster, and we switch to PM_RECOVERY state. The background writer and
293  * checkpointer are launched, while the startup process continues applying WAL.
294  * If Hot Standby is enabled, then, after reaching a consistent point in WAL
295  * redo, startup process signals us again, and we switch to PM_HOT_STANDBY
296  * state and begin accepting connections to perform read-only queries.  When
297  * archive recovery is finished, the startup process exits with exit code 0
298  * and we switch to PM_RUN state.
299  *
300  * Normal child backends can only be launched when we are in PM_RUN or
301  * PM_HOT_STANDBY state.  (connsAllowed can also restrict launching.)
302  * In other states we handle connection requests by launching "dead_end"
303  * child processes, which will simply send the client an error message and
304  * quit.  (We track these in the BackendList so that we can know when they
305  * are all gone; this is important because they're still connected to shared
306  * memory, and would interfere with an attempt to destroy the shmem segment,
307  * possibly leading to SHMALL failure when we try to make a new one.)
308  * In PM_WAIT_DEAD_END state we are waiting for all the dead_end children
309  * to drain out of the system, and therefore stop accepting connection
310  * requests at all until the last existing child has quit (which hopefully
311  * will not be very long).
312  *
313  * Notice that this state variable does not distinguish *why* we entered
314  * states later than PM_RUN --- Shutdown and FatalError must be consulted
315  * to find that out.  FatalError is never true in PM_RECOVERY, PM_HOT_STANDBY,
316  * or PM_RUN states, nor in PM_SHUTDOWN states (because we don't enter those
317  * states when trying to recover from a crash).  It can be true in PM_STARTUP
318  * state, because we don't clear it until we've successfully started WAL redo.
319  */
320 typedef enum
321 {
322 	PM_INIT,					/* postmaster starting */
323 	PM_STARTUP,					/* waiting for startup subprocess */
324 	PM_RECOVERY,				/* in archive recovery mode */
325 	PM_HOT_STANDBY,				/* in hot standby mode */
326 	PM_RUN,						/* normal "database is alive" state */
327 	PM_STOP_BACKENDS,			/* need to stop remaining backends */
328 	PM_WAIT_BACKENDS,			/* waiting for live backends to exit */
329 	PM_SHUTDOWN,				/* waiting for checkpointer to do shutdown
330 								 * ckpt */
331 	PM_SHUTDOWN_2,				/* waiting for archiver and walsenders to
332 								 * finish */
333 	PM_WAIT_DEAD_END,			/* waiting for dead_end children to exit */
334 	PM_NO_CHILDREN				/* all important children have exited */
335 } PMState;
336 
337 static PMState pmState = PM_INIT;
338 
339 /*
340  * While performing a "smart shutdown", we restrict new connections but stay
341  * in PM_RUN or PM_HOT_STANDBY state until all the client backends are gone.
342  * connsAllowed is a sub-state indicator showing the active restriction.
343  * It is of no interest unless pmState is PM_RUN or PM_HOT_STANDBY.
344  */
345 typedef enum
346 {
347 	ALLOW_ALL_CONNS,			/* normal not-shutting-down state */
348 	ALLOW_SUPERUSER_CONNS,		/* only superusers can connect */
349 	ALLOW_NO_CONNS				/* no new connections allowed, period */
350 } ConnsAllowedState;
351 
352 static ConnsAllowedState connsAllowed = ALLOW_ALL_CONNS;
353 
354 /* Start time of SIGKILL timeout during immediate shutdown or child crash */
355 /* Zero means timeout is not running */
356 static time_t AbortStartTime = 0;
357 
358 /* Length of said timeout */
359 #define SIGKILL_CHILDREN_AFTER_SECS		5
360 
361 static bool ReachedNormalRunning = false;	/* T if we've reached PM_RUN */
362 
363 bool		ClientAuthInProgress = false;	/* T during new-client
364 											 * authentication */
365 
366 bool		redirection_done = false;	/* stderr redirected for syslogger? */
367 
368 /* received START_AUTOVAC_LAUNCHER signal */
369 static volatile sig_atomic_t start_autovac_launcher = false;
370 
371 /* the launcher needs to be signalled to communicate some condition */
372 static volatile bool avlauncher_needs_signal = false;
373 
374 /* received START_WALRECEIVER signal */
375 static volatile sig_atomic_t WalReceiverRequested = false;
376 
377 /* set when there's a worker that needs to be started up */
378 static volatile bool StartWorkerNeeded = true;
379 static volatile bool HaveCrashedWorker = false;
380 
381 #ifdef USE_SSL
382 /* Set when and if SSL has been initialized properly */
383 static bool LoadedSSL = false;
384 #endif
385 
386 #ifdef USE_BONJOUR
387 static DNSServiceRef bonjour_sdref = NULL;
388 #endif
389 
390 /*
391  * postmaster.c - function prototypes
392  */
393 static void CloseServerPorts(int status, Datum arg);
394 static void unlink_external_pid_file(int status, Datum arg);
395 static void getInstallationPaths(const char *argv0);
396 static void checkControlFile(void);
397 static Port *ConnCreate(int serverFd);
398 static void ConnFree(Port *port);
399 static void reset_shared(int port);
400 static void SIGHUP_handler(SIGNAL_ARGS);
401 static void pmdie(SIGNAL_ARGS);
402 static void reaper(SIGNAL_ARGS);
403 static void sigusr1_handler(SIGNAL_ARGS);
404 static void process_startup_packet_die(SIGNAL_ARGS);
405 static void process_startup_packet_quickdie(SIGNAL_ARGS);
406 static void dummy_handler(SIGNAL_ARGS);
407 static void StartupPacketTimeoutHandler(void);
408 static void CleanupBackend(int pid, int exitstatus);
409 static bool CleanupBackgroundWorker(int pid, int exitstatus);
410 static void HandleChildCrash(int pid, int exitstatus, const char *procname);
411 static void LogChildExit(int lev, const char *procname,
412 						 int pid, int exitstatus);
413 static void PostmasterStateMachine(void);
414 static void BackendInitialize(Port *port);
415 static void BackendRun(Port *port) pg_attribute_noreturn();
416 static void ExitPostmaster(int status) pg_attribute_noreturn();
417 static int	ServerLoop(void);
418 static int	BackendStartup(Port *port);
419 static int	ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done);
420 static void SendNegotiateProtocolVersion(List *unrecognized_protocol_options);
421 static void processCancelRequest(Port *port, void *pkt);
422 static int	initMasks(fd_set *rmask);
423 static void report_fork_failure_to_client(Port *port, int errnum);
424 static CAC_state canAcceptConnections(int backend_type);
425 static bool RandomCancelKey(int32 *cancel_key);
426 static void signal_child(pid_t pid, int signal);
427 static bool SignalSomeChildren(int signal, int targets);
428 static void TerminateChildren(int signal);
429 
430 #define SignalChildren(sig)			   SignalSomeChildren(sig, BACKEND_TYPE_ALL)
431 
432 static int	CountChildren(int target);
433 static bool assign_backendlist_entry(RegisteredBgWorker *rw);
434 static void maybe_start_bgworkers(void);
435 static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
436 static pid_t StartChildProcess(AuxProcType type);
437 static void StartAutovacuumWorker(void);
438 static void MaybeStartWalReceiver(void);
439 static void InitPostmasterDeathWatchHandle(void);
440 
441 /*
442  * Archiver is allowed to start up at the current postmaster state?
443  *
444  * If WAL archiving is enabled always, we are allowed to start archiver
445  * even during recovery.
446  */
447 #define PgArchStartupAllowed()	\
448 	((XLogArchivingActive() && pmState == PM_RUN) ||	\
449 	 (XLogArchivingAlways() &&	\
450 	  (pmState == PM_RECOVERY || pmState == PM_HOT_STANDBY)))
451 
452 #ifdef EXEC_BACKEND
453 
454 #ifdef WIN32
455 #define WNOHANG 0				/* ignored, so any integer value will do */
456 
457 static pid_t waitpid(pid_t pid, int *exitstatus, int options);
458 static void WINAPI pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired);
459 
460 static HANDLE win32ChildQueue;
461 
462 typedef struct
463 {
464 	HANDLE		waitHandle;
465 	HANDLE		procHandle;
466 	DWORD		procId;
467 } win32_deadchild_waitinfo;
468 #endif							/* WIN32 */
469 
470 static pid_t backend_forkexec(Port *port);
471 static pid_t internal_forkexec(int argc, char *argv[], Port *port);
472 
473 /* Type for a socket that can be inherited to a client process */
474 #ifdef WIN32
475 typedef struct
476 {
477 	SOCKET		origsocket;		/* Original socket value, or PGINVALID_SOCKET
478 								 * if not a socket */
479 	WSAPROTOCOL_INFO wsainfo;
480 } InheritableSocket;
481 #else
482 typedef int InheritableSocket;
483 #endif
484 
485 /*
486  * Structure contains all variables passed to exec:ed backends
487  */
488 typedef struct
489 {
490 	Port		port;
491 	InheritableSocket portsocket;
492 	char		DataDir[MAXPGPATH];
493 	pgsocket	ListenSocket[MAXLISTEN];
494 	int32		MyCancelKey;
495 	int			MyPMChildSlot;
496 #ifndef WIN32
497 	unsigned long UsedShmemSegID;
498 #else
499 	void	   *ShmemProtectiveRegion;
500 	HANDLE		UsedShmemSegID;
501 #endif
502 	void	   *UsedShmemSegAddr;
503 	slock_t    *ShmemLock;
504 	VariableCache ShmemVariableCache;
505 	Backend    *ShmemBackendArray;
506 #ifndef HAVE_SPINLOCKS
507 	PGSemaphore *SpinlockSemaArray;
508 #endif
509 	int			NamedLWLockTrancheRequests;
510 	NamedLWLockTranche *NamedLWLockTrancheArray;
511 	LWLockPadded *MainLWLockArray;
512 	slock_t    *ProcStructLock;
513 	PROC_HDR   *ProcGlobal;
514 	PGPROC	   *AuxiliaryProcs;
515 	PGPROC	   *PreparedXactProcs;
516 	PMSignalData *PMSignalState;
517 	InheritableSocket pgStatSock;
518 	pid_t		PostmasterPid;
519 	TimestampTz PgStartTime;
520 	TimestampTz PgReloadTime;
521 	pg_time_t	first_syslogger_file_time;
522 	bool		redirection_done;
523 	bool		IsBinaryUpgrade;
524 	int			max_safe_fds;
525 	int			MaxBackends;
526 #ifdef WIN32
527 	HANDLE		PostmasterHandle;
528 	HANDLE		initial_signal_pipe;
529 	HANDLE		syslogPipe[2];
530 #else
531 	int			postmaster_alive_fds[2];
532 	int			syslogPipe[2];
533 #endif
534 	char		my_exec_path[MAXPGPATH];
535 	char		pkglib_path[MAXPGPATH];
536 	char		ExtraOptions[MAXPGPATH];
537 } BackendParameters;
538 
539 static void read_backend_variables(char *id, Port *port);
540 static void restore_backend_variables(BackendParameters *param, Port *port);
541 
542 #ifndef WIN32
543 static bool save_backend_variables(BackendParameters *param, Port *port);
544 #else
545 static bool save_backend_variables(BackendParameters *param, Port *port,
546 								   HANDLE childProcess, pid_t childPid);
547 #endif
548 
549 static void ShmemBackendArrayAdd(Backend *bn);
550 static void ShmemBackendArrayRemove(Backend *bn);
551 #endif							/* EXEC_BACKEND */
552 
553 #define StartupDataBase()		StartChildProcess(StartupProcess)
554 #define StartBackgroundWriter() StartChildProcess(BgWriterProcess)
555 #define StartCheckpointer()		StartChildProcess(CheckpointerProcess)
556 #define StartWalWriter()		StartChildProcess(WalWriterProcess)
557 #define StartWalReceiver()		StartChildProcess(WalReceiverProcess)
558 
559 /* Macros to check exit status of a child process */
560 #define EXIT_STATUS_0(st)  ((st) == 0)
561 #define EXIT_STATUS_1(st)  (WIFEXITED(st) && WEXITSTATUS(st) == 1)
562 #define EXIT_STATUS_3(st)  (WIFEXITED(st) && WEXITSTATUS(st) == 3)
563 
564 #ifndef WIN32
565 /*
566  * File descriptors for pipe used to monitor if postmaster is alive.
567  * First is POSTMASTER_FD_WATCH, second is POSTMASTER_FD_OWN.
568  */
569 int			postmaster_alive_fds[2] = {-1, -1};
570 #else
571 /* Process handle of postmaster used for the same purpose on Windows */
572 HANDLE		PostmasterHandle;
573 #endif
574 
575 /*
576  * Postmaster main entry point
577  */
578 void
579 PostmasterMain(int argc, char *argv[])
580 {
581 	int			opt;
582 	int			status;
583 	char	   *userDoption = NULL;
584 	bool		listen_addr_saved = false;
585 	int			i;
586 	char	   *output_config_variable = NULL;
587 
588 	InitProcessGlobals();
589 
590 	PostmasterPid = MyProcPid;
591 
592 	IsPostmasterEnvironment = true;
593 
594 	/*
595 	 * We should not be creating any files or directories before we check the
596 	 * data directory (see checkDataDir()), but just in case set the umask to
597 	 * the most restrictive (owner-only) permissions.
598 	 *
599 	 * checkDataDir() will reset the umask based on the data directory
600 	 * permissions.
601 	 */
602 	umask(PG_MODE_MASK_OWNER);
603 
604 	/*
605 	 * By default, palloc() requests in the postmaster will be allocated in
606 	 * the PostmasterContext, which is space that can be recycled by backends.
607 	 * Allocated data that needs to be available to backends should be
608 	 * allocated in TopMemoryContext.
609 	 */
610 	PostmasterContext = AllocSetContextCreate(TopMemoryContext,
611 											  "Postmaster",
612 											  ALLOCSET_DEFAULT_SIZES);
613 	MemoryContextSwitchTo(PostmasterContext);
614 
615 	/* Initialize paths to installation files */
616 	getInstallationPaths(argv[0]);
617 
618 	/*
619 	 * Set up signal handlers for the postmaster process.
620 	 *
621 	 * In the postmaster, we use pqsignal_pm() rather than pqsignal() (which
622 	 * is used by all child processes and client processes).  That has a
623 	 * couple of special behaviors:
624 	 *
625 	 * 1. Except on Windows, we tell sigaction() to block all signals for the
626 	 * duration of the signal handler.  This is faster than our old approach
627 	 * of blocking/unblocking explicitly in the signal handler, and it should
628 	 * also prevent excessive stack consumption if signals arrive quickly.
629 	 *
630 	 * 2. We do not set the SA_RESTART flag.  This is because signals will be
631 	 * blocked at all times except when ServerLoop is waiting for something to
632 	 * happen, and during that window, we want signals to exit the select(2)
633 	 * wait so that ServerLoop can respond if anything interesting happened.
634 	 * On some platforms, signals marked SA_RESTART would not cause the
635 	 * select() wait to end.
636 	 *
637 	 * Child processes will generally want SA_RESTART, so pqsignal() sets that
638 	 * flag.  We expect children to set up their own handlers before
639 	 * unblocking signals.
640 	 *
641 	 * CAUTION: when changing this list, check for side-effects on the signal
642 	 * handling setup of child processes.  See tcop/postgres.c,
643 	 * bootstrap/bootstrap.c, postmaster/bgwriter.c, postmaster/walwriter.c,
644 	 * postmaster/autovacuum.c, postmaster/pgarch.c, postmaster/pgstat.c,
645 	 * postmaster/syslogger.c, postmaster/bgworker.c and
646 	 * postmaster/checkpointer.c.
647 	 */
648 	pqinitmask();
649 	PG_SETMASK(&BlockSig);
650 
651 	pqsignal_pm(SIGHUP, SIGHUP_handler);	/* reread config file and have
652 											 * children do same */
653 	pqsignal_pm(SIGINT, pmdie); /* send SIGTERM and shut down */
654 	pqsignal_pm(SIGQUIT, pmdie);	/* send SIGQUIT and die */
655 	pqsignal_pm(SIGTERM, pmdie);	/* wait for children and shut down */
656 	pqsignal_pm(SIGALRM, SIG_IGN);	/* ignored */
657 	pqsignal_pm(SIGPIPE, SIG_IGN);	/* ignored */
658 	pqsignal_pm(SIGUSR1, sigusr1_handler);	/* message from child process */
659 	pqsignal_pm(SIGUSR2, dummy_handler);	/* unused, reserve for children */
660 	pqsignal_pm(SIGCHLD, reaper);	/* handle child termination */
661 
662 	/*
663 	 * No other place in Postgres should touch SIGTTIN/SIGTTOU handling.  We
664 	 * ignore those signals in a postmaster environment, so that there is no
665 	 * risk of a child process freezing up due to writing to stderr.  But for
666 	 * a standalone backend, their default handling is reasonable.  Hence, all
667 	 * child processes should just allow the inherited settings to stand.
668 	 */
669 #ifdef SIGTTIN
670 	pqsignal_pm(SIGTTIN, SIG_IGN);	/* ignored */
671 #endif
672 #ifdef SIGTTOU
673 	pqsignal_pm(SIGTTOU, SIG_IGN);	/* ignored */
674 #endif
675 
676 	/* ignore SIGXFSZ, so that ulimit violations work like disk full */
677 #ifdef SIGXFSZ
678 	pqsignal_pm(SIGXFSZ, SIG_IGN);	/* ignored */
679 #endif
680 
681 	/*
682 	 * Options setup
683 	 */
684 	InitializeGUCOptions();
685 
686 	opterr = 1;
687 
688 	/*
689 	 * Parse command-line options.  CAUTION: keep this in sync with
690 	 * tcop/postgres.c (the option sets should not conflict) and with the
691 	 * common help() function in main/main.c.
692 	 */
693 	while ((opt = getopt(argc, argv, "B:bc:C:D:d:EeFf:h:ijk:lN:nOo:Pp:r:S:sTt:W:-:")) != -1)
694 	{
695 		switch (opt)
696 		{
697 			case 'B':
698 				SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
699 				break;
700 
701 			case 'b':
702 				/* Undocumented flag used for binary upgrades */
703 				IsBinaryUpgrade = true;
704 				break;
705 
706 			case 'C':
707 				output_config_variable = strdup(optarg);
708 				break;
709 
710 			case 'D':
711 				userDoption = strdup(optarg);
712 				break;
713 
714 			case 'd':
715 				set_debug_options(atoi(optarg), PGC_POSTMASTER, PGC_S_ARGV);
716 				break;
717 
718 			case 'E':
719 				SetConfigOption("log_statement", "all", PGC_POSTMASTER, PGC_S_ARGV);
720 				break;
721 
722 			case 'e':
723 				SetConfigOption("datestyle", "euro", PGC_POSTMASTER, PGC_S_ARGV);
724 				break;
725 
726 			case 'F':
727 				SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
728 				break;
729 
730 			case 'f':
731 				if (!set_plan_disabling_options(optarg, PGC_POSTMASTER, PGC_S_ARGV))
732 				{
733 					write_stderr("%s: invalid argument for option -f: \"%s\"\n",
734 								 progname, optarg);
735 					ExitPostmaster(1);
736 				}
737 				break;
738 
739 			case 'h':
740 				SetConfigOption("listen_addresses", optarg, PGC_POSTMASTER, PGC_S_ARGV);
741 				break;
742 
743 			case 'i':
744 				SetConfigOption("listen_addresses", "*", PGC_POSTMASTER, PGC_S_ARGV);
745 				break;
746 
747 			case 'j':
748 				/* only used by interactive backend */
749 				break;
750 
751 			case 'k':
752 				SetConfigOption("unix_socket_directories", optarg, PGC_POSTMASTER, PGC_S_ARGV);
753 				break;
754 
755 			case 'l':
756 				SetConfigOption("ssl", "true", PGC_POSTMASTER, PGC_S_ARGV);
757 				break;
758 
759 			case 'N':
760 				SetConfigOption("max_connections", optarg, PGC_POSTMASTER, PGC_S_ARGV);
761 				break;
762 
763 			case 'n':
764 				/* Don't reinit shared mem after abnormal exit */
765 				Reinit = false;
766 				break;
767 
768 			case 'O':
769 				SetConfigOption("allow_system_table_mods", "true", PGC_POSTMASTER, PGC_S_ARGV);
770 				break;
771 
772 			case 'o':
773 				/* Other options to pass to the backend on the command line */
774 				snprintf(ExtraOptions + strlen(ExtraOptions),
775 						 sizeof(ExtraOptions) - strlen(ExtraOptions),
776 						 " %s", optarg);
777 				break;
778 
779 			case 'P':
780 				SetConfigOption("ignore_system_indexes", "true", PGC_POSTMASTER, PGC_S_ARGV);
781 				break;
782 
783 			case 'p':
784 				SetConfigOption("port", optarg, PGC_POSTMASTER, PGC_S_ARGV);
785 				break;
786 
787 			case 'r':
788 				/* only used by single-user backend */
789 				break;
790 
791 			case 'S':
792 				SetConfigOption("work_mem", optarg, PGC_POSTMASTER, PGC_S_ARGV);
793 				break;
794 
795 			case 's':
796 				SetConfigOption("log_statement_stats", "true", PGC_POSTMASTER, PGC_S_ARGV);
797 				break;
798 
799 			case 'T':
800 
801 				/*
802 				 * In the event that some backend dumps core, send SIGSTOP,
803 				 * rather than SIGQUIT, to all its peers.  This lets the wily
804 				 * post_hacker collect core dumps from everyone.
805 				 */
806 				SendStop = true;
807 				break;
808 
809 			case 't':
810 				{
811 					const char *tmp = get_stats_option_name(optarg);
812 
813 					if (tmp)
814 					{
815 						SetConfigOption(tmp, "true", PGC_POSTMASTER, PGC_S_ARGV);
816 					}
817 					else
818 					{
819 						write_stderr("%s: invalid argument for option -t: \"%s\"\n",
820 									 progname, optarg);
821 						ExitPostmaster(1);
822 					}
823 					break;
824 				}
825 
826 			case 'W':
827 				SetConfigOption("post_auth_delay", optarg, PGC_POSTMASTER, PGC_S_ARGV);
828 				break;
829 
830 			case 'c':
831 			case '-':
832 				{
833 					char	   *name,
834 							   *value;
835 
836 					ParseLongOption(optarg, &name, &value);
837 					if (!value)
838 					{
839 						if (opt == '-')
840 							ereport(ERROR,
841 									(errcode(ERRCODE_SYNTAX_ERROR),
842 									 errmsg("--%s requires a value",
843 											optarg)));
844 						else
845 							ereport(ERROR,
846 									(errcode(ERRCODE_SYNTAX_ERROR),
847 									 errmsg("-c %s requires a value",
848 											optarg)));
849 					}
850 
851 					SetConfigOption(name, value, PGC_POSTMASTER, PGC_S_ARGV);
852 					free(name);
853 					if (value)
854 						free(value);
855 					break;
856 				}
857 
858 			default:
859 				write_stderr("Try \"%s --help\" for more information.\n",
860 							 progname);
861 				ExitPostmaster(1);
862 		}
863 	}
864 
865 	/*
866 	 * Postmaster accepts no non-option switch arguments.
867 	 */
868 	if (optind < argc)
869 	{
870 		write_stderr("%s: invalid argument: \"%s\"\n",
871 					 progname, argv[optind]);
872 		write_stderr("Try \"%s --help\" for more information.\n",
873 					 progname);
874 		ExitPostmaster(1);
875 	}
876 
877 	/*
878 	 * Locate the proper configuration files and data directory, and read
879 	 * postgresql.conf for the first time.
880 	 */
881 	if (!SelectConfigFiles(userDoption, progname))
882 		ExitPostmaster(2);
883 
884 	if (output_config_variable != NULL)
885 	{
886 		/*
887 		 * "-C guc" was specified, so print GUC's value and exit.  No extra
888 		 * permission check is needed because the user is reading inside the
889 		 * data dir.
890 		 */
891 		const char *config_val = GetConfigOption(output_config_variable,
892 												 false, false);
893 
894 		puts(config_val ? config_val : "");
895 		ExitPostmaster(0);
896 	}
897 
898 	/* Verify that DataDir looks reasonable */
899 	checkDataDir();
900 
901 	/* Check that pg_control exists */
902 	checkControlFile();
903 
904 	/* And switch working directory into it */
905 	ChangeToDataDir();
906 
907 	/*
908 	 * Check for invalid combinations of GUC settings.
909 	 */
910 	if (ReservedBackends >= MaxConnections)
911 	{
912 		write_stderr("%s: superuser_reserved_connections (%d) must be less than max_connections (%d)\n",
913 					 progname,
914 					 ReservedBackends, MaxConnections);
915 		ExitPostmaster(1);
916 	}
917 	if (XLogArchiveMode > ARCHIVE_MODE_OFF && wal_level == WAL_LEVEL_MINIMAL)
918 		ereport(ERROR,
919 				(errmsg("WAL archival cannot be enabled when wal_level is \"minimal\"")));
920 	if (max_wal_senders > 0 && wal_level == WAL_LEVEL_MINIMAL)
921 		ereport(ERROR,
922 				(errmsg("WAL streaming (max_wal_senders > 0) requires wal_level \"replica\" or \"logical\"")));
923 
924 	/*
925 	 * Other one-time internal sanity checks can go here, if they are fast.
926 	 * (Put any slow processing further down, after postmaster.pid creation.)
927 	 */
928 	if (!CheckDateTokenTables())
929 	{
930 		write_stderr("%s: invalid datetoken tables, please fix\n", progname);
931 		ExitPostmaster(1);
932 	}
933 
934 	/*
935 	 * Now that we are done processing the postmaster arguments, reset
936 	 * getopt(3) library so that it will work correctly in subprocesses.
937 	 */
938 	optind = 1;
939 #ifdef HAVE_INT_OPTRESET
940 	optreset = 1;				/* some systems need this too */
941 #endif
942 
943 	/* For debugging: display postmaster environment */
944 	{
945 		extern char **environ;
946 		char	  **p;
947 
948 		ereport(DEBUG3,
949 				(errmsg_internal("%s: PostmasterMain: initial environment dump:",
950 								 progname)));
951 		ereport(DEBUG3,
952 				(errmsg_internal("-----------------------------------------")));
953 		for (p = environ; *p; ++p)
954 			ereport(DEBUG3,
955 					(errmsg_internal("\t%s", *p)));
956 		ereport(DEBUG3,
957 				(errmsg_internal("-----------------------------------------")));
958 	}
959 
960 	/*
961 	 * Create lockfile for data directory.
962 	 *
963 	 * We want to do this before we try to grab the input sockets, because the
964 	 * data directory interlock is more reliable than the socket-file
965 	 * interlock (thanks to whoever decided to put socket files in /tmp :-().
966 	 * For the same reason, it's best to grab the TCP socket(s) before the
967 	 * Unix socket(s).
968 	 *
969 	 * Also note that this internally sets up the on_proc_exit function that
970 	 * is responsible for removing both data directory and socket lockfiles;
971 	 * so it must happen before opening sockets so that at exit, the socket
972 	 * lockfiles go away after CloseServerPorts runs.
973 	 */
974 	CreateDataDirLockFile(true);
975 
976 	/*
977 	 * Read the control file (for error checking and config info).
978 	 *
979 	 * Since we verify the control file's CRC, this has a useful side effect
980 	 * on machines where we need a run-time test for CRC support instructions.
981 	 * The postmaster will do the test once at startup, and then its child
982 	 * processes will inherit the correct function pointer and not need to
983 	 * repeat the test.
984 	 */
985 	LocalProcessControlFile(false);
986 
987 	/*
988 	 * Initialize SSL library, if specified.
989 	 */
990 #ifdef USE_SSL
991 	if (EnableSSL)
992 	{
993 		(void) secure_initialize(true);
994 		LoadedSSL = true;
995 	}
996 #endif
997 
998 	/*
999 	 * Register the apply launcher.  Since it registers a background worker,
1000 	 * it needs to be called before InitializeMaxBackends(), and it's probably
1001 	 * a good idea to call it before any modules had chance to take the
1002 	 * background worker slots.
1003 	 */
1004 	ApplyLauncherRegister();
1005 
1006 	/*
1007 	 * process any libraries that should be preloaded at postmaster start
1008 	 */
1009 	process_shared_preload_libraries();
1010 
1011 	/*
1012 	 * Now that loadable modules have had their chance to register background
1013 	 * workers, calculate MaxBackends.
1014 	 */
1015 	InitializeMaxBackends();
1016 
1017 	/* Report server startup in log */
1018 	ereport(LOG,
1019 			(errmsg("starting %s", PG_VERSION_STR)));
1020 
1021 	/*
1022 	 * Establish input sockets.
1023 	 *
1024 	 * First, mark them all closed, and set up an on_proc_exit function that's
1025 	 * charged with closing the sockets again at postmaster shutdown.
1026 	 */
1027 	for (i = 0; i < MAXLISTEN; i++)
1028 		ListenSocket[i] = PGINVALID_SOCKET;
1029 
1030 	on_proc_exit(CloseServerPorts, 0);
1031 
1032 	if (ListenAddresses)
1033 	{
1034 		char	   *rawstring;
1035 		List	   *elemlist;
1036 		ListCell   *l;
1037 		int			success = 0;
1038 
1039 		/* Need a modifiable copy of ListenAddresses */
1040 		rawstring = pstrdup(ListenAddresses);
1041 
1042 		/* Parse string into list of hostnames */
1043 		if (!SplitIdentifierString(rawstring, ',', &elemlist))
1044 		{
1045 			/* syntax error in list */
1046 			ereport(FATAL,
1047 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1048 					 errmsg("invalid list syntax in parameter \"%s\"",
1049 							"listen_addresses")));
1050 		}
1051 
1052 		foreach(l, elemlist)
1053 		{
1054 			char	   *curhost = (char *) lfirst(l);
1055 
1056 			if (strcmp(curhost, "*") == 0)
1057 				status = StreamServerPort(AF_UNSPEC, NULL,
1058 										  (unsigned short) PostPortNumber,
1059 										  NULL,
1060 										  ListenSocket, MAXLISTEN);
1061 			else
1062 				status = StreamServerPort(AF_UNSPEC, curhost,
1063 										  (unsigned short) PostPortNumber,
1064 										  NULL,
1065 										  ListenSocket, MAXLISTEN);
1066 
1067 			if (status == STATUS_OK)
1068 			{
1069 				success++;
1070 				/* record the first successful host addr in lockfile */
1071 				if (!listen_addr_saved)
1072 				{
1073 					AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR, curhost);
1074 					listen_addr_saved = true;
1075 				}
1076 			}
1077 			else
1078 				ereport(WARNING,
1079 						(errmsg("could not create listen socket for \"%s\"",
1080 								curhost)));
1081 		}
1082 
1083 		if (!success && elemlist != NIL)
1084 			ereport(FATAL,
1085 					(errmsg("could not create any TCP/IP sockets")));
1086 
1087 		list_free(elemlist);
1088 		pfree(rawstring);
1089 	}
1090 
1091 #ifdef USE_BONJOUR
1092 	/* Register for Bonjour only if we opened TCP socket(s) */
1093 	if (enable_bonjour && ListenSocket[0] != PGINVALID_SOCKET)
1094 	{
1095 		DNSServiceErrorType err;
1096 
1097 		/*
1098 		 * We pass 0 for interface_index, which will result in registering on
1099 		 * all "applicable" interfaces.  It's not entirely clear from the
1100 		 * DNS-SD docs whether this would be appropriate if we have bound to
1101 		 * just a subset of the available network interfaces.
1102 		 */
1103 		err = DNSServiceRegister(&bonjour_sdref,
1104 								 0,
1105 								 0,
1106 								 bonjour_name,
1107 								 "_postgresql._tcp.",
1108 								 NULL,
1109 								 NULL,
1110 								 pg_hton16(PostPortNumber),
1111 								 0,
1112 								 NULL,
1113 								 NULL,
1114 								 NULL);
1115 		if (err != kDNSServiceErr_NoError)
1116 			elog(LOG, "DNSServiceRegister() failed: error code %ld",
1117 				 (long) err);
1118 
1119 		/*
1120 		 * We don't bother to read the mDNS daemon's reply, and we expect that
1121 		 * it will automatically terminate our registration when the socket is
1122 		 * closed at postmaster termination.  So there's nothing more to be
1123 		 * done here.  However, the bonjour_sdref is kept around so that
1124 		 * forked children can close their copies of the socket.
1125 		 */
1126 	}
1127 #endif
1128 
1129 #ifdef HAVE_UNIX_SOCKETS
1130 	if (Unix_socket_directories)
1131 	{
1132 		char	   *rawstring;
1133 		List	   *elemlist;
1134 		ListCell   *l;
1135 		int			success = 0;
1136 
1137 		/* Need a modifiable copy of Unix_socket_directories */
1138 		rawstring = pstrdup(Unix_socket_directories);
1139 
1140 		/* Parse string into list of directories */
1141 		if (!SplitDirectoriesString(rawstring, ',', &elemlist))
1142 		{
1143 			/* syntax error in list */
1144 			ereport(FATAL,
1145 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1146 					 errmsg("invalid list syntax in parameter \"%s\"",
1147 							"unix_socket_directories")));
1148 		}
1149 
1150 		foreach(l, elemlist)
1151 		{
1152 			char	   *socketdir = (char *) lfirst(l);
1153 
1154 			status = StreamServerPort(AF_UNIX, NULL,
1155 									  (unsigned short) PostPortNumber,
1156 									  socketdir,
1157 									  ListenSocket, MAXLISTEN);
1158 
1159 			if (status == STATUS_OK)
1160 			{
1161 				success++;
1162 				/* record the first successful Unix socket in lockfile */
1163 				if (success == 1)
1164 					AddToDataDirLockFile(LOCK_FILE_LINE_SOCKET_DIR, socketdir);
1165 			}
1166 			else
1167 				ereport(WARNING,
1168 						(errmsg("could not create Unix-domain socket in directory \"%s\"",
1169 								socketdir)));
1170 		}
1171 
1172 		if (!success && elemlist != NIL)
1173 			ereport(FATAL,
1174 					(errmsg("could not create any Unix-domain sockets")));
1175 
1176 		list_free_deep(elemlist);
1177 		pfree(rawstring);
1178 	}
1179 #endif
1180 
1181 	/*
1182 	 * check that we have some socket to listen on
1183 	 */
1184 	if (ListenSocket[0] == PGINVALID_SOCKET)
1185 		ereport(FATAL,
1186 				(errmsg("no socket created for listening")));
1187 
1188 	/*
1189 	 * If no valid TCP ports, write an empty line for listen address,
1190 	 * indicating the Unix socket must be used.  Note that this line is not
1191 	 * added to the lock file until there is a socket backing it.
1192 	 */
1193 	if (!listen_addr_saved)
1194 		AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR, "");
1195 
1196 	/*
1197 	 * Set up shared memory and semaphores.
1198 	 */
1199 	reset_shared(PostPortNumber);
1200 
1201 	/*
1202 	 * Estimate number of openable files.  This must happen after setting up
1203 	 * semaphores, because on some platforms semaphores count as open files.
1204 	 */
1205 	set_max_safe_fds();
1206 
1207 	/*
1208 	 * Set reference point for stack-depth checking.
1209 	 */
1210 	set_stack_base();
1211 
1212 	/*
1213 	 * Initialize pipe (or process handle on Windows) that allows children to
1214 	 * wake up from sleep on postmaster death.
1215 	 */
1216 	InitPostmasterDeathWatchHandle();
1217 
1218 #ifdef WIN32
1219 
1220 	/*
1221 	 * Initialize I/O completion port used to deliver list of dead children.
1222 	 */
1223 	win32ChildQueue = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 1);
1224 	if (win32ChildQueue == NULL)
1225 		ereport(FATAL,
1226 				(errmsg("could not create I/O completion port for child queue")));
1227 #endif
1228 
1229 	/*
1230 	 * Record postmaster options.  We delay this till now to avoid recording
1231 	 * bogus options (eg, NBuffers too high for available memory).
1232 	 */
1233 	if (!CreateOptsFile(argc, argv, my_exec_path))
1234 		ExitPostmaster(1);
1235 
1236 #ifdef EXEC_BACKEND
1237 	/* Write out nondefault GUC settings for child processes to use */
1238 	write_nondefault_variables(PGC_POSTMASTER);
1239 #endif
1240 
1241 	/*
1242 	 * Write the external PID file if requested
1243 	 */
1244 	if (external_pid_file)
1245 	{
1246 		FILE	   *fpidfile = fopen(external_pid_file, "w");
1247 
1248 		if (fpidfile)
1249 		{
1250 			fprintf(fpidfile, "%d\n", MyProcPid);
1251 			fclose(fpidfile);
1252 
1253 			/* Make PID file world readable */
1254 			if (chmod(external_pid_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) != 0)
1255 				write_stderr("%s: could not change permissions of external PID file \"%s\": %s\n",
1256 							 progname, external_pid_file, strerror(errno));
1257 		}
1258 		else
1259 			write_stderr("%s: could not write external PID file \"%s\": %s\n",
1260 						 progname, external_pid_file, strerror(errno));
1261 
1262 		on_proc_exit(unlink_external_pid_file, 0);
1263 	}
1264 
1265 	/*
1266 	 * Remove old temporary files.  At this point there can be no other
1267 	 * Postgres processes running in this directory, so this should be safe.
1268 	 */
1269 	RemovePgTempFiles();
1270 
1271 	/*
1272 	 * Forcibly remove the files signaling a standby promotion request.
1273 	 * Otherwise, the existence of those files triggers a promotion too early,
1274 	 * whether a user wants that or not.
1275 	 *
1276 	 * This removal of files is usually unnecessary because they can exist
1277 	 * only during a few moments during a standby promotion. However there is
1278 	 * a race condition: if pg_ctl promote is executed and creates the files
1279 	 * during a promotion, the files can stay around even after the server is
1280 	 * brought up to new master. Then, if new standby starts by using the
1281 	 * backup taken from that master, the files can exist at the server
1282 	 * startup and should be removed in order to avoid an unexpected
1283 	 * promotion.
1284 	 *
1285 	 * Note that promotion signal files need to be removed before the startup
1286 	 * process is invoked. Because, after that, they can be used by
1287 	 * postmaster's SIGUSR1 signal handler.
1288 	 */
1289 	RemovePromoteSignalFiles();
1290 
1291 	/* Do the same for logrotate signal file */
1292 	RemoveLogrotateSignalFiles();
1293 
1294 	/* Remove any outdated file holding the current log filenames. */
1295 	if (unlink(LOG_METAINFO_DATAFILE) < 0 && errno != ENOENT)
1296 		ereport(LOG,
1297 				(errcode_for_file_access(),
1298 				 errmsg("could not remove file \"%s\": %m",
1299 						LOG_METAINFO_DATAFILE)));
1300 
1301 	/*
1302 	 * If enabled, start up syslogger collection subprocess
1303 	 */
1304 	SysLoggerPID = SysLogger_Start();
1305 
1306 	/*
1307 	 * Reset whereToSendOutput from DestDebug (its starting state) to
1308 	 * DestNone. This stops ereport from sending log messages to stderr unless
1309 	 * Log_destination permits.  We don't do this until the postmaster is
1310 	 * fully launched, since startup failures may as well be reported to
1311 	 * stderr.
1312 	 *
1313 	 * If we are in fact disabling logging to stderr, first emit a log message
1314 	 * saying so, to provide a breadcrumb trail for users who may not remember
1315 	 * that their logging is configured to go somewhere else.
1316 	 */
1317 	if (!(Log_destination & LOG_DESTINATION_STDERR))
1318 		ereport(LOG,
1319 				(errmsg("ending log output to stderr"),
1320 				 errhint("Future log output will go to log destination \"%s\".",
1321 						 Log_destination_string)));
1322 
1323 	whereToSendOutput = DestNone;
1324 
1325 	/*
1326 	 * Initialize stats collection subsystem (this does NOT start the
1327 	 * collector process!)
1328 	 */
1329 	pgstat_init();
1330 
1331 	/*
1332 	 * Initialize the autovacuum subsystem (again, no process start yet)
1333 	 */
1334 	autovac_init();
1335 
1336 	/*
1337 	 * Load configuration files for client authentication.
1338 	 */
1339 	if (!load_hba())
1340 	{
1341 		/*
1342 		 * It makes no sense to continue if we fail to load the HBA file,
1343 		 * since there is no way to connect to the database in this case.
1344 		 */
1345 		ereport(FATAL,
1346 				(errmsg("could not load pg_hba.conf")));
1347 	}
1348 	if (!load_ident())
1349 	{
1350 		/*
1351 		 * We can start up without the IDENT file, although it means that you
1352 		 * cannot log in using any of the authentication methods that need a
1353 		 * user name mapping. load_ident() already logged the details of error
1354 		 * to the log.
1355 		 */
1356 	}
1357 
1358 #ifdef HAVE_PTHREAD_IS_THREADED_NP
1359 
1360 	/*
1361 	 * On macOS, libintl replaces setlocale() with a version that calls
1362 	 * CFLocaleCopyCurrent() when its second argument is "" and every relevant
1363 	 * environment variable is unset or empty.  CFLocaleCopyCurrent() makes
1364 	 * the process multithreaded.  The postmaster calls sigprocmask() and
1365 	 * calls fork() without an immediate exec(), both of which have undefined
1366 	 * behavior in a multithreaded program.  A multithreaded postmaster is the
1367 	 * normal case on Windows, which offers neither fork() nor sigprocmask().
1368 	 */
1369 	if (pthread_is_threaded_np() != 0)
1370 		ereport(FATAL,
1371 				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1372 				 errmsg("postmaster became multithreaded during startup"),
1373 				 errhint("Set the LC_ALL environment variable to a valid locale.")));
1374 #endif
1375 
1376 	/*
1377 	 * Remember postmaster startup time
1378 	 */
1379 	PgStartTime = GetCurrentTimestamp();
1380 
1381 	/*
1382 	 * Report postmaster status in the postmaster.pid file, to allow pg_ctl to
1383 	 * see what's happening.
1384 	 */
1385 	AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STARTING);
1386 
1387 	/*
1388 	 * We're ready to rock and roll...
1389 	 */
1390 	StartupPID = StartupDataBase();
1391 	Assert(StartupPID != 0);
1392 	StartupStatus = STARTUP_RUNNING;
1393 	pmState = PM_STARTUP;
1394 
1395 	/* Some workers may be scheduled to start now */
1396 	maybe_start_bgworkers();
1397 
1398 	status = ServerLoop();
1399 
1400 	/*
1401 	 * ServerLoop probably shouldn't ever return, but if it does, close down.
1402 	 */
1403 	ExitPostmaster(status != STATUS_OK);
1404 
1405 	abort();					/* not reached */
1406 }
1407 
1408 
1409 /*
1410  * on_proc_exit callback to close server's listen sockets
1411  */
1412 static void
1413 CloseServerPorts(int status, Datum arg)
1414 {
1415 	int			i;
1416 
1417 	/*
1418 	 * First, explicitly close all the socket FDs.  We used to just let this
1419 	 * happen implicitly at postmaster exit, but it's better to close them
1420 	 * before we remove the postmaster.pid lockfile; otherwise there's a race
1421 	 * condition if a new postmaster wants to re-use the TCP port number.
1422 	 */
1423 	for (i = 0; i < MAXLISTEN; i++)
1424 	{
1425 		if (ListenSocket[i] != PGINVALID_SOCKET)
1426 		{
1427 			StreamClose(ListenSocket[i]);
1428 			ListenSocket[i] = PGINVALID_SOCKET;
1429 		}
1430 	}
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 	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 }
1444 
1445 /*
1446  * on_proc_exit callback to delete external_pid_file
1447  */
1448 static void
1449 unlink_external_pid_file(int status, Datum arg)
1450 {
1451 	if (external_pid_file)
1452 		unlink(external_pid_file);
1453 }
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 getInstallationPaths(const char *argv0)
1462 {
1463 	DIR		   *pdir;
1464 
1465 	/* Locate the postgres executable itself */
1466 	if (find_my_exec(argv0, my_exec_path) < 0)
1467 		elog(FATAL, "%s: could not locate my own executable path", argv0);
1468 
1469 #ifdef EXEC_BACKEND
1470 	/* Locate executable backend before we change working directory */
1471 	if (find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
1472 						postgres_exec_path) < 0)
1473 		ereport(FATAL,
1474 				(errmsg("%s: could not locate matching postgres executable",
1475 						argv0)));
1476 #endif
1477 
1478 	/*
1479 	 * Locate the pkglib directory --- this has to be set early in case we try
1480 	 * to load any modules from it in response to postgresql.conf entries.
1481 	 */
1482 	get_pkglib_path(my_exec_path, pkglib_path);
1483 
1484 	/*
1485 	 * Verify that there's a readable directory there; otherwise the Postgres
1486 	 * installation is incomplete or corrupt.  (A typical cause of this
1487 	 * failure is that the postgres executable has been moved or hardlinked to
1488 	 * some directory that's not a sibling of the installation lib/
1489 	 * directory.)
1490 	 */
1491 	pdir = AllocateDir(pkglib_path);
1492 	if (pdir == NULL)
1493 		ereport(ERROR,
1494 				(errcode_for_file_access(),
1495 				 errmsg("could not open directory \"%s\": %m",
1496 						pkglib_path),
1497 				 errhint("This may indicate an incomplete PostgreSQL installation, or that the file \"%s\" has been moved away from its proper location.",
1498 						 my_exec_path)));
1499 	FreeDir(pdir);
1500 
1501 	/*
1502 	 * XXX is it worth similarly checking the share/ directory?  If the lib/
1503 	 * directory is there, then share/ probably is too.
1504 	 */
1505 }
1506 
1507 /*
1508  * Check that pg_control exists in the correct location in the data directory.
1509  *
1510  * No attempt is made to validate the contents of pg_control here.  This is
1511  * just a sanity check to see if we are looking at a real data directory.
1512  */
1513 static void
1514 checkControlFile(void)
1515 {
1516 	char		path[MAXPGPATH];
1517 	FILE	   *fp;
1518 
1519 	snprintf(path, sizeof(path), "%s/global/pg_control", DataDir);
1520 
1521 	fp = AllocateFile(path, PG_BINARY_R);
1522 	if (fp == NULL)
1523 	{
1524 		write_stderr("%s: could not find the database system\n"
1525 					 "Expected to find it in the directory \"%s\",\n"
1526 					 "but could not open file \"%s\": %s\n",
1527 					 progname, DataDir, path, strerror(errno));
1528 		ExitPostmaster(2);
1529 	}
1530 	FreeFile(fp);
1531 }
1532 
1533 /*
1534  * Determine how long should we let ServerLoop sleep.
1535  *
1536  * In normal conditions we wait at most one minute, to ensure that the other
1537  * background tasks handled by ServerLoop get done even when no requests are
1538  * arriving.  However, if there are background workers waiting to be started,
1539  * we don't actually sleep so that they are quickly serviced.  Other exception
1540  * cases are as shown in the code.
1541  */
1542 static void
1543 DetermineSleepTime(struct timeval *timeout)
1544 {
1545 	TimestampTz next_wakeup = 0;
1546 
1547 	/*
1548 	 * Normal case: either there are no background workers at all, or we're in
1549 	 * a shutdown sequence (during which we ignore bgworkers altogether).
1550 	 */
1551 	if (Shutdown > NoShutdown ||
1552 		(!StartWorkerNeeded && !HaveCrashedWorker))
1553 	{
1554 		if (AbortStartTime != 0)
1555 		{
1556 			/* time left to abort; clamp to 0 in case it already expired */
1557 			timeout->tv_sec = SIGKILL_CHILDREN_AFTER_SECS -
1558 				(time(NULL) - AbortStartTime);
1559 			timeout->tv_sec = Max(timeout->tv_sec, 0);
1560 			timeout->tv_usec = 0;
1561 		}
1562 		else
1563 		{
1564 			timeout->tv_sec = 60;
1565 			timeout->tv_usec = 0;
1566 		}
1567 		return;
1568 	}
1569 
1570 	if (StartWorkerNeeded)
1571 	{
1572 		timeout->tv_sec = 0;
1573 		timeout->tv_usec = 0;
1574 		return;
1575 	}
1576 
1577 	if (HaveCrashedWorker)
1578 	{
1579 		slist_mutable_iter siter;
1580 
1581 		/*
1582 		 * When there are crashed bgworkers, we sleep just long enough that
1583 		 * they are restarted when they request to be.  Scan the list to
1584 		 * determine the minimum of all wakeup times according to most recent
1585 		 * crash time and requested restart interval.
1586 		 */
1587 		slist_foreach_modify(siter, &BackgroundWorkerList)
1588 		{
1589 			RegisteredBgWorker *rw;
1590 			TimestampTz this_wakeup;
1591 
1592 			rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
1593 
1594 			if (rw->rw_crashed_at == 0)
1595 				continue;
1596 
1597 			if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART
1598 				|| rw->rw_terminate)
1599 			{
1600 				ForgetBackgroundWorker(&siter);
1601 				continue;
1602 			}
1603 
1604 			this_wakeup = TimestampTzPlusMilliseconds(rw->rw_crashed_at,
1605 													  1000L * rw->rw_worker.bgw_restart_time);
1606 			if (next_wakeup == 0 || this_wakeup < next_wakeup)
1607 				next_wakeup = this_wakeup;
1608 		}
1609 	}
1610 
1611 	if (next_wakeup != 0)
1612 	{
1613 		long		secs;
1614 		int			microsecs;
1615 
1616 		TimestampDifference(GetCurrentTimestamp(), next_wakeup,
1617 							&secs, &microsecs);
1618 		timeout->tv_sec = secs;
1619 		timeout->tv_usec = microsecs;
1620 
1621 		/* Ensure we don't exceed one minute */
1622 		if (timeout->tv_sec > 60)
1623 		{
1624 			timeout->tv_sec = 60;
1625 			timeout->tv_usec = 0;
1626 		}
1627 	}
1628 	else
1629 	{
1630 		timeout->tv_sec = 60;
1631 		timeout->tv_usec = 0;
1632 	}
1633 }
1634 
1635 /*
1636  * Main idle loop of postmaster
1637  *
1638  * NB: Needs to be called with signals blocked
1639  */
1640 static int
1641 ServerLoop(void)
1642 {
1643 	fd_set		readmask;
1644 	int			nSockets;
1645 	time_t		last_lockfile_recheck_time,
1646 				last_touch_time;
1647 
1648 	last_lockfile_recheck_time = last_touch_time = time(NULL);
1649 
1650 	nSockets = initMasks(&readmask);
1651 
1652 	for (;;)
1653 	{
1654 		fd_set		rmask;
1655 		int			selres;
1656 		time_t		now;
1657 
1658 		/*
1659 		 * Wait for a connection request to arrive.
1660 		 *
1661 		 * We block all signals except while sleeping. That makes it safe for
1662 		 * signal handlers, which again block all signals while executing, to
1663 		 * do nontrivial work.
1664 		 *
1665 		 * If we are in PM_WAIT_DEAD_END state, then we don't want to accept
1666 		 * any new connections, so we don't call select(), and just sleep.
1667 		 */
1668 		memcpy((char *) &rmask, (char *) &readmask, sizeof(fd_set));
1669 
1670 		if (pmState == PM_WAIT_DEAD_END)
1671 		{
1672 			PG_SETMASK(&UnBlockSig);
1673 
1674 			pg_usleep(100000L); /* 100 msec seems reasonable */
1675 			selres = 0;
1676 
1677 			PG_SETMASK(&BlockSig);
1678 		}
1679 		else
1680 		{
1681 			/* must set timeout each time; some OSes change it! */
1682 			struct timeval timeout;
1683 
1684 			/* Needs to run with blocked signals! */
1685 			DetermineSleepTime(&timeout);
1686 
1687 			PG_SETMASK(&UnBlockSig);
1688 
1689 			selres = select(nSockets, &rmask, NULL, NULL, &timeout);
1690 
1691 			PG_SETMASK(&BlockSig);
1692 		}
1693 
1694 		/* Now check the select() result */
1695 		if (selres < 0)
1696 		{
1697 			if (errno != EINTR && errno != EWOULDBLOCK)
1698 			{
1699 				ereport(LOG,
1700 						(errcode_for_socket_access(),
1701 						 errmsg("select() failed in postmaster: %m")));
1702 				return STATUS_ERROR;
1703 			}
1704 		}
1705 
1706 		/*
1707 		 * New connection pending on any of our sockets? If so, fork a child
1708 		 * process to deal with it.
1709 		 */
1710 		if (selres > 0)
1711 		{
1712 			int			i;
1713 
1714 			for (i = 0; i < MAXLISTEN; i++)
1715 			{
1716 				if (ListenSocket[i] == PGINVALID_SOCKET)
1717 					break;
1718 				if (FD_ISSET(ListenSocket[i], &rmask))
1719 				{
1720 					Port	   *port;
1721 
1722 					port = ConnCreate(ListenSocket[i]);
1723 					if (port)
1724 					{
1725 						BackendStartup(port);
1726 
1727 						/*
1728 						 * We no longer need the open socket or port structure
1729 						 * in this process
1730 						 */
1731 						StreamClose(port->sock);
1732 						ConnFree(port);
1733 					}
1734 				}
1735 			}
1736 		}
1737 
1738 		/* If we have lost the log collector, try to start a new one */
1739 		if (SysLoggerPID == 0 && Logging_collector)
1740 			SysLoggerPID = SysLogger_Start();
1741 
1742 		/*
1743 		 * If no background writer process is running, and we are not in a
1744 		 * state that prevents it, start one.  It doesn't matter if this
1745 		 * fails, we'll just try again later.  Likewise for the checkpointer.
1746 		 */
1747 		if (pmState == PM_RUN || pmState == PM_RECOVERY ||
1748 			pmState == PM_HOT_STANDBY)
1749 		{
1750 			if (CheckpointerPID == 0)
1751 				CheckpointerPID = StartCheckpointer();
1752 			if (BgWriterPID == 0)
1753 				BgWriterPID = StartBackgroundWriter();
1754 		}
1755 
1756 		/*
1757 		 * Likewise, if we have lost the walwriter process, try to start a new
1758 		 * one.  But this is needed only in normal operation (else we cannot
1759 		 * be writing any new WAL).
1760 		 */
1761 		if (WalWriterPID == 0 && pmState == PM_RUN)
1762 			WalWriterPID = StartWalWriter();
1763 
1764 		/*
1765 		 * If we have lost the autovacuum launcher, try to start a new one. We
1766 		 * don't want autovacuum to run in binary upgrade mode because
1767 		 * autovacuum might update relfrozenxid for empty tables before the
1768 		 * physical files are put in place.
1769 		 */
1770 		if (!IsBinaryUpgrade && AutoVacPID == 0 &&
1771 			(AutoVacuumingActive() || start_autovac_launcher) &&
1772 			pmState == PM_RUN)
1773 		{
1774 			AutoVacPID = StartAutoVacLauncher();
1775 			if (AutoVacPID != 0)
1776 				start_autovac_launcher = false; /* signal processed */
1777 		}
1778 
1779 		/* If we have lost the stats collector, try to start a new one */
1780 		if (PgStatPID == 0 &&
1781 			(pmState == PM_RUN || pmState == PM_HOT_STANDBY))
1782 			PgStatPID = pgstat_start();
1783 
1784 		/* If we have lost the archiver, try to start a new one. */
1785 		if (PgArchPID == 0 && PgArchStartupAllowed())
1786 			PgArchPID = pgarch_start();
1787 
1788 		/* If we need to signal the autovacuum launcher, do so now */
1789 		if (avlauncher_needs_signal)
1790 		{
1791 			avlauncher_needs_signal = false;
1792 			if (AutoVacPID != 0)
1793 				kill(AutoVacPID, SIGUSR2);
1794 		}
1795 
1796 		/* If we need to start a WAL receiver, try to do that now */
1797 		if (WalReceiverRequested)
1798 			MaybeStartWalReceiver();
1799 
1800 		/* Get other worker processes running, if needed */
1801 		if (StartWorkerNeeded || HaveCrashedWorker)
1802 			maybe_start_bgworkers();
1803 
1804 #ifdef HAVE_PTHREAD_IS_THREADED_NP
1805 
1806 		/*
1807 		 * With assertions enabled, check regularly for appearance of
1808 		 * additional threads.  All builds check at start and exit.
1809 		 */
1810 		Assert(pthread_is_threaded_np() == 0);
1811 #endif
1812 
1813 		/*
1814 		 * Lastly, check to see if it's time to do some things that we don't
1815 		 * want to do every single time through the loop, because they're a
1816 		 * bit expensive.  Note that there's up to a minute of slop in when
1817 		 * these tasks will be performed, since DetermineSleepTime() will let
1818 		 * us sleep at most that long; except for SIGKILL timeout which has
1819 		 * special-case logic there.
1820 		 */
1821 		now = time(NULL);
1822 
1823 		/*
1824 		 * If we already sent SIGQUIT to children and they are slow to shut
1825 		 * down, it's time to send them SIGKILL.  This doesn't happen
1826 		 * normally, but under certain conditions backends can get stuck while
1827 		 * shutting down.  This is a last measure to get them unwedged.
1828 		 *
1829 		 * Note we also do this during recovery from a process crash.
1830 		 */
1831 		if ((Shutdown >= ImmediateShutdown || (FatalError && !SendStop)) &&
1832 			AbortStartTime != 0 &&
1833 			(now - AbortStartTime) >= SIGKILL_CHILDREN_AFTER_SECS)
1834 		{
1835 			/* We were gentle with them before. Not anymore */
1836 			TerminateChildren(SIGKILL);
1837 			/* reset flag so we don't SIGKILL again */
1838 			AbortStartTime = 0;
1839 		}
1840 
1841 		/*
1842 		 * Once a minute, verify that postmaster.pid hasn't been removed or
1843 		 * overwritten.  If it has, we force a shutdown.  This avoids having
1844 		 * postmasters and child processes hanging around after their database
1845 		 * is gone, and maybe causing problems if a new database cluster is
1846 		 * created in the same place.  It also provides some protection
1847 		 * against a DBA foolishly removing postmaster.pid and manually
1848 		 * starting a new postmaster.  Data corruption is likely to ensue from
1849 		 * that anyway, but we can minimize the damage by aborting ASAP.
1850 		 */
1851 		if (now - last_lockfile_recheck_time >= 1 * SECS_PER_MINUTE)
1852 		{
1853 			if (!RecheckDataDirLockFile())
1854 			{
1855 				ereport(LOG,
1856 						(errmsg("performing immediate shutdown because data directory lock file is invalid")));
1857 				kill(MyProcPid, SIGQUIT);
1858 			}
1859 			last_lockfile_recheck_time = now;
1860 		}
1861 
1862 		/*
1863 		 * Touch Unix socket and lock files every 58 minutes, to ensure that
1864 		 * they are not removed by overzealous /tmp-cleaning tasks.  We assume
1865 		 * no one runs cleaners with cutoff times of less than an hour ...
1866 		 */
1867 		if (now - last_touch_time >= 58 * SECS_PER_MINUTE)
1868 		{
1869 			TouchSocketFiles();
1870 			TouchSocketLockFiles();
1871 			last_touch_time = now;
1872 		}
1873 	}
1874 }
1875 
1876 /*
1877  * Initialise the masks for select() for the ports we are listening on.
1878  * Return the number of sockets to listen on.
1879  */
1880 static int
1881 initMasks(fd_set *rmask)
1882 {
1883 	int			maxsock = -1;
1884 	int			i;
1885 
1886 	FD_ZERO(rmask);
1887 
1888 	for (i = 0; i < MAXLISTEN; i++)
1889 	{
1890 		int			fd = ListenSocket[i];
1891 
1892 		if (fd == PGINVALID_SOCKET)
1893 			break;
1894 		FD_SET(fd, rmask);
1895 
1896 		if (fd > maxsock)
1897 			maxsock = fd;
1898 	}
1899 
1900 	return maxsock + 1;
1901 }
1902 
1903 
1904 /*
1905  * Read a client's startup packet and do something according to it.
1906  *
1907  * Returns STATUS_OK or STATUS_ERROR, or might call ereport(FATAL) and
1908  * not return at all.
1909  *
1910  * (Note that ereport(FATAL) stuff is sent to the client, so only use it
1911  * if that's what you want.  Return STATUS_ERROR if you don't want to
1912  * send anything to the client, which would typically be appropriate
1913  * if we detect a communications failure.)
1914  *
1915  * Set ssl_done and/or gss_done when negotiation of an encrypted layer
1916  * (currently, TLS or GSSAPI) is completed. A successful negotiation of either
1917  * encryption layer sets both flags, but a rejected negotiation sets only the
1918  * flag for that layer, since the client may wish to try the other one. We
1919  * should make no assumption here about the order in which the client may make
1920  * requests.
1921  */
1922 static int
1923 ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
1924 {
1925 	int32		len;
1926 	void	   *buf;
1927 	ProtocolVersion proto;
1928 	MemoryContext oldcontext;
1929 
1930 	pq_startmsgread();
1931 
1932 	/*
1933 	 * Grab the first byte of the length word separately, so that we can tell
1934 	 * whether we have no data at all or an incomplete packet.  (This might
1935 	 * sound inefficient, but it's not really, because of buffering in
1936 	 * pqcomm.c.)
1937 	 */
1938 	if (pq_getbytes((char *) &len, 1) == EOF)
1939 	{
1940 		/*
1941 		 * If we get no data at all, don't clutter the log with a complaint;
1942 		 * such cases often occur for legitimate reasons.  An example is that
1943 		 * we might be here after responding to NEGOTIATE_SSL_CODE, and if the
1944 		 * client didn't like our response, it'll probably just drop the
1945 		 * connection.  Service-monitoring software also often just opens and
1946 		 * closes a connection without sending anything.  (So do port
1947 		 * scanners, which may be less benign, but it's not really our job to
1948 		 * notice those.)
1949 		 */
1950 		return STATUS_ERROR;
1951 	}
1952 
1953 	if (pq_getbytes(((char *) &len) + 1, 3) == EOF)
1954 	{
1955 		/* Got a partial length word, so bleat about that */
1956 		if (!ssl_done && !gss_done)
1957 			ereport(COMMERROR,
1958 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
1959 					 errmsg("incomplete startup packet")));
1960 		return STATUS_ERROR;
1961 	}
1962 
1963 	len = pg_ntoh32(len);
1964 	len -= 4;
1965 
1966 	if (len < (int32) sizeof(ProtocolVersion) ||
1967 		len > MAX_STARTUP_PACKET_LENGTH)
1968 	{
1969 		ereport(COMMERROR,
1970 				(errcode(ERRCODE_PROTOCOL_VIOLATION),
1971 				 errmsg("invalid length of startup packet")));
1972 		return STATUS_ERROR;
1973 	}
1974 
1975 	/*
1976 	 * Allocate at least the size of an old-style startup packet, plus one
1977 	 * extra byte, and make sure all are zeroes.  This ensures we will have
1978 	 * null termination of all strings, in both fixed- and variable-length
1979 	 * packet layouts.
1980 	 */
1981 	if (len <= (int32) sizeof(StartupPacket))
1982 		buf = palloc0(sizeof(StartupPacket) + 1);
1983 	else
1984 		buf = palloc0(len + 1);
1985 
1986 	if (pq_getbytes(buf, len) == EOF)
1987 	{
1988 		ereport(COMMERROR,
1989 				(errcode(ERRCODE_PROTOCOL_VIOLATION),
1990 				 errmsg("incomplete startup packet")));
1991 		return STATUS_ERROR;
1992 	}
1993 	pq_endmsgread();
1994 
1995 	/*
1996 	 * The first field is either a protocol version number or a special
1997 	 * request code.
1998 	 */
1999 	port->proto = proto = pg_ntoh32(*((ProtocolVersion *) buf));
2000 
2001 	if (proto == CANCEL_REQUEST_CODE)
2002 	{
2003 		processCancelRequest(port, buf);
2004 		/* Not really an error, but we don't want to proceed further */
2005 		return STATUS_ERROR;
2006 	}
2007 
2008 	if (proto == NEGOTIATE_SSL_CODE && !ssl_done)
2009 	{
2010 		char		SSLok;
2011 
2012 #ifdef USE_SSL
2013 		/* No SSL when disabled or on Unix sockets */
2014 		if (!LoadedSSL || IS_AF_UNIX(port->laddr.addr.ss_family))
2015 			SSLok = 'N';
2016 		else
2017 			SSLok = 'S';		/* Support for SSL */
2018 #else
2019 		SSLok = 'N';			/* No support for SSL */
2020 #endif
2021 
2022 retry1:
2023 		if (send(port->sock, &SSLok, 1, 0) != 1)
2024 		{
2025 			if (errno == EINTR)
2026 				goto retry1;	/* if interrupted, just retry */
2027 			ereport(COMMERROR,
2028 					(errcode_for_socket_access(),
2029 					 errmsg("failed to send SSL negotiation response: %m")));
2030 			return STATUS_ERROR;	/* close the connection */
2031 		}
2032 
2033 #ifdef USE_SSL
2034 		if (SSLok == 'S' && secure_open_server(port) == -1)
2035 			return STATUS_ERROR;
2036 #endif
2037 
2038 		/*
2039 		 * At this point we should have no data already buffered.  If we do,
2040 		 * it was received before we performed the SSL handshake, so it wasn't
2041 		 * encrypted and indeed may have been injected by a man-in-the-middle.
2042 		 * We report this case to the client.
2043 		 */
2044 		if (pq_buffer_has_data())
2045 			ereport(FATAL,
2046 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
2047 					 errmsg("received unencrypted data after SSL request"),
2048 					 errdetail("This could be either a client-software bug or evidence of an attempted man-in-the-middle attack.")));
2049 
2050 		/*
2051 		 * regular startup packet, cancel, etc packet should follow, but not
2052 		 * another SSL negotiation request, and a GSS request should only
2053 		 * follow if SSL was rejected (client may negotiate in either order)
2054 		 */
2055 		return ProcessStartupPacket(port, true, SSLok == 'S');
2056 	}
2057 	else if (proto == NEGOTIATE_GSS_CODE && !gss_done)
2058 	{
2059 		char		GSSok = 'N';
2060 
2061 #ifdef ENABLE_GSS
2062 		/* No GSSAPI encryption when on Unix socket */
2063 		if (!IS_AF_UNIX(port->laddr.addr.ss_family))
2064 			GSSok = 'G';
2065 #endif
2066 
2067 		while (send(port->sock, &GSSok, 1, 0) != 1)
2068 		{
2069 			if (errno == EINTR)
2070 				continue;
2071 			ereport(COMMERROR,
2072 					(errcode_for_socket_access(),
2073 					 errmsg("failed to send GSSAPI negotiation response: %m")));
2074 			return STATUS_ERROR;	/* close the connection */
2075 		}
2076 
2077 #ifdef ENABLE_GSS
2078 		if (GSSok == 'G' && secure_open_gssapi(port) == -1)
2079 			return STATUS_ERROR;
2080 #endif
2081 
2082 		/*
2083 		 * At this point we should have no data already buffered.  If we do,
2084 		 * it was received before we performed the GSS handshake, so it wasn't
2085 		 * encrypted and indeed may have been injected by a man-in-the-middle.
2086 		 * We report this case to the client.
2087 		 */
2088 		if (pq_buffer_has_data())
2089 			ereport(FATAL,
2090 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
2091 					 errmsg("received unencrypted data after GSSAPI encryption request"),
2092 					 errdetail("This could be either a client-software bug or evidence of an attempted man-in-the-middle attack.")));
2093 
2094 		/*
2095 		 * regular startup packet, cancel, etc packet should follow, but not
2096 		 * another GSS negotiation request, and an SSL request should only
2097 		 * follow if GSS was rejected (client may negotiate in either order)
2098 		 */
2099 		return ProcessStartupPacket(port, GSSok == 'G', true);
2100 	}
2101 
2102 	/* Could add additional special packet types here */
2103 
2104 	/*
2105 	 * Set FrontendProtocol now so that ereport() knows what format to send if
2106 	 * we fail during startup.
2107 	 */
2108 	FrontendProtocol = proto;
2109 
2110 	/* Check that the major protocol version is in range. */
2111 	if (PG_PROTOCOL_MAJOR(proto) < PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST) ||
2112 		PG_PROTOCOL_MAJOR(proto) > PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST))
2113 		ereport(FATAL,
2114 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2115 				 errmsg("unsupported frontend protocol %u.%u: server supports %u.0 to %u.%u",
2116 						PG_PROTOCOL_MAJOR(proto), PG_PROTOCOL_MINOR(proto),
2117 						PG_PROTOCOL_MAJOR(PG_PROTOCOL_EARLIEST),
2118 						PG_PROTOCOL_MAJOR(PG_PROTOCOL_LATEST),
2119 						PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST))));
2120 
2121 	/*
2122 	 * Now fetch parameters out of startup packet and save them into the Port
2123 	 * structure.  All data structures attached to the Port struct must be
2124 	 * allocated in TopMemoryContext so that they will remain available in a
2125 	 * running backend (even after PostmasterContext is destroyed).  We need
2126 	 * not worry about leaking this storage on failure, since we aren't in the
2127 	 * postmaster process anymore.
2128 	 */
2129 	oldcontext = MemoryContextSwitchTo(TopMemoryContext);
2130 
2131 	if (PG_PROTOCOL_MAJOR(proto) >= 3)
2132 	{
2133 		int32		offset = sizeof(ProtocolVersion);
2134 		List	   *unrecognized_protocol_options = NIL;
2135 
2136 		/*
2137 		 * Scan packet body for name/option pairs.  We can assume any string
2138 		 * beginning within the packet body is null-terminated, thanks to
2139 		 * zeroing extra byte above.
2140 		 */
2141 		port->guc_options = NIL;
2142 
2143 		while (offset < len)
2144 		{
2145 			char	   *nameptr = ((char *) buf) + offset;
2146 			int32		valoffset;
2147 			char	   *valptr;
2148 
2149 			if (*nameptr == '\0')
2150 				break;			/* found packet terminator */
2151 			valoffset = offset + strlen(nameptr) + 1;
2152 			if (valoffset >= len)
2153 				break;			/* missing value, will complain below */
2154 			valptr = ((char *) buf) + valoffset;
2155 
2156 			if (strcmp(nameptr, "database") == 0)
2157 				port->database_name = pstrdup(valptr);
2158 			else if (strcmp(nameptr, "user") == 0)
2159 				port->user_name = pstrdup(valptr);
2160 			else if (strcmp(nameptr, "options") == 0)
2161 				port->cmdline_options = pstrdup(valptr);
2162 			else if (strcmp(nameptr, "replication") == 0)
2163 			{
2164 				/*
2165 				 * Due to backward compatibility concerns the replication
2166 				 * parameter is a hybrid beast which allows the value to be
2167 				 * either boolean or the string 'database'. The latter
2168 				 * connects to a specific database which is e.g. required for
2169 				 * logical decoding while.
2170 				 */
2171 				if (strcmp(valptr, "database") == 0)
2172 				{
2173 					am_walsender = true;
2174 					am_db_walsender = true;
2175 				}
2176 				else if (!parse_bool(valptr, &am_walsender))
2177 					ereport(FATAL,
2178 							(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2179 							 errmsg("invalid value for parameter \"%s\": \"%s\"",
2180 									"replication",
2181 									valptr),
2182 							 errhint("Valid values are: \"false\", 0, \"true\", 1, \"database\".")));
2183 			}
2184 			else if (strncmp(nameptr, "_pq_.", 5) == 0)
2185 			{
2186 				/*
2187 				 * Any option beginning with _pq_. is reserved for use as a
2188 				 * protocol-level option, but at present no such options are
2189 				 * defined.
2190 				 */
2191 				unrecognized_protocol_options =
2192 					lappend(unrecognized_protocol_options, pstrdup(nameptr));
2193 			}
2194 			else
2195 			{
2196 				/* Assume it's a generic GUC option */
2197 				port->guc_options = lappend(port->guc_options,
2198 											pstrdup(nameptr));
2199 				port->guc_options = lappend(port->guc_options,
2200 											pstrdup(valptr));
2201 
2202 				/*
2203 				 * Copy application_name to port if we come across it.  This
2204 				 * is done so we can log the application_name in the
2205 				 * connection authorization message.  Note that the GUC would
2206 				 * be used but we haven't gone through GUC setup yet.
2207 				 */
2208 				if (strcmp(nameptr, "application_name") == 0)
2209 				{
2210 					char	   *tmp_app_name = pstrdup(valptr);
2211 
2212 					pg_clean_ascii(tmp_app_name);
2213 
2214 					port->application_name = tmp_app_name;
2215 				}
2216 			}
2217 			offset = valoffset + strlen(valptr) + 1;
2218 		}
2219 
2220 		/*
2221 		 * If we didn't find a packet terminator exactly at the end of the
2222 		 * given packet length, complain.
2223 		 */
2224 		if (offset != len - 1)
2225 			ereport(FATAL,
2226 					(errcode(ERRCODE_PROTOCOL_VIOLATION),
2227 					 errmsg("invalid startup packet layout: expected terminator as last byte")));
2228 
2229 		/*
2230 		 * If the client requested a newer protocol version or if the client
2231 		 * requested any protocol options we didn't recognize, let them know
2232 		 * the newest minor protocol version we do support and the names of
2233 		 * any unrecognized options.
2234 		 */
2235 		if (PG_PROTOCOL_MINOR(proto) > PG_PROTOCOL_MINOR(PG_PROTOCOL_LATEST) ||
2236 			unrecognized_protocol_options != NIL)
2237 			SendNegotiateProtocolVersion(unrecognized_protocol_options);
2238 	}
2239 	else
2240 	{
2241 		/*
2242 		 * Get the parameters from the old-style, fixed-width-fields startup
2243 		 * packet as C strings.  The packet destination was cleared first so a
2244 		 * short packet has zeros silently added.  We have to be prepared to
2245 		 * truncate the pstrdup result for oversize fields, though.
2246 		 */
2247 		StartupPacket *packet = (StartupPacket *) buf;
2248 
2249 		port->database_name = pstrdup(packet->database);
2250 		if (strlen(port->database_name) > sizeof(packet->database))
2251 			port->database_name[sizeof(packet->database)] = '\0';
2252 		port->user_name = pstrdup(packet->user);
2253 		if (strlen(port->user_name) > sizeof(packet->user))
2254 			port->user_name[sizeof(packet->user)] = '\0';
2255 		port->cmdline_options = pstrdup(packet->options);
2256 		if (strlen(port->cmdline_options) > sizeof(packet->options))
2257 			port->cmdline_options[sizeof(packet->options)] = '\0';
2258 		port->guc_options = NIL;
2259 	}
2260 
2261 	/* Check a user name was given. */
2262 	if (port->user_name == NULL || port->user_name[0] == '\0')
2263 		ereport(FATAL,
2264 				(errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
2265 				 errmsg("no PostgreSQL user name specified in startup packet")));
2266 
2267 	/* The database defaults to the user name. */
2268 	if (port->database_name == NULL || port->database_name[0] == '\0')
2269 		port->database_name = pstrdup(port->user_name);
2270 
2271 	if (Db_user_namespace)
2272 	{
2273 		/*
2274 		 * If user@, it is a global user, remove '@'. We only want to do this
2275 		 * if there is an '@' at the end and no earlier in the user string or
2276 		 * they may fake as a local user of another database attaching to this
2277 		 * database.
2278 		 */
2279 		if (strchr(port->user_name, '@') ==
2280 			port->user_name + strlen(port->user_name) - 1)
2281 			*strchr(port->user_name, '@') = '\0';
2282 		else
2283 		{
2284 			/* Append '@' and dbname */
2285 			port->user_name = psprintf("%s@%s", port->user_name, port->database_name);
2286 		}
2287 	}
2288 
2289 	/*
2290 	 * Truncate given database and user names to length of a Postgres name.
2291 	 * This avoids lookup failures when overlength names are given.
2292 	 */
2293 	if (strlen(port->database_name) >= NAMEDATALEN)
2294 		port->database_name[NAMEDATALEN - 1] = '\0';
2295 	if (strlen(port->user_name) >= NAMEDATALEN)
2296 		port->user_name[NAMEDATALEN - 1] = '\0';
2297 
2298 	/*
2299 	 * Normal walsender backends, e.g. for streaming replication, are not
2300 	 * connected to a particular database. But walsenders used for logical
2301 	 * replication need to connect to a specific database. We allow streaming
2302 	 * replication commands to be issued even if connected to a database as it
2303 	 * can make sense to first make a basebackup and then stream changes
2304 	 * starting from that.
2305 	 */
2306 	if (am_walsender && !am_db_walsender)
2307 		port->database_name[0] = '\0';
2308 
2309 	/*
2310 	 * Done putting stuff in TopMemoryContext.
2311 	 */
2312 	MemoryContextSwitchTo(oldcontext);
2313 
2314 	/*
2315 	 * If we're going to reject the connection due to database state, say so
2316 	 * now instead of wasting cycles on an authentication exchange. (This also
2317 	 * allows a pg_ping utility to be written.)
2318 	 */
2319 	switch (port->canAcceptConnections)
2320 	{
2321 		case CAC_STARTUP:
2322 			ereport(FATAL,
2323 					(errcode(ERRCODE_CANNOT_CONNECT_NOW),
2324 					 errmsg("the database system is starting up")));
2325 			break;
2326 		case CAC_SHUTDOWN:
2327 			ereport(FATAL,
2328 					(errcode(ERRCODE_CANNOT_CONNECT_NOW),
2329 					 errmsg("the database system is shutting down")));
2330 			break;
2331 		case CAC_RECOVERY:
2332 			ereport(FATAL,
2333 					(errcode(ERRCODE_CANNOT_CONNECT_NOW),
2334 					 errmsg("the database system is in recovery mode")));
2335 			break;
2336 		case CAC_TOOMANY:
2337 			ereport(FATAL,
2338 					(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
2339 					 errmsg("sorry, too many clients already")));
2340 			break;
2341 		case CAC_SUPERUSER:
2342 			/* OK for now, will check in InitPostgres */
2343 			break;
2344 		case CAC_OK:
2345 			break;
2346 	}
2347 
2348 	return STATUS_OK;
2349 }
2350 
2351 /*
2352  * Send a NegotiateProtocolVersion to the client.  This lets the client know
2353  * that they have requested a newer minor protocol version than we are able
2354  * to speak.  We'll speak the highest version we know about; the client can,
2355  * of course, abandon the connection if that's a problem.
2356  *
2357  * We also include in the response a list of protocol options we didn't
2358  * understand.  This allows clients to include optional parameters that might
2359  * be present either in newer protocol versions or third-party protocol
2360  * extensions without fear of having to reconnect if those options are not
2361  * understood, while at the same time making certain that the client is aware
2362  * of which options were actually accepted.
2363  */
2364 static void
2365 SendNegotiateProtocolVersion(List *unrecognized_protocol_options)
2366 {
2367 	StringInfoData buf;
2368 	ListCell   *lc;
2369 
2370 	pq_beginmessage(&buf, 'v'); /* NegotiateProtocolVersion */
2371 	pq_sendint32(&buf, PG_PROTOCOL_LATEST);
2372 	pq_sendint32(&buf, list_length(unrecognized_protocol_options));
2373 	foreach(lc, unrecognized_protocol_options)
2374 		pq_sendstring(&buf, lfirst(lc));
2375 	pq_endmessage(&buf);
2376 
2377 	/* no need to flush, some other message will follow */
2378 }
2379 
2380 /*
2381  * The client has sent a cancel request packet, not a normal
2382  * start-a-new-connection packet.  Perform the necessary processing.
2383  * Nothing is sent back to the client.
2384  */
2385 static void
2386 processCancelRequest(Port *port, void *pkt)
2387 {
2388 	CancelRequestPacket *canc = (CancelRequestPacket *) pkt;
2389 	int			backendPID;
2390 	int32		cancelAuthCode;
2391 	Backend    *bp;
2392 
2393 #ifndef EXEC_BACKEND
2394 	dlist_iter	iter;
2395 #else
2396 	int			i;
2397 #endif
2398 
2399 	backendPID = (int) pg_ntoh32(canc->backendPID);
2400 	cancelAuthCode = (int32) pg_ntoh32(canc->cancelAuthCode);
2401 
2402 	/*
2403 	 * See if we have a matching backend.  In the EXEC_BACKEND case, we can no
2404 	 * longer access the postmaster's own backend list, and must rely on the
2405 	 * duplicate array in shared memory.
2406 	 */
2407 #ifndef EXEC_BACKEND
2408 	dlist_foreach(iter, &BackendList)
2409 	{
2410 		bp = dlist_container(Backend, elem, iter.cur);
2411 #else
2412 	for (i = MaxLivePostmasterChildren() - 1; i >= 0; i--)
2413 	{
2414 		bp = (Backend *) &ShmemBackendArray[i];
2415 #endif
2416 		if (bp->pid == backendPID)
2417 		{
2418 			if (bp->cancel_key == cancelAuthCode)
2419 			{
2420 				/* Found a match; signal that backend to cancel current op */
2421 				ereport(DEBUG2,
2422 						(errmsg_internal("processing cancel request: sending SIGINT to process %d",
2423 										 backendPID)));
2424 				signal_child(bp->pid, SIGINT);
2425 			}
2426 			else
2427 				/* Right PID, wrong key: no way, Jose */
2428 				ereport(LOG,
2429 						(errmsg("wrong key in cancel request for process %d",
2430 								backendPID)));
2431 			return;
2432 		}
2433 #ifndef EXEC_BACKEND			/* make GNU Emacs 26.1 see brace balance */
2434 	}
2435 #else
2436 	}
2437 #endif
2438 
2439 	/* No matching backend */
2440 	ereport(LOG,
2441 			(errmsg("PID %d in cancel request did not match any process",
2442 					backendPID)));
2443 }
2444 
2445 /*
2446  * canAcceptConnections --- check to see if database state allows connections
2447  * of the specified type.  backend_type can be BACKEND_TYPE_NORMAL,
2448  * BACKEND_TYPE_AUTOVAC, or BACKEND_TYPE_BGWORKER.  (Note that we don't yet
2449  * know whether a NORMAL connection might turn into a walsender.)
2450  */
2451 static CAC_state
2452 canAcceptConnections(int backend_type)
2453 {
2454 	CAC_state	result = CAC_OK;
2455 
2456 	/*
2457 	 * Can't start backends when in startup/shutdown/inconsistent recovery
2458 	 * state.  We treat autovac workers the same as user backends for this
2459 	 * purpose.  However, bgworkers are excluded from this test; we expect
2460 	 * bgworker_should_start_now() decided whether the DB state allows them.
2461 	 */
2462 	if (pmState != PM_RUN && pmState != PM_HOT_STANDBY &&
2463 		backend_type != BACKEND_TYPE_BGWORKER)
2464 	{
2465 		if (Shutdown > NoShutdown)
2466 			return CAC_SHUTDOWN;	/* shutdown is pending */
2467 		else if (!FatalError &&
2468 				 (pmState == PM_STARTUP ||
2469 				  pmState == PM_RECOVERY))
2470 			return CAC_STARTUP; /* normal startup */
2471 		else
2472 			return CAC_RECOVERY;	/* else must be crash recovery */
2473 	}
2474 
2475 	/*
2476 	 * "Smart shutdown" restrictions are applied only to normal connections,
2477 	 * not to autovac workers or bgworkers.  When only superusers can connect,
2478 	 * we return CAC_SUPERUSER to indicate that superuserness must be checked
2479 	 * later.  Note that neither CAC_OK nor CAC_SUPERUSER can safely be
2480 	 * returned until we have checked for too many children.
2481 	 */
2482 	if (connsAllowed != ALLOW_ALL_CONNS &&
2483 		backend_type == BACKEND_TYPE_NORMAL)
2484 	{
2485 		if (connsAllowed == ALLOW_SUPERUSER_CONNS)
2486 			result = CAC_SUPERUSER; /* allow superusers only */
2487 		else
2488 			return CAC_SHUTDOWN;	/* shutdown is pending */
2489 	}
2490 
2491 	/*
2492 	 * Don't start too many children.
2493 	 *
2494 	 * We allow more connections here than we can have backends because some
2495 	 * might still be authenticating; they might fail auth, or some existing
2496 	 * backend might exit before the auth cycle is completed.  The exact
2497 	 * MaxBackends limit is enforced when a new backend tries to join the
2498 	 * shared-inval backend array.
2499 	 *
2500 	 * The limit here must match the sizes of the per-child-process arrays;
2501 	 * see comments for MaxLivePostmasterChildren().
2502 	 */
2503 	if (CountChildren(BACKEND_TYPE_ALL) >= MaxLivePostmasterChildren())
2504 		result = CAC_TOOMANY;
2505 
2506 	return result;
2507 }
2508 
2509 
2510 /*
2511  * ConnCreate -- create a local connection data structure
2512  *
2513  * Returns NULL on failure, other than out-of-memory which is fatal.
2514  */
2515 static Port *
2516 ConnCreate(int serverFd)
2517 {
2518 	Port	   *port;
2519 
2520 	if (!(port = (Port *) calloc(1, sizeof(Port))))
2521 	{
2522 		ereport(LOG,
2523 				(errcode(ERRCODE_OUT_OF_MEMORY),
2524 				 errmsg("out of memory")));
2525 		ExitPostmaster(1);
2526 	}
2527 
2528 	if (StreamConnection(serverFd, port) != STATUS_OK)
2529 	{
2530 		if (port->sock != PGINVALID_SOCKET)
2531 			StreamClose(port->sock);
2532 		ConnFree(port);
2533 		return NULL;
2534 	}
2535 
2536 	return port;
2537 }
2538 
2539 
2540 /*
2541  * ConnFree -- free a local connection data structure
2542  *
2543  * Caller has already closed the socket if any, so there's not much
2544  * to do here.
2545  */
2546 static void
2547 ConnFree(Port *conn)
2548 {
2549 	free(conn);
2550 }
2551 
2552 
2553 /*
2554  * ClosePostmasterPorts -- close all the postmaster's open sockets
2555  *
2556  * This is called during child process startup to release file descriptors
2557  * that are not needed by that child process.  The postmaster still has
2558  * them open, of course.
2559  *
2560  * Note: we pass am_syslogger as a boolean because we don't want to set
2561  * the global variable yet when this is called.
2562  */
2563 void
2564 ClosePostmasterPorts(bool am_syslogger)
2565 {
2566 	int			i;
2567 
2568 #ifndef WIN32
2569 
2570 	/*
2571 	 * Close the write end of postmaster death watch pipe. It's important to
2572 	 * do this as early as possible, so that if postmaster dies, others won't
2573 	 * think that it's still running because we're holding the pipe open.
2574 	 */
2575 	if (close(postmaster_alive_fds[POSTMASTER_FD_OWN]))
2576 		ereport(FATAL,
2577 				(errcode_for_file_access(),
2578 				 errmsg_internal("could not close postmaster death monitoring pipe in child process: %m")));
2579 	postmaster_alive_fds[POSTMASTER_FD_OWN] = -1;
2580 #endif
2581 
2582 	/* Close the listen sockets */
2583 	for (i = 0; i < MAXLISTEN; i++)
2584 	{
2585 		if (ListenSocket[i] != PGINVALID_SOCKET)
2586 		{
2587 			StreamClose(ListenSocket[i]);
2588 			ListenSocket[i] = PGINVALID_SOCKET;
2589 		}
2590 	}
2591 
2592 	/* If using syslogger, close the read side of the pipe */
2593 	if (!am_syslogger)
2594 	{
2595 #ifndef WIN32
2596 		if (syslogPipe[0] >= 0)
2597 			close(syslogPipe[0]);
2598 		syslogPipe[0] = -1;
2599 #else
2600 		if (syslogPipe[0])
2601 			CloseHandle(syslogPipe[0]);
2602 		syslogPipe[0] = 0;
2603 #endif
2604 	}
2605 
2606 #ifdef USE_BONJOUR
2607 	/* If using Bonjour, close the connection to the mDNS daemon */
2608 	if (bonjour_sdref)
2609 		close(DNSServiceRefSockFD(bonjour_sdref));
2610 #endif
2611 }
2612 
2613 
2614 /*
2615  * InitProcessGlobals -- set MyProcPid, MyStartTime[stamp], random seeds
2616  *
2617  * Called early in the postmaster and every backend.
2618  */
2619 void
2620 InitProcessGlobals(void)
2621 {
2622 	unsigned int rseed;
2623 
2624 	MyProcPid = getpid();
2625 	MyStartTimestamp = GetCurrentTimestamp();
2626 	MyStartTime = timestamptz_to_time_t(MyStartTimestamp);
2627 
2628 	/*
2629 	 * Set a different seed for random() in every process.  We want something
2630 	 * unpredictable, so if possible, use high-quality random bits for the
2631 	 * seed.  Otherwise, fall back to a seed based on timestamp and PID.
2632 	 */
2633 	if (!pg_strong_random(&rseed, sizeof(rseed)))
2634 	{
2635 		/*
2636 		 * Since PIDs and timestamps tend to change more frequently in their
2637 		 * least significant bits, shift the timestamp left to allow a larger
2638 		 * total number of seeds in a given time period.  Since that would
2639 		 * leave only 20 bits of the timestamp that cycle every ~1 second,
2640 		 * also mix in some higher bits.
2641 		 */
2642 		rseed = ((uint64) MyProcPid) ^
2643 			((uint64) MyStartTimestamp << 12) ^
2644 			((uint64) MyStartTimestamp >> 20);
2645 	}
2646 	srandom(rseed);
2647 }
2648 
2649 
2650 /*
2651  * reset_shared -- reset shared memory and semaphores
2652  */
2653 static void
2654 reset_shared(int port)
2655 {
2656 	/*
2657 	 * Create or re-create shared memory and semaphores.
2658 	 *
2659 	 * Note: in each "cycle of life" we will normally assign the same IPC keys
2660 	 * (if using SysV shmem and/or semas), since the port number is used to
2661 	 * determine IPC keys.  This helps ensure that we will clean up dead IPC
2662 	 * objects if the postmaster crashes and is restarted.
2663 	 */
2664 	CreateSharedMemoryAndSemaphores(port);
2665 }
2666 
2667 
2668 /*
2669  * SIGHUP -- reread config files, and tell children to do same
2670  */
2671 static void
2672 SIGHUP_handler(SIGNAL_ARGS)
2673 {
2674 	int			save_errno = errno;
2675 
2676 	/*
2677 	 * We rely on the signal mechanism to have blocked all signals ... except
2678 	 * on Windows, which lacks sigaction(), so we have to do it manually.
2679 	 */
2680 #ifdef WIN32
2681 	PG_SETMASK(&BlockSig);
2682 #endif
2683 
2684 	if (Shutdown <= SmartShutdown)
2685 	{
2686 		ereport(LOG,
2687 				(errmsg("received SIGHUP, reloading configuration files")));
2688 		ProcessConfigFile(PGC_SIGHUP);
2689 		SignalChildren(SIGHUP);
2690 		if (StartupPID != 0)
2691 			signal_child(StartupPID, SIGHUP);
2692 		if (BgWriterPID != 0)
2693 			signal_child(BgWriterPID, SIGHUP);
2694 		if (CheckpointerPID != 0)
2695 			signal_child(CheckpointerPID, SIGHUP);
2696 		if (WalWriterPID != 0)
2697 			signal_child(WalWriterPID, SIGHUP);
2698 		if (WalReceiverPID != 0)
2699 			signal_child(WalReceiverPID, SIGHUP);
2700 		if (AutoVacPID != 0)
2701 			signal_child(AutoVacPID, SIGHUP);
2702 		if (PgArchPID != 0)
2703 			signal_child(PgArchPID, SIGHUP);
2704 		if (SysLoggerPID != 0)
2705 			signal_child(SysLoggerPID, SIGHUP);
2706 		if (PgStatPID != 0)
2707 			signal_child(PgStatPID, SIGHUP);
2708 
2709 		/* Reload authentication config files too */
2710 		if (!load_hba())
2711 			ereport(LOG,
2712 			/* translator: %s is a configuration file */
2713 					(errmsg("%s was not reloaded", "pg_hba.conf")));
2714 
2715 		if (!load_ident())
2716 			ereport(LOG,
2717 					(errmsg("%s was not reloaded", "pg_ident.conf")));
2718 
2719 #ifdef USE_SSL
2720 		/* Reload SSL configuration as well */
2721 		if (EnableSSL)
2722 		{
2723 			if (secure_initialize(false) == 0)
2724 				LoadedSSL = true;
2725 			else
2726 				ereport(LOG,
2727 						(errmsg("SSL configuration was not reloaded")));
2728 		}
2729 		else
2730 		{
2731 			secure_destroy();
2732 			LoadedSSL = false;
2733 		}
2734 #endif
2735 
2736 #ifdef EXEC_BACKEND
2737 		/* Update the starting-point file for future children */
2738 		write_nondefault_variables(PGC_SIGHUP);
2739 #endif
2740 	}
2741 
2742 #ifdef WIN32
2743 	PG_SETMASK(&UnBlockSig);
2744 #endif
2745 
2746 	errno = save_errno;
2747 }
2748 
2749 
2750 /*
2751  * pmdie -- signal handler for processing various postmaster signals.
2752  */
2753 static void
2754 pmdie(SIGNAL_ARGS)
2755 {
2756 	int			save_errno = errno;
2757 
2758 	/*
2759 	 * We rely on the signal mechanism to have blocked all signals ... except
2760 	 * on Windows, which lacks sigaction(), so we have to do it manually.
2761 	 */
2762 #ifdef WIN32
2763 	PG_SETMASK(&BlockSig);
2764 #endif
2765 
2766 	ereport(DEBUG2,
2767 			(errmsg_internal("postmaster received signal %d",
2768 							 postgres_signal_arg)));
2769 
2770 	switch (postgres_signal_arg)
2771 	{
2772 		case SIGTERM:
2773 
2774 			/*
2775 			 * Smart Shutdown:
2776 			 *
2777 			 * Wait for children to end their work, then shut down.
2778 			 */
2779 			if (Shutdown >= SmartShutdown)
2780 				break;
2781 			Shutdown = SmartShutdown;
2782 			ereport(LOG,
2783 					(errmsg("received smart shutdown request")));
2784 
2785 			/* Report status */
2786 			AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STOPPING);
2787 #ifdef USE_SYSTEMD
2788 			sd_notify(0, "STOPPING=1");
2789 #endif
2790 
2791 			/*
2792 			 * If we reached normal running, we have to wait for any online
2793 			 * backup mode to end; otherwise go straight to waiting for client
2794 			 * backends to exit.  (The difference is that in the former state,
2795 			 * we'll still let in new superuser clients, so that somebody can
2796 			 * end the online backup mode.)  If already in PM_STOP_BACKENDS or
2797 			 * a later state, do not change it.
2798 			 */
2799 			if (pmState == PM_RUN)
2800 				connsAllowed = ALLOW_SUPERUSER_CONNS;
2801 			else if (pmState == PM_HOT_STANDBY)
2802 				connsAllowed = ALLOW_NO_CONNS;
2803 			else if (pmState == PM_STARTUP || pmState == PM_RECOVERY)
2804 			{
2805 				/* There should be no clients, so proceed to stop children */
2806 				pmState = PM_STOP_BACKENDS;
2807 			}
2808 
2809 			/*
2810 			 * Now wait for online backup mode to end and backends to exit. If
2811 			 * that is already the case, PostmasterStateMachine will take the
2812 			 * next step.
2813 			 */
2814 			PostmasterStateMachine();
2815 			break;
2816 
2817 		case SIGINT:
2818 
2819 			/*
2820 			 * Fast Shutdown:
2821 			 *
2822 			 * Abort all children with SIGTERM (rollback active transactions
2823 			 * and exit) and shut down when they are gone.
2824 			 */
2825 			if (Shutdown >= FastShutdown)
2826 				break;
2827 			Shutdown = FastShutdown;
2828 			ereport(LOG,
2829 					(errmsg("received fast shutdown request")));
2830 
2831 			/* Report status */
2832 			AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STOPPING);
2833 #ifdef USE_SYSTEMD
2834 			sd_notify(0, "STOPPING=1");
2835 #endif
2836 
2837 			if (pmState == PM_STARTUP || pmState == PM_RECOVERY)
2838 			{
2839 				/* Just shut down background processes silently */
2840 				pmState = PM_STOP_BACKENDS;
2841 			}
2842 			else if (pmState == PM_RUN ||
2843 					 pmState == PM_HOT_STANDBY)
2844 			{
2845 				/* Report that we're about to zap live client sessions */
2846 				ereport(LOG,
2847 						(errmsg("aborting any active transactions")));
2848 				pmState = PM_STOP_BACKENDS;
2849 			}
2850 
2851 			/*
2852 			 * PostmasterStateMachine will issue any necessary signals, or
2853 			 * take the next step if no child processes need to be killed.
2854 			 */
2855 			PostmasterStateMachine();
2856 			break;
2857 
2858 		case SIGQUIT:
2859 
2860 			/*
2861 			 * Immediate Shutdown:
2862 			 *
2863 			 * abort all children with SIGQUIT, wait for them to exit,
2864 			 * terminate remaining ones with SIGKILL, then exit without
2865 			 * attempt to properly shut down the data base system.
2866 			 */
2867 			if (Shutdown >= ImmediateShutdown)
2868 				break;
2869 			Shutdown = ImmediateShutdown;
2870 			ereport(LOG,
2871 					(errmsg("received immediate shutdown request")));
2872 
2873 			/* Report status */
2874 			AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STOPPING);
2875 #ifdef USE_SYSTEMD
2876 			sd_notify(0, "STOPPING=1");
2877 #endif
2878 
2879 			TerminateChildren(SIGQUIT);
2880 			pmState = PM_WAIT_BACKENDS;
2881 
2882 			/* set stopwatch for them to die */
2883 			AbortStartTime = time(NULL);
2884 
2885 			/*
2886 			 * Now wait for backends to exit.  If there are none,
2887 			 * PostmasterStateMachine will take the next step.
2888 			 */
2889 			PostmasterStateMachine();
2890 			break;
2891 	}
2892 
2893 #ifdef WIN32
2894 	PG_SETMASK(&UnBlockSig);
2895 #endif
2896 
2897 	errno = save_errno;
2898 }
2899 
2900 /*
2901  * Reaper -- signal handler to cleanup after a child process dies.
2902  */
2903 static void
2904 reaper(SIGNAL_ARGS)
2905 {
2906 	int			save_errno = errno;
2907 	int			pid;			/* process id of dead child process */
2908 	int			exitstatus;		/* its exit status */
2909 
2910 	/*
2911 	 * We rely on the signal mechanism to have blocked all signals ... except
2912 	 * on Windows, which lacks sigaction(), so we have to do it manually.
2913 	 */
2914 #ifdef WIN32
2915 	PG_SETMASK(&BlockSig);
2916 #endif
2917 
2918 	ereport(DEBUG4,
2919 			(errmsg_internal("reaping dead processes")));
2920 
2921 	while ((pid = waitpid(-1, &exitstatus, WNOHANG)) > 0)
2922 	{
2923 		/*
2924 		 * Check if this child was a startup process.
2925 		 */
2926 		if (pid == StartupPID)
2927 		{
2928 			StartupPID = 0;
2929 
2930 			/*
2931 			 * Startup process exited in response to a shutdown request (or it
2932 			 * completed normally regardless of the shutdown request).
2933 			 */
2934 			if (Shutdown > NoShutdown &&
2935 				(EXIT_STATUS_0(exitstatus) || EXIT_STATUS_1(exitstatus)))
2936 			{
2937 				StartupStatus = STARTUP_NOT_RUNNING;
2938 				pmState = PM_WAIT_BACKENDS;
2939 				/* PostmasterStateMachine logic does the rest */
2940 				continue;
2941 			}
2942 
2943 			if (EXIT_STATUS_3(exitstatus))
2944 			{
2945 				ereport(LOG,
2946 						(errmsg("shutdown at recovery target")));
2947 				StartupStatus = STARTUP_NOT_RUNNING;
2948 				Shutdown = Max(Shutdown, SmartShutdown);
2949 				TerminateChildren(SIGTERM);
2950 				pmState = PM_WAIT_BACKENDS;
2951 				/* PostmasterStateMachine logic does the rest */
2952 				continue;
2953 			}
2954 
2955 			/*
2956 			 * Unexpected exit of startup process (including FATAL exit)
2957 			 * during PM_STARTUP is treated as catastrophic. There are no
2958 			 * other processes running yet, so we can just exit.
2959 			 */
2960 			if (pmState == PM_STARTUP && !EXIT_STATUS_0(exitstatus))
2961 			{
2962 				LogChildExit(LOG, _("startup process"),
2963 							 pid, exitstatus);
2964 				ereport(LOG,
2965 						(errmsg("aborting startup due to startup process failure")));
2966 				ExitPostmaster(1);
2967 			}
2968 
2969 			/*
2970 			 * After PM_STARTUP, any unexpected exit (including FATAL exit) of
2971 			 * the startup process is catastrophic, so kill other children,
2972 			 * and set StartupStatus so we don't try to reinitialize after
2973 			 * they're gone.  Exception: if StartupStatus is STARTUP_SIGNALED,
2974 			 * then we previously sent the startup process a SIGQUIT; so
2975 			 * that's probably the reason it died, and we do want to try to
2976 			 * restart in that case.
2977 			 */
2978 			if (!EXIT_STATUS_0(exitstatus))
2979 			{
2980 				if (StartupStatus == STARTUP_SIGNALED)
2981 					StartupStatus = STARTUP_NOT_RUNNING;
2982 				else
2983 					StartupStatus = STARTUP_CRASHED;
2984 				HandleChildCrash(pid, exitstatus,
2985 								 _("startup process"));
2986 				continue;
2987 			}
2988 
2989 			/*
2990 			 * Startup succeeded, commence normal operations
2991 			 */
2992 			StartupStatus = STARTUP_NOT_RUNNING;
2993 			FatalError = false;
2994 			Assert(AbortStartTime == 0);
2995 			ReachedNormalRunning = true;
2996 			pmState = PM_RUN;
2997 			connsAllowed = ALLOW_ALL_CONNS;
2998 
2999 			/*
3000 			 * Crank up the background tasks, if we didn't do that already
3001 			 * when we entered consistent recovery state.  It doesn't matter
3002 			 * if this fails, we'll just try again later.
3003 			 */
3004 			if (CheckpointerPID == 0)
3005 				CheckpointerPID = StartCheckpointer();
3006 			if (BgWriterPID == 0)
3007 				BgWriterPID = StartBackgroundWriter();
3008 			if (WalWriterPID == 0)
3009 				WalWriterPID = StartWalWriter();
3010 
3011 			/*
3012 			 * Likewise, start other special children as needed.  In a restart
3013 			 * situation, some of them may be alive already.
3014 			 */
3015 			if (!IsBinaryUpgrade && AutoVacuumingActive() && AutoVacPID == 0)
3016 				AutoVacPID = StartAutoVacLauncher();
3017 			if (PgArchStartupAllowed() && PgArchPID == 0)
3018 				PgArchPID = pgarch_start();
3019 			if (PgStatPID == 0)
3020 				PgStatPID = pgstat_start();
3021 
3022 			/* workers may be scheduled to start now */
3023 			maybe_start_bgworkers();
3024 
3025 			/* at this point we are really open for business */
3026 			ereport(LOG,
3027 					(errmsg("database system is ready to accept connections")));
3028 
3029 			/* Report status */
3030 			AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_READY);
3031 #ifdef USE_SYSTEMD
3032 			sd_notify(0, "READY=1");
3033 #endif
3034 
3035 			continue;
3036 		}
3037 
3038 		/*
3039 		 * Was it the bgwriter?  Normal exit can be ignored; we'll start a new
3040 		 * one at the next iteration of the postmaster's main loop, if
3041 		 * necessary.  Any other exit condition is treated as a crash.
3042 		 */
3043 		if (pid == BgWriterPID)
3044 		{
3045 			BgWriterPID = 0;
3046 			if (!EXIT_STATUS_0(exitstatus))
3047 				HandleChildCrash(pid, exitstatus,
3048 								 _("background writer process"));
3049 			continue;
3050 		}
3051 
3052 		/*
3053 		 * Was it the checkpointer?
3054 		 */
3055 		if (pid == CheckpointerPID)
3056 		{
3057 			CheckpointerPID = 0;
3058 			if (EXIT_STATUS_0(exitstatus) && pmState == PM_SHUTDOWN)
3059 			{
3060 				/*
3061 				 * OK, we saw normal exit of the checkpointer after it's been
3062 				 * told to shut down.  We expect that it wrote a shutdown
3063 				 * checkpoint.  (If for some reason it didn't, recovery will
3064 				 * occur on next postmaster start.)
3065 				 *
3066 				 * At this point we should have no normal backend children
3067 				 * left (else we'd not be in PM_SHUTDOWN state) but we might
3068 				 * have dead_end children to wait for.
3069 				 *
3070 				 * If we have an archiver subprocess, tell it to do a last
3071 				 * archive cycle and quit. Likewise, if we have walsender
3072 				 * processes, tell them to send any remaining WAL and quit.
3073 				 */
3074 				Assert(Shutdown > NoShutdown);
3075 
3076 				/* Waken archiver for the last time */
3077 				if (PgArchPID != 0)
3078 					signal_child(PgArchPID, SIGUSR2);
3079 
3080 				/*
3081 				 * Waken walsenders for the last time. No regular backends
3082 				 * should be around anymore.
3083 				 */
3084 				SignalChildren(SIGUSR2);
3085 
3086 				pmState = PM_SHUTDOWN_2;
3087 
3088 				/*
3089 				 * We can also shut down the stats collector now; there's
3090 				 * nothing left for it to do.
3091 				 */
3092 				if (PgStatPID != 0)
3093 					signal_child(PgStatPID, SIGQUIT);
3094 			}
3095 			else
3096 			{
3097 				/*
3098 				 * Any unexpected exit of the checkpointer (including FATAL
3099 				 * exit) is treated as a crash.
3100 				 */
3101 				HandleChildCrash(pid, exitstatus,
3102 								 _("checkpointer process"));
3103 			}
3104 
3105 			continue;
3106 		}
3107 
3108 		/*
3109 		 * Was it the wal writer?  Normal exit can be ignored; we'll start a
3110 		 * new one at the next iteration of the postmaster's main loop, if
3111 		 * necessary.  Any other exit condition is treated as a crash.
3112 		 */
3113 		if (pid == WalWriterPID)
3114 		{
3115 			WalWriterPID = 0;
3116 			if (!EXIT_STATUS_0(exitstatus))
3117 				HandleChildCrash(pid, exitstatus,
3118 								 _("WAL writer process"));
3119 			continue;
3120 		}
3121 
3122 		/*
3123 		 * Was it the wal receiver?  If exit status is zero (normal) or one
3124 		 * (FATAL exit), we assume everything is all right just like normal
3125 		 * backends.  (If we need a new wal receiver, we'll start one at the
3126 		 * next iteration of the postmaster's main loop.)
3127 		 */
3128 		if (pid == WalReceiverPID)
3129 		{
3130 			WalReceiverPID = 0;
3131 			if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
3132 				HandleChildCrash(pid, exitstatus,
3133 								 _("WAL receiver process"));
3134 			continue;
3135 		}
3136 
3137 		/*
3138 		 * Was it the autovacuum launcher?	Normal exit can be ignored; we'll
3139 		 * start a new one at the next iteration of the postmaster's main
3140 		 * loop, if necessary.  Any other exit condition is treated as a
3141 		 * crash.
3142 		 */
3143 		if (pid == AutoVacPID)
3144 		{
3145 			AutoVacPID = 0;
3146 			if (!EXIT_STATUS_0(exitstatus))
3147 				HandleChildCrash(pid, exitstatus,
3148 								 _("autovacuum launcher process"));
3149 			continue;
3150 		}
3151 
3152 		/*
3153 		 * Was it the archiver?  If so, just try to start a new one; no need
3154 		 * to force reset of the rest of the system.  (If fail, we'll try
3155 		 * again in future cycles of the main loop.).  Unless we were waiting
3156 		 * for it to shut down; don't restart it in that case, and
3157 		 * PostmasterStateMachine() will advance to the next shutdown step.
3158 		 */
3159 		if (pid == PgArchPID)
3160 		{
3161 			PgArchPID = 0;
3162 			if (!EXIT_STATUS_0(exitstatus))
3163 				LogChildExit(LOG, _("archiver process"),
3164 							 pid, exitstatus);
3165 			if (PgArchStartupAllowed())
3166 				PgArchPID = pgarch_start();
3167 			continue;
3168 		}
3169 
3170 		/*
3171 		 * Was it the statistics collector?  If so, just try to start a new
3172 		 * one; no need to force reset of the rest of the system.  (If fail,
3173 		 * we'll try again in future cycles of the main loop.)
3174 		 */
3175 		if (pid == PgStatPID)
3176 		{
3177 			PgStatPID = 0;
3178 			if (!EXIT_STATUS_0(exitstatus))
3179 				LogChildExit(LOG, _("statistics collector process"),
3180 							 pid, exitstatus);
3181 			if (pmState == PM_RUN || pmState == PM_HOT_STANDBY)
3182 				PgStatPID = pgstat_start();
3183 			continue;
3184 		}
3185 
3186 		/* Was it the system logger?  If so, try to start a new one */
3187 		if (pid == SysLoggerPID)
3188 		{
3189 			SysLoggerPID = 0;
3190 			/* for safety's sake, launch new logger *first* */
3191 			SysLoggerPID = SysLogger_Start();
3192 			if (!EXIT_STATUS_0(exitstatus))
3193 				LogChildExit(LOG, _("system logger process"),
3194 							 pid, exitstatus);
3195 			continue;
3196 		}
3197 
3198 		/* Was it one of our background workers? */
3199 		if (CleanupBackgroundWorker(pid, exitstatus))
3200 		{
3201 			/* have it be restarted */
3202 			HaveCrashedWorker = true;
3203 			continue;
3204 		}
3205 
3206 		/*
3207 		 * Else do standard backend child cleanup.
3208 		 */
3209 		CleanupBackend(pid, exitstatus);
3210 	}							/* loop over pending child-death reports */
3211 
3212 	/*
3213 	 * After cleaning out the SIGCHLD queue, see if we have any state changes
3214 	 * or actions to make.
3215 	 */
3216 	PostmasterStateMachine();
3217 
3218 	/* Done with signal handler */
3219 #ifdef WIN32
3220 	PG_SETMASK(&UnBlockSig);
3221 #endif
3222 
3223 	errno = save_errno;
3224 }
3225 
3226 /*
3227  * Scan the bgworkers list and see if the given PID (which has just stopped
3228  * or crashed) is in it.  Handle its shutdown if so, and return true.  If not a
3229  * bgworker, return false.
3230  *
3231  * This is heavily based on CleanupBackend.  One important difference is that
3232  * we don't know yet that the dying process is a bgworker, so we must be silent
3233  * until we're sure it is.
3234  */
3235 static bool
3236 CleanupBackgroundWorker(int pid,
3237 						int exitstatus) /* child's exit status */
3238 {
3239 	char		namebuf[MAXPGPATH];
3240 	slist_mutable_iter iter;
3241 
3242 	slist_foreach_modify(iter, &BackgroundWorkerList)
3243 	{
3244 		RegisteredBgWorker *rw;
3245 
3246 		rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
3247 
3248 		if (rw->rw_pid != pid)
3249 			continue;
3250 
3251 #ifdef WIN32
3252 		/* see CleanupBackend */
3253 		if (exitstatus == ERROR_WAIT_NO_CHILDREN)
3254 			exitstatus = 0;
3255 #endif
3256 
3257 		snprintf(namebuf, MAXPGPATH, _("background worker \"%s\""),
3258 				 rw->rw_worker.bgw_type);
3259 
3260 
3261 		if (!EXIT_STATUS_0(exitstatus))
3262 		{
3263 			/* Record timestamp, so we know when to restart the worker. */
3264 			rw->rw_crashed_at = GetCurrentTimestamp();
3265 		}
3266 		else
3267 		{
3268 			/* Zero exit status means terminate */
3269 			rw->rw_crashed_at = 0;
3270 			rw->rw_terminate = true;
3271 		}
3272 
3273 		/*
3274 		 * Additionally, for shared-memory-connected workers, just like a
3275 		 * backend, any exit status other than 0 or 1 is considered a crash
3276 		 * and causes a system-wide restart.
3277 		 */
3278 		if ((rw->rw_worker.bgw_flags & BGWORKER_SHMEM_ACCESS) != 0)
3279 		{
3280 			if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
3281 			{
3282 				HandleChildCrash(pid, exitstatus, namebuf);
3283 				return true;
3284 			}
3285 		}
3286 
3287 		/*
3288 		 * We must release the postmaster child slot whether this worker is
3289 		 * connected to shared memory or not, but we only treat it as a crash
3290 		 * if it is in fact connected.
3291 		 */
3292 		if (!ReleasePostmasterChildSlot(rw->rw_child_slot) &&
3293 			(rw->rw_worker.bgw_flags & BGWORKER_SHMEM_ACCESS) != 0)
3294 		{
3295 			HandleChildCrash(pid, exitstatus, namebuf);
3296 			return true;
3297 		}
3298 
3299 		/* Get it out of the BackendList and clear out remaining data */
3300 		dlist_delete(&rw->rw_backend->elem);
3301 #ifdef EXEC_BACKEND
3302 		ShmemBackendArrayRemove(rw->rw_backend);
3303 #endif
3304 
3305 		/*
3306 		 * It's possible that this background worker started some OTHER
3307 		 * background worker and asked to be notified when that worker started
3308 		 * or stopped.  If so, cancel any notifications destined for the
3309 		 * now-dead backend.
3310 		 */
3311 		if (rw->rw_backend->bgworker_notify)
3312 			BackgroundWorkerStopNotifications(rw->rw_pid);
3313 		free(rw->rw_backend);
3314 		rw->rw_backend = NULL;
3315 		rw->rw_pid = 0;
3316 		rw->rw_child_slot = 0;
3317 		ReportBackgroundWorkerExit(&iter);	/* report child death */
3318 
3319 		LogChildExit(EXIT_STATUS_0(exitstatus) ? DEBUG1 : LOG,
3320 					 namebuf, pid, exitstatus);
3321 
3322 		return true;
3323 	}
3324 
3325 	return false;
3326 }
3327 
3328 /*
3329  * CleanupBackend -- cleanup after terminated backend.
3330  *
3331  * Remove all local state associated with backend.
3332  *
3333  * If you change this, see also CleanupBackgroundWorker.
3334  */
3335 static void
3336 CleanupBackend(int pid,
3337 			   int exitstatus)	/* child's exit status. */
3338 {
3339 	dlist_mutable_iter iter;
3340 
3341 	LogChildExit(DEBUG2, _("server process"), pid, exitstatus);
3342 
3343 	/*
3344 	 * If a backend dies in an ugly way then we must signal all other backends
3345 	 * to quickdie.  If exit status is zero (normal) or one (FATAL exit), we
3346 	 * assume everything is all right and proceed to remove the backend from
3347 	 * the active backend list.
3348 	 */
3349 
3350 #ifdef WIN32
3351 
3352 	/*
3353 	 * On win32, also treat ERROR_WAIT_NO_CHILDREN (128) as nonfatal case,
3354 	 * since that sometimes happens under load when the process fails to start
3355 	 * properly (long before it starts using shared memory). Microsoft reports
3356 	 * it is related to mutex failure:
3357 	 * http://archives.postgresql.org/pgsql-hackers/2010-09/msg00790.php
3358 	 */
3359 	if (exitstatus == ERROR_WAIT_NO_CHILDREN)
3360 	{
3361 		LogChildExit(LOG, _("server process"), pid, exitstatus);
3362 		exitstatus = 0;
3363 	}
3364 #endif
3365 
3366 	if (!EXIT_STATUS_0(exitstatus) && !EXIT_STATUS_1(exitstatus))
3367 	{
3368 		HandleChildCrash(pid, exitstatus, _("server process"));
3369 		return;
3370 	}
3371 
3372 	dlist_foreach_modify(iter, &BackendList)
3373 	{
3374 		Backend    *bp = dlist_container(Backend, elem, iter.cur);
3375 
3376 		if (bp->pid == pid)
3377 		{
3378 			if (!bp->dead_end)
3379 			{
3380 				if (!ReleasePostmasterChildSlot(bp->child_slot))
3381 				{
3382 					/*
3383 					 * Uh-oh, the child failed to clean itself up.  Treat as a
3384 					 * crash after all.
3385 					 */
3386 					HandleChildCrash(pid, exitstatus, _("server process"));
3387 					return;
3388 				}
3389 #ifdef EXEC_BACKEND
3390 				ShmemBackendArrayRemove(bp);
3391 #endif
3392 			}
3393 			if (bp->bgworker_notify)
3394 			{
3395 				/*
3396 				 * This backend may have been slated to receive SIGUSR1 when
3397 				 * some background worker started or stopped.  Cancel those
3398 				 * notifications, as we don't want to signal PIDs that are not
3399 				 * PostgreSQL backends.  This gets skipped in the (probably
3400 				 * very common) case where the backend has never requested any
3401 				 * such notifications.
3402 				 */
3403 				BackgroundWorkerStopNotifications(bp->pid);
3404 			}
3405 			dlist_delete(iter.cur);
3406 			free(bp);
3407 			break;
3408 		}
3409 	}
3410 }
3411 
3412 /*
3413  * HandleChildCrash -- cleanup after failed backend, bgwriter, checkpointer,
3414  * walwriter, autovacuum, or background worker.
3415  *
3416  * The objectives here are to clean up our local state about the child
3417  * process, and to signal all other remaining children to quickdie.
3418  */
3419 static void
3420 HandleChildCrash(int pid, int exitstatus, const char *procname)
3421 {
3422 	dlist_mutable_iter iter;
3423 	slist_iter	siter;
3424 	Backend    *bp;
3425 	bool		take_action;
3426 
3427 	/*
3428 	 * We only log messages and send signals if this is the first process
3429 	 * crash and we're not doing an immediate shutdown; otherwise, we're only
3430 	 * here to update postmaster's idea of live processes.  If we have already
3431 	 * signalled children, nonzero exit status is to be expected, so don't
3432 	 * clutter log.
3433 	 */
3434 	take_action = !FatalError && Shutdown != ImmediateShutdown;
3435 
3436 	if (take_action)
3437 	{
3438 		LogChildExit(LOG, procname, pid, exitstatus);
3439 		ereport(LOG,
3440 				(errmsg("terminating any other active server processes")));
3441 	}
3442 
3443 	/* Process background workers. */
3444 	slist_foreach(siter, &BackgroundWorkerList)
3445 	{
3446 		RegisteredBgWorker *rw;
3447 
3448 		rw = slist_container(RegisteredBgWorker, rw_lnode, siter.cur);
3449 		if (rw->rw_pid == 0)
3450 			continue;			/* not running */
3451 		if (rw->rw_pid == pid)
3452 		{
3453 			/*
3454 			 * Found entry for freshly-dead worker, so remove it.
3455 			 */
3456 			(void) ReleasePostmasterChildSlot(rw->rw_child_slot);
3457 			dlist_delete(&rw->rw_backend->elem);
3458 #ifdef EXEC_BACKEND
3459 			ShmemBackendArrayRemove(rw->rw_backend);
3460 #endif
3461 			free(rw->rw_backend);
3462 			rw->rw_backend = NULL;
3463 			rw->rw_pid = 0;
3464 			rw->rw_child_slot = 0;
3465 			/* don't reset crashed_at */
3466 			/* don't report child stop, either */
3467 			/* Keep looping so we can signal remaining workers */
3468 		}
3469 		else
3470 		{
3471 			/*
3472 			 * This worker is still alive.  Unless we did so already, tell it
3473 			 * to commit hara-kiri.
3474 			 *
3475 			 * SIGQUIT is the special signal that says exit without proc_exit
3476 			 * and let the user know what's going on. But if SendStop is set
3477 			 * (-s on command line), then we send SIGSTOP instead, so that we
3478 			 * can get core dumps from all backends by hand.
3479 			 */
3480 			if (take_action)
3481 			{
3482 				ereport(DEBUG2,
3483 						(errmsg_internal("sending %s to process %d",
3484 										 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3485 										 (int) rw->rw_pid)));
3486 				signal_child(rw->rw_pid, (SendStop ? SIGSTOP : SIGQUIT));
3487 			}
3488 		}
3489 	}
3490 
3491 	/* Process regular backends */
3492 	dlist_foreach_modify(iter, &BackendList)
3493 	{
3494 		bp = dlist_container(Backend, elem, iter.cur);
3495 
3496 		if (bp->pid == pid)
3497 		{
3498 			/*
3499 			 * Found entry for freshly-dead backend, so remove it.
3500 			 */
3501 			if (!bp->dead_end)
3502 			{
3503 				(void) ReleasePostmasterChildSlot(bp->child_slot);
3504 #ifdef EXEC_BACKEND
3505 				ShmemBackendArrayRemove(bp);
3506 #endif
3507 			}
3508 			dlist_delete(iter.cur);
3509 			free(bp);
3510 			/* Keep looping so we can signal remaining backends */
3511 		}
3512 		else
3513 		{
3514 			/*
3515 			 * This backend is still alive.  Unless we did so already, tell it
3516 			 * to commit hara-kiri.
3517 			 *
3518 			 * SIGQUIT is the special signal that says exit without proc_exit
3519 			 * and let the user know what's going on. But if SendStop is set
3520 			 * (-s on command line), then we send SIGSTOP instead, so that we
3521 			 * can get core dumps from all backends by hand.
3522 			 *
3523 			 * We could exclude dead_end children here, but at least in the
3524 			 * SIGSTOP case it seems better to include them.
3525 			 *
3526 			 * Background workers were already processed above; ignore them
3527 			 * here.
3528 			 */
3529 			if (bp->bkend_type == BACKEND_TYPE_BGWORKER)
3530 				continue;
3531 
3532 			if (take_action)
3533 			{
3534 				ereport(DEBUG2,
3535 						(errmsg_internal("sending %s to process %d",
3536 										 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3537 										 (int) bp->pid)));
3538 				signal_child(bp->pid, (SendStop ? SIGSTOP : SIGQUIT));
3539 			}
3540 		}
3541 	}
3542 
3543 	/* Take care of the startup process too */
3544 	if (pid == StartupPID)
3545 	{
3546 		StartupPID = 0;
3547 		StartupStatus = STARTUP_CRASHED;
3548 	}
3549 	else if (StartupPID != 0 && take_action)
3550 	{
3551 		ereport(DEBUG2,
3552 				(errmsg_internal("sending %s to process %d",
3553 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3554 								 (int) StartupPID)));
3555 		signal_child(StartupPID, (SendStop ? SIGSTOP : SIGQUIT));
3556 		StartupStatus = STARTUP_SIGNALED;
3557 	}
3558 
3559 	/* Take care of the bgwriter too */
3560 	if (pid == BgWriterPID)
3561 		BgWriterPID = 0;
3562 	else if (BgWriterPID != 0 && take_action)
3563 	{
3564 		ereport(DEBUG2,
3565 				(errmsg_internal("sending %s to process %d",
3566 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3567 								 (int) BgWriterPID)));
3568 		signal_child(BgWriterPID, (SendStop ? SIGSTOP : SIGQUIT));
3569 	}
3570 
3571 	/* Take care of the checkpointer too */
3572 	if (pid == CheckpointerPID)
3573 		CheckpointerPID = 0;
3574 	else if (CheckpointerPID != 0 && take_action)
3575 	{
3576 		ereport(DEBUG2,
3577 				(errmsg_internal("sending %s to process %d",
3578 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3579 								 (int) CheckpointerPID)));
3580 		signal_child(CheckpointerPID, (SendStop ? SIGSTOP : SIGQUIT));
3581 	}
3582 
3583 	/* Take care of the walwriter too */
3584 	if (pid == WalWriterPID)
3585 		WalWriterPID = 0;
3586 	else if (WalWriterPID != 0 && take_action)
3587 	{
3588 		ereport(DEBUG2,
3589 				(errmsg_internal("sending %s to process %d",
3590 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3591 								 (int) WalWriterPID)));
3592 		signal_child(WalWriterPID, (SendStop ? SIGSTOP : SIGQUIT));
3593 	}
3594 
3595 	/* Take care of the walreceiver too */
3596 	if (pid == WalReceiverPID)
3597 		WalReceiverPID = 0;
3598 	else if (WalReceiverPID != 0 && take_action)
3599 	{
3600 		ereport(DEBUG2,
3601 				(errmsg_internal("sending %s to process %d",
3602 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3603 								 (int) WalReceiverPID)));
3604 		signal_child(WalReceiverPID, (SendStop ? SIGSTOP : SIGQUIT));
3605 	}
3606 
3607 	/* Take care of the autovacuum launcher too */
3608 	if (pid == AutoVacPID)
3609 		AutoVacPID = 0;
3610 	else if (AutoVacPID != 0 && take_action)
3611 	{
3612 		ereport(DEBUG2,
3613 				(errmsg_internal("sending %s to process %d",
3614 								 (SendStop ? "SIGSTOP" : "SIGQUIT"),
3615 								 (int) AutoVacPID)));
3616 		signal_child(AutoVacPID, (SendStop ? SIGSTOP : SIGQUIT));
3617 	}
3618 
3619 	/*
3620 	 * Force a power-cycle of the pgarch process too.  (This isn't absolutely
3621 	 * necessary, but it seems like a good idea for robustness, and it
3622 	 * simplifies the state-machine logic in the case where a shutdown request
3623 	 * arrives during crash processing.)
3624 	 */
3625 	if (PgArchPID != 0 && take_action)
3626 	{
3627 		ereport(DEBUG2,
3628 				(errmsg_internal("sending %s to process %d",
3629 								 "SIGQUIT",
3630 								 (int) PgArchPID)));
3631 		signal_child(PgArchPID, SIGQUIT);
3632 	}
3633 
3634 	/*
3635 	 * Force a power-cycle of the pgstat process too.  (This isn't absolutely
3636 	 * necessary, but it seems like a good idea for robustness, and it
3637 	 * simplifies the state-machine logic in the case where a shutdown request
3638 	 * arrives during crash processing.)
3639 	 */
3640 	if (PgStatPID != 0 && take_action)
3641 	{
3642 		ereport(DEBUG2,
3643 				(errmsg_internal("sending %s to process %d",
3644 								 "SIGQUIT",
3645 								 (int) PgStatPID)));
3646 		signal_child(PgStatPID, SIGQUIT);
3647 		allow_immediate_pgstat_restart();
3648 	}
3649 
3650 	/* We do NOT restart the syslogger */
3651 
3652 	if (Shutdown != ImmediateShutdown)
3653 		FatalError = true;
3654 
3655 	/* We now transit into a state of waiting for children to die */
3656 	if (pmState == PM_RECOVERY ||
3657 		pmState == PM_HOT_STANDBY ||
3658 		pmState == PM_RUN ||
3659 		pmState == PM_STOP_BACKENDS ||
3660 		pmState == PM_SHUTDOWN)
3661 		pmState = PM_WAIT_BACKENDS;
3662 
3663 	/*
3664 	 * .. and if this doesn't happen quickly enough, now the clock is ticking
3665 	 * for us to kill them without mercy.
3666 	 */
3667 	if (AbortStartTime == 0)
3668 		AbortStartTime = time(NULL);
3669 }
3670 
3671 /*
3672  * Log the death of a child process.
3673  */
3674 static void
3675 LogChildExit(int lev, const char *procname, int pid, int exitstatus)
3676 {
3677 	/*
3678 	 * size of activity_buffer is arbitrary, but set equal to default
3679 	 * track_activity_query_size
3680 	 */
3681 	char		activity_buffer[1024];
3682 	const char *activity = NULL;
3683 
3684 	if (!EXIT_STATUS_0(exitstatus))
3685 		activity = pgstat_get_crashed_backend_activity(pid,
3686 													   activity_buffer,
3687 													   sizeof(activity_buffer));
3688 
3689 	if (WIFEXITED(exitstatus))
3690 		ereport(lev,
3691 
3692 		/*------
3693 		  translator: %s is a noun phrase describing a child process, such as
3694 		  "server process" */
3695 				(errmsg("%s (PID %d) exited with exit code %d",
3696 						procname, pid, WEXITSTATUS(exitstatus)),
3697 				 activity ? errdetail("Failed process was running: %s", activity) : 0));
3698 	else if (WIFSIGNALED(exitstatus))
3699 	{
3700 #if defined(WIN32)
3701 		ereport(lev,
3702 
3703 		/*------
3704 		  translator: %s is a noun phrase describing a child process, such as
3705 		  "server process" */
3706 				(errmsg("%s (PID %d) was terminated by exception 0x%X",
3707 						procname, pid, WTERMSIG(exitstatus)),
3708 				 errhint("See C include file \"ntstatus.h\" for a description of the hexadecimal value."),
3709 				 activity ? errdetail("Failed process was running: %s", activity) : 0));
3710 #else
3711 		ereport(lev,
3712 
3713 		/*------
3714 		  translator: %s is a noun phrase describing a child process, such as
3715 		  "server process" */
3716 				(errmsg("%s (PID %d) was terminated by signal %d: %s",
3717 						procname, pid, WTERMSIG(exitstatus),
3718 						pg_strsignal(WTERMSIG(exitstatus))),
3719 				 activity ? errdetail("Failed process was running: %s", activity) : 0));
3720 #endif
3721 	}
3722 	else
3723 		ereport(lev,
3724 
3725 		/*------
3726 		  translator: %s is a noun phrase describing a child process, such as
3727 		  "server process" */
3728 				(errmsg("%s (PID %d) exited with unrecognized status %d",
3729 						procname, pid, exitstatus),
3730 				 activity ? errdetail("Failed process was running: %s", activity) : 0));
3731 }
3732 
3733 /*
3734  * Advance the postmaster's state machine and take actions as appropriate
3735  *
3736  * This is common code for pmdie(), reaper() and sigusr1_handler(), which
3737  * receive the signals that might mean we need to change state.
3738  */
3739 static void
3740 PostmasterStateMachine(void)
3741 {
3742 	/* If we're doing a smart shutdown, try to advance that state. */
3743 	if (pmState == PM_RUN || pmState == PM_HOT_STANDBY)
3744 	{
3745 		if (connsAllowed == ALLOW_SUPERUSER_CONNS)
3746 		{
3747 			/*
3748 			 * ALLOW_SUPERUSER_CONNS state ends as soon as online backup mode
3749 			 * is not active.
3750 			 */
3751 			if (!BackupInProgress())
3752 				connsAllowed = ALLOW_NO_CONNS;
3753 		}
3754 
3755 		if (connsAllowed == ALLOW_NO_CONNS)
3756 		{
3757 			/*
3758 			 * ALLOW_NO_CONNS state ends when we have no normal client
3759 			 * backends running.  Then we're ready to stop other children.
3760 			 */
3761 			if (CountChildren(BACKEND_TYPE_NORMAL) == 0)
3762 				pmState = PM_STOP_BACKENDS;
3763 		}
3764 	}
3765 
3766 	/*
3767 	 * If we're ready to do so, signal child processes to shut down.  (This
3768 	 * isn't a persistent state, but treating it as a distinct pmState allows
3769 	 * us to share this code across multiple shutdown code paths.)
3770 	 */
3771 	if (pmState == PM_STOP_BACKENDS)
3772 	{
3773 		/*
3774 		 * Forget any pending requests for background workers, since we're no
3775 		 * longer willing to launch any new workers.  (If additional requests
3776 		 * arrive, BackgroundWorkerStateChange will reject them.)
3777 		 */
3778 		ForgetUnstartedBackgroundWorkers();
3779 
3780 		/* Signal all backend children except walsenders */
3781 		SignalSomeChildren(SIGTERM,
3782 						   BACKEND_TYPE_ALL - BACKEND_TYPE_WALSND);
3783 		/* and the autovac launcher too */
3784 		if (AutoVacPID != 0)
3785 			signal_child(AutoVacPID, SIGTERM);
3786 		/* and the bgwriter too */
3787 		if (BgWriterPID != 0)
3788 			signal_child(BgWriterPID, SIGTERM);
3789 		/* and the walwriter too */
3790 		if (WalWriterPID != 0)
3791 			signal_child(WalWriterPID, SIGTERM);
3792 		/* If we're in recovery, also stop startup and walreceiver procs */
3793 		if (StartupPID != 0)
3794 			signal_child(StartupPID, SIGTERM);
3795 		if (WalReceiverPID != 0)
3796 			signal_child(WalReceiverPID, SIGTERM);
3797 		/* checkpointer, archiver, stats, and syslogger may continue for now */
3798 
3799 		/* Now transition to PM_WAIT_BACKENDS state to wait for them to die */
3800 		pmState = PM_WAIT_BACKENDS;
3801 	}
3802 
3803 	/*
3804 	 * If we are in a state-machine state that implies waiting for backends to
3805 	 * exit, see if they're all gone, and change state if so.
3806 	 */
3807 	if (pmState == PM_WAIT_BACKENDS)
3808 	{
3809 		/*
3810 		 * PM_WAIT_BACKENDS state ends when we have no regular backends
3811 		 * (including autovac workers), no bgworkers (including unconnected
3812 		 * ones), and no walwriter, autovac launcher or bgwriter.  If we are
3813 		 * doing crash recovery or an immediate shutdown then we expect the
3814 		 * checkpointer to exit as well, otherwise not. The archiver, stats,
3815 		 * and syslogger processes are disregarded since they are not
3816 		 * connected to shared memory; we also disregard dead_end children
3817 		 * here. Walsenders are also disregarded, they will be terminated
3818 		 * later after writing the checkpoint record, like the archiver
3819 		 * process.
3820 		 */
3821 		if (CountChildren(BACKEND_TYPE_ALL - BACKEND_TYPE_WALSND) == 0 &&
3822 			StartupPID == 0 &&
3823 			WalReceiverPID == 0 &&
3824 			BgWriterPID == 0 &&
3825 			(CheckpointerPID == 0 ||
3826 			 (!FatalError && Shutdown < ImmediateShutdown)) &&
3827 			WalWriterPID == 0 &&
3828 			AutoVacPID == 0)
3829 		{
3830 			if (Shutdown >= ImmediateShutdown || FatalError)
3831 			{
3832 				/*
3833 				 * Start waiting for dead_end children to die.  This state
3834 				 * change causes ServerLoop to stop creating new ones.
3835 				 */
3836 				pmState = PM_WAIT_DEAD_END;
3837 
3838 				/*
3839 				 * We already SIGQUIT'd the archiver and stats processes, if
3840 				 * any, when we started immediate shutdown or entered
3841 				 * FatalError state.
3842 				 */
3843 			}
3844 			else
3845 			{
3846 				/*
3847 				 * If we get here, we are proceeding with normal shutdown. All
3848 				 * the regular children are gone, and it's time to tell the
3849 				 * checkpointer to do a shutdown checkpoint.
3850 				 */
3851 				Assert(Shutdown > NoShutdown);
3852 				/* Start the checkpointer if not running */
3853 				if (CheckpointerPID == 0)
3854 					CheckpointerPID = StartCheckpointer();
3855 				/* And tell it to shut down */
3856 				if (CheckpointerPID != 0)
3857 				{
3858 					signal_child(CheckpointerPID, SIGUSR2);
3859 					pmState = PM_SHUTDOWN;
3860 				}
3861 				else
3862 				{
3863 					/*
3864 					 * If we failed to fork a checkpointer, just shut down.
3865 					 * Any required cleanup will happen at next restart. We
3866 					 * set FatalError so that an "abnormal shutdown" message
3867 					 * gets logged when we exit.
3868 					 */
3869 					FatalError = true;
3870 					pmState = PM_WAIT_DEAD_END;
3871 
3872 					/* Kill the walsenders, archiver and stats collector too */
3873 					SignalChildren(SIGQUIT);
3874 					if (PgArchPID != 0)
3875 						signal_child(PgArchPID, SIGQUIT);
3876 					if (PgStatPID != 0)
3877 						signal_child(PgStatPID, SIGQUIT);
3878 				}
3879 			}
3880 		}
3881 	}
3882 
3883 	if (pmState == PM_SHUTDOWN_2)
3884 	{
3885 		/*
3886 		 * PM_SHUTDOWN_2 state ends when there's no other children than
3887 		 * dead_end children left. There shouldn't be any regular backends
3888 		 * left by now anyway; what we're really waiting for is walsenders and
3889 		 * archiver.
3890 		 */
3891 		if (PgArchPID == 0 && CountChildren(BACKEND_TYPE_ALL) == 0)
3892 		{
3893 			pmState = PM_WAIT_DEAD_END;
3894 		}
3895 	}
3896 
3897 	if (pmState == PM_WAIT_DEAD_END)
3898 	{
3899 		/*
3900 		 * PM_WAIT_DEAD_END state ends when the BackendList is entirely empty
3901 		 * (ie, no dead_end children remain), and the archiver and stats
3902 		 * collector are gone too.
3903 		 *
3904 		 * The reason we wait for those two is to protect them against a new
3905 		 * postmaster starting conflicting subprocesses; this isn't an
3906 		 * ironclad protection, but it at least helps in the
3907 		 * shutdown-and-immediately-restart scenario.  Note that they have
3908 		 * already been sent appropriate shutdown signals, either during a
3909 		 * normal state transition leading up to PM_WAIT_DEAD_END, or during
3910 		 * FatalError processing.
3911 		 */
3912 		if (dlist_is_empty(&BackendList) &&
3913 			PgArchPID == 0 && PgStatPID == 0)
3914 		{
3915 			/* These other guys should be dead already */
3916 			Assert(StartupPID == 0);
3917 			Assert(WalReceiverPID == 0);
3918 			Assert(BgWriterPID == 0);
3919 			Assert(CheckpointerPID == 0);
3920 			Assert(WalWriterPID == 0);
3921 			Assert(AutoVacPID == 0);
3922 			/* syslogger is not considered here */
3923 			pmState = PM_NO_CHILDREN;
3924 		}
3925 	}
3926 
3927 	/*
3928 	 * If we've been told to shut down, we exit as soon as there are no
3929 	 * remaining children.  If there was a crash, cleanup will occur at the
3930 	 * next startup.  (Before PostgreSQL 8.3, we tried to recover from the
3931 	 * crash before exiting, but that seems unwise if we are quitting because
3932 	 * we got SIGTERM from init --- there may well not be time for recovery
3933 	 * before init decides to SIGKILL us.)
3934 	 *
3935 	 * Note that the syslogger continues to run.  It will exit when it sees
3936 	 * EOF on its input pipe, which happens when there are no more upstream
3937 	 * processes.
3938 	 */
3939 	if (Shutdown > NoShutdown && pmState == PM_NO_CHILDREN)
3940 	{
3941 		if (FatalError)
3942 		{
3943 			ereport(LOG, (errmsg("abnormal database system shutdown")));
3944 			ExitPostmaster(1);
3945 		}
3946 		else
3947 		{
3948 			/*
3949 			 * Terminate exclusive backup mode to avoid recovery after a clean
3950 			 * fast shutdown.  Since an exclusive backup can only be taken
3951 			 * during normal running (and not, for example, while running
3952 			 * under Hot Standby) it only makes sense to do this if we reached
3953 			 * normal running. If we're still in recovery, the backup file is
3954 			 * one we're recovering *from*, and we must keep it around so that
3955 			 * recovery restarts from the right place.
3956 			 */
3957 			if (ReachedNormalRunning)
3958 				CancelBackup();
3959 
3960 			/* Normal exit from the postmaster is here */
3961 			ExitPostmaster(0);
3962 		}
3963 	}
3964 
3965 	/*
3966 	 * If the startup process failed, or the user does not want an automatic
3967 	 * restart after backend crashes, wait for all non-syslogger children to
3968 	 * exit, and then exit postmaster.  We don't try to reinitialize when the
3969 	 * startup process fails, because more than likely it will just fail again
3970 	 * and we will keep trying forever.
3971 	 */
3972 	if (pmState == PM_NO_CHILDREN &&
3973 		(StartupStatus == STARTUP_CRASHED || !restart_after_crash))
3974 		ExitPostmaster(1);
3975 
3976 	/*
3977 	 * If we need to recover from a crash, wait for all non-syslogger children
3978 	 * to exit, then reset shmem and StartupDataBase.
3979 	 */
3980 	if (FatalError && pmState == PM_NO_CHILDREN)
3981 	{
3982 		ereport(LOG,
3983 				(errmsg("all server processes terminated; reinitializing")));
3984 
3985 		/* allow background workers to immediately restart */
3986 		ResetBackgroundWorkerCrashTimes();
3987 
3988 		shmem_exit(1);
3989 
3990 		/* re-read control file into local memory */
3991 		LocalProcessControlFile(true);
3992 
3993 		reset_shared(PostPortNumber);
3994 
3995 		StartupPID = StartupDataBase();
3996 		Assert(StartupPID != 0);
3997 		StartupStatus = STARTUP_RUNNING;
3998 		pmState = PM_STARTUP;
3999 		/* crash recovery started, reset SIGKILL flag */
4000 		AbortStartTime = 0;
4001 	}
4002 }
4003 
4004 
4005 /*
4006  * Send a signal to a postmaster child process
4007  *
4008  * On systems that have setsid(), each child process sets itself up as a
4009  * process group leader.  For signals that are generally interpreted in the
4010  * appropriate fashion, we signal the entire process group not just the
4011  * direct child process.  This allows us to, for example, SIGQUIT a blocked
4012  * archive_recovery script, or SIGINT a script being run by a backend via
4013  * system().
4014  *
4015  * There is a race condition for recently-forked children: they might not
4016  * have executed setsid() yet.  So we signal the child directly as well as
4017  * the group.  We assume such a child will handle the signal before trying
4018  * to spawn any grandchild processes.  We also assume that signaling the
4019  * child twice will not cause any problems.
4020  */
4021 static void
4022 signal_child(pid_t pid, int signal)
4023 {
4024 	if (kill(pid, signal) < 0)
4025 		elog(DEBUG3, "kill(%ld,%d) failed: %m", (long) pid, signal);
4026 #ifdef HAVE_SETSID
4027 	switch (signal)
4028 	{
4029 		case SIGINT:
4030 		case SIGTERM:
4031 		case SIGQUIT:
4032 		case SIGSTOP:
4033 		case SIGKILL:
4034 			if (kill(-pid, signal) < 0)
4035 				elog(DEBUG3, "kill(%ld,%d) failed: %m", (long) (-pid), signal);
4036 			break;
4037 		default:
4038 			break;
4039 	}
4040 #endif
4041 }
4042 
4043 /*
4044  * Send a signal to the targeted children (but NOT special children;
4045  * dead_end children are never signaled, either).
4046  */
4047 static bool
4048 SignalSomeChildren(int signal, int target)
4049 {
4050 	dlist_iter	iter;
4051 	bool		signaled = false;
4052 
4053 	dlist_foreach(iter, &BackendList)
4054 	{
4055 		Backend    *bp = dlist_container(Backend, elem, iter.cur);
4056 
4057 		if (bp->dead_end)
4058 			continue;
4059 
4060 		/*
4061 		 * Since target == BACKEND_TYPE_ALL is the most common case, we test
4062 		 * it first and avoid touching shared memory for every child.
4063 		 */
4064 		if (target != BACKEND_TYPE_ALL)
4065 		{
4066 			/*
4067 			 * Assign bkend_type for any recently announced WAL Sender
4068 			 * processes.
4069 			 */
4070 			if (bp->bkend_type == BACKEND_TYPE_NORMAL &&
4071 				IsPostmasterChildWalSender(bp->child_slot))
4072 				bp->bkend_type = BACKEND_TYPE_WALSND;
4073 
4074 			if (!(target & bp->bkend_type))
4075 				continue;
4076 		}
4077 
4078 		ereport(DEBUG4,
4079 				(errmsg_internal("sending signal %d to process %d",
4080 								 signal, (int) bp->pid)));
4081 		signal_child(bp->pid, signal);
4082 		signaled = true;
4083 	}
4084 	return signaled;
4085 }
4086 
4087 /*
4088  * Send a termination signal to children.  This considers all of our children
4089  * processes, except syslogger and dead_end backends.
4090  */
4091 static void
4092 TerminateChildren(int signal)
4093 {
4094 	SignalChildren(signal);
4095 	if (StartupPID != 0)
4096 	{
4097 		signal_child(StartupPID, signal);
4098 		if (signal == SIGQUIT || signal == SIGKILL)
4099 			StartupStatus = STARTUP_SIGNALED;
4100 	}
4101 	if (BgWriterPID != 0)
4102 		signal_child(BgWriterPID, signal);
4103 	if (CheckpointerPID != 0)
4104 		signal_child(CheckpointerPID, signal);
4105 	if (WalWriterPID != 0)
4106 		signal_child(WalWriterPID, signal);
4107 	if (WalReceiverPID != 0)
4108 		signal_child(WalReceiverPID, signal);
4109 	if (AutoVacPID != 0)
4110 		signal_child(AutoVacPID, signal);
4111 	if (PgArchPID != 0)
4112 		signal_child(PgArchPID, signal);
4113 	if (PgStatPID != 0)
4114 		signal_child(PgStatPID, signal);
4115 }
4116 
4117 /*
4118  * BackendStartup -- start backend process
4119  *
4120  * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise.
4121  *
4122  * Note: if you change this code, also consider StartAutovacuumWorker.
4123  */
4124 static int
4125 BackendStartup(Port *port)
4126 {
4127 	Backend    *bn;				/* for backend cleanup */
4128 	pid_t		pid;
4129 
4130 	/*
4131 	 * Create backend data structure.  Better before the fork() so we can
4132 	 * handle failure cleanly.
4133 	 */
4134 	bn = (Backend *) malloc(sizeof(Backend));
4135 	if (!bn)
4136 	{
4137 		ereport(LOG,
4138 				(errcode(ERRCODE_OUT_OF_MEMORY),
4139 				 errmsg("out of memory")));
4140 		return STATUS_ERROR;
4141 	}
4142 
4143 	/*
4144 	 * Compute the cancel key that will be assigned to this backend. The
4145 	 * backend will have its own copy in the forked-off process' value of
4146 	 * MyCancelKey, so that it can transmit the key to the frontend.
4147 	 */
4148 	if (!RandomCancelKey(&MyCancelKey))
4149 	{
4150 		free(bn);
4151 		ereport(LOG,
4152 				(errcode(ERRCODE_INTERNAL_ERROR),
4153 				 errmsg("could not generate random cancel key")));
4154 		return STATUS_ERROR;
4155 	}
4156 
4157 	bn->cancel_key = MyCancelKey;
4158 
4159 	/* Pass down canAcceptConnections state */
4160 	port->canAcceptConnections = canAcceptConnections(BACKEND_TYPE_NORMAL);
4161 	bn->dead_end = (port->canAcceptConnections != CAC_OK &&
4162 					port->canAcceptConnections != CAC_SUPERUSER);
4163 
4164 	/*
4165 	 * Unless it's a dead_end child, assign it a child slot number
4166 	 */
4167 	if (!bn->dead_end)
4168 		bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
4169 	else
4170 		bn->child_slot = 0;
4171 
4172 	/* Hasn't asked to be notified about any bgworkers yet */
4173 	bn->bgworker_notify = false;
4174 
4175 #ifdef EXEC_BACKEND
4176 	pid = backend_forkexec(port);
4177 #else							/* !EXEC_BACKEND */
4178 	pid = fork_process();
4179 	if (pid == 0)				/* child */
4180 	{
4181 		free(bn);
4182 
4183 		/* Detangle from postmaster */
4184 		InitPostmasterChild();
4185 
4186 		/* Close the postmaster's sockets */
4187 		ClosePostmasterPorts(false);
4188 
4189 		/* Perform additional initialization and collect startup packet */
4190 		BackendInitialize(port);
4191 
4192 		/* And run the backend */
4193 		BackendRun(port);
4194 	}
4195 #endif							/* EXEC_BACKEND */
4196 
4197 	if (pid < 0)
4198 	{
4199 		/* in parent, fork failed */
4200 		int			save_errno = errno;
4201 
4202 		if (!bn->dead_end)
4203 			(void) ReleasePostmasterChildSlot(bn->child_slot);
4204 		free(bn);
4205 		errno = save_errno;
4206 		ereport(LOG,
4207 				(errmsg("could not fork new process for connection: %m")));
4208 		report_fork_failure_to_client(port, save_errno);
4209 		return STATUS_ERROR;
4210 	}
4211 
4212 	/* in parent, successful fork */
4213 	ereport(DEBUG2,
4214 			(errmsg_internal("forked new backend, pid=%d socket=%d",
4215 							 (int) pid, (int) port->sock)));
4216 
4217 	/*
4218 	 * Everything's been successful, it's safe to add this backend to our list
4219 	 * of backends.
4220 	 */
4221 	bn->pid = pid;
4222 	bn->bkend_type = BACKEND_TYPE_NORMAL;	/* Can change later to WALSND */
4223 	dlist_push_head(&BackendList, &bn->elem);
4224 
4225 #ifdef EXEC_BACKEND
4226 	if (!bn->dead_end)
4227 		ShmemBackendArrayAdd(bn);
4228 #endif
4229 
4230 	return STATUS_OK;
4231 }
4232 
4233 /*
4234  * Try to report backend fork() failure to client before we close the
4235  * connection.  Since we do not care to risk blocking the postmaster on
4236  * this connection, we set the connection to non-blocking and try only once.
4237  *
4238  * This is grungy special-purpose code; we cannot use backend libpq since
4239  * it's not up and running.
4240  */
4241 static void
4242 report_fork_failure_to_client(Port *port, int errnum)
4243 {
4244 	char		buffer[1000];
4245 	int			rc;
4246 
4247 	/* Format the error message packet (always V2 protocol) */
4248 	snprintf(buffer, sizeof(buffer), "E%s%s\n",
4249 			 _("could not fork new process for connection: "),
4250 			 strerror(errnum));
4251 
4252 	/* Set port to non-blocking.  Don't do send() if this fails */
4253 	if (!pg_set_noblock(port->sock))
4254 		return;
4255 
4256 	/* We'll retry after EINTR, but ignore all other failures */
4257 	do
4258 	{
4259 		rc = send(port->sock, buffer, strlen(buffer) + 1, 0);
4260 	} while (rc < 0 && errno == EINTR);
4261 }
4262 
4263 
4264 /*
4265  * BackendInitialize -- initialize an interactive (postmaster-child)
4266  *				backend process, and collect the client's startup packet.
4267  *
4268  * returns: nothing.  Will not return at all if there's any failure.
4269  *
4270  * Note: this code does not depend on having any access to shared memory.
4271  * In the EXEC_BACKEND case, we are physically attached to shared memory
4272  * but have not yet set up most of our local pointers to shmem structures.
4273  */
4274 static void
4275 BackendInitialize(Port *port)
4276 {
4277 	int			status;
4278 	int			ret;
4279 	char		remote_host[NI_MAXHOST];
4280 	char		remote_port[NI_MAXSERV];
4281 	char		remote_ps_data[NI_MAXHOST];
4282 
4283 	/* Save port etc. for ps status */
4284 	MyProcPort = port;
4285 
4286 	/*
4287 	 * PreAuthDelay is a debugging aid for investigating problems in the
4288 	 * authentication cycle: it can be set in postgresql.conf to allow time to
4289 	 * attach to the newly-forked backend with a debugger.  (See also
4290 	 * PostAuthDelay, which we allow clients to pass through PGOPTIONS, but it
4291 	 * is not honored until after authentication.)
4292 	 */
4293 	if (PreAuthDelay > 0)
4294 		pg_usleep(PreAuthDelay * 1000000L);
4295 
4296 	/* This flag will remain set until InitPostgres finishes authentication */
4297 	ClientAuthInProgress = true;	/* limit visibility of log messages */
4298 
4299 	/* set these to empty in case they are needed before we set them up */
4300 	port->remote_host = "";
4301 	port->remote_port = "";
4302 
4303 	/*
4304 	 * Initialize libpq and enable reporting of ereport errors to the client.
4305 	 * Must do this now because authentication uses libpq to send messages.
4306 	 */
4307 	pq_init();					/* initialize libpq to talk to client */
4308 	whereToSendOutput = DestRemote; /* now safe to ereport to client */
4309 
4310 	/*
4311 	 * We arrange to do proc_exit(1) if we receive SIGTERM or timeout while
4312 	 * trying to collect the startup packet; while SIGQUIT results in
4313 	 * _exit(2).  Otherwise the postmaster cannot shutdown the database FAST
4314 	 * or IMMED cleanly if a buggy client fails to send the packet promptly.
4315 	 *
4316 	 * XXX this is pretty dangerous; signal handlers should not call anything
4317 	 * as complex as proc_exit() directly.  We minimize the hazard by not
4318 	 * keeping these handlers active for longer than we must.  However, it
4319 	 * seems necessary to be able to escape out of DNS lookups as well as the
4320 	 * startup packet reception proper, so we can't narrow the scope further
4321 	 * than is done here.
4322 	 *
4323 	 * XXX it follows that the remainder of this function must tolerate losing
4324 	 * control at any instant.  Likewise, any pg_on_exit_callback registered
4325 	 * before or during this function must be prepared to execute at any
4326 	 * instant between here and the end of this function.  Furthermore,
4327 	 * affected callbacks execute partially or not at all when a second
4328 	 * exit-inducing signal arrives after proc_exit_prepare() decrements
4329 	 * on_proc_exit_index.  (Thanks to that mechanic, callbacks need not
4330 	 * anticipate more than one call.)  This is fragile; it ought to instead
4331 	 * follow the norm of handling interrupts at selected, safe opportunities.
4332 	 */
4333 	pqsignal(SIGTERM, process_startup_packet_die);
4334 	pqsignal(SIGQUIT, process_startup_packet_quickdie);
4335 	InitializeTimeouts();		/* establishes SIGALRM handler */
4336 	PG_SETMASK(&StartupBlockSig);
4337 
4338 	/*
4339 	 * Get the remote host name and port for logging and status display.
4340 	 */
4341 	remote_host[0] = '\0';
4342 	remote_port[0] = '\0';
4343 	if ((ret = pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
4344 								  remote_host, sizeof(remote_host),
4345 								  remote_port, sizeof(remote_port),
4346 								  (log_hostname ? 0 : NI_NUMERICHOST) | NI_NUMERICSERV)) != 0)
4347 		ereport(WARNING,
4348 				(errmsg_internal("pg_getnameinfo_all() failed: %s",
4349 								 gai_strerror(ret))));
4350 	if (remote_port[0] == '\0')
4351 		snprintf(remote_ps_data, sizeof(remote_ps_data), "%s", remote_host);
4352 	else
4353 		snprintf(remote_ps_data, sizeof(remote_ps_data), "%s(%s)", remote_host, remote_port);
4354 
4355 	/*
4356 	 * Save remote_host and remote_port in port structure (after this, they
4357 	 * will appear in log_line_prefix data for log messages).
4358 	 */
4359 	port->remote_host = strdup(remote_host);
4360 	port->remote_port = strdup(remote_port);
4361 
4362 	/* And now we can issue the Log_connections message, if wanted */
4363 	if (Log_connections)
4364 	{
4365 		if (remote_port[0])
4366 			ereport(LOG,
4367 					(errmsg("connection received: host=%s port=%s",
4368 							remote_host,
4369 							remote_port)));
4370 		else
4371 			ereport(LOG,
4372 					(errmsg("connection received: host=%s",
4373 							remote_host)));
4374 	}
4375 
4376 	/*
4377 	 * If we did a reverse lookup to name, we might as well save the results
4378 	 * rather than possibly repeating the lookup during authentication.
4379 	 *
4380 	 * Note that we don't want to specify NI_NAMEREQD above, because then we'd
4381 	 * get nothing useful for a client without an rDNS entry.  Therefore, we
4382 	 * must check whether we got a numeric IPv4 or IPv6 address, and not save
4383 	 * it into remote_hostname if so.  (This test is conservative and might
4384 	 * sometimes classify a hostname as numeric, but an error in that
4385 	 * direction is safe; it only results in a possible extra lookup.)
4386 	 */
4387 	if (log_hostname &&
4388 		ret == 0 &&
4389 		strspn(remote_host, "0123456789.") < strlen(remote_host) &&
4390 		strspn(remote_host, "0123456789ABCDEFabcdef:") < strlen(remote_host))
4391 		port->remote_hostname = strdup(remote_host);
4392 
4393 	/*
4394 	 * Ready to begin client interaction.  We will give up and proc_exit(1)
4395 	 * after a time delay, so that a broken client can't hog a connection
4396 	 * indefinitely.  PreAuthDelay and any DNS interactions above don't count
4397 	 * against the time limit.
4398 	 *
4399 	 * Note: AuthenticationTimeout is applied here while waiting for the
4400 	 * startup packet, and then again in InitPostgres for the duration of any
4401 	 * authentication operations.  So a hostile client could tie up the
4402 	 * process for nearly twice AuthenticationTimeout before we kick him off.
4403 	 *
4404 	 * Note: because PostgresMain will call InitializeTimeouts again, the
4405 	 * registration of STARTUP_PACKET_TIMEOUT will be lost.  This is okay
4406 	 * since we never use it again after this function.
4407 	 */
4408 	RegisterTimeout(STARTUP_PACKET_TIMEOUT, StartupPacketTimeoutHandler);
4409 	enable_timeout_after(STARTUP_PACKET_TIMEOUT, AuthenticationTimeout * 1000);
4410 
4411 	/*
4412 	 * Receive the startup packet (which might turn out to be a cancel request
4413 	 * packet).
4414 	 */
4415 	status = ProcessStartupPacket(port, false, false);
4416 
4417 	/*
4418 	 * Disable the timeout, and prevent SIGTERM/SIGQUIT again.
4419 	 */
4420 	disable_timeout(STARTUP_PACKET_TIMEOUT, false);
4421 	PG_SETMASK(&BlockSig);
4422 
4423 	/*
4424 	 * Stop here if it was bad or a cancel packet.  ProcessStartupPacket
4425 	 * already did any appropriate error reporting.
4426 	 */
4427 	if (status != STATUS_OK)
4428 		proc_exit(0);
4429 
4430 	/*
4431 	 * Now that we have the user and database name, we can set the process
4432 	 * title for ps.  It's good to do this as early as possible in startup.
4433 	 *
4434 	 * For a walsender, the ps display is set in the following form:
4435 	 *
4436 	 * postgres: walsender <user> <host> <activity>
4437 	 *
4438 	 * To achieve that, we pass "walsender" as username and username as dbname
4439 	 * to init_ps_display(). XXX: should add a new variant of
4440 	 * init_ps_display() to avoid abusing the parameters like this.
4441 	 */
4442 	if (am_walsender)
4443 		init_ps_display(pgstat_get_backend_desc(B_WAL_SENDER), port->user_name, remote_ps_data,
4444 						update_process_title ? "authentication" : "");
4445 	else
4446 		init_ps_display(port->user_name, port->database_name, remote_ps_data,
4447 						update_process_title ? "authentication" : "");
4448 }
4449 
4450 
4451 /*
4452  * BackendRun -- set up the backend's argument list and invoke PostgresMain()
4453  *
4454  * returns:
4455  *		Shouldn't return at all.
4456  *		If PostgresMain() fails, return status.
4457  */
4458 static void
4459 BackendRun(Port *port)
4460 {
4461 	char	  **av;
4462 	int			maxac;
4463 	int			ac;
4464 	int			i;
4465 
4466 	/*
4467 	 * Now, build the argv vector that will be given to PostgresMain.
4468 	 *
4469 	 * The maximum possible number of commandline arguments that could come
4470 	 * from ExtraOptions is (strlen(ExtraOptions) + 1) / 2; see
4471 	 * pg_split_opts().
4472 	 */
4473 	maxac = 2;					/* for fixed args supplied below */
4474 	maxac += (strlen(ExtraOptions) + 1) / 2;
4475 
4476 	av = (char **) MemoryContextAlloc(TopMemoryContext,
4477 									  maxac * sizeof(char *));
4478 	ac = 0;
4479 
4480 	av[ac++] = "postgres";
4481 
4482 	/*
4483 	 * Pass any backend switches specified with -o on the postmaster's own
4484 	 * command line.  We assume these are secure.
4485 	 */
4486 	pg_split_opts(av, &ac, ExtraOptions);
4487 
4488 	av[ac] = NULL;
4489 
4490 	Assert(ac < maxac);
4491 
4492 	/*
4493 	 * Debug: print arguments being passed to backend
4494 	 */
4495 	ereport(DEBUG3,
4496 			(errmsg_internal("%s child[%d]: starting with (",
4497 							 progname, (int) getpid())));
4498 	for (i = 0; i < ac; ++i)
4499 		ereport(DEBUG3,
4500 				(errmsg_internal("\t%s", av[i])));
4501 	ereport(DEBUG3,
4502 			(errmsg_internal(")")));
4503 
4504 	/*
4505 	 * Make sure we aren't in PostmasterContext anymore.  (We can't delete it
4506 	 * just yet, though, because InitPostgres will need the HBA data.)
4507 	 */
4508 	MemoryContextSwitchTo(TopMemoryContext);
4509 
4510 	PostgresMain(ac, av, port->database_name, port->user_name);
4511 }
4512 
4513 
4514 #ifdef EXEC_BACKEND
4515 
4516 /*
4517  * postmaster_forkexec -- fork and exec a postmaster subprocess
4518  *
4519  * The caller must have set up the argv array already, except for argv[2]
4520  * which will be filled with the name of the temp variable file.
4521  *
4522  * Returns the child process PID, or -1 on fork failure (a suitable error
4523  * message has been logged on failure).
4524  *
4525  * All uses of this routine will dispatch to SubPostmasterMain in the
4526  * child process.
4527  */
4528 pid_t
4529 postmaster_forkexec(int argc, char *argv[])
4530 {
4531 	Port		port;
4532 
4533 	/* This entry point passes dummy values for the Port variables */
4534 	memset(&port, 0, sizeof(port));
4535 	return internal_forkexec(argc, argv, &port);
4536 }
4537 
4538 /*
4539  * backend_forkexec -- fork/exec off a backend process
4540  *
4541  * Some operating systems (WIN32) don't have fork() so we have to simulate
4542  * it by storing parameters that need to be passed to the child and
4543  * then create a new child process.
4544  *
4545  * returns the pid of the fork/exec'd process, or -1 on failure
4546  */
4547 static pid_t
4548 backend_forkexec(Port *port)
4549 {
4550 	char	   *av[4];
4551 	int			ac = 0;
4552 
4553 	av[ac++] = "postgres";
4554 	av[ac++] = "--forkbackend";
4555 	av[ac++] = NULL;			/* filled in by internal_forkexec */
4556 
4557 	av[ac] = NULL;
4558 	Assert(ac < lengthof(av));
4559 
4560 	return internal_forkexec(ac, av, port);
4561 }
4562 
4563 #ifndef WIN32
4564 
4565 /*
4566  * internal_forkexec non-win32 implementation
4567  *
4568  * - writes out backend variables to the parameter file
4569  * - fork():s, and then exec():s the child process
4570  */
4571 static pid_t
4572 internal_forkexec(int argc, char *argv[], Port *port)
4573 {
4574 	static unsigned long tmpBackendFileNum = 0;
4575 	pid_t		pid;
4576 	char		tmpfilename[MAXPGPATH];
4577 	BackendParameters param;
4578 	FILE	   *fp;
4579 
4580 	if (!save_backend_variables(&param, port))
4581 		return -1;				/* log made by save_backend_variables */
4582 
4583 	/* Calculate name for temp file */
4584 	snprintf(tmpfilename, MAXPGPATH, "%s/%s.backend_var.%d.%lu",
4585 			 PG_TEMP_FILES_DIR, PG_TEMP_FILE_PREFIX,
4586 			 MyProcPid, ++tmpBackendFileNum);
4587 
4588 	/* Open file */
4589 	fp = AllocateFile(tmpfilename, PG_BINARY_W);
4590 	if (!fp)
4591 	{
4592 		/*
4593 		 * As in OpenTemporaryFileInTablespace, try to make the temp-file
4594 		 * directory, ignoring errors.
4595 		 */
4596 		(void) MakePGDirectory(PG_TEMP_FILES_DIR);
4597 
4598 		fp = AllocateFile(tmpfilename, PG_BINARY_W);
4599 		if (!fp)
4600 		{
4601 			ereport(LOG,
4602 					(errcode_for_file_access(),
4603 					 errmsg("could not create file \"%s\": %m",
4604 							tmpfilename)));
4605 			return -1;
4606 		}
4607 	}
4608 
4609 	if (fwrite(&param, sizeof(param), 1, fp) != 1)
4610 	{
4611 		ereport(LOG,
4612 				(errcode_for_file_access(),
4613 				 errmsg("could not write to file \"%s\": %m", tmpfilename)));
4614 		FreeFile(fp);
4615 		return -1;
4616 	}
4617 
4618 	/* Release file */
4619 	if (FreeFile(fp))
4620 	{
4621 		ereport(LOG,
4622 				(errcode_for_file_access(),
4623 				 errmsg("could not write to file \"%s\": %m", tmpfilename)));
4624 		return -1;
4625 	}
4626 
4627 	/* Make sure caller set up argv properly */
4628 	Assert(argc >= 3);
4629 	Assert(argv[argc] == NULL);
4630 	Assert(strncmp(argv[1], "--fork", 6) == 0);
4631 	Assert(argv[2] == NULL);
4632 
4633 	/* Insert temp file name after --fork argument */
4634 	argv[2] = tmpfilename;
4635 
4636 	/* Fire off execv in child */
4637 	if ((pid = fork_process()) == 0)
4638 	{
4639 		if (execv(postgres_exec_path, argv) < 0)
4640 		{
4641 			ereport(LOG,
4642 					(errmsg("could not execute server process \"%s\": %m",
4643 							postgres_exec_path)));
4644 			/* We're already in the child process here, can't return */
4645 			exit(1);
4646 		}
4647 	}
4648 
4649 	return pid;					/* Parent returns pid, or -1 on fork failure */
4650 }
4651 #else							/* WIN32 */
4652 
4653 /*
4654  * internal_forkexec win32 implementation
4655  *
4656  * - starts backend using CreateProcess(), in suspended state
4657  * - writes out backend variables to the parameter file
4658  *	- during this, duplicates handles and sockets required for
4659  *	  inheritance into the new process
4660  * - resumes execution of the new process once the backend parameter
4661  *	 file is complete.
4662  */
4663 static pid_t
4664 internal_forkexec(int argc, char *argv[], Port *port)
4665 {
4666 	int			retry_count = 0;
4667 	STARTUPINFO si;
4668 	PROCESS_INFORMATION pi;
4669 	int			i;
4670 	int			j;
4671 	char		cmdLine[MAXPGPATH * 2];
4672 	HANDLE		paramHandle;
4673 	BackendParameters *param;
4674 	SECURITY_ATTRIBUTES sa;
4675 	char		paramHandleStr[32];
4676 	win32_deadchild_waitinfo *childinfo;
4677 
4678 	/* Make sure caller set up argv properly */
4679 	Assert(argc >= 3);
4680 	Assert(argv[argc] == NULL);
4681 	Assert(strncmp(argv[1], "--fork", 6) == 0);
4682 	Assert(argv[2] == NULL);
4683 
4684 	/* Resume here if we need to retry */
4685 retry:
4686 
4687 	/* Set up shared memory for parameter passing */
4688 	ZeroMemory(&sa, sizeof(sa));
4689 	sa.nLength = sizeof(sa);
4690 	sa.bInheritHandle = TRUE;
4691 	paramHandle = CreateFileMapping(INVALID_HANDLE_VALUE,
4692 									&sa,
4693 									PAGE_READWRITE,
4694 									0,
4695 									sizeof(BackendParameters),
4696 									NULL);
4697 	if (paramHandle == INVALID_HANDLE_VALUE)
4698 	{
4699 		elog(LOG, "could not create backend parameter file mapping: error code %lu",
4700 			 GetLastError());
4701 		return -1;
4702 	}
4703 
4704 	param = MapViewOfFile(paramHandle, FILE_MAP_WRITE, 0, 0, sizeof(BackendParameters));
4705 	if (!param)
4706 	{
4707 		elog(LOG, "could not map backend parameter memory: error code %lu",
4708 			 GetLastError());
4709 		CloseHandle(paramHandle);
4710 		return -1;
4711 	}
4712 
4713 	/* Insert temp file name after --fork argument */
4714 #ifdef _WIN64
4715 	sprintf(paramHandleStr, "%llu", (LONG_PTR) paramHandle);
4716 #else
4717 	sprintf(paramHandleStr, "%lu", (DWORD) paramHandle);
4718 #endif
4719 	argv[2] = paramHandleStr;
4720 
4721 	/* Format the cmd line */
4722 	cmdLine[sizeof(cmdLine) - 1] = '\0';
4723 	cmdLine[sizeof(cmdLine) - 2] = '\0';
4724 	snprintf(cmdLine, sizeof(cmdLine) - 1, "\"%s\"", postgres_exec_path);
4725 	i = 0;
4726 	while (argv[++i] != NULL)
4727 	{
4728 		j = strlen(cmdLine);
4729 		snprintf(cmdLine + j, sizeof(cmdLine) - 1 - j, " \"%s\"", argv[i]);
4730 	}
4731 	if (cmdLine[sizeof(cmdLine) - 2] != '\0')
4732 	{
4733 		elog(LOG, "subprocess command line too long");
4734 		return -1;
4735 	}
4736 
4737 	memset(&pi, 0, sizeof(pi));
4738 	memset(&si, 0, sizeof(si));
4739 	si.cb = sizeof(si);
4740 
4741 	/*
4742 	 * Create the subprocess in a suspended state. This will be resumed later,
4743 	 * once we have written out the parameter file.
4744 	 */
4745 	if (!CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, CREATE_SUSPENDED,
4746 					   NULL, NULL, &si, &pi))
4747 	{
4748 		elog(LOG, "CreateProcess call failed: %m (error code %lu)",
4749 			 GetLastError());
4750 		return -1;
4751 	}
4752 
4753 	if (!save_backend_variables(param, port, pi.hProcess, pi.dwProcessId))
4754 	{
4755 		/*
4756 		 * log made by save_backend_variables, but we have to clean up the
4757 		 * mess with the half-started process
4758 		 */
4759 		if (!TerminateProcess(pi.hProcess, 255))
4760 			ereport(LOG,
4761 					(errmsg_internal("could not terminate unstarted process: error code %lu",
4762 									 GetLastError())));
4763 		CloseHandle(pi.hProcess);
4764 		CloseHandle(pi.hThread);
4765 		return -1;				/* log made by save_backend_variables */
4766 	}
4767 
4768 	/* Drop the parameter shared memory that is now inherited to the backend */
4769 	if (!UnmapViewOfFile(param))
4770 		elog(LOG, "could not unmap view of backend parameter file: error code %lu",
4771 			 GetLastError());
4772 	if (!CloseHandle(paramHandle))
4773 		elog(LOG, "could not close handle to backend parameter file: error code %lu",
4774 			 GetLastError());
4775 
4776 	/*
4777 	 * Reserve the memory region used by our main shared memory segment before
4778 	 * we resume the child process.  Normally this should succeed, but if ASLR
4779 	 * is active then it might sometimes fail due to the stack or heap having
4780 	 * gotten mapped into that range.  In that case, just terminate the
4781 	 * process and retry.
4782 	 */
4783 	if (!pgwin32_ReserveSharedMemoryRegion(pi.hProcess))
4784 	{
4785 		/* pgwin32_ReserveSharedMemoryRegion already made a log entry */
4786 		if (!TerminateProcess(pi.hProcess, 255))
4787 			ereport(LOG,
4788 					(errmsg_internal("could not terminate process that failed to reserve memory: error code %lu",
4789 									 GetLastError())));
4790 		CloseHandle(pi.hProcess);
4791 		CloseHandle(pi.hThread);
4792 		if (++retry_count < 100)
4793 			goto retry;
4794 		ereport(LOG,
4795 				(errmsg("giving up after too many tries to reserve shared memory"),
4796 				 errhint("This might be caused by ASLR or antivirus software.")));
4797 		return -1;
4798 	}
4799 
4800 	/*
4801 	 * Now that the backend variables are written out, we start the child
4802 	 * thread so it can start initializing while we set up the rest of the
4803 	 * parent state.
4804 	 */
4805 	if (ResumeThread(pi.hThread) == -1)
4806 	{
4807 		if (!TerminateProcess(pi.hProcess, 255))
4808 		{
4809 			ereport(LOG,
4810 					(errmsg_internal("could not terminate unstartable process: error code %lu",
4811 									 GetLastError())));
4812 			CloseHandle(pi.hProcess);
4813 			CloseHandle(pi.hThread);
4814 			return -1;
4815 		}
4816 		CloseHandle(pi.hProcess);
4817 		CloseHandle(pi.hThread);
4818 		ereport(LOG,
4819 				(errmsg_internal("could not resume thread of unstarted process: error code %lu",
4820 								 GetLastError())));
4821 		return -1;
4822 	}
4823 
4824 	/*
4825 	 * Queue a waiter to signal when this child dies. The wait will be handled
4826 	 * automatically by an operating system thread pool.
4827 	 *
4828 	 * Note: use malloc instead of palloc, since it needs to be thread-safe.
4829 	 * Struct will be free():d from the callback function that runs on a
4830 	 * different thread.
4831 	 */
4832 	childinfo = malloc(sizeof(win32_deadchild_waitinfo));
4833 	if (!childinfo)
4834 		ereport(FATAL,
4835 				(errcode(ERRCODE_OUT_OF_MEMORY),
4836 				 errmsg("out of memory")));
4837 
4838 	childinfo->procHandle = pi.hProcess;
4839 	childinfo->procId = pi.dwProcessId;
4840 
4841 	if (!RegisterWaitForSingleObject(&childinfo->waitHandle,
4842 									 pi.hProcess,
4843 									 pgwin32_deadchild_callback,
4844 									 childinfo,
4845 									 INFINITE,
4846 									 WT_EXECUTEONLYONCE | WT_EXECUTEINWAITTHREAD))
4847 		ereport(FATAL,
4848 				(errmsg_internal("could not register process for wait: error code %lu",
4849 								 GetLastError())));
4850 
4851 	/* Don't close pi.hProcess here - the wait thread needs access to it */
4852 
4853 	CloseHandle(pi.hThread);
4854 
4855 	return pi.dwProcessId;
4856 }
4857 #endif							/* WIN32 */
4858 
4859 
4860 /*
4861  * SubPostmasterMain -- Get the fork/exec'd process into a state equivalent
4862  *			to what it would be if we'd simply forked on Unix, and then
4863  *			dispatch to the appropriate place.
4864  *
4865  * The first two command line arguments are expected to be "--forkFOO"
4866  * (where FOO indicates which postmaster child we are to become), and
4867  * the name of a variables file that we can read to load data that would
4868  * have been inherited by fork() on Unix.  Remaining arguments go to the
4869  * subprocess FooMain() routine.
4870  */
4871 void
4872 SubPostmasterMain(int argc, char *argv[])
4873 {
4874 	Port		port;
4875 
4876 	/* In EXEC_BACKEND case we will not have inherited these settings */
4877 	IsPostmasterEnvironment = true;
4878 	whereToSendOutput = DestNone;
4879 
4880 	/* Setup as postmaster child */
4881 	InitPostmasterChild();
4882 
4883 	/* Setup essential subsystems (to ensure elog() behaves sanely) */
4884 	InitializeGUCOptions();
4885 
4886 	/* Check we got appropriate args */
4887 	if (argc < 3)
4888 		elog(FATAL, "invalid subpostmaster invocation");
4889 
4890 	/* Read in the variables file */
4891 	memset(&port, 0, sizeof(Port));
4892 	read_backend_variables(argv[2], &port);
4893 
4894 	/* Close the postmaster's sockets (as soon as we know them) */
4895 	ClosePostmasterPorts(strcmp(argv[1], "--forklog") == 0);
4896 
4897 	/*
4898 	 * Set reference point for stack-depth checking
4899 	 */
4900 	set_stack_base();
4901 
4902 	/*
4903 	 * If appropriate, physically re-attach to shared memory segment. We want
4904 	 * to do this before going any further to ensure that we can attach at the
4905 	 * same address the postmaster used.  On the other hand, if we choose not
4906 	 * to re-attach, we may have other cleanup to do.
4907 	 *
4908 	 * If testing EXEC_BACKEND on Linux, you should run this as root before
4909 	 * starting the postmaster:
4910 	 *
4911 	 * echo 0 >/proc/sys/kernel/randomize_va_space
4912 	 *
4913 	 * This prevents using randomized stack and code addresses that cause the
4914 	 * child process's memory map to be different from the parent's, making it
4915 	 * sometimes impossible to attach to shared memory at the desired address.
4916 	 * Return the setting to its old value (usually '1' or '2') when finished.
4917 	 */
4918 	if (strcmp(argv[1], "--forkbackend") == 0 ||
4919 		strcmp(argv[1], "--forkavlauncher") == 0 ||
4920 		strcmp(argv[1], "--forkavworker") == 0 ||
4921 		strcmp(argv[1], "--forkboot") == 0 ||
4922 		strncmp(argv[1], "--forkbgworker=", 15) == 0)
4923 		PGSharedMemoryReAttach();
4924 	else
4925 		PGSharedMemoryNoReAttach();
4926 
4927 	/* autovacuum needs this set before calling InitProcess */
4928 	if (strcmp(argv[1], "--forkavlauncher") == 0)
4929 		AutovacuumLauncherIAm();
4930 	if (strcmp(argv[1], "--forkavworker") == 0)
4931 		AutovacuumWorkerIAm();
4932 
4933 	/*
4934 	 * Start our win32 signal implementation. This has to be done after we
4935 	 * read the backend variables, because we need to pick up the signal pipe
4936 	 * from the parent process.
4937 	 */
4938 #ifdef WIN32
4939 	pgwin32_signal_initialize();
4940 #endif
4941 
4942 	/* In EXEC_BACKEND case we will not have inherited these settings */
4943 	pqinitmask();
4944 	PG_SETMASK(&BlockSig);
4945 
4946 	/* Read in remaining GUC variables */
4947 	read_nondefault_variables();
4948 
4949 	/*
4950 	 * Check that the data directory looks valid, which will also check the
4951 	 * privileges on the data directory and update our umask and file/group
4952 	 * variables for creating files later.  Note: this should really be done
4953 	 * before we create any files or directories.
4954 	 */
4955 	checkDataDir();
4956 
4957 	/*
4958 	 * (re-)read control file, as it contains config. The postmaster will
4959 	 * already have read this, but this process doesn't know about that.
4960 	 */
4961 	LocalProcessControlFile(false);
4962 
4963 	/*
4964 	 * Reload any libraries that were preloaded by the postmaster.  Since we
4965 	 * exec'd this process, those libraries didn't come along with us; but we
4966 	 * should load them into all child processes to be consistent with the
4967 	 * non-EXEC_BACKEND behavior.
4968 	 */
4969 	process_shared_preload_libraries();
4970 
4971 	/* Run backend or appropriate child */
4972 	if (strcmp(argv[1], "--forkbackend") == 0)
4973 	{
4974 		Assert(argc == 3);		/* shouldn't be any more args */
4975 
4976 		/*
4977 		 * Need to reinitialize the SSL library in the backend, since the
4978 		 * context structures contain function pointers and cannot be passed
4979 		 * through the parameter file.
4980 		 *
4981 		 * If for some reason reload fails (maybe the user installed broken
4982 		 * key files), soldier on without SSL; that's better than all
4983 		 * connections becoming impossible.
4984 		 *
4985 		 * XXX should we do this in all child processes?  For the moment it's
4986 		 * enough to do it in backend children.
4987 		 */
4988 #ifdef USE_SSL
4989 		if (EnableSSL)
4990 		{
4991 			if (secure_initialize(false) == 0)
4992 				LoadedSSL = true;
4993 			else
4994 				ereport(LOG,
4995 						(errmsg("SSL configuration could not be loaded in child process")));
4996 		}
4997 #endif
4998 
4999 		/*
5000 		 * Perform additional initialization and collect startup packet.
5001 		 *
5002 		 * We want to do this before InitProcess() for a couple of reasons: 1.
5003 		 * so that we aren't eating up a PGPROC slot while waiting on the
5004 		 * client. 2. so that if InitProcess() fails due to being out of
5005 		 * PGPROC slots, we have already initialized libpq and are able to
5006 		 * report the error to the client.
5007 		 */
5008 		BackendInitialize(&port);
5009 
5010 		/* Restore basic shared memory pointers */
5011 		InitShmemAccess(UsedShmemSegAddr);
5012 
5013 		/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
5014 		InitProcess();
5015 
5016 		/* Attach process to shared data structures */
5017 		CreateSharedMemoryAndSemaphores(0);
5018 
5019 		/* And run the backend */
5020 		BackendRun(&port);		/* does not return */
5021 	}
5022 	if (strcmp(argv[1], "--forkboot") == 0)
5023 	{
5024 		/* Restore basic shared memory pointers */
5025 		InitShmemAccess(UsedShmemSegAddr);
5026 
5027 		/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
5028 		InitAuxiliaryProcess();
5029 
5030 		/* Attach process to shared data structures */
5031 		CreateSharedMemoryAndSemaphores(0);
5032 
5033 		AuxiliaryProcessMain(argc - 2, argv + 2);	/* does not return */
5034 	}
5035 	if (strcmp(argv[1], "--forkavlauncher") == 0)
5036 	{
5037 		/* Restore basic shared memory pointers */
5038 		InitShmemAccess(UsedShmemSegAddr);
5039 
5040 		/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
5041 		InitProcess();
5042 
5043 		/* Attach process to shared data structures */
5044 		CreateSharedMemoryAndSemaphores(0);
5045 
5046 		AutoVacLauncherMain(argc - 2, argv + 2);	/* does not return */
5047 	}
5048 	if (strcmp(argv[1], "--forkavworker") == 0)
5049 	{
5050 		/* Restore basic shared memory pointers */
5051 		InitShmemAccess(UsedShmemSegAddr);
5052 
5053 		/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
5054 		InitProcess();
5055 
5056 		/* Attach process to shared data structures */
5057 		CreateSharedMemoryAndSemaphores(0);
5058 
5059 		AutoVacWorkerMain(argc - 2, argv + 2);	/* does not return */
5060 	}
5061 	if (strncmp(argv[1], "--forkbgworker=", 15) == 0)
5062 	{
5063 		int			shmem_slot;
5064 
5065 		/* do this as early as possible; in particular, before InitProcess() */
5066 		IsBackgroundWorker = true;
5067 
5068 		/* Restore basic shared memory pointers */
5069 		InitShmemAccess(UsedShmemSegAddr);
5070 
5071 		/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
5072 		InitProcess();
5073 
5074 		/* Attach process to shared data structures */
5075 		CreateSharedMemoryAndSemaphores(0);
5076 
5077 		/* Fetch MyBgworkerEntry from shared memory */
5078 		shmem_slot = atoi(argv[1] + 15);
5079 		MyBgworkerEntry = BackgroundWorkerEntry(shmem_slot);
5080 
5081 		StartBackgroundWorker();
5082 	}
5083 	if (strcmp(argv[1], "--forkarch") == 0)
5084 	{
5085 		/* Do not want to attach to shared memory */
5086 
5087 		PgArchiverMain(argc, argv); /* does not return */
5088 	}
5089 	if (strcmp(argv[1], "--forkcol") == 0)
5090 	{
5091 		/* Do not want to attach to shared memory */
5092 
5093 		PgstatCollectorMain(argc, argv);	/* does not return */
5094 	}
5095 	if (strcmp(argv[1], "--forklog") == 0)
5096 	{
5097 		/* Do not want to attach to shared memory */
5098 
5099 		SysLoggerMain(argc, argv);	/* does not return */
5100 	}
5101 
5102 	abort();					/* shouldn't get here */
5103 }
5104 #endif							/* EXEC_BACKEND */
5105 
5106 
5107 /*
5108  * ExitPostmaster -- cleanup
5109  *
5110  * Do NOT call exit() directly --- always go through here!
5111  */
5112 static void
5113 ExitPostmaster(int status)
5114 {
5115 #ifdef HAVE_PTHREAD_IS_THREADED_NP
5116 
5117 	/*
5118 	 * There is no known cause for a postmaster to become multithreaded after
5119 	 * startup.  Recheck to account for the possibility of unknown causes.
5120 	 * This message uses LOG level, because an unclean shutdown at this point
5121 	 * would usually not look much different from a clean shutdown.
5122 	 */
5123 	if (pthread_is_threaded_np() != 0)
5124 		ereport(LOG,
5125 				(errcode(ERRCODE_INTERNAL_ERROR),
5126 				 errmsg_internal("postmaster became multithreaded"),
5127 				 errdetail("Please report this to <pgsql-bugs@lists.postgresql.org>.")));
5128 #endif
5129 
5130 	/* should cleanup shared memory and kill all backends */
5131 
5132 	/*
5133 	 * Not sure of the semantics here.  When the Postmaster dies, should the
5134 	 * backends all be killed? probably not.
5135 	 *
5136 	 * MUST		-- vadim 05-10-1999
5137 	 */
5138 
5139 	proc_exit(status);
5140 }
5141 
5142 /*
5143  * sigusr1_handler - handle signal conditions from child processes
5144  */
5145 static void
5146 sigusr1_handler(SIGNAL_ARGS)
5147 {
5148 	int			save_errno = errno;
5149 
5150 	/*
5151 	 * We rely on the signal mechanism to have blocked all signals ... except
5152 	 * on Windows, which lacks sigaction(), so we have to do it manually.
5153 	 */
5154 #ifdef WIN32
5155 	PG_SETMASK(&BlockSig);
5156 #endif
5157 
5158 	/*
5159 	 * RECOVERY_STARTED and BEGIN_HOT_STANDBY signals are ignored in
5160 	 * unexpected states. If the startup process quickly starts up, completes
5161 	 * recovery, exits, we might process the death of the startup process
5162 	 * first. We don't want to go back to recovery in that case.
5163 	 */
5164 	if (CheckPostmasterSignal(PMSIGNAL_RECOVERY_STARTED) &&
5165 		pmState == PM_STARTUP && Shutdown == NoShutdown)
5166 	{
5167 		/* WAL redo has started. We're out of reinitialization. */
5168 		FatalError = false;
5169 		Assert(AbortStartTime == 0);
5170 
5171 		/*
5172 		 * Crank up the background tasks.  It doesn't matter if this fails,
5173 		 * we'll just try again later.
5174 		 */
5175 		Assert(CheckpointerPID == 0);
5176 		CheckpointerPID = StartCheckpointer();
5177 		Assert(BgWriterPID == 0);
5178 		BgWriterPID = StartBackgroundWriter();
5179 
5180 		/*
5181 		 * Start the archiver if we're responsible for (re-)archiving received
5182 		 * files.
5183 		 */
5184 		Assert(PgArchPID == 0);
5185 		if (XLogArchivingAlways())
5186 			PgArchPID = pgarch_start();
5187 
5188 		/*
5189 		 * If we aren't planning to enter hot standby mode later, treat
5190 		 * RECOVERY_STARTED as meaning we're out of startup, and report status
5191 		 * accordingly.
5192 		 */
5193 		if (!EnableHotStandby)
5194 		{
5195 			AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_STANDBY);
5196 #ifdef USE_SYSTEMD
5197 			sd_notify(0, "READY=1");
5198 #endif
5199 		}
5200 
5201 		pmState = PM_RECOVERY;
5202 	}
5203 
5204 	if (CheckPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY) &&
5205 		pmState == PM_RECOVERY && Shutdown == NoShutdown)
5206 	{
5207 		/*
5208 		 * Likewise, start other special children as needed.
5209 		 */
5210 		Assert(PgStatPID == 0);
5211 		PgStatPID = pgstat_start();
5212 
5213 		ereport(LOG,
5214 				(errmsg("database system is ready to accept read only connections")));
5215 
5216 		/* Report status */
5217 		AddToDataDirLockFile(LOCK_FILE_LINE_PM_STATUS, PM_STATUS_READY);
5218 #ifdef USE_SYSTEMD
5219 		sd_notify(0, "READY=1");
5220 #endif
5221 
5222 		pmState = PM_HOT_STANDBY;
5223 		connsAllowed = ALLOW_ALL_CONNS;
5224 
5225 		/* Some workers may be scheduled to start now */
5226 		StartWorkerNeeded = true;
5227 	}
5228 
5229 	/* Process background worker state changes. */
5230 	if (CheckPostmasterSignal(PMSIGNAL_BACKGROUND_WORKER_CHANGE))
5231 	{
5232 		/* Accept new worker requests only if not stopping. */
5233 		BackgroundWorkerStateChange(pmState < PM_STOP_BACKENDS);
5234 		StartWorkerNeeded = true;
5235 	}
5236 
5237 	if (StartWorkerNeeded || HaveCrashedWorker)
5238 		maybe_start_bgworkers();
5239 
5240 	if (CheckPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER) &&
5241 		PgArchPID != 0)
5242 	{
5243 		/*
5244 		 * Send SIGUSR1 to archiver process, to wake it up and begin archiving
5245 		 * next WAL file.
5246 		 */
5247 		signal_child(PgArchPID, SIGUSR1);
5248 	}
5249 
5250 	/* Tell syslogger to rotate logfile if requested */
5251 	if (SysLoggerPID != 0)
5252 	{
5253 		if (CheckLogrotateSignal())
5254 		{
5255 			signal_child(SysLoggerPID, SIGUSR1);
5256 			RemoveLogrotateSignalFiles();
5257 		}
5258 		else if (CheckPostmasterSignal(PMSIGNAL_ROTATE_LOGFILE))
5259 		{
5260 			signal_child(SysLoggerPID, SIGUSR1);
5261 		}
5262 	}
5263 
5264 	if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER) &&
5265 		Shutdown <= SmartShutdown && pmState < PM_STOP_BACKENDS)
5266 	{
5267 		/*
5268 		 * Start one iteration of the autovacuum daemon, even if autovacuuming
5269 		 * is nominally not enabled.  This is so we can have an active defense
5270 		 * against transaction ID wraparound.  We set a flag for the main loop
5271 		 * to do it rather than trying to do it here --- this is because the
5272 		 * autovac process itself may send the signal, and we want to handle
5273 		 * that by launching another iteration as soon as the current one
5274 		 * completes.
5275 		 */
5276 		start_autovac_launcher = true;
5277 	}
5278 
5279 	if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER) &&
5280 		Shutdown <= SmartShutdown && pmState < PM_STOP_BACKENDS)
5281 	{
5282 		/* The autovacuum launcher wants us to start a worker process. */
5283 		StartAutovacuumWorker();
5284 	}
5285 
5286 	if (CheckPostmasterSignal(PMSIGNAL_START_WALRECEIVER))
5287 	{
5288 		/* Startup Process wants us to start the walreceiver process. */
5289 		/* Start immediately if possible, else remember request for later. */
5290 		WalReceiverRequested = true;
5291 		MaybeStartWalReceiver();
5292 	}
5293 
5294 	/*
5295 	 * Try to advance postmaster's state machine, if a child requests it.
5296 	 *
5297 	 * Be careful about the order of this action relative to sigusr1_handler's
5298 	 * other actions.  Generally, this should be after other actions, in case
5299 	 * they have effects PostmasterStateMachine would need to know about.
5300 	 * However, we should do it before the CheckPromoteSignal step, which
5301 	 * cannot have any (immediate) effect on the state machine, but does
5302 	 * depend on what state we're in now.
5303 	 */
5304 	if (CheckPostmasterSignal(PMSIGNAL_ADVANCE_STATE_MACHINE))
5305 	{
5306 		PostmasterStateMachine();
5307 	}
5308 
5309 	if (StartupPID != 0 &&
5310 		(pmState == PM_STARTUP || pmState == PM_RECOVERY ||
5311 		 pmState == PM_HOT_STANDBY) &&
5312 		CheckPromoteSignal())
5313 	{
5314 		/* Tell startup process to finish recovery */
5315 		signal_child(StartupPID, SIGUSR2);
5316 	}
5317 
5318 #ifdef WIN32
5319 	PG_SETMASK(&UnBlockSig);
5320 #endif
5321 
5322 	errno = save_errno;
5323 }
5324 
5325 /*
5326  * SIGTERM while processing startup packet.
5327  * Clean up and exit(1).
5328  *
5329  * Running proc_exit() from a signal handler is pretty unsafe, since we
5330  * can't know what code we've interrupted.  But the alternative of using
5331  * _exit(2) is also unpalatable, since it'd mean that a "fast shutdown"
5332  * would cause a database crash cycle (forcing WAL replay at restart)
5333  * if any sessions are in authentication.  So we live with it for now.
5334  *
5335  * One might be tempted to try to send a message indicating why we are
5336  * disconnecting.  However, that would make this even more unsafe.  Also,
5337  * it seems undesirable to provide clues about the database's state to
5338  * a client that has not yet completed authentication.
5339  */
5340 static void
5341 process_startup_packet_die(SIGNAL_ARGS)
5342 {
5343 	proc_exit(1);
5344 }
5345 
5346 /*
5347  * SIGQUIT while processing startup packet.
5348  *
5349  * Some backend has bought the farm,
5350  * so we need to stop what we're doing and exit.
5351  */
5352 static void
5353 process_startup_packet_quickdie(SIGNAL_ARGS)
5354 {
5355 	/*
5356 	 * We DO NOT want to run proc_exit() or atexit() callbacks; they wouldn't
5357 	 * be safe to run from a signal handler.  Just nail the windows shut and
5358 	 * get out of town.
5359 	 *
5360 	 * Note we do _exit(2) not _exit(1).  This is to force the postmaster into
5361 	 * a system reset cycle if someone sends a manual SIGQUIT to a random
5362 	 * backend.  (While it might be safe to do _exit(1), since this session
5363 	 * shouldn't have touched shared memory yet, there seems little point in
5364 	 * taking any risks.)
5365 	 */
5366 	_exit(2);
5367 }
5368 
5369 /*
5370  * Dummy signal handler
5371  *
5372  * We use this for signals that we don't actually use in the postmaster,
5373  * but we do use in backends.  If we were to SIG_IGN such signals in the
5374  * postmaster, then a newly started backend might drop a signal that arrives
5375  * before it's able to reconfigure its signal processing.  (See notes in
5376  * tcop/postgres.c.)
5377  */
5378 static void
5379 dummy_handler(SIGNAL_ARGS)
5380 {
5381 }
5382 
5383 /*
5384  * Timeout while processing startup packet.
5385  * As for process_startup_packet_die(), we clean up and exit(1).
5386  *
5387  * This is theoretically just as hazardous as in process_startup_packet_die(),
5388  * although in practice we're almost certainly waiting for client input,
5389  * which greatly reduces the risk.
5390  */
5391 static void
5392 StartupPacketTimeoutHandler(void)
5393 {
5394 	proc_exit(1);
5395 }
5396 
5397 
5398 /*
5399  * Generate a random cancel key.
5400  */
5401 static bool
5402 RandomCancelKey(int32 *cancel_key)
5403 {
5404 	return pg_strong_random(cancel_key, sizeof(int32));
5405 }
5406 
5407 /*
5408  * Count up number of child processes of specified types (dead_end children
5409  * are always excluded).
5410  */
5411 static int
5412 CountChildren(int target)
5413 {
5414 	dlist_iter	iter;
5415 	int			cnt = 0;
5416 
5417 	dlist_foreach(iter, &BackendList)
5418 	{
5419 		Backend    *bp = dlist_container(Backend, elem, iter.cur);
5420 
5421 		if (bp->dead_end)
5422 			continue;
5423 
5424 		/*
5425 		 * Since target == BACKEND_TYPE_ALL is the most common case, we test
5426 		 * it first and avoid touching shared memory for every child.
5427 		 */
5428 		if (target != BACKEND_TYPE_ALL)
5429 		{
5430 			/*
5431 			 * Assign bkend_type for any recently announced WAL Sender
5432 			 * processes.
5433 			 */
5434 			if (bp->bkend_type == BACKEND_TYPE_NORMAL &&
5435 				IsPostmasterChildWalSender(bp->child_slot))
5436 				bp->bkend_type = BACKEND_TYPE_WALSND;
5437 
5438 			if (!(target & bp->bkend_type))
5439 				continue;
5440 		}
5441 
5442 		cnt++;
5443 	}
5444 	return cnt;
5445 }
5446 
5447 
5448 /*
5449  * StartChildProcess -- start an auxiliary process for the postmaster
5450  *
5451  * "type" determines what kind of child will be started.  All child types
5452  * initially go to AuxiliaryProcessMain, which will handle common setup.
5453  *
5454  * Return value of StartChildProcess is subprocess' PID, or 0 if failed
5455  * to start subprocess.
5456  */
5457 static pid_t
5458 StartChildProcess(AuxProcType type)
5459 {
5460 	pid_t		pid;
5461 	char	   *av[10];
5462 	int			ac = 0;
5463 	char		typebuf[32];
5464 
5465 	/*
5466 	 * Set up command-line arguments for subprocess
5467 	 */
5468 	av[ac++] = "postgres";
5469 
5470 #ifdef EXEC_BACKEND
5471 	av[ac++] = "--forkboot";
5472 	av[ac++] = NULL;			/* filled in by postmaster_forkexec */
5473 #endif
5474 
5475 	snprintf(typebuf, sizeof(typebuf), "-x%d", type);
5476 	av[ac++] = typebuf;
5477 
5478 	av[ac] = NULL;
5479 	Assert(ac < lengthof(av));
5480 
5481 #ifdef EXEC_BACKEND
5482 	pid = postmaster_forkexec(ac, av);
5483 #else							/* !EXEC_BACKEND */
5484 	pid = fork_process();
5485 
5486 	if (pid == 0)				/* child */
5487 	{
5488 		InitPostmasterChild();
5489 
5490 		/* Close the postmaster's sockets */
5491 		ClosePostmasterPorts(false);
5492 
5493 		/* Release postmaster's working memory context */
5494 		MemoryContextSwitchTo(TopMemoryContext);
5495 		MemoryContextDelete(PostmasterContext);
5496 		PostmasterContext = NULL;
5497 
5498 		AuxiliaryProcessMain(ac, av);
5499 		ExitPostmaster(0);
5500 	}
5501 #endif							/* EXEC_BACKEND */
5502 
5503 	if (pid < 0)
5504 	{
5505 		/* in parent, fork failed */
5506 		int			save_errno = errno;
5507 
5508 		errno = save_errno;
5509 		switch (type)
5510 		{
5511 			case StartupProcess:
5512 				ereport(LOG,
5513 						(errmsg("could not fork startup process: %m")));
5514 				break;
5515 			case BgWriterProcess:
5516 				ereport(LOG,
5517 						(errmsg("could not fork background writer process: %m")));
5518 				break;
5519 			case CheckpointerProcess:
5520 				ereport(LOG,
5521 						(errmsg("could not fork checkpointer process: %m")));
5522 				break;
5523 			case WalWriterProcess:
5524 				ereport(LOG,
5525 						(errmsg("could not fork WAL writer process: %m")));
5526 				break;
5527 			case WalReceiverProcess:
5528 				ereport(LOG,
5529 						(errmsg("could not fork WAL receiver process: %m")));
5530 				break;
5531 			default:
5532 				ereport(LOG,
5533 						(errmsg("could not fork process: %m")));
5534 				break;
5535 		}
5536 
5537 		/*
5538 		 * fork failure is fatal during startup, but there's no need to choke
5539 		 * immediately if starting other child types fails.
5540 		 */
5541 		if (type == StartupProcess)
5542 			ExitPostmaster(1);
5543 		return 0;
5544 	}
5545 
5546 	/*
5547 	 * in parent, successful fork
5548 	 */
5549 	return pid;
5550 }
5551 
5552 /*
5553  * StartAutovacuumWorker
5554  *		Start an autovac worker process.
5555  *
5556  * This function is here because it enters the resulting PID into the
5557  * postmaster's private backends list.
5558  *
5559  * NB -- this code very roughly matches BackendStartup.
5560  */
5561 static void
5562 StartAutovacuumWorker(void)
5563 {
5564 	Backend    *bn;
5565 
5566 	/*
5567 	 * If not in condition to run a process, don't try, but handle it like a
5568 	 * fork failure.  This does not normally happen, since the signal is only
5569 	 * supposed to be sent by autovacuum launcher when it's OK to do it, but
5570 	 * we have to check to avoid race-condition problems during DB state
5571 	 * changes.
5572 	 */
5573 	if (canAcceptConnections(BACKEND_TYPE_AUTOVAC) == CAC_OK)
5574 	{
5575 		/*
5576 		 * Compute the cancel key that will be assigned to this session. We
5577 		 * probably don't need cancel keys for autovac workers, but we'd
5578 		 * better have something random in the field to prevent unfriendly
5579 		 * people from sending cancels to them.
5580 		 */
5581 		if (!RandomCancelKey(&MyCancelKey))
5582 		{
5583 			ereport(LOG,
5584 					(errcode(ERRCODE_INTERNAL_ERROR),
5585 					 errmsg("could not generate random cancel key")));
5586 			return;
5587 		}
5588 
5589 		bn = (Backend *) malloc(sizeof(Backend));
5590 		if (bn)
5591 		{
5592 			bn->cancel_key = MyCancelKey;
5593 
5594 			/* Autovac workers are not dead_end and need a child slot */
5595 			bn->dead_end = false;
5596 			bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
5597 			bn->bgworker_notify = false;
5598 
5599 			bn->pid = StartAutoVacWorker();
5600 			if (bn->pid > 0)
5601 			{
5602 				bn->bkend_type = BACKEND_TYPE_AUTOVAC;
5603 				dlist_push_head(&BackendList, &bn->elem);
5604 #ifdef EXEC_BACKEND
5605 				ShmemBackendArrayAdd(bn);
5606 #endif
5607 				/* all OK */
5608 				return;
5609 			}
5610 
5611 			/*
5612 			 * fork failed, fall through to report -- actual error message was
5613 			 * logged by StartAutoVacWorker
5614 			 */
5615 			(void) ReleasePostmasterChildSlot(bn->child_slot);
5616 			free(bn);
5617 		}
5618 		else
5619 			ereport(LOG,
5620 					(errcode(ERRCODE_OUT_OF_MEMORY),
5621 					 errmsg("out of memory")));
5622 	}
5623 
5624 	/*
5625 	 * Report the failure to the launcher, if it's running.  (If it's not, we
5626 	 * might not even be connected to shared memory, so don't try to call
5627 	 * AutoVacWorkerFailed.)  Note that we also need to signal it so that it
5628 	 * responds to the condition, but we don't do that here, instead waiting
5629 	 * for ServerLoop to do it.  This way we avoid a ping-pong signalling in
5630 	 * quick succession between the autovac launcher and postmaster in case
5631 	 * things get ugly.
5632 	 */
5633 	if (AutoVacPID != 0)
5634 	{
5635 		AutoVacWorkerFailed();
5636 		avlauncher_needs_signal = true;
5637 	}
5638 }
5639 
5640 /*
5641  * MaybeStartWalReceiver
5642  *		Start the WAL receiver process, if not running and our state allows.
5643  *
5644  * Note: if WalReceiverPID is already nonzero, it might seem that we should
5645  * clear WalReceiverRequested.  However, there's a race condition if the
5646  * walreceiver terminates and the startup process immediately requests a new
5647  * one: it's quite possible to get the signal for the request before reaping
5648  * the dead walreceiver process.  Better to risk launching an extra
5649  * walreceiver than to miss launching one we need.  (The walreceiver code
5650  * has logic to recognize that it should go away if not needed.)
5651  */
5652 static void
5653 MaybeStartWalReceiver(void)
5654 {
5655 	if (WalReceiverPID == 0 &&
5656 		(pmState == PM_STARTUP || pmState == PM_RECOVERY ||
5657 		 pmState == PM_HOT_STANDBY) &&
5658 		Shutdown <= SmartShutdown)
5659 	{
5660 		WalReceiverPID = StartWalReceiver();
5661 		if (WalReceiverPID != 0)
5662 			WalReceiverRequested = false;
5663 		/* else leave the flag set, so we'll try again later */
5664 	}
5665 }
5666 
5667 
5668 /*
5669  * Create the opts file
5670  */
5671 static bool
5672 CreateOptsFile(int argc, char *argv[], char *fullprogname)
5673 {
5674 	FILE	   *fp;
5675 	int			i;
5676 
5677 #define OPTS_FILE	"postmaster.opts"
5678 
5679 	if ((fp = fopen(OPTS_FILE, "w")) == NULL)
5680 	{
5681 		elog(LOG, "could not create file \"%s\": %m", OPTS_FILE);
5682 		return false;
5683 	}
5684 
5685 	fprintf(fp, "%s", fullprogname);
5686 	for (i = 1; i < argc; i++)
5687 		fprintf(fp, " \"%s\"", argv[i]);
5688 	fputs("\n", fp);
5689 
5690 	if (fclose(fp))
5691 	{
5692 		elog(LOG, "could not write file \"%s\": %m", OPTS_FILE);
5693 		return false;
5694 	}
5695 
5696 	return true;
5697 }
5698 
5699 
5700 /*
5701  * MaxLivePostmasterChildren
5702  *
5703  * This reports the number of entries needed in per-child-process arrays
5704  * (the PMChildFlags array, and if EXEC_BACKEND the ShmemBackendArray).
5705  * These arrays include regular backends, autovac workers, walsenders
5706  * and background workers, but not special children nor dead_end children.
5707  * This allows the arrays to have a fixed maximum size, to wit the same
5708  * too-many-children limit enforced by canAcceptConnections().  The exact value
5709  * isn't too critical as long as it's more than MaxBackends.
5710  */
5711 int
5712 MaxLivePostmasterChildren(void)
5713 {
5714 	return 2 * (MaxConnections + autovacuum_max_workers + 1 +
5715 				max_wal_senders + max_worker_processes);
5716 }
5717 
5718 /*
5719  * Connect background worker to a database.
5720  */
5721 void
5722 BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
5723 {
5724 	BackgroundWorker *worker = MyBgworkerEntry;
5725 
5726 	/* XXX is this the right errcode? */
5727 	if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
5728 		ereport(FATAL,
5729 				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5730 				 errmsg("database connection requirement not indicated during registration")));
5731 
5732 	InitPostgres(dbname, InvalidOid, username, InvalidOid, NULL, (flags & BGWORKER_BYPASS_ALLOWCONN) != 0);
5733 
5734 	/* it had better not gotten out of "init" mode yet */
5735 	if (!IsInitProcessingMode())
5736 		ereport(ERROR,
5737 				(errmsg("invalid processing mode in background worker")));
5738 	SetProcessingMode(NormalProcessing);
5739 }
5740 
5741 /*
5742  * Connect background worker to a database using OIDs.
5743  */
5744 void
5745 BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
5746 {
5747 	BackgroundWorker *worker = MyBgworkerEntry;
5748 
5749 	/* XXX is this the right errcode? */
5750 	if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
5751 		ereport(FATAL,
5752 				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
5753 				 errmsg("database connection requirement not indicated during registration")));
5754 
5755 	InitPostgres(NULL, dboid, NULL, useroid, NULL, (flags & BGWORKER_BYPASS_ALLOWCONN) != 0);
5756 
5757 	/* it had better not gotten out of "init" mode yet */
5758 	if (!IsInitProcessingMode())
5759 		ereport(ERROR,
5760 				(errmsg("invalid processing mode in background worker")));
5761 	SetProcessingMode(NormalProcessing);
5762 }
5763 
5764 /*
5765  * Block/unblock signals in a background worker
5766  */
5767 void
5768 BackgroundWorkerBlockSignals(void)
5769 {
5770 	PG_SETMASK(&BlockSig);
5771 }
5772 
5773 void
5774 BackgroundWorkerUnblockSignals(void)
5775 {
5776 	PG_SETMASK(&UnBlockSig);
5777 }
5778 
5779 #ifdef EXEC_BACKEND
5780 static pid_t
5781 bgworker_forkexec(int shmem_slot)
5782 {
5783 	char	   *av[10];
5784 	int			ac = 0;
5785 	char		forkav[MAXPGPATH];
5786 
5787 	snprintf(forkav, MAXPGPATH, "--forkbgworker=%d", shmem_slot);
5788 
5789 	av[ac++] = "postgres";
5790 	av[ac++] = forkav;
5791 	av[ac++] = NULL;			/* filled in by postmaster_forkexec */
5792 	av[ac] = NULL;
5793 
5794 	Assert(ac < lengthof(av));
5795 
5796 	return postmaster_forkexec(ac, av);
5797 }
5798 #endif
5799 
5800 /*
5801  * Start a new bgworker.
5802  * Starting time conditions must have been checked already.
5803  *
5804  * Returns true on success, false on failure.
5805  * In either case, update the RegisteredBgWorker's state appropriately.
5806  *
5807  * This code is heavily based on autovacuum.c, q.v.
5808  */
5809 static bool
5810 do_start_bgworker(RegisteredBgWorker *rw)
5811 {
5812 	pid_t		worker_pid;
5813 
5814 	Assert(rw->rw_pid == 0);
5815 
5816 	/*
5817 	 * Allocate and assign the Backend element.  Note we must do this before
5818 	 * forking, so that we can handle failures (out of memory or child-process
5819 	 * slots) cleanly.
5820 	 *
5821 	 * Treat failure as though the worker had crashed.  That way, the
5822 	 * postmaster will wait a bit before attempting to start it again; if we
5823 	 * tried again right away, most likely we'd find ourselves hitting the
5824 	 * same resource-exhaustion condition.
5825 	 */
5826 	if (!assign_backendlist_entry(rw))
5827 	{
5828 		rw->rw_crashed_at = GetCurrentTimestamp();
5829 		return false;
5830 	}
5831 
5832 	ereport(DEBUG1,
5833 			(errmsg("starting background worker process \"%s\"",
5834 					rw->rw_worker.bgw_name)));
5835 
5836 #ifdef EXEC_BACKEND
5837 	switch ((worker_pid = bgworker_forkexec(rw->rw_shmem_slot)))
5838 #else
5839 	switch ((worker_pid = fork_process()))
5840 #endif
5841 	{
5842 		case -1:
5843 			/* in postmaster, fork failed ... */
5844 			ereport(LOG,
5845 					(errmsg("could not fork worker process: %m")));
5846 			/* undo what assign_backendlist_entry did */
5847 			ReleasePostmasterChildSlot(rw->rw_child_slot);
5848 			rw->rw_child_slot = 0;
5849 			free(rw->rw_backend);
5850 			rw->rw_backend = NULL;
5851 			/* mark entry as crashed, so we'll try again later */
5852 			rw->rw_crashed_at = GetCurrentTimestamp();
5853 			break;
5854 
5855 #ifndef EXEC_BACKEND
5856 		case 0:
5857 			/* in postmaster child ... */
5858 			InitPostmasterChild();
5859 
5860 			/* Close the postmaster's sockets */
5861 			ClosePostmasterPorts(false);
5862 
5863 			/*
5864 			 * Before blowing away PostmasterContext, save this bgworker's
5865 			 * data where it can find it.
5866 			 */
5867 			MyBgworkerEntry = (BackgroundWorker *)
5868 				MemoryContextAlloc(TopMemoryContext, sizeof(BackgroundWorker));
5869 			memcpy(MyBgworkerEntry, &rw->rw_worker, sizeof(BackgroundWorker));
5870 
5871 			/* Release postmaster's working memory context */
5872 			MemoryContextSwitchTo(TopMemoryContext);
5873 			MemoryContextDelete(PostmasterContext);
5874 			PostmasterContext = NULL;
5875 
5876 			StartBackgroundWorker();
5877 
5878 			exit(1);			/* should not get here */
5879 			break;
5880 #endif
5881 		default:
5882 			/* in postmaster, fork successful ... */
5883 			rw->rw_pid = worker_pid;
5884 			rw->rw_backend->pid = rw->rw_pid;
5885 			ReportBackgroundWorkerPID(rw);
5886 			/* add new worker to lists of backends */
5887 			dlist_push_head(&BackendList, &rw->rw_backend->elem);
5888 #ifdef EXEC_BACKEND
5889 			ShmemBackendArrayAdd(rw->rw_backend);
5890 #endif
5891 			return true;
5892 	}
5893 
5894 	return false;
5895 }
5896 
5897 /*
5898  * Does the current postmaster state require starting a worker with the
5899  * specified start_time?
5900  */
5901 static bool
5902 bgworker_should_start_now(BgWorkerStartTime start_time)
5903 {
5904 	switch (pmState)
5905 	{
5906 		case PM_NO_CHILDREN:
5907 		case PM_WAIT_DEAD_END:
5908 		case PM_SHUTDOWN_2:
5909 		case PM_SHUTDOWN:
5910 		case PM_WAIT_BACKENDS:
5911 		case PM_STOP_BACKENDS:
5912 			break;
5913 
5914 		case PM_RUN:
5915 			if (start_time == BgWorkerStart_RecoveryFinished)
5916 				return true;
5917 			/* fall through */
5918 
5919 		case PM_HOT_STANDBY:
5920 			if (start_time == BgWorkerStart_ConsistentState)
5921 				return true;
5922 			/* fall through */
5923 
5924 		case PM_RECOVERY:
5925 		case PM_STARTUP:
5926 		case PM_INIT:
5927 			if (start_time == BgWorkerStart_PostmasterStart)
5928 				return true;
5929 			/* fall through */
5930 
5931 	}
5932 
5933 	return false;
5934 }
5935 
5936 /*
5937  * Allocate the Backend struct for a connected background worker, but don't
5938  * add it to the list of backends just yet.
5939  *
5940  * On failure, return false without changing any worker state.
5941  *
5942  * Some info from the Backend is copied into the passed rw.
5943  */
5944 static bool
5945 assign_backendlist_entry(RegisteredBgWorker *rw)
5946 {
5947 	Backend    *bn;
5948 
5949 	/*
5950 	 * Check that database state allows another connection.  Currently the
5951 	 * only possible failure is CAC_TOOMANY, so we just log an error message
5952 	 * based on that rather than checking the error code precisely.
5953 	 */
5954 	if (canAcceptConnections(BACKEND_TYPE_BGWORKER) != CAC_OK)
5955 	{
5956 		ereport(LOG,
5957 				(errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
5958 				 errmsg("no slot available for new worker process")));
5959 		return false;
5960 	}
5961 
5962 	/*
5963 	 * Compute the cancel key that will be assigned to this session. We
5964 	 * probably don't need cancel keys for background workers, but we'd better
5965 	 * have something random in the field to prevent unfriendly people from
5966 	 * sending cancels to them.
5967 	 */
5968 	if (!RandomCancelKey(&MyCancelKey))
5969 	{
5970 		ereport(LOG,
5971 				(errcode(ERRCODE_INTERNAL_ERROR),
5972 				 errmsg("could not generate random cancel key")));
5973 		return false;
5974 	}
5975 
5976 	bn = malloc(sizeof(Backend));
5977 	if (bn == NULL)
5978 	{
5979 		ereport(LOG,
5980 				(errcode(ERRCODE_OUT_OF_MEMORY),
5981 				 errmsg("out of memory")));
5982 		return false;
5983 	}
5984 
5985 	bn->cancel_key = MyCancelKey;
5986 	bn->child_slot = MyPMChildSlot = AssignPostmasterChildSlot();
5987 	bn->bkend_type = BACKEND_TYPE_BGWORKER;
5988 	bn->dead_end = false;
5989 	bn->bgworker_notify = false;
5990 
5991 	rw->rw_backend = bn;
5992 	rw->rw_child_slot = bn->child_slot;
5993 
5994 	return true;
5995 }
5996 
5997 /*
5998  * If the time is right, start background worker(s).
5999  *
6000  * As a side effect, the bgworker control variables are set or reset
6001  * depending on whether more workers may need to be started.
6002  *
6003  * We limit the number of workers started per call, to avoid consuming the
6004  * postmaster's attention for too long when many such requests are pending.
6005  * As long as StartWorkerNeeded is true, ServerLoop will not block and will
6006  * call this function again after dealing with any other issues.
6007  */
6008 static void
6009 maybe_start_bgworkers(void)
6010 {
6011 #define MAX_BGWORKERS_TO_LAUNCH 100
6012 	int			num_launched = 0;
6013 	TimestampTz now = 0;
6014 	slist_mutable_iter iter;
6015 
6016 	/*
6017 	 * During crash recovery, we have no need to be called until the state
6018 	 * transition out of recovery.
6019 	 */
6020 	if (FatalError)
6021 	{
6022 		StartWorkerNeeded = false;
6023 		HaveCrashedWorker = false;
6024 		return;
6025 	}
6026 
6027 	/* Don't need to be called again unless we find a reason for it below */
6028 	StartWorkerNeeded = false;
6029 	HaveCrashedWorker = false;
6030 
6031 	slist_foreach_modify(iter, &BackgroundWorkerList)
6032 	{
6033 		RegisteredBgWorker *rw;
6034 
6035 		rw = slist_container(RegisteredBgWorker, rw_lnode, iter.cur);
6036 
6037 		/* ignore if already running */
6038 		if (rw->rw_pid != 0)
6039 			continue;
6040 
6041 		/* if marked for death, clean up and remove from list */
6042 		if (rw->rw_terminate)
6043 		{
6044 			ForgetBackgroundWorker(&iter);
6045 			continue;
6046 		}
6047 
6048 		/*
6049 		 * If this worker has crashed previously, maybe it needs to be
6050 		 * restarted (unless on registration it specified it doesn't want to
6051 		 * be restarted at all).  Check how long ago did a crash last happen.
6052 		 * If the last crash is too recent, don't start it right away; let it
6053 		 * be restarted once enough time has passed.
6054 		 */
6055 		if (rw->rw_crashed_at != 0)
6056 		{
6057 			if (rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART)
6058 			{
6059 				int			notify_pid;
6060 
6061 				notify_pid = rw->rw_worker.bgw_notify_pid;
6062 
6063 				ForgetBackgroundWorker(&iter);
6064 
6065 				/* Report worker is gone now. */
6066 				if (notify_pid != 0)
6067 					kill(notify_pid, SIGUSR1);
6068 
6069 				continue;
6070 			}
6071 
6072 			/* read system time only when needed */
6073 			if (now == 0)
6074 				now = GetCurrentTimestamp();
6075 
6076 			if (!TimestampDifferenceExceeds(rw->rw_crashed_at, now,
6077 											rw->rw_worker.bgw_restart_time * 1000))
6078 			{
6079 				/* Set flag to remember that we have workers to start later */
6080 				HaveCrashedWorker = true;
6081 				continue;
6082 			}
6083 		}
6084 
6085 		if (bgworker_should_start_now(rw->rw_worker.bgw_start_time))
6086 		{
6087 			/* reset crash time before trying to start worker */
6088 			rw->rw_crashed_at = 0;
6089 
6090 			/*
6091 			 * Try to start the worker.
6092 			 *
6093 			 * On failure, give up processing workers for now, but set
6094 			 * StartWorkerNeeded so we'll come back here on the next iteration
6095 			 * of ServerLoop to try again.  (We don't want to wait, because
6096 			 * there might be additional ready-to-run workers.)  We could set
6097 			 * HaveCrashedWorker as well, since this worker is now marked
6098 			 * crashed, but there's no need because the next run of this
6099 			 * function will do that.
6100 			 */
6101 			if (!do_start_bgworker(rw))
6102 			{
6103 				StartWorkerNeeded = true;
6104 				return;
6105 			}
6106 
6107 			/*
6108 			 * If we've launched as many workers as allowed, quit, but have
6109 			 * ServerLoop call us again to look for additional ready-to-run
6110 			 * workers.  There might not be any, but we'll find out the next
6111 			 * time we run.
6112 			 */
6113 			if (++num_launched >= MAX_BGWORKERS_TO_LAUNCH)
6114 			{
6115 				StartWorkerNeeded = true;
6116 				return;
6117 			}
6118 		}
6119 	}
6120 }
6121 
6122 /*
6123  * When a backend asks to be notified about worker state changes, we
6124  * set a flag in its backend entry.  The background worker machinery needs
6125  * to know when such backends exit.
6126  */
6127 bool
6128 PostmasterMarkPIDForWorkerNotify(int pid)
6129 {
6130 	dlist_iter	iter;
6131 	Backend    *bp;
6132 
6133 	dlist_foreach(iter, &BackendList)
6134 	{
6135 		bp = dlist_container(Backend, elem, iter.cur);
6136 		if (bp->pid == pid)
6137 		{
6138 			bp->bgworker_notify = true;
6139 			return true;
6140 		}
6141 	}
6142 	return false;
6143 }
6144 
6145 #ifdef EXEC_BACKEND
6146 
6147 /*
6148  * The following need to be available to the save/restore_backend_variables
6149  * functions.  They are marked NON_EXEC_STATIC in their home modules.
6150  */
6151 extern slock_t *ShmemLock;
6152 extern slock_t *ProcStructLock;
6153 extern PGPROC *AuxiliaryProcs;
6154 extern PMSignalData *PMSignalState;
6155 extern pgsocket pgStatSock;
6156 extern pg_time_t first_syslogger_file_time;
6157 
6158 #ifndef WIN32
6159 #define write_inheritable_socket(dest, src, childpid) ((*(dest) = (src)), true)
6160 #define read_inheritable_socket(dest, src) (*(dest) = *(src))
6161 #else
6162 static bool write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE child);
6163 static bool write_inheritable_socket(InheritableSocket *dest, SOCKET src,
6164 									 pid_t childPid);
6165 static void read_inheritable_socket(SOCKET *dest, InheritableSocket *src);
6166 #endif
6167 
6168 
6169 /* Save critical backend variables into the BackendParameters struct */
6170 #ifndef WIN32
6171 static bool
6172 save_backend_variables(BackendParameters *param, Port *port)
6173 #else
6174 static bool
6175 save_backend_variables(BackendParameters *param, Port *port,
6176 					   HANDLE childProcess, pid_t childPid)
6177 #endif
6178 {
6179 	memcpy(&param->port, port, sizeof(Port));
6180 	if (!write_inheritable_socket(&param->portsocket, port->sock, childPid))
6181 		return false;
6182 
6183 	strlcpy(param->DataDir, DataDir, MAXPGPATH);
6184 
6185 	memcpy(&param->ListenSocket, &ListenSocket, sizeof(ListenSocket));
6186 
6187 	param->MyCancelKey = MyCancelKey;
6188 	param->MyPMChildSlot = MyPMChildSlot;
6189 
6190 #ifdef WIN32
6191 	param->ShmemProtectiveRegion = ShmemProtectiveRegion;
6192 #endif
6193 	param->UsedShmemSegID = UsedShmemSegID;
6194 	param->UsedShmemSegAddr = UsedShmemSegAddr;
6195 
6196 	param->ShmemLock = ShmemLock;
6197 	param->ShmemVariableCache = ShmemVariableCache;
6198 	param->ShmemBackendArray = ShmemBackendArray;
6199 
6200 #ifndef HAVE_SPINLOCKS
6201 	param->SpinlockSemaArray = SpinlockSemaArray;
6202 #endif
6203 	param->NamedLWLockTrancheRequests = NamedLWLockTrancheRequests;
6204 	param->NamedLWLockTrancheArray = NamedLWLockTrancheArray;
6205 	param->MainLWLockArray = MainLWLockArray;
6206 	param->ProcStructLock = ProcStructLock;
6207 	param->ProcGlobal = ProcGlobal;
6208 	param->AuxiliaryProcs = AuxiliaryProcs;
6209 	param->PreparedXactProcs = PreparedXactProcs;
6210 	param->PMSignalState = PMSignalState;
6211 	if (!write_inheritable_socket(&param->pgStatSock, pgStatSock, childPid))
6212 		return false;
6213 
6214 	param->PostmasterPid = PostmasterPid;
6215 	param->PgStartTime = PgStartTime;
6216 	param->PgReloadTime = PgReloadTime;
6217 	param->first_syslogger_file_time = first_syslogger_file_time;
6218 
6219 	param->redirection_done = redirection_done;
6220 	param->IsBinaryUpgrade = IsBinaryUpgrade;
6221 	param->max_safe_fds = max_safe_fds;
6222 
6223 	param->MaxBackends = MaxBackends;
6224 
6225 #ifdef WIN32
6226 	param->PostmasterHandle = PostmasterHandle;
6227 	if (!write_duplicated_handle(&param->initial_signal_pipe,
6228 								 pgwin32_create_signal_listener(childPid),
6229 								 childProcess))
6230 		return false;
6231 #else
6232 	memcpy(&param->postmaster_alive_fds, &postmaster_alive_fds,
6233 		   sizeof(postmaster_alive_fds));
6234 #endif
6235 
6236 	memcpy(&param->syslogPipe, &syslogPipe, sizeof(syslogPipe));
6237 
6238 	strlcpy(param->my_exec_path, my_exec_path, MAXPGPATH);
6239 
6240 	strlcpy(param->pkglib_path, pkglib_path, MAXPGPATH);
6241 
6242 	strlcpy(param->ExtraOptions, ExtraOptions, MAXPGPATH);
6243 
6244 	return true;
6245 }
6246 
6247 
6248 #ifdef WIN32
6249 /*
6250  * Duplicate a handle for usage in a child process, and write the child
6251  * process instance of the handle to the parameter file.
6252  */
6253 static bool
6254 write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE childProcess)
6255 {
6256 	HANDLE		hChild = INVALID_HANDLE_VALUE;
6257 
6258 	if (!DuplicateHandle(GetCurrentProcess(),
6259 						 src,
6260 						 childProcess,
6261 						 &hChild,
6262 						 0,
6263 						 TRUE,
6264 						 DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS))
6265 	{
6266 		ereport(LOG,
6267 				(errmsg_internal("could not duplicate handle to be written to backend parameter file: error code %lu",
6268 								 GetLastError())));
6269 		return false;
6270 	}
6271 
6272 	*dest = hChild;
6273 	return true;
6274 }
6275 
6276 /*
6277  * Duplicate a socket for usage in a child process, and write the resulting
6278  * structure to the parameter file.
6279  * This is required because a number of LSPs (Layered Service Providers) very
6280  * common on Windows (antivirus, firewalls, download managers etc) break
6281  * straight socket inheritance.
6282  */
6283 static bool
6284 write_inheritable_socket(InheritableSocket *dest, SOCKET src, pid_t childpid)
6285 {
6286 	dest->origsocket = src;
6287 	if (src != 0 && src != PGINVALID_SOCKET)
6288 	{
6289 		/* Actual socket */
6290 		if (WSADuplicateSocket(src, childpid, &dest->wsainfo) != 0)
6291 		{
6292 			ereport(LOG,
6293 					(errmsg("could not duplicate socket %d for use in backend: error code %d",
6294 							(int) src, WSAGetLastError())));
6295 			return false;
6296 		}
6297 	}
6298 	return true;
6299 }
6300 
6301 /*
6302  * Read a duplicate socket structure back, and get the socket descriptor.
6303  */
6304 static void
6305 read_inheritable_socket(SOCKET *dest, InheritableSocket *src)
6306 {
6307 	SOCKET		s;
6308 
6309 	if (src->origsocket == PGINVALID_SOCKET || src->origsocket == 0)
6310 	{
6311 		/* Not a real socket! */
6312 		*dest = src->origsocket;
6313 	}
6314 	else
6315 	{
6316 		/* Actual socket, so create from structure */
6317 		s = WSASocket(FROM_PROTOCOL_INFO,
6318 					  FROM_PROTOCOL_INFO,
6319 					  FROM_PROTOCOL_INFO,
6320 					  &src->wsainfo,
6321 					  0,
6322 					  0);
6323 		if (s == INVALID_SOCKET)
6324 		{
6325 			write_stderr("could not create inherited socket: error code %d\n",
6326 						 WSAGetLastError());
6327 			exit(1);
6328 		}
6329 		*dest = s;
6330 
6331 		/*
6332 		 * To make sure we don't get two references to the same socket, close
6333 		 * the original one. (This would happen when inheritance actually
6334 		 * works..
6335 		 */
6336 		closesocket(src->origsocket);
6337 	}
6338 }
6339 #endif
6340 
6341 static void
6342 read_backend_variables(char *id, Port *port)
6343 {
6344 	BackendParameters param;
6345 
6346 #ifndef WIN32
6347 	/* Non-win32 implementation reads from file */
6348 	FILE	   *fp;
6349 
6350 	/* Open file */
6351 	fp = AllocateFile(id, PG_BINARY_R);
6352 	if (!fp)
6353 	{
6354 		write_stderr("could not open backend variables file \"%s\": %s\n",
6355 					 id, strerror(errno));
6356 		exit(1);
6357 	}
6358 
6359 	if (fread(&param, sizeof(param), 1, fp) != 1)
6360 	{
6361 		write_stderr("could not read from backend variables file \"%s\": %s\n",
6362 					 id, strerror(errno));
6363 		exit(1);
6364 	}
6365 
6366 	/* Release file */
6367 	FreeFile(fp);
6368 	if (unlink(id) != 0)
6369 	{
6370 		write_stderr("could not remove file \"%s\": %s\n",
6371 					 id, strerror(errno));
6372 		exit(1);
6373 	}
6374 #else
6375 	/* Win32 version uses mapped file */
6376 	HANDLE		paramHandle;
6377 	BackendParameters *paramp;
6378 
6379 #ifdef _WIN64
6380 	paramHandle = (HANDLE) _atoi64(id);
6381 #else
6382 	paramHandle = (HANDLE) atol(id);
6383 #endif
6384 	paramp = MapViewOfFile(paramHandle, FILE_MAP_READ, 0, 0, 0);
6385 	if (!paramp)
6386 	{
6387 		write_stderr("could not map view of backend variables: error code %lu\n",
6388 					 GetLastError());
6389 		exit(1);
6390 	}
6391 
6392 	memcpy(&param, paramp, sizeof(BackendParameters));
6393 
6394 	if (!UnmapViewOfFile(paramp))
6395 	{
6396 		write_stderr("could not unmap view of backend variables: error code %lu\n",
6397 					 GetLastError());
6398 		exit(1);
6399 	}
6400 
6401 	if (!CloseHandle(paramHandle))
6402 	{
6403 		write_stderr("could not close handle to backend parameter variables: error code %lu\n",
6404 					 GetLastError());
6405 		exit(1);
6406 	}
6407 #endif
6408 
6409 	restore_backend_variables(&param, port);
6410 }
6411 
6412 /* Restore critical backend variables from the BackendParameters struct */
6413 static void
6414 restore_backend_variables(BackendParameters *param, Port *port)
6415 {
6416 	memcpy(port, &param->port, sizeof(Port));
6417 	read_inheritable_socket(&port->sock, &param->portsocket);
6418 
6419 	SetDataDir(param->DataDir);
6420 
6421 	memcpy(&ListenSocket, &param->ListenSocket, sizeof(ListenSocket));
6422 
6423 	MyCancelKey = param->MyCancelKey;
6424 	MyPMChildSlot = param->MyPMChildSlot;
6425 
6426 #ifdef WIN32
6427 	ShmemProtectiveRegion = param->ShmemProtectiveRegion;
6428 #endif
6429 	UsedShmemSegID = param->UsedShmemSegID;
6430 	UsedShmemSegAddr = param->UsedShmemSegAddr;
6431 
6432 	ShmemLock = param->ShmemLock;
6433 	ShmemVariableCache = param->ShmemVariableCache;
6434 	ShmemBackendArray = param->ShmemBackendArray;
6435 
6436 #ifndef HAVE_SPINLOCKS
6437 	SpinlockSemaArray = param->SpinlockSemaArray;
6438 #endif
6439 	NamedLWLockTrancheRequests = param->NamedLWLockTrancheRequests;
6440 	NamedLWLockTrancheArray = param->NamedLWLockTrancheArray;
6441 	MainLWLockArray = param->MainLWLockArray;
6442 	ProcStructLock = param->ProcStructLock;
6443 	ProcGlobal = param->ProcGlobal;
6444 	AuxiliaryProcs = param->AuxiliaryProcs;
6445 	PreparedXactProcs = param->PreparedXactProcs;
6446 	PMSignalState = param->PMSignalState;
6447 	read_inheritable_socket(&pgStatSock, &param->pgStatSock);
6448 
6449 	PostmasterPid = param->PostmasterPid;
6450 	PgStartTime = param->PgStartTime;
6451 	PgReloadTime = param->PgReloadTime;
6452 	first_syslogger_file_time = param->first_syslogger_file_time;
6453 
6454 	redirection_done = param->redirection_done;
6455 	IsBinaryUpgrade = param->IsBinaryUpgrade;
6456 	max_safe_fds = param->max_safe_fds;
6457 
6458 	MaxBackends = param->MaxBackends;
6459 
6460 #ifdef WIN32
6461 	PostmasterHandle = param->PostmasterHandle;
6462 	pgwin32_initial_signal_pipe = param->initial_signal_pipe;
6463 #else
6464 	memcpy(&postmaster_alive_fds, &param->postmaster_alive_fds,
6465 		   sizeof(postmaster_alive_fds));
6466 #endif
6467 
6468 	memcpy(&syslogPipe, &param->syslogPipe, sizeof(syslogPipe));
6469 
6470 	strlcpy(my_exec_path, param->my_exec_path, MAXPGPATH);
6471 
6472 	strlcpy(pkglib_path, param->pkglib_path, MAXPGPATH);
6473 
6474 	strlcpy(ExtraOptions, param->ExtraOptions, MAXPGPATH);
6475 }
6476 
6477 
6478 Size
6479 ShmemBackendArraySize(void)
6480 {
6481 	return mul_size(MaxLivePostmasterChildren(), sizeof(Backend));
6482 }
6483 
6484 void
6485 ShmemBackendArrayAllocation(void)
6486 {
6487 	Size		size = ShmemBackendArraySize();
6488 
6489 	ShmemBackendArray = (Backend *) ShmemAlloc(size);
6490 	/* Mark all slots as empty */
6491 	memset(ShmemBackendArray, 0, size);
6492 }
6493 
6494 static void
6495 ShmemBackendArrayAdd(Backend *bn)
6496 {
6497 	/* The array slot corresponding to my PMChildSlot should be free */
6498 	int			i = bn->child_slot - 1;
6499 
6500 	Assert(ShmemBackendArray[i].pid == 0);
6501 	ShmemBackendArray[i] = *bn;
6502 }
6503 
6504 static void
6505 ShmemBackendArrayRemove(Backend *bn)
6506 {
6507 	int			i = bn->child_slot - 1;
6508 
6509 	Assert(ShmemBackendArray[i].pid == bn->pid);
6510 	/* Mark the slot as empty */
6511 	ShmemBackendArray[i].pid = 0;
6512 }
6513 #endif							/* EXEC_BACKEND */
6514 
6515 
6516 #ifdef WIN32
6517 
6518 /*
6519  * Subset implementation of waitpid() for Windows.  We assume pid is -1
6520  * (that is, check all child processes) and options is WNOHANG (don't wait).
6521  */
6522 static pid_t
6523 waitpid(pid_t pid, int *exitstatus, int options)
6524 {
6525 	DWORD		dwd;
6526 	ULONG_PTR	key;
6527 	OVERLAPPED *ovl;
6528 
6529 	/*
6530 	 * Check if there are any dead children. If there are, return the pid of
6531 	 * the first one that died.
6532 	 */
6533 	if (GetQueuedCompletionStatus(win32ChildQueue, &dwd, &key, &ovl, 0))
6534 	{
6535 		*exitstatus = (int) key;
6536 		return dwd;
6537 	}
6538 
6539 	return -1;
6540 }
6541 
6542 /*
6543  * Note! Code below executes on a thread pool! All operations must
6544  * be thread safe! Note that elog() and friends must *not* be used.
6545  */
6546 static void WINAPI
6547 pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
6548 {
6549 	win32_deadchild_waitinfo *childinfo = (win32_deadchild_waitinfo *) lpParameter;
6550 	DWORD		exitcode;
6551 
6552 	if (TimerOrWaitFired)
6553 		return;					/* timeout. Should never happen, since we use
6554 								 * INFINITE as timeout value. */
6555 
6556 	/*
6557 	 * Remove handle from wait - required even though it's set to wait only
6558 	 * once
6559 	 */
6560 	UnregisterWaitEx(childinfo->waitHandle, NULL);
6561 
6562 	if (!GetExitCodeProcess(childinfo->procHandle, &exitcode))
6563 	{
6564 		/*
6565 		 * Should never happen. Inform user and set a fixed exitcode.
6566 		 */
6567 		write_stderr("could not read exit code for process\n");
6568 		exitcode = 255;
6569 	}
6570 
6571 	if (!PostQueuedCompletionStatus(win32ChildQueue, childinfo->procId, (ULONG_PTR) exitcode, NULL))
6572 		write_stderr("could not post child completion status\n");
6573 
6574 	/*
6575 	 * Handle is per-process, so we close it here instead of in the
6576 	 * originating thread
6577 	 */
6578 	CloseHandle(childinfo->procHandle);
6579 
6580 	/*
6581 	 * Free struct that was allocated before the call to
6582 	 * RegisterWaitForSingleObject()
6583 	 */
6584 	free(childinfo);
6585 
6586 	/* Queue SIGCHLD signal */
6587 	pg_queue_signal(SIGCHLD);
6588 }
6589 #endif							/* WIN32 */
6590 
6591 /*
6592  * Initialize one and only handle for monitoring postmaster death.
6593  *
6594  * Called once in the postmaster, so that child processes can subsequently
6595  * monitor if their parent is dead.
6596  */
6597 static void
6598 InitPostmasterDeathWatchHandle(void)
6599 {
6600 #ifndef WIN32
6601 
6602 	/*
6603 	 * Create a pipe. Postmaster holds the write end of the pipe open
6604 	 * (POSTMASTER_FD_OWN), and children hold the read end. Children can pass
6605 	 * the read file descriptor to select() to wake up in case postmaster
6606 	 * dies, or check for postmaster death with a (read() == 0). Children must
6607 	 * close the write end as soon as possible after forking, because EOF
6608 	 * won't be signaled in the read end until all processes have closed the
6609 	 * write fd. That is taken care of in ClosePostmasterPorts().
6610 	 */
6611 	Assert(MyProcPid == PostmasterPid);
6612 	if (pipe(postmaster_alive_fds) < 0)
6613 		ereport(FATAL,
6614 				(errcode_for_file_access(),
6615 				 errmsg_internal("could not create pipe to monitor postmaster death: %m")));
6616 
6617 	/*
6618 	 * Set O_NONBLOCK to allow testing for the fd's presence with a read()
6619 	 * call.
6620 	 */
6621 	if (fcntl(postmaster_alive_fds[POSTMASTER_FD_WATCH], F_SETFL, O_NONBLOCK) == -1)
6622 		ereport(FATAL,
6623 				(errcode_for_socket_access(),
6624 				 errmsg_internal("could not set postmaster death monitoring pipe to nonblocking mode: %m")));
6625 #else
6626 
6627 	/*
6628 	 * On Windows, we use a process handle for the same purpose.
6629 	 */
6630 	if (DuplicateHandle(GetCurrentProcess(),
6631 						GetCurrentProcess(),
6632 						GetCurrentProcess(),
6633 						&PostmasterHandle,
6634 						0,
6635 						TRUE,
6636 						DUPLICATE_SAME_ACCESS) == 0)
6637 		ereport(FATAL,
6638 				(errmsg_internal("could not duplicate postmaster handle: error code %lu",
6639 								 GetLastError())));
6640 #endif							/* WIN32 */
6641 }
6642