1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-29 10:41:53 +03:00

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
This commit is contained in:
Thomas Munro
2022-03-18 17:45:04 +13:00
parent 7a7cd84893
commit 3f1ce97346
10 changed files with 686 additions and 174 deletions

View File

@ -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;