Skip to content

Commit 7148cbb

Browse files
committed
postmaster: Update pmState via a wrapper function
This makes logging of state changes easier - state transitions are now logged at DEBUG1. Without that logging it was surprisingly hard to understand the current state of the system while debugging. Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com> Reviewed-by: Nazir Bilal Yavuz <byavuz81@gmail.com> Discussion: https://postgr.es/m/kgng5nrvnlv335evmsuvpnh354rw7qyazl73kdysev2cr2v5zu@m3cfzxicm5kp
1 parent cc811f9 commit 7148cbb

File tree

1 file changed

+58
-21
lines changed

1 file changed

+58
-21
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ static void HandleChildCrash(int pid, int exitstatus, const char *procname);
411411
static void LogChildExit(int lev, const char *procname,
412412
int pid, int exitstatus);
413413
static void PostmasterStateMachine(void);
414+
static void UpdatePMState(PMState newState);
414415

415416
static void ExitPostmaster(int status) pg_attribute_noreturn();
416417
static int ServerLoop(void);
@@ -1363,7 +1364,7 @@ PostmasterMain(int argc, char *argv[])
13631364
StartupPMChild = StartChildProcess(B_STARTUP);
13641365
Assert(StartupPMChild != NULL);
13651366
StartupStatus = STARTUP_RUNNING;
1366-
pmState = PM_STARTUP;
1367+
UpdatePMState(PM_STARTUP);
13671368

13681369
/* Some workers may be scheduled to start now */
13691370
maybe_start_bgworkers();
@@ -2099,7 +2100,7 @@ process_pm_shutdown_request(void)
20992100
else if (pmState == PM_STARTUP || pmState == PM_RECOVERY)
21002101
{
21012102
/* There should be no clients, so proceed to stop children */
2102-
pmState = PM_STOP_BACKENDS;
2103+
UpdatePMState(PM_STOP_BACKENDS);
21032104
}
21042105

21052106
/*
@@ -2133,15 +2134,15 @@ process_pm_shutdown_request(void)
21332134
if (pmState == PM_STARTUP || pmState == PM_RECOVERY)
21342135
{
21352136
/* Just shut down background processes silently */
2136-
pmState = PM_STOP_BACKENDS;
2137+
UpdatePMState(PM_STOP_BACKENDS);
21372138
}
21382139
else if (pmState == PM_RUN ||
21392140
pmState == PM_HOT_STANDBY)
21402141
{
21412142
/* Report that we're about to zap live client sessions */
21422143
ereport(LOG,
21432144
(errmsg("aborting any active transactions")));
2144-
pmState = PM_STOP_BACKENDS;
2145+
UpdatePMState(PM_STOP_BACKENDS);
21452146
}
21462147

21472148
/*
@@ -2176,7 +2177,7 @@ process_pm_shutdown_request(void)
21762177
/* (note we don't apply send_abort_for_crash here) */
21772178
SetQuitSignalReason(PMQUIT_FOR_STOP);
21782179
TerminateChildren(SIGQUIT);
2179-
pmState = PM_WAIT_BACKENDS;
2180+
UpdatePMState(PM_WAIT_BACKENDS);
21802181

