mirror of
https://github.com/postgres/postgres.git
synced 2025-06-19 04:21:08 +03:00
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
This commit is contained in:
@ -759,6 +759,7 @@ pg_wal_replay_wait(PG_FUNCTION_ARGS)
|
|||||||
{
|
{
|
||||||
XLogRecPtr target_lsn = PG_GETARG_LSN(0);
|
XLogRecPtr target_lsn = PG_GETARG_LSN(0);
|
||||||
int64 timeout = PG_GETARG_INT64(1);
|
int64 timeout = PG_GETARG_INT64(1);
|
||||||
|
WaitLSNResult result;
|
||||||
|
|
||||||
if (timeout < 0)
|
if (timeout < 0)
|
||||||
ereport(ERROR,
|
ereport(ERROR,
|
||||||
@ -799,7 +800,35 @@ pg_wal_replay_wait(PG_FUNCTION_ARGS)
|
|||||||
*/
|
*/
|
||||||
Assert(MyProc->xmin == InvalidTransactionId);
|
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();
|
PG_RETURN_VOID();
|
||||||
}
|
}
|
||||||
|
@ -217,7 +217,7 @@ WaitLSNCleanup(void)
|
|||||||
* Wait using MyLatch till the given LSN is replayed, the postmaster dies or
|
* Wait using MyLatch till the given LSN is replayed, the postmaster dies or
|
||||||
* timeout happens.
|
* timeout happens.
|
||||||
*/
|
*/
|
||||||
void
|
WaitLSNResult
|
||||||
WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
|
WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
|
||||||
{
|
{
|
||||||
XLogRecPtr currentLSN;
|
XLogRecPtr currentLSN;
|
||||||
@ -240,17 +240,14 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
|
|||||||
* check the last replay LSN before reporting an error.
|
* check the last replay LSN before reporting an error.
|
||||||
*/
|
*/
|
||||||
if (targetLSN <= GetXLogReplayRecPtr(NULL))
|
if (targetLSN <= GetXLogReplayRecPtr(NULL))
|
||||||
return;
|
return WAIT_LSN_RESULT_SUCCESS;
|
||||||
ereport(ERROR,
|
return WAIT_LSN_RESULT_NOT_IN_RECOVERY;
|
||||||
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
||||||
errmsg("recovery is not in progress"),
|
|
||||||
errhint("Waiting for LSN can only be executed during recovery.")));
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* If target LSN is already replayed, exit immediately */
|
/* If target LSN is already replayed, exit immediately */
|
||||||
if (targetLSN <= GetXLogReplayRecPtr(NULL))
|
if (targetLSN <= GetXLogReplayRecPtr(NULL))
|
||||||
return;
|
return WAIT_LSN_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeout > 0)
|
if (timeout > 0)
|
||||||
@ -276,17 +273,13 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
|
|||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Recovery was ended, but recheck if target LSN was already
|
* Recovery was ended, but recheck if target LSN was already
|
||||||
* replayed.
|
* replayed. See the comment regarding deleteLSNWaiter() below.
|
||||||
*/
|
*/
|
||||||
|
deleteLSNWaiter();
|
||||||
currentLSN = GetXLogReplayRecPtr(NULL);
|
currentLSN = GetXLogReplayRecPtr(NULL);
|
||||||
if (targetLSN <= currentLSN)
|
if (targetLSN <= currentLSN)
|
||||||
return;
|
return WAIT_LSN_RESULT_SUCCESS;
|
||||||
ereport(ERROR,
|
return WAIT_LSN_RESULT_NOT_IN_RECOVERY;
|
||||||
(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(targetLSN),
|
|
||||||
LSN_FORMAT_ARGS(currentLSN))));
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -338,11 +331,7 @@ WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout)
|
|||||||
* If we didn't reach the target LSN, we must be exited by timeout.
|
* If we didn't reach the target LSN, we must be exited by timeout.
|
||||||
*/
|
*/
|
||||||
if (targetLSN > currentLSN)
|
if (targetLSN > currentLSN)
|
||||||
{
|
return WAIT_LSN_RESULT_TIMEOUT;
|
||||||
ereport(ERROR,
|
|
||||||
(errcode(ERRCODE_QUERY_CANCELED),
|
return WAIT_LSN_RESULT_SUCCESS;
|
||||||
errmsg("timed out while waiting for target LSN %X/%X to be replayed; current replay LSN %X/%X",
|
|
||||||
LSN_FORMAT_ARGS(targetLSN),
|
|
||||||
LSN_FORMAT_ARGS(currentLSN))));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -70,12 +70,23 @@ typedef struct WaitLSNState
|
|||||||
WaitLSNProcInfo procInfos[FLEXIBLE_ARRAY_MEMBER];
|
WaitLSNProcInfo procInfos[FLEXIBLE_ARRAY_MEMBER];
|
||||||
} WaitLSNState;
|
} WaitLSNState;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Result statuses for WaitForLSNReplay().
|
||||||
|
*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
WAIT_LSN_RESULT_SUCCESS, /* Target LSN is reached */
|
||||||
|
WAIT_LSN_RESULT_TIMEOUT, /* Timeout occurred */
|
||||||
|
WAIT_LSN_RESULT_NOT_IN_RECOVERY, /* Recovery ended before or during our
|
||||||
|
* wait */
|
||||||
|
} WaitLSNResult;
|
||||||
|
|
||||||
extern PGDLLIMPORT WaitLSNState *waitLSNState;
|
extern PGDLLIMPORT WaitLSNState *waitLSNState;
|
||||||
|
|
||||||
extern Size WaitLSNShmemSize(void);
|
extern Size WaitLSNShmemSize(void);
|
||||||
extern void WaitLSNShmemInit(void);
|
extern void WaitLSNShmemInit(void);
|
||||||
extern void WaitLSNSetLatches(XLogRecPtr currentLSN);
|
extern void WaitLSNSetLatches(XLogRecPtr currentLSN);
|
||||||
extern void WaitLSNCleanup(void);
|
extern void WaitLSNCleanup(void);
|
||||||
extern void WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout);
|
extern WaitLSNResult WaitForLSNReplay(XLogRecPtr targetLSN, int64 timeout);
|
||||||
|
|
||||||
#endif /* XLOG_WAIT_H */
|
#endif /* XLOG_WAIT_H */
|
||||||
|
@ -3126,6 +3126,7 @@ WaitEventIPC
|
|||||||
WaitEventSet
|
WaitEventSet
|
||||||
WaitEventTimeout
|
WaitEventTimeout
|
||||||
WaitLSNProcInfo
|
WaitLSNProcInfo
|
||||||
|
WaitLSNResult
|
||||||
WaitLSNState
|
WaitLSNState
|
||||||
WaitPMResult
|
WaitPMResult
|
||||||
WalCloseMethod
|
WalCloseMethod
|
||||||
|
Reference in New Issue
Block a user