From 2075aecdcc6a70fea1b33c9e90878e90c2a61612 Mon Sep 17 00:00:00 2001 From: Robert Pang Date: Tue, 10 Mar 2026 21:11:49 -0700 Subject: [PATCH] Fix premature timeout in pg_promote() caused by signal interruptions Previously, pg_promote() looped a fixed number of times calculated from the specified timeout and waited 100 ms on the backend latch per iteration for standby promotion. However, unrelated signals to the backend could set the latch and wake up the backend early, resulting in a wait time significantly shorter than the specified timeout if the signals happened frequently. This commit refines the logic to track actual elapsed time. By looping until the requested duration has truly passed, pg_promote() now ensures that the wait does not end prematurely due to signals. Author: Robert Pang --- src/backend/access/transam/xlogfuncs.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c index ecb3c8c0820..6137af2d35e 100644 --- a/src/backend/access/transam/xlogfuncs.c +++ b/src/backend/access/transam/xlogfuncs.c @@ -690,7 +690,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, @@ -731,8 +731,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 = GetCurrentTimestamp() + wait_seconds * 1000000L; + while (GetCurrentTimestamp() < end_time) { int rc; @@ -745,7 +745,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); /* -- 2.53.0.473.g4a7958ca14-goog