Skip to content

Commit f8642fb

Browse files
committed
Fix some minor postmaster-state-machine issues.
In sigusr1_handler, don't ignore PMSIGNAL_ADVANCE_STATE_MACHINE based on pmState. The restriction is unnecessary (PostmasterStateMachine should work in any state), not future-proof (since it makes too many assumptions about why the signal might be sent), and broken even today because a race condition can make it necessary to respond to the signal in PM_WAIT_READONLY state. The race condition seems unlikely, but if it did happen, a hot-standby postmaster could fail to shut down after receiving a smart-shutdown request. In MaybeStartWalReceiver, don't clear the WalReceiverRequested flag if the fork attempt fails. Leaving it set allows us to try again in future iterations of the postmaster idle loop. (The startup process would eventually send a fresh request signal, but this change may allow us to retry the fork sooner.) Remove an obsolete comment and unnecessary test in PostmasterStateMachine's handling of PM_SHUTDOWN_2 state. It's not possible to have a live walreceiver in that state, and AFAICT has not been possible since commit 5e85315. This isn't a live bug, but the false comment is quite confusing to readers. In passing, rearrange sigusr1_handler's CheckPromoteSignal tests so that we don't uselessly perform stat() calls that we're going to ignore the results of. Add some comments clarifying the behavior of MaybeStartWalReceiver; I very nearly rearranged it in a way that'd reintroduce the race condition fixed in e5d494d. Mea culpa for not commenting that properly at the time. Back-patch to all supported branches. The PMSIGNAL_ADVANCE_STATE_MACHINE change is the only one of even minor significance, but we might as well keep this code in sync across branches. Discussion: https://postgr.es/m/9001.1556046681@sss.pgh.pa.us
1 parent 7a3d055 commit f8642fb

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3724,12 +3724,8 @@ PostmasterStateMachine(void)
37243724
* dead_end children left. There shouldn't be any regular backends
37253725
* left by now anyway; what we're really waiting for is walsenders and
37263726
* archiver.
3727-
*
3728-
* Walreceiver should normally be dead by now, but not when a fast
3729-
* shutdown is performed during recovery.
37303727
*/
3731-
if (PgArchPID == 0 && CountChildren(BACKEND_TYPE_ALL) == 0 &&
3732-
WalReceiverPID == 0)
3728+
if (PgArchPID == 0 && CountChildren(BACKEND_TYPE_ALL) == 0)
37333729
{
37343730
pmState = PM_WAIT_DEAD_END;
37353731
}
@@ -5138,16 +5134,25 @@ sigusr1_handler(SIGNAL_ARGS)
51385134
MaybeStartWalReceiver();
51395135
}
51405136

5141-
if (CheckPostmasterSignal(PMSIGNAL_ADVANCE_STATE_MACHINE) &&
5142-
(pmState == PM_WAIT_BACKUP || pmState == PM_WAIT_BACKENDS))
5137+
/*
5138+
* Try to advance postmaster's state machine, if a child requests it.
5139+
*
5140+
* Be careful about the order of this action relative to sigusr1_handler's
5141+
* other actions. Generally, this should be after other actions, in case
5142+
* they have effects PostmasterStateMachine would need to know about.
5143+
* However, we should do it before the CheckPromoteSignal step, which
5144+
* cannot have any (immediate) effect on the state machine, but does
5145+
* depend on what state we're in now.
5146+
*/
5147+
if (CheckPostmasterSignal(PMSIGNAL_ADVANCE_STATE_MACHINE))
51435148
{
5144-
/* Advance postmaster's state machine */
51455149
PostmasterStateMachine();
51465150
}
51475151

5148-
if (CheckPromoteSignal() && StartupPID != 0 &&
5152+
if (StartupPID != 0 &&
51495153
(pmState == PM_STARTUP || pmState == PM_RECOVERY ||
5150-
pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY))
5154+
pmState == PM_HOT_STANDBY || pmState == PM_WAIT_READONLY) &&
5155+
CheckPromoteSignal())
51515156
{
51525157
/* Tell startup process to finish recovery */
51535158
signal_child(StartupPID, SIGUSR2);
@@ -5474,6 +5479,14 @@ StartAutovacuumWorker(void)
54745479
/*
54755480
* MaybeStartWalReceiver
54765481
* Start the WAL receiver process, if not running and our state allows.
5482+
*
5483+
* Note: if WalReceiverPID is already nonzero, it might seem that we should
5484+
* clear WalReceiverRequested. However, there's a race condition if the
5485+
* walreceiver terminates and the startup process immediately requests a new
5486+
* one: it's quite possible to get the signal for the request before reaping
5487+
* the dead walreceiver process. Better to risk launching an extra
5488+
* walreceiver than to miss launching one we need. (The walreceiver code
5489+
* has logic to recognize that it should go away if not needed.)
54775490
*/
54785491
static void
54795492
MaybeStartWalReceiver(void)
@@ -5484,7 +5497,9 @@ MaybeStartWalReceiver(void)
54845497
Shutdown == NoShutdown)
54855498
{
54865499
WalReceiverPID = StartWalReceiver();
5487-
WalReceiverRequested = false;
5500+
if (WalReceiverPID != 0)
5501+
WalReceiverRequested = false;
5502+
/* else leave the flag set, so we'll try again later */
54885503
}
54895504
}
54905505

0 commit comments

Comments
 (0)