diff options
author | Alexander Korotkov <akorotkov@postgresql.org> | 2024-10-24 14:38:27 +0300 |
---|---|---|
committer | Alexander Korotkov <akorotkov@postgresql.org> | 2024-10-24 14:38:27 +0300 |
commit | 73da6b8d1b3e8b7541961c3534e584243cb0470e (patch) | |
tree | 586206c3c5cda81c49b05be18357066d30bb09a8 /src/backend/access/transam/xlogfuncs.c | |
parent | 6cfebfe88b9a753152862b83d42d1103125b01bd (diff) | |
download | postgresql-73da6b8d1b3e8b7541961c3534e584243cb0470e.tar.gz postgresql-73da6b8d1b3e8b7541961c3534e584243cb0470e.zip |
Refactor WaitForLSNReplay() to return the result of waiting
Currently, WaitForLSNReplay() immediately throws an error if waiting for LSN
replay is not successful. This commit teaches WaitForLSNReplay() to return
the result of waiting, while making pg_wal_replay_wait() responsible for
throwing an appropriate error.
This is preparation to adding 'no_error' argument to pg_wal_replay_wait() and
new function pg_wal_replay_wait_status(), which returns the last wait result
status.
Additionally, we stop distinguishing situations when we find our instance to
be not in a recovery state before entering the waiting loop and inside
the waiting loop. Standby promotion may happen at any moment, even between
issuing a procedure call statement and pg_wal_replay_wait() doing a first
check of recovery status. Thus, there is no pointing distinguishing these
situations.
Also, since we may exit the waiting loop and see our instance not in recovery
without throwing an error, we need to deleteLSNWaiter() in that case. We do
this unconditionally for the sake of simplicity, even if standby was already
promoted after reaching the target LSN, the startup process surely already
deleted us.
Reported-by: Michael Paquier
Discussion: https://postgr.es/m/ZtUF17gF0pNpwZDI%40paquier.xyz
Reviewed-by: Michael Paquier, Pavel Borisov
Diffstat (limited to 'src/backend/access/transam/xlogfuncs.c')
-rw-r--r-- | src/backend/access/transam/xlogfuncs.c | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/src/backend/access/transam/xlogfuncs.c b/src/backend/access/transam/xlogfuncs.c index cbf84ef7d8f..ddca78d3717 100644 --- a/src/backend/access/transam/xlogfuncs.c +++ b/src/backend/access/transam/xlogfuncs.c @@ -759,6 +759,7 @@ pg_wal_replay_wait(PG_FUNCTION_ARGS) { XLogRecPtr target_lsn = PG_GETARG_LSN(0); int64 timeout = PG_GETARG_INT64(1); + WaitLSNResult result; if (timeout < 0) ereport(ERROR, @@ -799,7 +800,35 @@ pg_wal_replay_wait(PG_FUNCTION_ARGS) */ Assert(MyProc->xmin == InvalidTransactionId); - (void) WaitForLSNReplay(target_lsn, timeout); + result = WaitForLSNReplay(target_lsn, timeout); + + /* + * Process the result of WaitForLSNReplay(). Throw appropriate error if + * needed. + */ + switch (result) + { + case WAIT_LSN_RESULT_SUCCESS: + /* Nothing to do on success */ + break; + + case WAIT_LSN_RESULT_TIMEOUT: + ereport(ERROR, + (errcode(ERRCODE_QUERY_CANCELED), + errmsg("timed out while waiting for target LSN %X/%X to be replayed; current replay LSN %X/%X", + LSN_FORMAT_ARGS(target_lsn), + LSN_FORMAT_ARGS(GetXLogReplayRecPtr(NULL))))); + break; + + case WAIT_LSN_RESULT_NOT_IN_RECOVERY: + ereport(ERROR, + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("recovery is not in progress"), + errdetail("Recovery ended before replaying target LSN %X/%X; last replay LSN %X/%X.", + LSN_FORMAT_ARGS(target_lsn), + LSN_FORMAT_ARGS(GetXLogReplayRecPtr(NULL))))); + break; + } PG_RETURN_VOID(); } |