Coverage Report

Created: 2026-10-16 06:48

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