Skip to content

Commit 866f2dd

Browse files
committed
Don't launch new child processes after we've been told to shut down.
Once we've received a shutdown signal (SIGINT or SIGTERM), we should not launch any more child processes, even if we get signals requesting such. The normal code path for spawning backends has always understood that, but the postmaster's infrastructure for hot standby and autovacuum didn't get the memo. As reported by Hari Babu in bug #7643, this could lead to failure to shut down at all in some cases, such as when SIGINT is received just before the startup process sends PMSIGNAL_RECOVERY_STARTED: we'd launch a bgwriter and checkpointer, and then those processes would have no idea that they ought to quit. Similarly, launching a new autovacuum worker would result in waiting till it finished before shutting down. Also, switch the order of the code blocks in reaper() that detect startup process crash versus shutdown termination. Once we've sent it a signal, we should not consider that exit(1) is surprising. This is just a cosmetic fix since shutdown occurs correctly anyway, but better not to log a phony complaint about startup process crash. Back-patch to 9.0. Some parts of this might be applicable before that, but given the lack of prior complaints I'm not going to worry too much about older branches.
1 parent 4387cc9 commit 866f2dd

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,6 +2375,18 @@ reaper(SIGNAL_ARGS)
23752375
{
23762376
StartupPID = 0;
23772377

2378+
/*
2379+
* Startup process exited in response to a shutdown request (or it
2380+
* completed normally regardless of the shutdown request).
2381+
*/
2382+
if (Shutdown > NoShutdown &&
2383+
(EXIT_STATUS_0(exitstatus) || EXIT_STATUS_1(exitstatus)))
2384+
{
2385+
pmState = PM_WAIT_BACKENDS;
2386+
/* PostmasterStateMachine logic does the rest */
2387+
continue;
2388+
}
2389+
23782390
/*
23792391
* Unexpected exit of startup process (including FATAL exit)
23802392
* during PM_STARTUP is treated as catastrophic. There are no
@@ -2389,18 +2401,6 @@ reaper(SIGNAL_ARGS)
23892401
ExitPostmaster(1);
23902402
}
23912403

2392-
/*
2393-
* Startup process exited in response to a shutdown request (or it
2394-
* completed normally regardless of the shutdown request).
2395-
*/
2396-
if (Shutdown > NoShutdown &&
2397-
(EXIT_STATUS_0(exitstatus) || EXIT_STATUS_1(exitstatus)))
2398-
{
2399-
pmState = PM_WAIT_BACKENDS;
2400-
/* PostmasterStateMachine logic does the rest */
2401-
continue;
2402-
}
2403-
24042404
/*
24052405
* After PM_STARTUP, any unexpected exit (including FATAL exit) of
24062406
* the startup process is catastrophic, so kill other children,
@@ -4241,7 +4241,7 @@ sigusr1_handler(SIGNAL_ARGS)
42414241
* first. We don't want to go back to recovery in that case.
42424242
*/
42434243
if (CheckPostmasterSignal(PMSIGNAL_RECOVERY_STARTED) &&
4244-
pmState == PM_STARTUP)
4244+
pmState == PM_STARTUP && Shutdown == NoShutdown)
42454245
{
42464246
/* WAL redo has started. We're out of reinitialization. */
42474247
FatalError = false;
@@ -4256,7 +4256,7 @@ sigusr1_handler(SIGNAL_ARGS)
42564256
pmState = PM_RECOVERY;
42574257
}
42584258
if (CheckPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY) &&
4259-
pmState == PM_RECOVERY)
4259+
pmState == PM_RECOVERY && Shutdown == NoShutdown)
42604260
{
42614261
/*
42624262
* Likewise, start other special children as needed.
@@ -4287,7 +4287,8 @@ sigusr1_handler(SIGNAL_ARGS)
42874287
signal_child(SysLoggerPID, SIGUSR1);
42884288
}
42894289

4290-
if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER))
4290+
if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_LAUNCHER) &&
4291+
Shutdown == NoShutdown)
42914292
{
42924293
/*
42934294
* Start one iteration of the autovacuum daemon, even if autovacuuming
@@ -4301,7 +4302,8 @@ sigusr1_handler(SIGNAL_ARGS)
43014302
start_autovac_launcher = true;
43024303
}
43034304

4304-
if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER))
4305+
if (CheckPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER) &&
4306+
Shutdown == NoShutdown)
43054307
{
43064308
/* The autovacuum launcher wants us to start a worker process. */
43074309
StartAutovacuumWorker();
@@ -4310,7 +4312,8 @@ sigusr1_handler(SIGNAL_ARGS)
43104312
if (CheckPostmasterSignal(PMSIGNAL_START_WALRECEIVER) &&
43114313
WalReceiverPID == 0 &&
43124314
(pmState == PM_STARTUP || pmState == PM_RECOVERY ||
4313-
pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY))
4315+
pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY) &&
4316+
Shutdown == NoShutdown)
43144317
{
43154318
/* Startup Process wants us to start the walreceiver process. */
43164319
WalReceiverPID = StartWalReceiver();

0 commit comments

Comments
 (0)