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