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