aboutsummaryrefslogtreecommitdiff
path: root/src/backend/access/transam/xlog.c
diff options
context:
space:
mode:
authorThomas Munro <tmunro@postgresql.org>2022-03-18 17:45:04 +1300
committerThomas Munro <tmunro@postgresql.org>2022-03-18 18:45:47 +1300
commit3f1ce973467a0d285961bf2f99b11d06e264e2c1 (patch)
tree1a9aede7e26233950719171c8a5228f99a9979ff /src/backend/access/transam/xlog.c
parent7a7cd84893e02e79d4b5e2d72b4ae327b031b217 (diff)
downloadpostgresql-3f1ce973467a0d285961bf2f99b11d06e264e2c1.tar.gz
postgresql-3f1ce973467a0d285961bf2f99b11d06e264e2c1.zip
Add circular WAL decoding buffer, take II.
Teach xlogreader.c to decode the WAL into a circular buffer. This will support optimizations based on looking ahead, to follow in a later commit. * XLogReadRecord() works as before, decoding records one by one, and allowing them to be examined via the traditional XLogRecGetXXX() macros and certain traditional members like xlogreader->ReadRecPtr. * An alternative new interface XLogReadAhead()/XLogNextRecord() is added that returns pointers to DecodedXLogRecord objects so that it's now possible to look ahead in the WAL stream while replaying. * In order to be able to use the new interface effectively while streaming data, support is added for the page_read() callback to respond to a new nonblocking mode with XLREAD_WOULDBLOCK instead of waiting for more data to arrive. No direct user of the new interface is included in this commit, though XLogReadRecord() uses it internally. Existing code doesn't need to change, except in a few places where it was accessing reader internals directly and now needs to go through accessor macros. Reviewed-by: Julien Rouhaud <rjuju123@gmail.com> Reviewed-by: Tomas Vondra <tomas.vondra@enterprisedb.com> Reviewed-by: Andres Freund <andres@anarazel.de> (earlier versions) Discussion: https://postgr.es/m/CA+hUKGJ4VJN8ttxScUFM8dOKX0BrBiboo5uz1cq=AovOddfHpA@mail.gmail.com
Diffstat (limited to 'src/backend/access/transam/xlog.c')
-rw-r--r--src/backend/access/transam/xlog.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index f436471b276..4ac3871c74f 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -971,6 +971,8 @@ XLogInsertRecord(XLogRecData *rdata,
if (XLOG_DEBUG)
{
static XLogReaderState *debug_reader = NULL;
+ XLogRecord *record;
+ DecodedXLogRecord *decoded;
StringInfoData buf;
StringInfoData recordBuf;
char *errormsg = NULL;
@@ -990,6 +992,11 @@ XLogInsertRecord(XLogRecData *rdata,
for (; rdata != NULL; rdata = rdata->next)
appendBinaryStringInfo(&recordBuf, rdata->data, rdata->len);
+ /* We also need temporary space to decode the record. */
+ record = (XLogRecord *) recordBuf.data;
+ decoded = (DecodedXLogRecord *)
+ palloc(DecodeXLogRecordRequiredSpace(record->xl_tot_len));
+
if (!debug_reader)
debug_reader = XLogReaderAllocate(wal_segment_size, NULL,
XL_ROUTINE(), NULL);
@@ -998,7 +1005,10 @@ XLogInsertRecord(XLogRecData *rdata,
{
appendStringInfoString(&buf, "error decoding record: out of memory while allocating a WAL reading processor");
}
- else if (!DecodeXLogRecord(debug_reader, (XLogRecord *) recordBuf.data,
+ else if (!DecodeXLogRecord(debug_reader,
+ decoded,
+ record,
+ EndPos,
&errormsg))
{
appendStringInfo(&buf, "error decoding record: %s",
@@ -1007,10 +1017,14 @@ XLogInsertRecord(XLogRecData *rdata,
else
{
appendStringInfoString(&buf, " - ");
+
+ debug_reader->record = decoded;
xlog_outdesc(&buf, debug_reader);
+ debug_reader->record = NULL;
}
elog(LOG, "%s", buf.data);
+ pfree(decoded);
pfree(buf.data);
pfree(recordBuf.data);
MemoryContextSwitchTo(oldCxt);
@@ -7738,7 +7752,7 @@ xlog_redo(XLogReaderState *record)
* resource manager needs to generate conflicts, it has to define a
* separate WAL record type and redo routine.
*/
- for (uint8 block_id = 0; block_id <= record->max_block_id; block_id++)
+ for (uint8 block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
{
Buffer buffer;