Repository navigation
Commit e975832
authored
Fix Postgres readiness race in FSM transitions (#1147)
Fix postgres readiness race in FSM transitions
Three related fixes for a race where a keeper FSM transition could
report a new state to the monitor while Postgres was still refusing TCP
connections.
1. pg_setup_is_ready: accept PM_STATUS_STANDBY as "ready"
PostgreSQL writes "standby" (not "ready") to postmaster.pid when
hot_standby=on and the instance is accepting read-only connections
but has not yet been promoted. pg_ctl -w treats both statuses as
"server started"; pg_setup_is_ready previously only accepted "ready",
causing it to loop indefinitely for hot standby nodes.
Update the while condition, the early-exit break, and the return
value to treat POSTMASTER_STATUS_READY and POSTMASTER_STATUS_STANDBY
identically. Also fix the same hardcoded check in the pg_setup_init
short-circuit inside pg_setup_wait_until_is_ready.
2. local_postgres_wait_until_ready: always wait for PM_STATUS
Previously this function short-circuited via pg_is_running() (=
pg_ctl status, which exits 0 as soon as the PID file exists) and
skipped pg_setup_wait_until_is_ready when the process was already
present. The PID file appears before the postmaster writes its
ready/standby status, so callers could see pgIsRunning=true while
the postmaster was still in the "starting" phase.
Drop the pg_is_running pre-check entirely. pg_setup_wait_until_is_ready
handles the "already running" case correctly: its first loop terminates
immediately when get_pgpid finds a live pid, then the second loop checks
pm_status. Every caller wants "accepting connections", not "process
has a pid", so the short-circuit was never correct.
3. fsm_prepare_for_secondary: guard with ensure_postgres_service_is_running
The CATCHINGUP -> SECONDARY transition assumed Postgres was ready
because the prior WAIT_STANDBY -> CATCHINGUP transition started it.
But fix #2 above was the primary path for closing that window; this
call is an explicit belt-and-suspenders guard: if Postgres is
somehow still in startup when fsm_prepare_for_secondary runs, the
keeper retries the transition rather than reporting SECONDARY to the
monitor prematurely.1 parent 48c02be commit e975832
3 files changed
Lines changed: 58 additions & 35 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
907 | 907 | | |
908 | 908 | | |
909 | 909 | | |
910 | | - | |
911 | | - | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
912 | 916 | | |
913 | 917 | | |
914 | 918 | | |
| |||
935 | 939 | | |
936 | 940 | | |
937 | 941 | | |
938 | | - | |
| 942 | + | |
939 | 943 | | |
940 | 944 | | |
941 | 945 | | |
| |||
945 | 949 | | |
946 | 950 | | |
947 | 951 | | |
948 | | - | |
| 952 | + | |
| 953 | + | |
949 | 954 | | |
950 | 955 | | |
951 | 956 | | |
| |||
996 | 1001 | | |
997 | 1002 | | |
998 | 1003 | | |
999 | | - | |
| 1004 | + | |
| 1005 | + | |
1000 | 1006 | | |
1001 | 1007 | | |
1002 | 1008 | | |
| |||
1013 | 1019 | | |
1014 | 1020 | | |
1015 | 1021 | | |
1016 | | - | |
| 1022 | + | |
| 1023 | + | |
1017 | 1024 | | |
1018 | 1025 | | |
1019 | 1026 | | |
| |||
1091 | 1098 | | |
1092 | 1099 | | |
1093 | 1100 | | |
1094 | | - | |
| 1101 | + | |
| 1102 | + | |
1095 | 1103 | | |
1096 | 1104 | | |
1097 | 1105 | | |
1098 | 1106 | | |
1099 | 1107 | | |
1100 | | - | |
1101 | | - | |
| 1108 | + | |
| 1109 | + | |
1102 | 1110 | | |
1103 | 1111 | | |
1104 | 1112 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1059 | 1059 | | |
1060 | 1060 | | |
1061 | 1061 | | |
1062 | | - | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
1063 | 1077 | | |
1064 | 1078 | | |
1065 | 1079 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
274 | 274 | | |
275 | 275 | | |
276 | 276 | | |
277 | | - | |
278 | | - | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
279 | 287 | | |
280 | 288 | | |
281 | 289 | | |
282 | 290 | | |
283 | 291 | | |
284 | 292 | | |
285 | 293 | | |
286 | | - | |
287 | 294 | | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
295 | 298 | | |
296 | | - | |
297 | | - | |
298 | | - | |
| 299 | + | |
| 300 | + | |
299 | 301 | | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
304 | 306 | | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
313 | 314 | | |
314 | 315 | | |
315 | 316 | | |
| |||
0 commit comments