mirror of
https://github.com/postgres/postgres.git
synced 2025-04-25 21:42:33 +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:
parent
50861cd683
commit
3b02ea4f07
@ -10,9 +10,11 @@
|
|||||||
*
|
*
|
||||||
* NOTES
|
* NOTES
|
||||||
* See xlogreader.h for more notes on this facility.
|
* 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 "postgres.h"
|
||||||
|
|
||||||
#include "access/transam.h"
|
#include "access/transam.h"
|
||||||
@ -192,7 +194,7 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
|||||||
{
|
{
|
||||||
XLogRecord *record;
|
XLogRecord *record;
|
||||||
XLogRecPtr targetPagePtr;
|
XLogRecPtr targetPagePtr;
|
||||||
bool randAccess = false;
|
bool randAccess;
|
||||||
uint32 len,
|
uint32 len,
|
||||||
total_len;
|
total_len;
|
||||||
uint32 targetRecOff;
|
uint32 targetRecOff;
|
||||||
@ -200,6 +202,13 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
|||||||
bool gotheader;
|
bool gotheader;
|
||||||
int readOff;
|
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 */
|
/* reset error state */
|
||||||
*errormsg = NULL;
|
*errormsg = NULL;
|
||||||
state->errormsg_buf[0] = '\0';
|
state->errormsg_buf[0] = '\0';
|
||||||
@ -208,6 +217,7 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
|||||||
|
|
||||||
if (RecPtr == InvalidXLogRecPtr)
|
if (RecPtr == InvalidXLogRecPtr)
|
||||||
{
|
{
|
||||||
|
/* No explicit start point; read the record after the one we just read */
|
||||||
RecPtr = state->EndRecPtr;
|
RecPtr = state->EndRecPtr;
|
||||||
|
|
||||||
if (state->ReadRecPtr == InvalidXLogRecPtr)
|
if (state->ReadRecPtr == InvalidXLogRecPtr)
|
||||||
@ -223,11 +233,13 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
|
* Caller supplied a position to start at.
|
||||||
|
*
|
||||||
* In this case, the passed-in record pointer should already be
|
* In this case, the passed-in record pointer should already be
|
||||||
* pointing to a valid record starting position.
|
* pointing to a valid record starting position.
|
||||||
*/
|
*/
|
||||||
Assert(XRecOffIsValid(RecPtr));
|
Assert(XRecOffIsValid(RecPtr));
|
||||||
randAccess = true; /* allow readPageTLI to go backwards too */
|
randAccess = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
state->currRecPtr = RecPtr;
|
state->currRecPtr = RecPtr;
|
||||||
@ -309,8 +321,10 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
|||||||
/* XXX: more validation should be done here */
|
/* XXX: more validation should be done here */
|
||||||
if (total_len < SizeOfXLogRecord)
|
if (total_len < SizeOfXLogRecord)
|
||||||
{
|
{
|
||||||
report_invalid_record(state, "invalid record length at %X/%X",
|
report_invalid_record(state,
|
||||||
(uint32) (RecPtr >> 32), (uint32) RecPtr);
|
"invalid record length at %X/%X: wanted %lu, got %u",
|
||||||
|
(uint32) (RecPtr >> 32), (uint32) RecPtr,
|
||||||
|
SizeOfXLogRecord, total_len);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
gotheader = false;
|
gotheader = false;
|
||||||
@ -463,12 +477,10 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
|
|||||||
err:
|
err:
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Invalidate the xlog page we've cached. We might read from a different
|
* Invalidate the read state. We might read from a different source after
|
||||||
* source after failure.
|
* failure.
|
||||||
*/
|
*/
|
||||||
state->readSegNo = 0;
|
XLogReaderInvalReadState(state);
|
||||||
state->readOff = 0;
|
|
||||||
state->readLen = 0;
|
|
||||||
|
|
||||||
if (state->errormsg_buf[0] != '\0')
|
if (state->errormsg_buf[0] != '\0')
|
||||||
*errormsg = state->errormsg_buf;
|
*errormsg = state->errormsg_buf;
|
||||||
@ -572,7 +584,7 @@ ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
|
|||||||
if (!ValidXLogPageHeader(state, pageptr, hdr))
|
if (!ValidXLogPageHeader(state, pageptr, hdr))
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
/* update cache information */
|
/* update read state information */
|
||||||
state->readSegNo = targetSegNo;
|
state->readSegNo = targetSegNo;
|
||||||
state->readOff = targetPageOff;
|
state->readOff = targetPageOff;
|
||||||
state->readLen = readLen;
|
state->readLen = readLen;
|
||||||
@ -580,10 +592,19 @@ ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
|
|||||||
return readLen;
|
return readLen;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
|
XLogReaderInvalReadState(state);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Invalidate the xlogreader's read state to force a re-read.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
XLogReaderInvalReadState(XLogReaderState *state)
|
||||||
|
{
|
||||||
state->readSegNo = 0;
|
state->readSegNo = 0;
|
||||||
state->readOff = 0;
|
state->readOff = 0;
|
||||||
state->readLen = 0;
|
state->readLen = 0;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -600,8 +621,9 @@ ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr,
|
|||||||
if (record->xl_tot_len < SizeOfXLogRecord)
|
if (record->xl_tot_len < SizeOfXLogRecord)
|
||||||
{
|
{
|
||||||
report_invalid_record(state,
|
report_invalid_record(state,
|
||||||
"invalid record length at %X/%X",
|
"invalid record length at %X/%X: wanted %lu, got %u",
|
||||||
(uint32) (RecPtr >> 32), (uint32) RecPtr);
|
(uint32) (RecPtr >> 32), (uint32) RecPtr,
|
||||||
|
SizeOfXLogRecord, record->xl_tot_len);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (record->xl_rmid > RM_MAX_ID)
|
if (record->xl_rmid > RM_MAX_ID)
|
||||||
@ -907,11 +929,9 @@ XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr)
|
|||||||
err:
|
err:
|
||||||
out:
|
out:
|
||||||
/* Reset state to what we had before finding the record */
|
/* 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->ReadRecPtr = saved_state.ReadRecPtr;
|
||||||
state->EndRecPtr = saved_state.EndRecPtr;
|
state->EndRecPtr = saved_state.EndRecPtr;
|
||||||
|
XLogReaderInvalReadState(state);
|
||||||
|
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
@ -19,12 +19,11 @@
|
|||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "miscadmin.h"
|
|
||||||
|
|
||||||
#include "access/xlog.h"
|
#include "access/xlog.h"
|
||||||
#include "access/xlog_internal.h"
|
#include "access/xlog_internal.h"
|
||||||
#include "access/xlogutils.h"
|
#include "access/xlogutils.h"
|
||||||
#include "catalog/catalog.h"
|
#include "catalog/catalog.h"
|
||||||
|
#include "miscadmin.h"
|
||||||
#include "storage/smgr.h"
|
#include "storage/smgr.h"
|
||||||
#include "utils/guc.h"
|
#include "utils/guc.h"
|
||||||
#include "utils/hsearch.h"
|
#include "utils/hsearch.h"
|
||||||
@ -638,8 +637,17 @@ XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TODO: This is duplicate code with pg_xlogdump, similar to walsender.c, but
|
* Read 'count' bytes from WAL into 'buf', starting at location 'startptr'
|
||||||
* we currently don't have the infrastructure (elog!) to share it.
|
* in timeline 'tli'.
|
||||||
|
*
|
||||||
|
* Will open, and keep open, one WAL segment stored in the static file
|
||||||
|
* descriptor 'sendFile'. This means if XLogRead is used once, there will
|
||||||
|
* always be one descriptor left open until the process ends, but never
|
||||||
|
* more than one.
|
||||||
|
*
|
||||||
|
* XXX This is very similar to pg_xlogdump's XLogDumpXLogRead and to XLogRead
|
||||||
|
* in walsender.c but for small differences (such as lack of elog() in
|
||||||
|
* frontend). Probably these should be merged at some point.
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
XLogRead(char *buf, TimeLineID tli, XLogRecPtr startptr, Size count)
|
XLogRead(char *buf, TimeLineID tli, XLogRecPtr startptr, Size count)
|
||||||
@ -648,6 +656,7 @@ XLogRead(char *buf, TimeLineID tli, XLogRecPtr startptr, Size count)
|
|||||||
XLogRecPtr recptr;
|
XLogRecPtr recptr;
|
||||||
Size nbytes;
|
Size nbytes;
|
||||||
|
|
||||||
|
/* state maintained across calls */
|
||||||
static int sendFile = -1;
|
static int sendFile = -1;
|
||||||
static XLogSegNo sendSegNo = 0;
|
static XLogSegNo sendSegNo = 0;
|
||||||
static uint32 sendOff = 0;
|
static uint32 sendOff = 0;
|
||||||
@ -664,11 +673,11 @@ XLogRead(char *buf, TimeLineID tli, XLogRecPtr startptr, Size count)
|
|||||||
|
|
||||||
startoff = recptr % XLogSegSize;
|
startoff = recptr % XLogSegSize;
|
||||||
|
|
||||||
|
/* Do we need to switch to a different xlog segment? */
|
||||||
if (sendFile < 0 || !XLByteInSeg(recptr, sendSegNo))
|
if (sendFile < 0 || !XLByteInSeg(recptr, sendSegNo))
|
||||||
{
|
{
|
||||||
char path[MAXPGPATH];
|
char path[MAXPGPATH];
|
||||||
|
|
||||||
/* Switch to another logfile segment */
|
|
||||||
if (sendFile >= 0)
|
if (sendFile >= 0)
|
||||||
close(sendFile);
|
close(sendFile);
|
||||||
|
|
||||||
@ -745,7 +754,7 @@ XLogRead(char *buf, TimeLineID tli, XLogRecPtr startptr, Size count)
|
|||||||
* Public because it would likely be very helpful for someone writing another
|
* Public because it would likely be very helpful for someone writing another
|
||||||
* output method outside walsender, e.g. in a bgworker.
|
* output method outside walsender, e.g. in a bgworker.
|
||||||
*
|
*
|
||||||
* TODO: The walsender has it's own version of this, but it relies on the
|
* TODO: The walsender has its own version of this, but it relies on the
|
||||||
* walsender's latch being set whenever WAL is flushed. No such infrastructure
|
* walsender's latch being set whenever WAL is flushed. No such infrastructure
|
||||||
* exists for normal backends, so we have to do a check/sleep/repeat style of
|
* exists for normal backends, so we have to do a check/sleep/repeat style of
|
||||||
* loop for now.
|
* loop for now.
|
||||||
|
@ -241,6 +241,10 @@ pg_logical_slot_get_changes_guts(FunctionCallInfo fcinfo, bool confirm, bool bin
|
|||||||
|
|
||||||
PG_TRY();
|
PG_TRY();
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
* Passing InvalidXLogRecPtr here causes replay to start at the slot's
|
||||||
|
* confirmed_flush.
|
||||||
|
*/
|
||||||
ctx = CreateDecodingContext(InvalidXLogRecPtr,
|
ctx = CreateDecodingContext(InvalidXLogRecPtr,
|
||||||
options,
|
options,
|
||||||
logical_read_local_xlog_page,
|
logical_read_local_xlog_page,
|
||||||
@ -263,6 +267,14 @@ pg_logical_slot_get_changes_guts(FunctionCallInfo fcinfo, bool confirm, bool bin
|
|||||||
|
|
||||||
ctx->output_writer_private = p;
|
ctx->output_writer_private = p;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We start reading xlog from the restart lsn, even though in
|
||||||
|
* CreateDecodingContext we set the snapshot builder up using the
|
||||||
|
* slot's confirmed_flush. This means we might read xlog we don't
|
||||||
|
* actually decode rows from, but the snapshot builder might need it
|
||||||
|
* to get to a consistent point. The point we start returning data to
|
||||||
|
* *users* at is the candidate restart lsn from the decoding context.
|
||||||
|
*/
|
||||||
startptr = MyReplicationSlot->data.restart_lsn;
|
startptr = MyReplicationSlot->data.restart_lsn;
|
||||||
|
|
||||||
CurrentResourceOwner = ResourceOwnerCreate(CurrentResourceOwner, "logical decoding");
|
CurrentResourceOwner = ResourceOwnerCreate(CurrentResourceOwner, "logical decoding");
|
||||||
@ -280,6 +292,10 @@ pg_logical_slot_get_changes_guts(FunctionCallInfo fcinfo, bool confirm, bool bin
|
|||||||
if (errm)
|
if (errm)
|
||||||
elog(ERROR, "%s", errm);
|
elog(ERROR, "%s", errm);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Now that we've set up the xlog reader state, subsequent calls
|
||||||
|
* pass InvalidXLogRecPtr to say "continue from last record"
|
||||||
|
*/
|
||||||
startptr = InvalidXLogRecPtr;
|
startptr = InvalidXLogRecPtr;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -139,16 +139,22 @@ struct XLogReaderState
|
|||||||
* ----------------------------------------
|
* ----------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Buffer for currently read page (XLOG_BLCKSZ bytes) */
|
/*
|
||||||
|
* Buffer for currently read page (XLOG_BLCKSZ bytes, valid up to at least
|
||||||
|
* readLen bytes)
|
||||||
|
*/
|
||||||
char *readBuf;
|
char *readBuf;
|
||||||
|
uint32 readLen;
|
||||||
|
|
||||||
/* last read segment, segment offset, read length, TLI */
|
/* last read segment, segment offset, TLI for data currently in readBuf */
|
||||||
XLogSegNo readSegNo;
|
XLogSegNo readSegNo;
|
||||||
uint32 readOff;
|
uint32 readOff;
|
||||||
uint32 readLen;
|
|
||||||
TimeLineID readPageTLI;
|
TimeLineID readPageTLI;
|
||||||
|
|
||||||
/* beginning of last page read, and its TLI */
|
/*
|
||||||
|
* beginning of prior page read, and its TLI. Doesn't necessarily
|
||||||
|
* correspond to what's in readBuf; used for timeline sanity checks.
|
||||||
|
*/
|
||||||
XLogRecPtr latestPagePtr;
|
XLogRecPtr latestPagePtr;
|
||||||
TimeLineID latestPageTLI;
|
TimeLineID latestPageTLI;
|
||||||
|
|
||||||
@ -174,6 +180,9 @@ extern void XLogReaderFree(XLogReaderState *state);
|
|||||||
extern struct XLogRecord *XLogReadRecord(XLogReaderState *state,
|
extern struct XLogRecord *XLogReadRecord(XLogReaderState *state,
|
||||||
XLogRecPtr recptr, char **errormsg);
|
XLogRecPtr recptr, char **errormsg);
|
||||||
|
|
||||||
|
/* Invalidate read state */
|
||||||
|
extern void XLogReaderInvalReadState(XLogReaderState *state);
|
||||||
|
|
||||||
#ifdef FRONTEND
|
#ifdef FRONTEND
|
||||||
extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr);
|
extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr);
|
||||||
#endif /* FRONTEND */
|
#endif /* FRONTEND */
|
||||||
|
@ -47,7 +47,9 @@ extern Buffer XLogReadBufferExtended(RelFileNode rnode, ForkNumber forknum,
|
|||||||
extern Relation CreateFakeRelcacheEntry(RelFileNode rnode);
|
extern Relation CreateFakeRelcacheEntry(RelFileNode rnode);
|
||||||
extern void FreeFakeRelcacheEntry(Relation fakerel);
|
extern void FreeFakeRelcacheEntry(Relation fakerel);
|
||||||
|
|
||||||
extern int read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr,
|
extern int read_local_xlog_page(XLogReaderState *state,
|
||||||
int reqLen, XLogRecPtr targetRecPtr, char *cur_page, TimeLineID *pageTLI);
|
XLogRecPtr targetPagePtr, int reqLen,
|
||||||
|
XLogRecPtr targetRecPtr, char *cur_page,
|
||||||
|
TimeLineID *pageTLI);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user