Skip to content

Commit e975832

Browse files
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

File tree

‎src/bin/common/pgsetup.c‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,12 @@ pg_setup_is_running(PostgresSetup *pgSetup)
907907

908908

909909
/*
910-
* pg_setup_is_ready returns true when the postmaster.pid file has a "ready"
911-
* status in it, which we parse in pgSetup->pm_status.
910+
* pg_setup_is_ready returns true when the postmaster is accepting connections.
911+
* That means pm_status "ready" (primary or promoted standby) or "standby" (hot
912+
* standby still in streaming recovery). PostgreSQL writes "standby" to
913+
* postmaster.pid when hot_standby=on and the instance is accepting read-only
914+
* connections but has not yet been promoted; pg_ctl -w treats both statuses as
915+
* "server started", and so do we.
912916
*/
913917
bool
914918
pg_setup_is_ready(PostgresSetup *pgSetup, bool pgIsNotRunningIsOk)
@@ -935,7 +939,7 @@ pg_setup_is_ready(PostgresSetup *pgSetup, bool pgIsNotRunningIsOk)
935939
/*
936940
* Sometimes `pg_ctl start` returns with success and Postgres is still
937941
* in crash recovery replaying WAL files, in the "starting" state
938-
* rather than the "ready" state.
942+
* rather than the "ready" or "standby" state.
939943
*
940944
* In that case, we wait until Postgres is ready for connections. The
941945
* whole pg_autoctl code is expecting to be able to connect to
@@ -945,7 +949,8 @@ pg_setup_is_ready(PostgresSetup *pgSetup, bool pgIsNotRunningIsOk)
945949
* ERROR Connection to database failed: FATAL: the database system is
946950
* starting up
947951
*/
948-
while (pgSetup->pm_status != POSTMASTER_STATUS_READY)
952+
while (pgSetup->pm_status != POSTMASTER_STATUS_READY &&
953+
pgSetup->pm_status != POSTMASTER_STATUS_STANDBY)
949954
{
950955
int maxRetries = 5;
951956

@@ -996,7 +1001,8 @@ pg_setup_is_ready(PostgresSetup *pgSetup, bool pgIsNotRunningIsOk)
9961001
}
9971002

9981003
/* avoid an extra wait if that's possible */
999-
if (pgSetup->pm_status == POSTMASTER_STATUS_READY)
1004+
if (pgSetup->pm_status == POSTMASTER_STATUS_READY ||
1005+
pgSetup->pm_status == POSTMASTER_STATUS_STANDBY)
10001006
{
10011007
break;
10021008
}
@@ -1013,7 +1019,8 @@ pg_setup_is_ready(PostgresSetup *pgSetup, bool pgIsNotRunningIsOk)
10131019
log_trace("pg_setup_is_ready: %s", pmStatusToString(pgSetup->pm_status));
10141020
}
10151021

1016-
return pgSetup->pm_status == POSTMASTER_STATUS_READY;
1022+
return pgSetup->pm_status == POSTMASTER_STATUS_READY ||
1023+
pgSetup->pm_status == POSTMASTER_STATUS_STANDBY;
10171024
}
10181025

10191026

@@ -1091,14 +1098,15 @@ pg_setup_wait_until_is_ready(PostgresSetup *pgSetup, int timeout, int logLevel)
10911098
*pgSetup = newPgSetup;
10921099

10931100
/* avoid an extra pg_setup_is_ready call if we're all good already */
1094-
pgIsReady = pgSetup->pm_status == POSTMASTER_STATUS_READY;
1101+
pgIsReady = pgSetup->pm_status == POSTMASTER_STATUS_READY ||
1102+
pgSetup->pm_status == POSTMASTER_STATUS_STANDBY;
10951103
}
10961104

