mirror of
https://github.com/postgres/postgres.git
synced 2026-04-22 14:47:29 -04:00
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 <robertpang@google.com> Reviewed-by: Tiancheng Ge <getiancheng_2012@163.com> Discussion: https://postgr.es/m/CAJhEC07OK8J7tLUbyiccnuOXRE7UKxBNqD2-pLfeFXa=tBoWtw@mail.gmail.com
This commit is contained in:
parent
e9d723487b
commit
4287c50fc2
1 changed files with 4 additions and 4 deletions
|
|
@ -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);
|
||||
|
||||
/*
|
||||
|
|
|
|||
Loading…
Reference in a new issue