Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf8642fb

Browse files
committed
Fix some minor postmaster-state-machine issues.
In sigusr1_handler, don't ignore PMSIGNAL_ADVANCE_STATE_MACHINE basedon pmState. The restriction is unnecessary (PostmasterStateMachineshould work in any state), not future-proof (since it makes too manyassumptions about why the signal might be sent), and broken even todaybecause a race condition can make it necessary to respond to the signalin PM_WAIT_READONLY state. The race condition seems unlikely, butif it did happen, a hot-standby postmaster could fail to shut downafter receiving a smart-shutdown request.In MaybeStartWalReceiver, don't clear the WalReceiverRequested flagif the fork attempt fails. Leaving it set allows us to tryagain in future iterations of the postmaster idle loop. (The startupprocess would eventually send a fresh request signal, but this changemay allow us to retry the fork sooner.)Remove an obsolete comment and unnecessary test inPostmasterStateMachine's handling of PM_SHUTDOWN_2 state. It's notpossible to have a live walreceiver in that state, and AFAICT has notbeen possible since commit5e85315. This isn't a live bug, but thefalse comment is quite confusing to readers.In passing, rearrange sigusr1_handler's CheckPromoteSignal tests so thatwe don't uselessly perform stat() calls that we're going to ignore theresults of.Add some comments clarifying the behavior of MaybeStartWalReceiver;I very nearly rearranged it in a way that'd reintroduce the racecondition fixed ine5d494d. Mea culpa for not commenting thatproperly at the time.Back-patch to all supported branches. The PMSIGNAL_ADVANCE_STATE_MACHINEchange is the only one of even minor significance, but we might as wellkeep this code in sync across branches.Discussion:https://postgr.es/m/9001.1556046681@sss.pgh.pa.us
1 parent7a3d055 commitf8642fb

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
staticvoid
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)

[8]ページ先頭

©2009-2025 Movatter.jp