Debian Codegraph @

src/backend/postmaster/postmaster.c

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