10971105
/*
10981106
* Ok so we have a postmaster.pid file with a pid > 0 (not a standalone
10991107
* backend, the service has started). Postgres might still be "starting"
1100-
* rather than "ready" though, so let's continue our attempts and make sure
1101-
* that Postgres is ready.
1108+
* rather than "ready" or "standby" though, so let's continue our attempts
1109+
* and make sure that Postgres is ready.
11021110
*/
11031111
for (; !pgIsReady; attempts++)
11041112
{

‎src/bin/pg_autoctl/fsm_transition.c‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,21 @@ fsm_prepare_for_secondary(Keeper *keeper)
10591059
{
10601060
LocalPostgresServer *postgres = &(keeper->postgres);
10611061

1062-
/* first. check that we're on the same timeline as the new primary */
1062+
/*
1063+
* Verify that Postgres is accepting connections before proceeding.
1064+
* Postgres was started in the WAIT_STANDBY -> CATCHINGUP transition, but
1065+
* local_postgres_wait_until_ready may have returned as soon as the PID
1066+
* file appeared, before the postmaster finished startup recovery and began
1067+
* accepting connections. Calling ensure_postgres_service_is_running here
1068+
* re-checks readiness (via pg_setup_wait_until_is_ready) and closes that
1069+
* race window before we attempt SQL against the local instance.
1070+
*/
1071+
if (!ensure_postgres_service_is_running(postgres))
1072+
{
1073+
return false;
1074+
}
1075+
1076+
/* check that we're on the same timeline as the new primary */
10631077
if (!standby_check_timeline_with_upstream(postgres))
10641078
{
10651079
/* errors have already been logged */

‎src/bin/pg_autoctl/primary_standby.c‎

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -274,42 +274,43 @@ local_postgres_update(LocalPostgresServer *postgres, bool postgresNotRunningIsOk
274274

275275

276276
/*
277-
* local_postgres_wait_until_ready waits until Postgres is running and updates
278-
* our failure tracking counters for the Postgres service accordingly.
277+
* local_postgres_wait_until_ready waits until Postgres is accepting
278+
* connections and updates our failure tracking counters accordingly.
279+
*
280+
* We always go through pg_setup_wait_until_is_ready rather than short-
281+
* circuiting on pg_is_running(). pg_is_running() (= pg_ctl status) returns
282+
* true as soon as the postmaster PID file exists, which can happen before the
283+
* postmaster has written PM_STATUS_READY or PM_STATUS_STANDBY to that file.
284+
* Skipping the wait when the process already existed was the source of a race
285+
* where a FSM transition could report a new state to the monitor while
286+
* Postgres was still refusing TCP connections.
279287
*/
280288
static bool
281289
local_postgres_wait_until_ready(LocalPostgresServer *postgres)
282290
{
283291
PostgresSetup *pgSetup = &(postgres->postgresSetup);
284292

285293
int timeout = 10; /* wait for Postgres for 10s */
286-
bool pgIsRunning = pg_is_running(pgSetup->pg_ctl, pgSetup->pgdata);
287294

288-
log_trace("local_postgres_wait_until_ready: Postgres %s in \"%s\"",
289-
pgIsRunning ? "is running" : "is not running", pgSetup->pgdata);
290-
291-
if (!pgIsRunning)
292-
{
293-
/* main logging is done in the Postgres controller sub-process */
294-
pgIsRunning = pg_setup_wait_until_is_ready(pgSetup, timeout, LOG_DEBUG);
295+
/* main logging is done in the Postgres controller sub-process */
296+
bool pgIsRunning =
297+
pg_setup_wait_until_is_ready(pgSetup, timeout, LOG_DEBUG);
295298

296-
/* update connection string for connection to postgres */
297-
(void)
298-
local_postgres_update_pg_failures_tracking(postgres, pgIsRunning);
299+
/* update connection string for connection to postgres */
300+
(void) local_postgres_update_pg_failures_tracking(postgres, pgIsRunning);
299301

300-
if (pgIsRunning)
301-
{
302-
/* update pgSetup cache with new Postgres pid and all */
303-
local_postgres_init(postgres, pgSetup);
302+
if (pgIsRunning)
303+
{
304+
/* update pgSetup cache with new Postgres pid and all */
305+
local_postgres_init(postgres, pgSetup);
304306

305-
log_debug("local_postgres_wait_until_ready: Postgres is running "
306-
"with pid %d", pgSetup->pidFile.pid);
307-
}
308-
else
309-
{
310-
log_error("Failed to ensure that Postgres is running in \"%s\"",
311-
pgSetup->pgdata);
312-
}
307+
log_debug("local_postgres_wait_until_ready: Postgres is running "
308+
"with pid %d", pgSetup->pidFile.pid);
309+
}
310+
else
311+
{
312+
log_error("Failed to ensure that Postgres is running in \"%s\"",
313+
pgSetup->pgdata);
313314
}
314315

315316
return pgIsRunning;

0 commit comments

Comments
 (0)