21812182
/* set stopwatch for them to die */
21822183
AbortStartTime = time(NULL);
@@ -2231,7 +2232,7 @@ process_pm_child_exit(void)
22312232
(EXIT_STATUS_0(exitstatus) || EXIT_STATUS_1(exitstatus)))
22322233
{
22332234
StartupStatus = STARTUP_NOT_RUNNING;
2234-
pmState = PM_WAIT_BACKENDS;
2235+
UpdatePMState(PM_WAIT_BACKENDS);
22352236
/* PostmasterStateMachine logic does the rest */
22362237
continue;
22372238
}
@@ -2243,7 +2244,7 @@ process_pm_child_exit(void)
22432244
StartupStatus = STARTUP_NOT_RUNNING;
22442245
Shutdown = Max(Shutdown, SmartShutdown);
22452246
TerminateChildren(SIGTERM);
2246-
pmState = PM_WAIT_BACKENDS;
2247+
UpdatePMState(PM_WAIT_BACKENDS);
22472248
/* PostmasterStateMachine logic does the rest */
22482249
continue;
22492250
}
@@ -2288,7 +2289,7 @@ process_pm_child_exit(void)
22882289
{
22892290
StartupStatus = STARTUP_NOT_RUNNING;
22902291
if (pmState == PM_STARTUP)
2291-
pmState = PM_WAIT_BACKENDS;
2292+
UpdatePMState(PM_WAIT_BACKENDS);
22922293
}
22932294
else
22942295
StartupStatus = STARTUP_CRASHED;
@@ -2304,7 +2305,7 @@ process_pm_child_exit(void)
23042305
FatalError = false;
23052306
AbortStartTime = 0;
23062307
ReachedNormalRunning = true;
2307-
pmState = PM_RUN;
2308+
UpdatePMState(PM_RUN);
23082309
connsAllowed = true;
23092310

23102311
/*
@@ -2377,7 +2378,7 @@ process_pm_child_exit(void)
23772378
*/
23782379
SignalChildren(SIGUSR2, btmask(B_WAL_SENDER));
23792380

2380-
pmState = PM_SHUTDOWN_2;
2381+
UpdatePMState(PM_SHUTDOWN_2);
23812382
}
23822383
else
23832384
{
@@ -2729,7 +2730,7 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
27292730
pmState == PM_RUN ||
27302731
pmState == PM_STOP_BACKENDS ||
27312732
pmState == PM_SHUTDOWN)
2732-
pmState = PM_WAIT_BACKENDS;
2733+
UpdatePMState(PM_WAIT_BACKENDS);
27332734

27342735
/*
27352736
* .. and if this doesn't happen quickly enough, now the clock is ticking
@@ -2821,7 +2822,7 @@ PostmasterStateMachine(void)
28212822
* Then we're ready to stop other children.
28222823
*/
28232824
if (CountChildren(btmask(B_BACKEND)) == 0)
2824-
pmState = PM_STOP_BACKENDS;
2825+
UpdatePMState(PM_STOP_BACKENDS);
28252826
}
28262827
}
28272828

@@ -2909,7 +2910,7 @@ PostmasterStateMachine(void)
29092910

29102911
SignalChildren(SIGTERM, targetMask);
29112912

2912-
pmState = PM_WAIT_BACKENDS;
2913+
UpdatePMState(PM_WAIT_BACKENDS);
29132914
}
29142915

29152916
/* Are any of the target processes still running? */
@@ -2920,7 +2921,7 @@ PostmasterStateMachine(void)
29202921
/*
29212922
* Stop any dead-end children and stop creating new ones.
29222923
*/
2923-
pmState = PM_WAIT_DEAD_END;
2924+
UpdatePMState(PM_WAIT_DEAD_END);
29242925
ConfigurePostmasterWaitSet(false);
29252926
SignalChildren(SIGQUIT, btmask(B_DEAD_END_BACKEND));
29262927

