From 3f1ce973467a0d285961bf2f99b11d06e264e2c1 Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Fri, 18 Mar 2022 17:45:04 +1300 Subject: 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 Reviewed-by: Tomas Vondra Reviewed-by: Andres Freund (earlier versions) Discussion: https://postgr.es/m/CA+hUKGJ4VJN8ttxScUFM8dOKX0BrBiboo5uz1cq=AovOddfHpA@mail.gmail.com --- src/backend/access/transam/xlog.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src/backend/access/transam/xlog.c') 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; -- cgit v1.2.3