mirror of
https://github.com/postgres/postgres.git
synced 2025-07-08 11:42:09 +03:00
XLogReader general code cleanup
Some minor tweaks and comment additions, for cleanliness sake and to avoid having the upcoming timeline-following patch be polluted with unrelated cleanup. Extracted from a larger patch by Craig Ringer, reviewed by Andres Freund, with some additions by myself.
This commit is contained in:
@ -10,9 +10,11 @@
|
||||
*
|
||||
* NOTES
|
||||
* See xlogreader.h for more notes on this facility.
|
||||
*
|
||||
* This file is compiled as both front-end and backend code, so it
|
||||
* may not use ereport, server-defined static variables, etc.
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
#include "access/transam.h"
|
||||
@ -192,7 +194,7 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
||||
{
|
||||
XLogRecord *record;
|
||||
XLogRecPtr targetPagePtr;
|
||||
bool randAccess = false;
|
||||
bool randAccess;
|
||||
uint32 len,
|
||||
total_len;
|
||||
uint32 targetRecOff;
|
||||
@ -200,6 +202,13 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
||||
bool gotheader;
|
||||
int readOff;
|
||||
|
||||
/*
|
||||
* randAccess indicates whether to verify the previous-record pointer of
|
||||
* the record we're reading. We only do this if we're reading
|
||||
* sequentially, which is what we initially assume.
|
||||
*/
|
||||
randAccess = false;
|
||||
|
||||
/* reset error state */
|
||||
*errormsg = NULL;
|
||||
state->errormsg_buf[0] = '\0';
|
||||
@ -208,6 +217,7 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
||||
|
||||
if (RecPtr == InvalidXLogRecPtr)
|
||||
{
|
||||
/* No explicit start point; read the record after the one we just read */
|
||||
RecPtr = state->EndRecPtr;
|
||||
|
||||
if (state->ReadRecPtr == InvalidXLogRecPtr)
|
||||
@ -223,11 +233,13 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
||||
else
|
||||
{
|
||||
/*
|
||||
* Caller supplied a position to start at.
|
||||
*
|
||||
* In this case, the passed-in record pointer should already be
|
||||
* pointing to a valid record starting position.
|
||||
*/
|
||||
Assert(XRecOffIsValid(RecPtr));
|
||||
randAccess = true; /* allow readPageTLI to go backwards too */
|
||||
randAccess = true;
|
||||
}
|
||||
|
||||
state->currRecPtr = RecPtr;
|
||||
@ -309,8 +321,10 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
||||
/* XXX: more validation should be done here */
|
||||
if (total_len < SizeOfXLogRecord)
|
||||
{
|
||||
report_invalid_record(state, "invalid record length at %X/%X",
|
||||
(uint32) (RecPtr >> 32), (uint32) RecPtr);
|
||||
report_invalid_record(state,
|
||||
"invalid record length at %X/%X: wanted %lu, got %u",
|
||||
(uint32) (RecPtr >> 32), (uint32) RecPtr,
|
||||
SizeOfXLogRecord, total_len);
|
||||
goto err;
|
||||
}
|
||||
gotheader = false;
|
||||
@ -463,12 +477,10 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
||||
err:
|
||||
|
||||
/*
|
||||
* Invalidate the xlog page we've cached. We might read from a different
|
||||
* source after failure.
|
||||
* Invalidate the read state. We might read from a different source after
|
||||
* failure.
|
||||
*/
|
||||
state->readSegNo = 0;
|
||||
state->readOff = 0;
|
||||
state->readLen = 0;
|
||||
XLogReaderInvalReadState(state);
|
||||
|
||||
if (state->errormsg_buf[0] != '\0')
|
||||
*errormsg = state->errormsg_buf;
|
||||
@ -572,7 +584,7 @@ ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
|
||||
if (!ValidXLogPageHeader(state, pageptr, hdr))
|
||||
goto err;
|
||||
|
||||
/* update cache information */
|
||||
/* update read state information */
|
||||
state->readSegNo = targetSegNo;
|
||||
state->readOff = targetPageOff;
|
||||
state->readLen = readLen;
|
||||
@ -580,10 +592,19 @@ ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
|
||||
return readLen;
|
||||
|
||||
err:
|
||||
XLogReaderInvalReadState(state);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Invalidate the xlogreader's read state to force a re-read.
|
||||
*/
|
||||
void
|
||||
XLogReaderInvalReadState(XLogReaderState *state)
|
||||
{
|
||||
state->readSegNo = 0;
|
||||
state->readOff = 0;
|
||||
state->readLen = 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -600,8 +621,9 @@ ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr,
|
||||
if (record->xl_tot_len < SizeOfXLogRecord)
|
||||
{
|
||||
report_invalid_record(state,
|
||||
"invalid record length at %X/%X",
|
||||
(uint32) (RecPtr >> 32), (uint32) RecPtr);
|
||||
"invalid record length at %X/%X: wanted %lu, got %u",
|
||||
(uint32) (RecPtr >> 32), (uint32) RecPtr,
|
||||
SizeOfXLogRecord, record->xl_tot_len);
|
||||
return false;
|
||||
}
|
||||
if (record->xl_rmid > RM_MAX_ID)
|
||||
@ -907,11 +929,9 @@ XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr)
|
||||
err:
|
||||
out:
|
||||
/* Reset state to what we had before finding the record */
|
||||
state->readSegNo = 0;
|
||||
state->readOff = 0;
|
||||
state->readLen = 0;
|
||||
state->ReadRecPtr = saved_state.ReadRecPtr;
|
||||
state->EndRecPtr = saved_state.EndRecPtr;
|
||||
XLogReaderInvalReadState(state);
|
||||
|
||||
return found;
|
||||
}
|
||||
|
Reference in New Issue
Block a user