@@ -2945,7 +2946,7 @@ PostmasterStateMachine(void)
29452946
if (CheckpointerPMChild != NULL)
29462947
{
29472948
signal_child(CheckpointerPMChild, SIGUSR2);
2948-
pmState = PM_SHUTDOWN;
2949+
UpdatePMState(PM_SHUTDOWN);
29492950
}
29502951
else
29512952
{
@@ -2960,7 +2961,7 @@ PostmasterStateMachine(void)
29602961
* for checkpointer fork failure.
29612962
*/
29622963
FatalError = true;
2963-
pmState = PM_WAIT_DEAD_END;
2964+
UpdatePMState(PM_WAIT_DEAD_END);
29642965
ConfigurePostmasterWaitSet(false);
29652966

29662967
/* Kill the walsenders and archiver too */
@@ -2980,7 +2981,7 @@ PostmasterStateMachine(void)
29802981
*/
29812982
if (CountChildren(btmask_all_except2(B_LOGGER, B_DEAD_END_BACKEND)) == 0)
29822983
{
2983-
pmState = PM_WAIT_DEAD_END;
2984+
UpdatePMState(PM_WAIT_DEAD_END);
29842985
ConfigurePostmasterWaitSet(false);
29852986
SignalChildren(SIGTERM, btmask_all_except(B_LOGGER));
29862987
}
@@ -3013,7 +3014,7 @@ PostmasterStateMachine(void)
30133014
Assert(AutoVacLauncherPMChild == NULL);
30143015
Assert(SlotSyncWorkerPMChild == NULL);
30153016
/* syslogger is not considered here */
3016-
pmState = PM_NO_CHILDREN;
3017+
UpdatePMState(PM_NO_CHILDREN);
30173018
}
30183019
}
30193020

@@ -3097,7 +3098,7 @@ PostmasterStateMachine(void)
30973098
StartupPMChild = StartChildProcess(B_STARTUP);
30983099
Assert(StartupPMChild != NULL);
30993100
StartupStatus = STARTUP_RUNNING;
3100-
pmState = PM_STARTUP;
3101+
UpdatePMState(PM_STARTUP);
31013102
/* crash recovery started, reset SIGKILL flag */
31023103
AbortStartTime = 0;
31033104

@@ -3106,6 +3107,42 @@ PostmasterStateMachine(void)
31063107
}
31073108
}
31083109

3110+
static const char *
3111+
pmstate_name(PMState state)
3112+
{
3113+
#define PM_TOSTR_CASE(sym) case sym: return #sym
3114+
switch (state)
3115+
{
3116+
PM_TOSTR_CASE(PM_INIT);
3117+
PM_TOSTR_CASE(PM_STARTUP);
3118+
PM_TOSTR_CASE(PM_RECOVERY);
3119+
PM_TOSTR_CASE(PM_HOT_STANDBY);
3120+
PM_TOSTR_CASE(PM_RUN);
3121+
PM_TOSTR_CASE(PM_STOP_BACKENDS);
3122+
PM_TOSTR_CASE(PM_WAIT_BACKENDS);
3123+
PM_TOSTR_CASE(PM_SHUTDOWN);
3124+
PM_TOSTR_CASE(PM_SHUTDOWN_2);
3125+
PM_TOSTR_CASE(PM_WAIT_DEAD_END);
3126+
PM_TOSTR_CASE(PM_NO_CHILDREN);
3127+
}
3128+
#undef PM_TOSTR_CASE
3129+
3130+
pg_unreachable();
3131+
return ""; /* silence compiler */
3132+
}
3133+
3134+
/*
3135+
* Simple wrapper for updating pmState. The main reason to have this wrapper
3136+
* is that it makes it easy to log all state transitions.
3137+
*/
3138+
static void
3139+
UpdatePMState(PMState newState)
3140+
{
3141+
elog(DEBUG1, "updating PMState from %s to %s",
3142+
pmstate_name(pmState), pmstate_name(newState));
3143+
pmState = newState;
3144+
}
3145+
31093146
/*
31103147
* Launch background processes after state change, or relaunch after an
31113148
* existing process has exited.
@@ -3524,7 +3561,7 @@ process_pm_pmsignal(void)
35243561
#endif
35253562
}
35263563

3527-
pmState = PM_RECOVERY;
3564+
UpdatePMState(PM_RECOVERY);
35283565
}
35293566

35303567
if (CheckPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY) &&
@@ -3539,7 +3576,7 @@ process_pm_pmsignal(void)
35393576
sd_notify(0, "READY=1");
35403577
#endif
35413578

3542-
pmState = PM_HOT_STANDBY;
3579+
UpdatePMState(PM_HOT_STANDBY);
35433580
connsAllowed = true;
35443581

35453582
/* Some workers may be scheduled to start now */

0 commit comments

Comments
 (0)