@@ -411,6 +411,7 @@ static void HandleChildCrash(int pid, int exitstatus, const char *procname);
411
411
static void LogChildExit (int lev , const char * procname ,
412
412
int pid , int exitstatus );
413
413
static void PostmasterStateMachine (void );
414
+ static void UpdatePMState (PMState newState );
414
415
415
416
static void ExitPostmaster (int status ) pg_attribute_noreturn ();
416
417
static int ServerLoop (void );
@@ -1363,7 +1364,7 @@ PostmasterMain(int argc, char *argv[])
1363
1364
StartupPMChild = StartChildProcess (B_STARTUP );
1364
1365
Assert (StartupPMChild != NULL );
1365
1366
StartupStatus = STARTUP_RUNNING ;
1366
- pmState = PM_STARTUP ;
1367
+ UpdatePMState ( PM_STARTUP ) ;
1367
1368
1368
1369
/* Some workers may be scheduled to start now */
1369
1370
maybe_start_bgworkers ();
@@ -2099,7 +2100,7 @@ process_pm_shutdown_request(void)
2099
2100
else if (pmState == PM_STARTUP || pmState == PM_RECOVERY )
2100
2101
{
2101
2102
/* There should be no clients, so proceed to stop children */
2102
- pmState = PM_STOP_BACKENDS ;
2103
+ UpdatePMState ( PM_STOP_BACKENDS ) ;
2103
2104
}
2104
2105
2105
2106
/*
@@ -2133,15 +2134,15 @@ process_pm_shutdown_request(void)
2133
2134
if (pmState == PM_STARTUP || pmState == PM_RECOVERY )
2134
2135
{
2135
2136
/* Just shut down background processes silently */
2136
- pmState = PM_STOP_BACKENDS ;
2137
+ UpdatePMState ( PM_STOP_BACKENDS ) ;
2137
2138
}
2138
2139
else if (pmState == PM_RUN ||
2139
2140
pmState == PM_HOT_STANDBY )
2140
2141
{
2141
2142
/* Report that we're about to zap live client sessions */
2142
2143
ereport (LOG ,
2143
2144
(errmsg ("aborting any active transactions" )));
2144
- pmState = PM_STOP_BACKENDS ;
2145
+ UpdatePMState ( PM_STOP_BACKENDS ) ;
2145
2146
}
2146
2147
2147
2148
/*
@@ -2176,7 +2177,7 @@ process_pm_shutdown_request(void)
2176
2177
/* (note we don't apply send_abort_for_crash here) */
2177
2178
SetQuitSignalReason (PMQUIT_FOR_STOP );
2178
2179
TerminateChildren (SIGQUIT );
2179
- pmState = PM_WAIT_BACKENDS ;
2180
+ UpdatePMState ( PM_WAIT_BACKENDS ) ;
2180
2181
2181
2182
/* set stopwatch for them to die */
2182
2183
AbortStartTime = time (NULL );
@@ -2231,7 +2232,7 @@ process_pm_child_exit(void)
2231
2232
(EXIT_STATUS_0 (exitstatus ) || EXIT_STATUS_1 (exitstatus )))
2232
2233
{
2233
2234
StartupStatus = STARTUP_NOT_RUNNING ;
2234
- pmState = PM_WAIT_BACKENDS ;
2235
+ UpdatePMState ( PM_WAIT_BACKENDS ) ;
2235
2236
/* PostmasterStateMachine logic does the rest */
2236
2237
continue ;
2237
2238
}
@@ -2243,7 +2244,7 @@ process_pm_child_exit(void)
2243
2244
StartupStatus = STARTUP_NOT_RUNNING ;
2244
2245
Shutdown = Max (Shutdown , SmartShutdown );
2245
2246
TerminateChildren (SIGTERM );
2246
- pmState = PM_WAIT_BACKENDS ;
2247
+ UpdatePMState ( PM_WAIT_BACKENDS ) ;
2247
2248
/* PostmasterStateMachine logic does the rest */
2248
2249
continue ;
2249
2250
}
@@ -2288,7 +2289,7 @@ process_pm_child_exit(void)
2288
2289
{
2289
2290
StartupStatus = STARTUP_NOT_RUNNING ;
2290
2291
if (pmState == PM_STARTUP )
2291
- pmState = PM_WAIT_BACKENDS ;
2292
+ UpdatePMState ( PM_WAIT_BACKENDS ) ;
2292
2293
}
2293
2294
else
2294
2295
StartupStatus = STARTUP_CRASHED ;
@@ -2304,7 +2305,7 @@ process_pm_child_exit(void)
2304
2305
FatalError = false;
2305
2306
AbortStartTime = 0 ;
2306
2307
ReachedNormalRunning = true;
2307
- pmState = PM_RUN ;
2308
+ UpdatePMState ( PM_RUN ) ;
2308
2309
connsAllowed = true;
2309
2310
2310
2311
/*
@@ -2377,7 +2378,7 @@ process_pm_child_exit(void)
2377
2378
*/
2378
2379
SignalChildren (SIGUSR2 , btmask (B_WAL_SENDER ));
2379
2380
2380
- pmState = PM_SHUTDOWN_2 ;
2381
+ UpdatePMState ( PM_SHUTDOWN_2 ) ;
2381
2382
}
2382
2383
else
2383
2384
{
@@ -2729,7 +2730,7 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
2729
2730
pmState == PM_RUN ||
2730
2731
pmState == PM_STOP_BACKENDS ||
2731
2732
pmState == PM_SHUTDOWN )
2732
- pmState = PM_WAIT_BACKENDS ;
2733
+ UpdatePMState ( PM_WAIT_BACKENDS ) ;
2733
2734
2734
2735
/*
2735
2736
* .. and if this doesn't happen quickly enough, now the clock is ticking
@@ -2821,7 +2822,7 @@ PostmasterStateMachine(void)
2821
2822
* Then we're ready to stop other children.
2822
2823
*/
2823
2824
if (CountChildren (btmask (B_BACKEND )) == 0 )
2824
- pmState = PM_STOP_BACKENDS ;
2825
+ UpdatePMState ( PM_STOP_BACKENDS ) ;
2825
2826
}
2826
2827
}
2827
2828
@@ -2909,7 +2910,7 @@ PostmasterStateMachine(void)
2909
2910
2910
2911
SignalChildren (SIGTERM , targetMask );
2911
2912
2912
- pmState = PM_WAIT_BACKENDS ;
2913
+ UpdatePMState ( PM_WAIT_BACKENDS ) ;
2913
2914
}
2914
2915
2915
2916
/* Are any of the target processes still running? */
@@ -2920,7 +2921,7 @@ PostmasterStateMachine(void)
2920
2921
/*
2921
2922
* Stop any dead-end children and stop creating new ones.
2922
2923
*/
2923
- pmState = PM_WAIT_DEAD_END ;
2924
+ UpdatePMState ( PM_WAIT_DEAD_END ) ;
2924
2925
ConfigurePostmasterWaitSet (false);
2925
2926
SignalChildren (SIGQUIT , btmask (B_DEAD_END_BACKEND ));
2926
2927
@@ -2945,7 +2946,7 @@ PostmasterStateMachine(void)
2945
2946
if (CheckpointerPMChild != NULL )
2946
2947
{
2947
2948
signal_child (CheckpointerPMChild , SIGUSR2 );
2948
- pmState = PM_SHUTDOWN ;
2949
+ UpdatePMState ( PM_SHUTDOWN ) ;
2949
2950
}
2950
2951
else
2951
2952
{
@@ -2960,7 +2961,7 @@ PostmasterStateMachine(void)
2960
2961
* for checkpointer fork failure.
2961
2962
*/
2962
2963
FatalError = true;
2963
- pmState = PM_WAIT_DEAD_END ;
2964
+ UpdatePMState ( PM_WAIT_DEAD_END ) ;
2964
2965
ConfigurePostmasterWaitSet (false);
2965
2966
2966
2967
/* Kill the walsenders and archiver too */
@@ -2980,7 +2981,7 @@ PostmasterStateMachine(void)
2980
2981
*/
2981
2982
if (CountChildren (btmask_all_except2 (B_LOGGER , B_DEAD_END_BACKEND )) == 0 )
2982
2983
{
2983
- pmState = PM_WAIT_DEAD_END ;
2984
+ UpdatePMState ( PM_WAIT_DEAD_END ) ;
2984
2985
ConfigurePostmasterWaitSet (false);
2985
2986
SignalChildren (SIGTERM , btmask_all_except (B_LOGGER ));
2986
2987
}
@@ -3013,7 +3014,7 @@ PostmasterStateMachine(void)
3013
3014
Assert (AutoVacLauncherPMChild == NULL );
3014
3015
Assert (SlotSyncWorkerPMChild == NULL );
3015
3016
/* syslogger is not considered here */
3016
- pmState = PM_NO_CHILDREN ;
3017
+ UpdatePMState ( PM_NO_CHILDREN ) ;
3017
3018
}
3018
3019
}
3019
3020
@@ -3097,7 +3098,7 @@ PostmasterStateMachine(void)
3097
3098
StartupPMChild = StartChildProcess (B_STARTUP );
3098
3099
Assert (StartupPMChild != NULL );
3099
3100
StartupStatus = STARTUP_RUNNING ;
3100
- pmState = PM_STARTUP ;
3101
+ UpdatePMState ( PM_STARTUP ) ;
3101
3102
/* crash recovery started, reset SIGKILL flag */
3102
3103
AbortStartTime = 0 ;
3103
3104
@@ -3106,6 +3107,42 @@ PostmasterStateMachine(void)
3106
3107
}
3107
3108
}
3108
3109
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
+
3109
3146
/*
3110
3147
* Launch background processes after state change, or relaunch after an
3111
3148
* existing process has exited.
@@ -3524,7 +3561,7 @@ process_pm_pmsignal(void)
3524
3561
#endif
3525
3562
}
3526
3563
3527
- pmState = PM_RECOVERY ;
3564
+ UpdatePMState ( PM_RECOVERY ) ;
3528
3565
}
3529
3566
3530
3567
if (CheckPostmasterSignal (PMSIGNAL_BEGIN_HOT_STANDBY ) &&
@@ -3539,7 +3576,7 @@ process_pm_pmsignal(void)
3539
3576
sd_notify (0 , "READY=1" );
3540
3577
#endif
3541
3578
3542
- pmState = PM_HOT_STANDBY ;
3579
+ UpdatePMState ( PM_HOT_STANDBY ) ;
3543
3580
connsAllowed = true;
3544
3581
3545
3582
/* Some workers may be scheduled to start now */
0 commit comments