From: Michael Paquier Date: Thu, 26 Mar 2026 01:39:40 +0000 (+0900) Subject: Improve timeout handling of pg_promote() X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4287c50fc21e6d1c7798955020f8438ae2327472;p=thirdparty%2Fpostgresql.git Improve timeout handling of pg_promote() Previously, pg_promote() looped a fixed number of times, calculated from the specified timeout, and waited 100ms on a latch, once per iteration, for the promotion of a standby to complete. However, unrelated signals to the backend could set the latch and wake up the backend early, resulting in a faster consumption of the loops and an execution time of the function that does not match with the timeout input given in input. This could be confusing for the function caller, especially if some backend-side timeout is aggressive, because the function would return much earlier than expected and report that the promote request has not completed within the time requested. This commit refines the logic to track the time actually elapsed, by looping until the requested duration has truly passed. The code calculates the end time we expect, then uses it when looping. Author: Robert Pang Reviewed-by: Tiancheng Ge Discussion: https://postgr.es/m/CAJhEC07OK8J7tLUbyiccnuOXRE7UKxBNqD2-pLfeFXa=tBoWtw@mail.gmail.com --- diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c index 4e35311b2f3..65bbaeda59c 100644 --- a/src/backend/access/transam/xlogfuncs.c +++ b/src/backend/access/transam/xlogfuncs.c @@ -691,7 +691,7 @@ pg_promote(PG_FUNCTION_ARGS) bool wait = PG_GETARG_BOOL(0); int wait_seconds = PG_GETARG_INT32(1); FILE *promote_file; - int i; + TimestampTz end_time; if (!RecoveryInProgress()) ereport(ERROR, @@ -732,8 +732,8 @@ pg_promote(PG_FUNCTION_ARGS) PG_RETURN_BOOL(true); /* wait for the amount of time wanted until promotion */ -#define WAITS_PER_SECOND 10 - for (i = 0; i < WAITS_PER_SECOND * wait_seconds; i++) + end_time = TimestampTzPlusSeconds(GetCurrentTimestamp(), wait_seconds); + while (GetCurrentTimestamp() < end_time) { int rc; @@ -746,7 +746,7 @@ pg_promote(PG_FUNCTION_ARGS) rc = WaitLatch(MyLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, - 1000L / WAITS_PER_SECOND, + 100L, WAIT_EVENT_PROMOTE); /*