LCOV - code coverage report
Current view: top level - src/bin/pg_rewind - xlogreader.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16devel Lines: 379 732 51.8 %
Date: 2022-10-07 16:11:04 Functions: 19 28 67.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * xlogreader.c
       4             :  *      Generic XLog reading facility
       5             :  *
       6             :  * Portions Copyright (c) 2013-2022, PostgreSQL Global Development Group
       7             :  *
       8             :  * IDENTIFICATION
       9             :  *      src/backend/access/transam/xlogreader.c
      10             :  *
      11             :  * NOTES
      12             :  *      See xlogreader.h for more notes on this facility.
      13             :  *
      14             :  *      This file is compiled as both front-end and backend code, so it
      15             :  *      may not use ereport, server-defined static variables, etc.
      16             :  *-------------------------------------------------------------------------
      17             :  */
      18             : #include "postgres.h"
      19             : 
      20             : #include <unistd.h>
      21             : #ifdef USE_LZ4
      22             : #include <lz4.h>
      23             : #endif
      24             : #ifdef USE_ZSTD
      25             : #include <zstd.h>
      26             : #endif
      27             : 
      28             : #include "access/transam.h"
      29             : #include "access/xlog_internal.h"
      30             : #include "access/xlogreader.h"
      31             : #include "access/xlogrecord.h"
      32             : #include "catalog/pg_control.h"
      33             : #include "common/pg_lzcompress.h"
      34             : #include "replication/origin.h"
      35             : 
      36             : #ifndef FRONTEND
      37             : #include "miscadmin.h"
      38             : #include "pgstat.h"
      39             : #include "utils/memutils.h"
      40             : #else
      41             : #include "common/logging.h"
      42             : #endif
      43             : 
      44             : static void report_invalid_record(XLogReaderState *state, const char *fmt,...)
      45             :             pg_attribute_printf(2, 3);
      46             : static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength);
      47             : static int  ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr,
      48             :                              int reqLen);
      49             : static void XLogReaderInvalReadState(XLogReaderState *state);
      50             : static XLogPageReadResult XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking);
      51             : static bool ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr,
      52             :                                   XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess);
      53             : static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record,
      54             :                             XLogRecPtr recptr);
      55             : static void ResetDecoder(XLogReaderState *state);
      56             : static void WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt,
      57             :                                int segsize, const char *waldir);
      58             : 
      59             : /* size of the buffer allocated for error message. */
      60             : #define MAX_ERRORMSG_LEN 1000
      61             : 
      62             : /*
      63             :  * Default size; large enough that typical users of XLogReader won't often need
      64             :  * to use the 'oversized' memory allocation code path.
      65             :  */
      66             : #define DEFAULT_DECODE_BUFFER_SIZE (64 * 1024)
      67             : 
      68             : /*
      69             :  * Construct a string in state->errormsg_buf explaining what's wrong with
      70             :  * the current record being read.
      71             :  */
      72             : static void
      73           0 : report_invalid_record(XLogReaderState *state, const char *fmt,...)
      74             : {
      75             :     va_list     args;
      76             : 
      77           0 :     fmt = _(fmt);
      78             : 
      79           0 :     va_start(args, fmt);
      80           0 :     vsnprintf(state->errormsg_buf, MAX_ERRORMSG_LEN, fmt, args);
      81           0 :     va_end(args);
      82             : 
      83           0 :     state->errormsg_deferred = true;
      84           0 : }
      85             : 
      86             : /*
      87             :  * Set the size of the decoding buffer.  A pointer to a caller supplied memory
      88             :  * region may also be passed in, in which case non-oversized records will be
      89             :  * decoded there.
      90             :  */
      91             : void
      92           0 : XLogReaderSetDecodeBuffer(XLogReaderState *state, void *buffer, size_t size)
      93             : {
      94             :     Assert(state->decode_buffer == NULL);
      95             : 
      96           0 :     state->decode_buffer = buffer;
      97           0 :     state->decode_buffer_size = size;
      98           0 :     state->decode_buffer_tail = buffer;
      99           0 :     state->decode_buffer_head = buffer;
     100           0 : }
     101             : 
     102             : /*
     103             :  * Allocate and initialize a new XLogReader.
     104             :  *
     105             :  * Returns NULL if the xlogreader couldn't be allocated.
     106             :  */
     107             : XLogReaderState *
     108          78 : XLogReaderAllocate(int wal_segment_size, const char *waldir,
     109             :                    XLogReaderRoutine *routine, void *private_data)
     110             : {
     111             :     XLogReaderState *state;
     112             : 
     113             :     state = (XLogReaderState *)
     114          78 :         palloc_extended(sizeof(XLogReaderState),
     115             :                         MCXT_ALLOC_NO_OOM | MCXT_ALLOC_ZERO);
     116          78 :     if (!state)
     117           0 :         return NULL;
     118             : 
     119             :     /* initialize caller-provided support functions */
     120          78 :     state->routine = *routine;
     121             : 
     122             :     /*
     123             :      * Permanently allocate readBuf.  We do it this way, rather than just
     124             :      * making a static array, for two reasons: (1) no need to waste the
     125             :      * storage in most instantiations of the backend; (2) a static char array
     126             :      * isn't guaranteed to have any particular alignment, whereas
     127             :      * palloc_extended() will provide MAXALIGN'd storage.
     128             :      */
     129          78 :     state->readBuf = (char *) palloc_extended(XLOG_BLCKSZ,
     130             :                                               MCXT_ALLOC_NO_OOM);
     131          78 :     if (!state->readBuf)
     132             :     {
     133           0 :         pfree(state);
     134           0 :         return NULL;
     135             :     }
     136             : 
     137             :     /* Initialize segment info. */
     138          78 :     WALOpenSegmentInit(&state->seg, &state->segcxt, wal_segment_size,
     139             :                        waldir);
     140             : 
     141             :     /* system_identifier initialized to zeroes above */
     142          78 :     state->private_data = private_data;
     143             :     /* ReadRecPtr, EndRecPtr and readLen initialized to zeroes above */
     144          78 :     state->errormsg_buf = palloc_extended(MAX_ERRORMSG_LEN + 1,
     145             :                                           MCXT_ALLOC_NO_OOM);
     146          78 :     if (!state->errormsg_buf)
     147             :     {
     148           0 :         pfree(state->readBuf);
     149           0 :         pfree(state);
     150           0 :         return NULL;
     151             :     }
     152          78 :     state->errormsg_buf[0] = '\0';
     153             : 
     154             :     /*
     155             :      * Allocate an initial readRecordBuf of minimal size, which can later be
     156             :      * enlarged if necessary.
     157             :      */
     158          78 :     if (!allocate_recordbuf(state, 0))
     159             :     {
     160           0 :         pfree(state->errormsg_buf);
     161           0 :         pfree(state->readBuf);
     162           0 :         pfree(state);
     163           0 :         return NULL;
     164             :     }
     165             : 
     166          78 :     return state;
     167             : }
     168             : 
     169             : void
     170          78 : XLogReaderFree(XLogReaderState *state)
     171             : {
     172          78 :     if (state->seg.ws_file != -1)
     173           0 :         state->routine.segment_close(state);
     174             : 
     175          78 :     if (state->decode_buffer && state->free_decode_buffer)
     176          78 :         pfree(state->decode_buffer);
     177             : 
     178          78 :     pfree(state->errormsg_buf);
     179          78 :     if (state->readRecordBuf)
     180          78 :         pfree(state->readRecordBuf);
     181          78 :     pfree(state->readBuf);
     182          78 :     pfree(state);
     183          78 : }
     184             : 
     185             : /*
     186             :  * Allocate readRecordBuf to fit a record of at least the given length.
     187             :  * Returns true if successful, false if out of memory.
     188             :  *
     189             :  * readRecordBufSize is set to the new buffer size.
     190             :  *
     191             :  * To avoid useless small increases, round its size to a multiple of
     192             :  * XLOG_BLCKSZ, and make sure it's at least 5*Max(BLCKSZ, XLOG_BLCKSZ) to start
     193             :  * with.  (That is enough for all "normal" records, but very large commit or
     194             :  * abort records might need more space.)
     195             :  */
     196             : static bool
     197          78 : allocate_recordbuf(XLogReaderState *state, uint32 reclength)
     198             : {
     199          78 :     uint32      newSize = reclength;
     200             : 
     201          78 :     newSize += XLOG_BLCKSZ - (newSize % XLOG_BLCKSZ);
     202          78 :     newSize = Max(newSize, 5 * Max(BLCKSZ, XLOG_BLCKSZ));
     203             : 
     204             : #ifndef FRONTEND
     205             : 
     206             :     /*
     207             :      * Note that in much unlucky circumstances, the random data read from a
     208             :      * recycled segment can cause this routine to be called with a size
     209             :      * causing a hard failure at allocation.  For a standby, this would cause
     210             :      * the instance to stop suddenly with a hard failure, preventing it to
     211             :      * retry fetching WAL from one of its sources which could allow it to move
     212             :      * on with replay without a manual restart. If the data comes from a past
     213             :      * recycled segment and is still valid, then the allocation may succeed
     214             :      * but record checks are going to fail so this would be short-lived.  If
     215             :      * the allocation fails because of a memory shortage, then this is not a
     216             :      * hard failure either per the guarantee given by MCXT_ALLOC_NO_OOM.
     217             :      */
     218             :     if (!AllocSizeIsValid(newSize))
     219             :         return false;
     220             : 
     221             : #endif
     222             : 
     223          78 :     if (state->readRecordBuf)
     224           0 :         pfree(state->readRecordBuf);
     225          78 :     state->readRecordBuf =
     226          78 :         (char *) palloc_extended(newSize, MCXT_ALLOC_NO_OOM);
     227          78 :     if (state->readRecordBuf == NULL)
     228             :     {
     229           0 :         state->readRecordBufSize = 0;
     230           0 :         return false;
     231             :     }
     232          78 :     state->readRecordBufSize = newSize;
     233          78 :     return true;
     234             : }
     235             : 
     236             : /*
     237             :  * Initialize the passed segment structs.
     238             :  */
     239             : static void
     240          78 : WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt,
     241             :                    int segsize, const char *waldir)
     242             : {
     243          78 :     seg->ws_file = -1;
     244          78 :     seg->ws_segno = 0;
     245          78 :     seg->ws_tli = 0;
     246             : 
     247          78 :     segcxt->ws_segsize = segsize;
     248          78 :     if (waldir)
     249          78 :         snprintf(segcxt->ws_dir, MAXPGPATH, "%s", waldir);
     250          78 : }
     251             : 
     252             : /*
     253             :  * Begin reading WAL at 'RecPtr'.
     254             :  *
     255             :  * 'RecPtr' should point to the beginning of a valid WAL record.  Pointing at
     256             :  * the beginning of a page is also OK, if there is a new record right after
     257             :  * the page header, i.e. not a continuation.
     258             :  *
     259             :  * This does not make any attempt to read the WAL yet, and hence cannot fail.
     260             :  * If the starting address is not correct, the first call to XLogReadRecord()
     261             :  * will error out.
     262             :  */
     263             : void
     264        5012 : XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
     265             : {
     266             :     Assert(!XLogRecPtrIsInvalid(RecPtr));
     267             : 
     268        5012 :     ResetDecoder(state);
     269             : 
     270             :     /* Begin at the passed-in record pointer. */
     271        5012 :     state->EndRecPtr = RecPtr;
     272        5012 :     state->NextRecPtr = RecPtr;
     273        5012 :     state->ReadRecPtr = InvalidXLogRecPtr;
     274        5012 :     state->DecodeRecPtr = InvalidXLogRecPtr;
     275        5012 : }
     276             : 
     277             : /*
     278             :  * Release the last record that was returned by XLogNextRecord(), if any, to
     279             :  * free up space.  Returns the LSN past the end of the record.
     280             :  */
     281             : XLogRecPtr
     282      355932 : XLogReleasePreviousRecord(XLogReaderState *state)
     283             : {
     284             :     DecodedXLogRecord *record;
     285             :     XLogRecPtr      next_lsn;
     286             : 
     287      355932 :     if (!state->record)
     288      182978 :         return InvalidXLogRecPtr;
     289             : 
     290             :     /*
     291             :      * Remove it from the decoded record queue.  It must be the oldest item
     292             :      * decoded, decode_queue_head.
     293             :      */
     294      172954 :     record = state->record;
     295      172954 :     next_lsn = record->next_lsn;
     296             :     Assert(record == state->decode_queue_head);
     297      172954 :     state->record = NULL;
     298      172954 :     state->decode_queue_head = record->next;
     299             : 
     300             :     /* It might also be the newest item decoded, decode_queue_tail. */
     301      172954 :     if (state->decode_queue_tail == record)
     302      172954 :         state->decode_queue_tail = NULL;
     303             : 
     304             :     /* Release the space. */
     305      172954 :     if (unlikely(record->oversized))
     306             :     {
     307             :         /* It's not in the decode buffer, so free it to release space. */
     308           0 :         pfree(record);
     309             :     }
     310             :     else
     311             :     {
     312             :         /* It must be the head (oldest) record in the decode buffer. */
     313             :         Assert(state->decode_buffer_head == (char *) record);
     314             : 
     315             :         /*
     316             :          * We need to update head to point to the next record that is in the
     317             :          * decode buffer, if any, being careful to skip oversized ones
     318             :          * (they're not in the decode buffer).
     319             :          */
     320      172954 :         record = record->next;
     321      172954 :         while (unlikely(record && record->oversized))
     322           0 :             record = record->next;
     323             : 
     324      172954 :         if (record)
     325             :         {
     326             :             /* Adjust head to release space up to the next record. */
     327           0 :             state->decode_buffer_head = (char *) record;
     328             :         }
     329             :         else
     330             :         {
     331             :             /*
     332             :              * Otherwise we might as well just reset head and tail to the
     333             :              * start of the buffer space, because we're empty.  This means
     334             :              * we'll keep overwriting the same piece of memory if we're not
     335             :              * doing any prefetching.
     336             :              */
     337      172954 :             state->decode_buffer_head = state->decode_buffer;
     338      172954 :             state->decode_buffer_tail = state->decode_buffer;
     339             :         }
     340             :     }
     341             : 
     342      172954 :     return next_lsn;
     343             : }
     344             : 
     345             : /*
     346             :  * Attempt to read an XLOG record.
     347             :  *
     348             :  * XLogBeginRead() or XLogFindNextRecord() and then XLogReadAhead() must be
     349             :  * called before the first call to XLogNextRecord().  This functions returns
     350             :  * records and errors that were put into an internal queue by XLogReadAhead().
     351             :  *
     352             :  * On success, a record is returned.
     353             :  *
     354             :  * The returned record (or *errormsg) points to an internal buffer that's
     355             :  * valid until the next call to XLogNextRecord.
     356             :  */
     357             : DecodedXLogRecord *
     358      177966 : XLogNextRecord(XLogReaderState *state, char **errormsg)
     359             : {
     360             :     /* Release the last record returned by XLogNextRecord(). */
     361      177966 :     XLogReleasePreviousRecord(state);
     362             : 
     363      177966 :     if (state->decode_queue_head == NULL)
     364             :     {
     365           0 :         *errormsg = NULL;
     366           0 :         if (state->errormsg_deferred)
     367             :         {
     368           0 :             if (state->errormsg_buf[0] != '\0')
     369           0 :                 *errormsg = state->errormsg_buf;
     370           0 :             state->errormsg_deferred = false;
     371             :         }
     372             : 
     373             :         /*
     374             :          * state->EndRecPtr is expected to have been set by the last call to
     375             :          * XLogBeginRead() or XLogNextRecord(), and is the location of the
     376             :          * error.
     377             :          */
     378             :         Assert(!XLogRecPtrIsInvalid(state->EndRecPtr));
     379             : 
     380           0 :         return NULL;
     381             :     }
     382             : 
     383             :     /*
     384             :      * Record this as the most recent record returned, so that we'll release
     385             :      * it next time.  This also exposes it to the traditional
     386             :      * XLogRecXXX(xlogreader) macros, which work with the decoder rather than
     387             :      * the record for historical reasons.
     388             :      */
     389      177966 :     state->record = state->decode_queue_head;
     390             : 
     391             :     /*
     392             :      * Update the pointers to the beginning and one-past-the-end of this
     393             :      * record, again for the benefit of historical code that expected the
     394             :      * decoder to track this rather than accessing these fields of the record
     395             :      * itself.
     396             :      */
     397      177966 :     state->ReadRecPtr = state->record->lsn;
     398      177966 :     state->EndRecPtr = state->record->next_lsn;
     399             : 
     400      177966 :     *errormsg = NULL;
     401             : 
     402      177966 :     return state->record;
     403             : }
     404             : 
     405             : /*
     406             :  * Attempt to read an XLOG record.
     407             :  *
     408             :  * XLogBeginRead() or XLogFindNextRecord() must be called before the first call
     409             :  * to XLogReadRecord().
     410             :  *
     411             :  * If the page_read callback fails to read the requested data, NULL is
     412             :  * returned.  The callback is expected to have reported the error; errormsg
     413             :  * is set to NULL.
     414             :  *
     415             :  * If the reading fails for some other reason, NULL is also returned, and
     416             :  * *errormsg is set to a string with details of the failure.
     417             :  *
     418             :  * The returned pointer (or *errormsg) points to an internal buffer that's
     419             :  * valid until the next call to XLogReadRecord.
     420             :  */
     421             : XLogRecord *
     422      177966 : XLogReadRecord(XLogReaderState *state, char **errormsg)
     423             : {
     424             :     DecodedXLogRecord *decoded;
     425             : 
     426             :     /*
     427             :      * Release last returned record, if there is one.  We need to do this so
     428             :      * that we can check for empty decode queue accurately.
     429             :      */
     430      177966 :     XLogReleasePreviousRecord(state);
     431             : 
     432             :     /*
     433             :      * Call XLogReadAhead() in blocking mode to make sure there is something
     434             :      * in the queue, though we don't use the result.
     435             :      */
     436      177966 :     if (!XLogReaderHasQueuedRecordOrError(state))
     437      177966 :         XLogReadAhead(state, false /* nonblocking */ );
     438             : 
     439             :     /* Consume the head record or error. */
     440      177966 :     decoded = XLogNextRecord(state, errormsg);
     441      177966 :     if (decoded)
     442             :     {
     443             :         /*
     444             :          * This function returns a pointer to the record's header, not the
     445             :          * actual decoded record.  The caller will access the decoded record
     446             :          * through the XLogRecGetXXX() macros, which reach the decoded
     447             :          * recorded as xlogreader->record.
     448             :          */
     449             :         Assert(state->record == decoded);
     450      177966 :         return &decoded->header;
     451             :     }
     452             : 
     453           0 :     return NULL;
     454             : }
     455             : 
     456             : /*
     457             :  * Allocate space for a decoded record.  The only member of the returned
     458             :  * object that is initialized is the 'oversized' flag, indicating that the
     459             :  * decoded record wouldn't fit in the decode buffer and must eventually be
     460             :  * freed explicitly.
     461             :  *
     462             :  * The caller is responsible for adjusting decode_buffer_tail with the real
     463             :  * size after successfully decoding a record into this space.  This way, if
     464             :  * decoding fails, then there is nothing to undo unless the 'oversized' flag
     465             :  * was set and pfree() must be called.
     466             :  *
     467             :  * Return NULL if there is no space in the decode buffer and allow_oversized
     468             :  * is false, or if memory allocation fails for an oversized buffer.
     469             :  */
     470             : static DecodedXLogRecord *
     471      177966 : XLogReadRecordAlloc(XLogReaderState *state, size_t xl_tot_len, bool allow_oversized)
     472             : {
     473      177966 :     size_t      required_space = DecodeXLogRecordRequiredSpace(xl_tot_len);
     474      177966 :     DecodedXLogRecord *decoded = NULL;
     475             : 
     476             :     /* Allocate a circular decode buffer if we don't have one already. */
     477      177966 :     if (unlikely(state->decode_buffer == NULL))
     478             :     {
     479          78 :         if (state->decode_buffer_size == 0)
     480          78 :             state->decode_buffer_size = DEFAULT_DECODE_BUFFER_SIZE;
     481          78 :         state->decode_buffer = palloc(state->decode_buffer_size);
     482          78 :         state->decode_buffer_head = state->decode_buffer;
     483          78 :         state->decode_buffer_tail = state->decode_buffer;
     484          78 :         state->free_decode_buffer = true;
     485             :     }
     486             : 
     487             :     /* Try to allocate space in the circular decode buffer. */
     488      177966 :     if (state->decode_buffer_tail >= state->decode_buffer_head)
     489             :     {
     490             :         /* Empty, or tail is to the right of head. */
     491      177966 :         if (state->decode_buffer_tail + required_space <=
     492      177966 :             state->decode_buffer + state->decode_buffer_size)
     493             :         {
     494             :             /* There is space between tail and end. */
     495      177966 :             decoded = (DecodedXLogRecord *) state->decode_buffer_tail;
     496      177966 :             decoded->oversized = false;
     497      177966 :             return decoded;
     498             :         }
     499           0 :         else if (state->decode_buffer + required_space <
     500           0 :                  state->decode_buffer_head)
     501             :         {
     502             :             /* There is space between start and head. */
     503           0 :             decoded = (DecodedXLogRecord *) state->decode_buffer;
     504           0 :             decoded->oversized = false;
     505           0 :             return decoded;
     506             :         }
     507             :     }
     508             :     else
     509             :     {
     510             :         /* Tail is to the left of head. */
     511           0 :         if (state->decode_buffer_tail + required_space <
     512           0 :             state->decode_buffer_head)
     513             :         {
     514             :             /* There is space between tail and head. */
     515           0 :             decoded = (DecodedXLogRecord *) state->decode_buffer_tail;
     516           0 :             decoded->oversized = false;
     517           0 :             return decoded;
     518             :         }
     519             :     }
     520             : 
     521             :     /* Not enough space in the decode buffer.  Are we allowed to allocate? */
     522           0 :     if (allow_oversized)
     523             :     {
     524           0 :         decoded = palloc_extended(required_space, MCXT_ALLOC_NO_OOM);
     525           0 :         if (decoded == NULL)
     526           0 :             return NULL;
     527           0 :         decoded->oversized = true;
     528           0 :         return decoded;
     529             :     }
     530             : 
     531           0 :     return NULL;
     532             : }
     533             : 
     534             : static XLogPageReadResult
     535      177966 : XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking)
     536             : {
     537             :     XLogRecPtr  RecPtr;
     538             :     XLogRecord *record;
     539             :     XLogRecPtr  targetPagePtr;
     540             :     bool        randAccess;
     541             :     uint32      len,
     542             :                 total_len;
     543             :     uint32      targetRecOff;
     544             :     uint32      pageHeaderSize;
     545             :     bool        assembled;
     546             :     bool        gotheader;
     547             :     int         readOff;
     548             :     DecodedXLogRecord *decoded;
     549             :     char       *errormsg;       /* not used */
     550             : 
     551             :     /*
     552             :      * randAccess indicates whether to verify the previous-record pointer of
     553             :      * the record we're reading.  We only do this if we're reading
     554             :      * sequentially, which is what we initially assume.
     555             :      */
     556      177966 :     randAccess = false;
     557             : 
     558             :     /* reset error state */
     559      177966 :     state->errormsg_buf[0] = '\0';
     560      177966 :     decoded = NULL;
     561             : 
     562      177966 :     state->abortedRecPtr = InvalidXLogRecPtr;
     563      177966 :     state->missingContrecPtr = InvalidXLogRecPtr;
     564             : 
     565      177966 :     RecPtr = state->NextRecPtr;
     566             : 
     567      177966 :     if (state->DecodeRecPtr != InvalidXLogRecPtr)
     568             :     {
     569             :         /* read the record after the one we just read */
     570             : 
     571             :         /*
     572             :          * NextRecPtr is pointing to end+1 of the previous WAL record.  If
     573             :          * we're at a page boundary, no more records can fit on the current
     574             :          * page. We must skip over the page header, but we can't do that until
     575             :          * we've read in the page, since the header size is variable.
     576             :          */
     577             :     }
     578             :     else
     579             :     {
     580             :         /*
     581             :          * Caller supplied a position to start at.
     582             :          *
     583             :          * In this case, NextRecPtr should already be pointing either to a
     584             :          * valid record starting position or alternatively to the beginning of
     585             :          * a page. See the header comments for XLogBeginRead.
     586             :          */
     587             :         Assert(RecPtr % XLOG_BLCKSZ == 0 || XRecOffIsValid(RecPtr));
     588        5012 :         randAccess = true;
     589             :     }
     590             : 
     591      177966 : restart:
     592      177966 :     state->nonblocking = nonblocking;
     593      177966 :     state->currRecPtr = RecPtr;
     594      177966 :     assembled = false;
     595             : 
     596      177966 :     targetPagePtr = RecPtr - (RecPtr % XLOG_BLCKSZ);
     597      177966 :     targetRecOff = RecPtr % XLOG_BLCKSZ;
     598             : 
     599             :     /*
     600             :      * Read the page containing the record into state->readBuf. Request enough
     601             :      * byte to cover the whole record header, or at least the part of it that
     602             :      * fits on the same page.
     603             :      */
     604      177966 :     readOff = ReadPageInternal(state, targetPagePtr,
     605      177966 :                                Min(targetRecOff + SizeOfXLogRecord, XLOG_BLCKSZ));
     606      177966 :     if (readOff == XLREAD_WOULDBLOCK)
     607           0 :         return XLREAD_WOULDBLOCK;
     608      177966 :     else if (readOff < 0)
     609           0 :         goto err;
     610             : 
     611             :     /*
     612             :      * ReadPageInternal always returns at least the page header, so we can
     613             :      * examine it now.
     614             :      */
     615      177966 :     pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
     616      177966 :     if (targetRecOff == 0)
     617             :     {
     618             :         /*
     619             :          * At page start, so skip over page header.
     620             :          */
     621         180 :         RecPtr += pageHeaderSize;
     622         180 :         targetRecOff = pageHeaderSize;
     623             :     }
     624      177786 :     else if (targetRecOff < pageHeaderSize)
     625             :     {
     626           0 :         report_invalid_record(state, "invalid record offset at %X/%X",
     627           0 :                               LSN_FORMAT_ARGS(RecPtr));
     628           0 :         goto err;
     629             :     }
     630             : 
     631      177966 :     if ((((XLogPageHeader) state->readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD) &&
     632             :         targetRecOff == pageHeaderSize)
     633             :     {
     634           0 :         report_invalid_record(state, "contrecord is requested by %X/%X",
     635           0 :                               LSN_FORMAT_ARGS(RecPtr));
     636           0 :         goto err;
     637             :     }
     638             : 
     639             :     /* ReadPageInternal has verified the page header */
     640             :     Assert(pageHeaderSize <= readOff);
     641             : 
     642             :     /*
     643             :      * Read the record length.
     644             :      *
     645             :      * NB: Even though we use an XLogRecord pointer here, the whole record
     646             :      * header might not fit on this page. xl_tot_len is the first field of the
     647             :      * struct, so it must be on this page (the records are MAXALIGNed), but we
     648             :      * cannot access any other fields until we've verified that we got the
     649             :      * whole header.
     650             :      */
     651      177966 :     record = (XLogRecord *) (state->readBuf + RecPtr % XLOG_BLCKSZ);
     652      177966 :     total_len = record->xl_tot_len;
     653             : 
     654             :     /*
     655             :      * If the whole record header is on this page, validate it immediately.
     656             :      * Otherwise do just a basic sanity check on xl_tot_len, and validate the
     657             :      * rest of the header after reading it from the next page.  The xl_tot_len
     658             :      * check is necessary here to ensure that we enter the "Need to reassemble
     659             :      * record" code path below; otherwise we might fail to apply
     660             :      * ValidXLogRecordHeader at all.
     661             :      */
     662      177966 :     if (targetRecOff <= XLOG_BLCKSZ - SizeOfXLogRecord)
     663             :     {
     664      177638 :         if (!ValidXLogRecordHeader(state, RecPtr, state->DecodeRecPtr, record,
     665             :                                    randAccess))
     666           0 :             goto err;
     667      177638 :         gotheader = true;
     668             :     }
     669             :     else
     670             :     {
     671             :         /* XXX: more validation should be done here */
     672         328 :         if (total_len < SizeOfXLogRecord)
     673             :         {
     674           0 :             report_invalid_record(state,
     675             :                                   "invalid record length at %X/%X: wanted %u, got %u",
     676           0 :                                   LSN_FORMAT_ARGS(RecPtr),
     677             :                                   (uint32) SizeOfXLogRecord, total_len);
     678           0 :             goto err;
     679             :         }
     680         328 :         gotheader = false;
     681             :     }
     682             : 
     683             :     /*
     684             :      * Find space to decode this record.  Don't allow oversized allocation if
     685             :      * the caller requested nonblocking.  Otherwise, we *have* to try to
     686             :      * decode the record now because the caller has nothing else to do, so
     687             :      * allow an oversized record to be palloc'd if that turns out to be
     688             :      * necessary.
     689             :      */
     690      177966 :     decoded = XLogReadRecordAlloc(state,
     691             :                                   total_len,
     692      177966 :                                   !nonblocking /* allow_oversized */ );
     693      177966 :     if (decoded == NULL)
     694             :     {
     695             :         /*
     696             :          * There is no space in the decode buffer.  The caller should help
     697             :          * with that problem by consuming some records.
     698             :          */
     699           0 :         if (nonblocking)
     700           0 :             return XLREAD_WOULDBLOCK;
     701             : 
     702             :         /* We failed to allocate memory for an oversized record. */
     703           0 :         report_invalid_record(state,
     704             :                               "out of memory while trying to decode a record of length %u", total_len);
     705           0 :         goto err;
     706             :     }
     707             : 
     708      177966 :     len = XLOG_BLCKSZ - RecPtr % XLOG_BLCKSZ;
     709      177966 :     if (total_len > len)
     710             :     {
     711             :         /* Need to reassemble record */
     712             :         char       *contdata;
     713             :         XLogPageHeader pageHeader;
     714             :         char       *buffer;
     715             :         uint32      gotlen;
     716             : 
     717        8124 :         assembled = true;
     718             : 
     719             :         /*
     720             :          * Enlarge readRecordBuf as needed.
     721             :          */
     722        8124 :         if (total_len > state->readRecordBufSize &&
     723           0 :             !allocate_recordbuf(state, total_len))
     724             :         {
     725             :             /* We treat this as a "bogus data" condition */
     726           0 :             report_invalid_record(state, "record length %u at %X/%X too long",
     727           0 :                                   total_len, LSN_FORMAT_ARGS(RecPtr));
     728           0 :             goto err;
     729             :         }
     730             : 
     731             :         /* Copy the first fragment of the record from the first page. */
     732        8124 :         memcpy(state->readRecordBuf,
     733        8124 :                state->readBuf + RecPtr % XLOG_BLCKSZ, len);
     734        8124 :         buffer = state->readRecordBuf + len;
     735        8124 :         gotlen = len;
     736             : 
     737             :         do
     738             :         {
     739             :             /* Calculate pointer to beginning of next page */
     740        8136 :             targetPagePtr += XLOG_BLCKSZ;
     741             : 
     742             :             /* Wait for the next page to become available */
     743        8136 :             readOff = ReadPageInternal(state, targetPagePtr,
     744        8136 :                                        Min(total_len - gotlen + SizeOfXLogShortPHD,
     745             :                                            XLOG_BLCKSZ));
     746             : 
     747        8136 :             if (readOff == XLREAD_WOULDBLOCK)
     748           0 :                 return XLREAD_WOULDBLOCK;
     749        8136 :             else if (readOff < 0)
     750           0 :                 goto err;
     751             : 
     752             :             Assert(SizeOfXLogShortPHD <= readOff);
     753             : 
     754        8136 :             pageHeader = (XLogPageHeader) state->readBuf;
     755             : 
     756             :             /*
     757             :              * If we were expecting a continuation record and got an
     758             :              * "overwrite contrecord" flag, that means the continuation record
     759             :              * was overwritten with a different record.  Restart the read by
     760             :              * assuming the address to read is the location where we found
     761             :              * this flag; but keep track of the LSN of the record we were
     762             :              * reading, for later verification.
     763             :              */
     764        8136 :             if (pageHeader->xlp_info & XLP_FIRST_IS_OVERWRITE_CONTRECORD)
     765             :             {
     766           0 :                 state->overwrittenRecPtr = RecPtr;
     767           0 :                 RecPtr = targetPagePtr;
     768           0 :                 goto restart;
     769             :             }
     770             : 
     771             :             /* Check that the continuation on next page looks valid */
     772        8136 :             if (!(pageHeader->xlp_info & XLP_FIRST_IS_CONTRECORD))
     773             :             {
     774           0 :                 report_invalid_record(state,
     775             :                                       "there is no contrecord flag at %X/%X",
     776           0 :                                       LSN_FORMAT_ARGS(RecPtr));
     777           0 :                 goto err;
     778             :             }
     779             : 
     780             :             /*
     781             :              * Cross-check that xlp_rem_len agrees with how much of the record
     782             :              * we expect there to be left.
     783             :              */
     784        8136 :             if (pageHeader->xlp_rem_len == 0 ||
     785        8136 :                 total_len != (pageHeader->xlp_rem_len + gotlen))
     786             :             {
     787           0 :                 report_invalid_record(state,
     788             :                                       "invalid contrecord length %u (expected %lld) at %X/%X",
     789             :                                       pageHeader->xlp_rem_len,
     790           0 :                                       ((long long) total_len) - gotlen,
     791           0 :                                       LSN_FORMAT_ARGS(RecPtr));
     792           0 :                 goto err;
     793             :             }
     794             : 
     795             :             /* Append the continuation from this page to the buffer */
     796        8136 :             pageHeaderSize = XLogPageHeaderSize(pageHeader);
     797             : 
     798        8136 :             if (readOff < pageHeaderSize)
     799           0 :                 readOff = ReadPageInternal(state, targetPagePtr,
     800             :                                            pageHeaderSize);
     801             : 
     802             :             Assert(pageHeaderSize <= readOff);
     803             : 
     804        8136 :             contdata = (char *) state->readBuf + pageHeaderSize;
     805        8136 :             len = XLOG_BLCKSZ - pageHeaderSize;
     806        8136 :             if (pageHeader->xlp_rem_len < len)
     807        8124 :                 len = pageHeader->xlp_rem_len;
     808             : 
     809        8136 :             if (readOff < pageHeaderSize + len)
     810           0 :                 readOff = ReadPageInternal(state, targetPagePtr,
     811           0 :                                            pageHeaderSize + len);
     812             : 
     813        8136 :             memcpy(buffer, (char *) contdata, len);
     814        8136 :             buffer += len;
     815        8136 :             gotlen += len;
     816             : 
     817             :             /* If we just reassembled the record header, validate it. */
     818        8136 :             if (!gotheader)
     819             :             {
     820         328 :                 record = (XLogRecord *) state->readRecordBuf;
     821         328 :                 if (!ValidXLogRecordHeader(state, RecPtr, state->DecodeRecPtr,
     822             :                                            record, randAccess))
     823           0 :                     goto err;
     824         328 :                 gotheader = true;
     825             :             }
     826        8136 :         } while (gotlen < total_len);
     827             : 
     828             :         Assert(gotheader);
     829             : 
     830        8124 :         record = (XLogRecord *) state->readRecordBuf;
     831        8124 :         if (!ValidXLogRecord(state, record, RecPtr))
     832           0 :             goto err;
     833             : 
     834        8124 :         pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
     835        8124 :         state->DecodeRecPtr = RecPtr;
     836        8124 :         state->NextRecPtr = targetPagePtr + pageHeaderSize
     837        8124 :             + MAXALIGN(pageHeader->xlp_rem_len);
     838             :     }
     839             :     else
     840             :     {
     841             :         /* Wait for the record data to become available */
     842      169842 :         readOff = ReadPageInternal(state, targetPagePtr,
     843      169842 :                                    Min(targetRecOff + total_len, XLOG_BLCKSZ));
     844      169842 :         if (readOff == XLREAD_WOULDBLOCK)
     845           0 :             return XLREAD_WOULDBLOCK;
     846      169842 :         else if (readOff < 0)
     847           0 :             goto err;
     848             : 
     849             :         /* Record does not cross a page boundary */
     850      169842 :         if (!ValidXLogRecord(state, record, RecPtr))
     851           0 :             goto err;
     852             : 
     853      169842 :         state->NextRecPtr = RecPtr + MAXALIGN(total_len);
     854             : 
     855      169842 :         state->DecodeRecPtr = RecPtr;
     856             :     }
     857             : 
     858             :     /*
     859             :      * Special processing if it's an XLOG SWITCH record
     860             :      */
     861      177966 :     if (record->xl_rmid == RM_XLOG_ID &&
     862       11538 :         (record->xl_info & ~XLR_INFO_MASK) == XLOG_SWITCH)
     863             :     {
     864             :         /* Pretend it extends to end of segment */
     865          16 :         state->NextRecPtr += state->segcxt.ws_segsize - 1;
     866          16 :         state->NextRecPtr -= XLogSegmentOffset(state->NextRecPtr, state->segcxt.ws_segsize);
     867             :     }
     868             : 
     869      177966 :     if (DecodeXLogRecord(state, decoded, record, RecPtr, &errormsg))
     870             :     {
     871             :         /* Record the location of the next record. */
     872      177966 :         decoded->next_lsn = state->NextRecPtr;
     873             : 
     874             :         /*
     875             :          * If it's in the decode buffer, mark the decode buffer space as
     876             :          * occupied.
     877             :          */
     878      177966 :         if (!decoded->oversized)
     879             :         {
     880             :             /* The new decode buffer head must be MAXALIGNed. */
     881             :             Assert(decoded->size == MAXALIGN(decoded->size));
     882      177966 :             if ((char *) decoded == state->decode_buffer)
     883      177966 :                 state->decode_buffer_tail = state->decode_buffer + decoded->size;
     884             :             else
     885           0 :                 state->decode_buffer_tail += decoded->size;
     886             :         }
     887             : 
     888             :         /* Insert it into the queue of decoded records. */
     889             :         Assert(state->decode_queue_tail != decoded);
     890      177966 :         if (state->decode_queue_tail)
     891           0 :             state->decode_queue_tail->next = decoded;
     892      177966 :         state->decode_queue_tail = decoded;
     893      177966 :         if (!state->decode_queue_head)
     894      177966 :             state->decode_queue_head = decoded;
     895      177966 :         return XLREAD_SUCCESS;
     896             :     }
     897             :     else
     898           0 :         return XLREAD_FAIL;
     899             : 
     900           0 : err:
     901           0 :     if (assembled)
     902             :     {
     903             :         /*
     904             :          * We get here when a record that spans multiple pages needs to be
     905             :          * assembled, but something went wrong -- perhaps a contrecord piece
     906             :          * was lost.  If caller is WAL replay, it will know where the aborted
     907             :          * record was and where to direct followup WAL to be written, marking
     908             :          * the next piece with XLP_FIRST_IS_OVERWRITE_CONTRECORD, which will
     909             :          * in turn signal downstream WAL consumers that the broken WAL record
     910             :          * is to be ignored.
     911             :          */
     912           0 :         state->abortedRecPtr = RecPtr;
     913           0 :         state->missingContrecPtr = targetPagePtr;
     914             : 
     915             :         /*
     916             :          * If we got here without reporting an error, report one now so that
     917             :          * XLogPrefetcherReadRecord() doesn't bring us back a second time and
     918             :          * clobber the above state.  Otherwise, the existing error takes
     919             :          * precedence.
     920             :          */
     921           0 :         if (!state->errormsg_buf[0])
     922           0 :             report_invalid_record(state,
     923             :                                   "missing contrecord at %X/%X",
     924           0 :                                   LSN_FORMAT_ARGS(RecPtr));
     925             :     }
     926             : 
     927           0 :     if (decoded && decoded->oversized)
     928           0 :         pfree(decoded);
     929             : 
     930             :     /*
     931             :      * Invalidate the read state. We might read from a different source after
     932             :      * failure.
     933             :      */
     934           0 :     XLogReaderInvalReadState(state);
     935             : 
     936             :     /*
     937             :      * If an error was written to errmsg_buf, it'll be returned to the caller
     938             :      * of XLogReadRecord() after all successfully decoded records from the
     939             :      * read queue.
     940             :      */
     941             : 
     942           0 :     return XLREAD_FAIL;
     943             : }
     944             : 
     945             : /*
     946             :  * Try to decode the next available record, and return it.  The record will
     947             :  * also be returned to XLogNextRecord(), which must be called to 'consume'
     948             :  * each record.
     949             :  *
     950             :  * If nonblocking is true, may return NULL due to lack of data or WAL decoding
     951             :  * space.
     952             :  */
     953             : DecodedXLogRecord *
     954      177966 : XLogReadAhead(XLogReaderState *state, bool nonblocking)
     955             : {
     956             :     XLogPageReadResult result;
     957             : 
     958      177966 :     if (state->errormsg_deferred)
     959           0 :         return NULL;
     960             : 
     961      177966 :     result = XLogDecodeNextRecord(state, nonblocking);
     962      177966 :     if (result == XLREAD_SUCCESS)
     963             :     {
     964             :         Assert(state->decode_queue_tail != NULL);
     965      177966 :         return state->decode_queue_tail;
     966             :     }
     967             : 
     968           0 :     return NULL;
     969             : }
     970             : 
     971             : /*
     972             :  * Read a single xlog page including at least [pageptr, reqLen] of valid data
     973             :  * via the page_read() callback.
     974             :  *
     975             :  * Returns XLREAD_FAIL if the required page cannot be read for some
     976             :  * reason; errormsg_buf is set in that case (unless the error occurs in the
     977             :  * page_read callback).
     978             :  *
     979             :  * Returns XLREAD_WOULDBLOCK if the requested data can't be read without
     980             :  * waiting.  This can be returned only if the installed page_read callback
     981             :  * respects the state->nonblocking flag, and cannot read the requested data
     982             :  * immediately.
     983             :  *
     984             :  * We fetch the page from a reader-local cache if we know we have the required
     985             :  * data and if there hasn't been any error since caching the data.
     986             :  */
     987             : static int
     988      355944 : ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
     989             : {
     990             :     int         readLen;
     991             :     uint32      targetPageOff;
     992             :     XLogSegNo   targetSegNo;
     993             :     XLogPageHeader hdr;
     994             : 
     995             :     Assert((pageptr % XLOG_BLCKSZ) == 0);
     996             : 
     997      355944 :     XLByteToSeg(pageptr, targetSegNo, state->segcxt.ws_segsize);
     998      355944 :     targetPageOff = XLogSegmentOffset(pageptr, state->segcxt.ws_segsize);
     999             : 
    1000             :     /* check whether we have all the requested data already */
    1001      355944 :     if (targetSegNo == state->seg.ws_segno &&
    1002      355850 :         targetPageOff == state->segoff && reqLen <= state->readLen)
    1003      345154 :         return state->readLen;
    1004             : 
    1005             :     /*
    1006             :      * Invalidate contents of internal buffer before read attempt.  Just set
    1007             :      * the length to 0, rather than a full XLogReaderInvalReadState(), so we
    1008             :      * don't forget the segment we last successfully read.
    1009             :      */
    1010       10790 :     state->readLen = 0;
    1011             : 
    1012             :     /*
    1013             :      * Data is not in our buffer.
    1014             :      *
    1015             :      * Every time we actually read the segment, even if we looked at parts of
    1016             :      * it before, we need to do verification as the page_read callback might
    1017             :      * now be rereading data from a different source.
    1018             :      *
    1019             :      * Whenever switching to a new WAL segment, we read the first page of the
    1020             :      * file and validate its header, even if that's not where the target
    1021             :      * record is.  This is so that we can check the additional identification
    1022             :      * info that is present in the first page's "long" header.
    1023             :      */
    1024       10790 :     if (targetSegNo != state->seg.ws_segno && targetPageOff != 0)
    1025             :     {
    1026          40 :         XLogRecPtr  targetSegmentPtr = pageptr - targetPageOff;
    1027             : 
    1028          40 :         readLen = state->routine.page_read(state, targetSegmentPtr, XLOG_BLCKSZ,
    1029             :                                            state->currRecPtr,
    1030             :                                            state->readBuf);
    1031          40 :         if (readLen == XLREAD_WOULDBLOCK)
    1032           0 :             return XLREAD_WOULDBLOCK;
    1033          40 :         else if (readLen < 0)
    1034           0 :             goto err;
    1035             : 
    1036             :         /* we can be sure to have enough WAL available, we scrolled back */
    1037             :         Assert(readLen == XLOG_BLCKSZ);
    1038             : 
    1039          40 :         if (!XLogReaderValidatePageHeader(state, targetSegmentPtr,
    1040             :                                           state->readBuf))
    1041           0 :             goto err;
    1042             :     }
    1043             : 
    1044             :     /*
    1045             :      * First, read the requested data length, but at least a short page header
    1046             :      * so that we can validate it.
    1047             :      */
    1048       10790 :     readLen = state->routine.page_read(state, pageptr, Max(reqLen, SizeOfXLogShortPHD),
    1049             :                                        state->currRecPtr,
    1050             :                                        state->readBuf);
    1051       10790 :     if (readLen == XLREAD_WOULDBLOCK)
    1052           0 :         return XLREAD_WOULDBLOCK;
    1053       10790 :     else if (readLen < 0)
    1054           0 :         goto err;
    1055             : 
    1056             :     Assert(readLen <= XLOG_BLCKSZ);
    1057             : 
    1058             :     /* Do we have enough data to check the header length? */
    1059       10790 :     if (readLen <= SizeOfXLogShortPHD)
    1060           0 :         goto err;
    1061             : 
    1062             :     Assert(readLen >= reqLen);
    1063             : 
    1064       10790 :     hdr = (XLogPageHeader) state->readBuf;
    1065             : 
    1066             :     /* still not enough */
    1067       10790 :     if (readLen < XLogPageHeaderSize(hdr))
    1068             :     {
    1069           0 :         readLen = state->routine.page_read(state, pageptr, XLogPageHeaderSize(hdr),
    1070             :                                            state->currRecPtr,
    1071             :                                            state->readBuf);
    1072           0 :         if (readLen == XLREAD_WOULDBLOCK)
    1073           0 :             return XLREAD_WOULDBLOCK;
    1074           0 :         else if (readLen < 0)
    1075           0 :             goto err;
    1076             :     }
    1077             : 
    1078             :     /*
    1079             :      * Now that we know we have the full header, validate it.
    1080             :      */
    1081       10790 :     if (!XLogReaderValidatePageHeader(state, pageptr, (char *) hdr))
    1082           0 :         goto err;
    1083             : 
    1084             :     /* update read state information */
    1085       10790 :     state->seg.ws_segno = targetSegNo;
    1086       10790 :     state->segoff = targetPageOff;
    1087       10790 :     state->readLen = readLen;
    1088             : 
    1089       10790 :     return readLen;
    1090             : 
    1091           0 : err:
    1092           0 :     XLogReaderInvalReadState(state);
    1093             : 
    1094           0 :     return XLREAD_FAIL;
    1095             : }
    1096             : 
    1097             : /*
    1098             :  * Invalidate the xlogreader's read state to force a re-read.
    1099             :  */
    1100             : static void
    1101           0 : XLogReaderInvalReadState(XLogReaderState *state)
    1102             : {
    1103           0 :     state->seg.ws_segno = 0;
    1104           0 :     state->segoff = 0;
    1105           0 :     state->readLen = 0;
    1106           0 : }
    1107             : 
    1108             : /*
    1109             :  * Validate an XLOG record header.
    1110             :  *
    1111             :  * This is just a convenience subroutine to avoid duplicated code in
    1112             :  * XLogReadRecord.  It's not intended for use from anywhere else.
    1113             :  */
    1114             : static bool
    1115      177966 : ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr,
    1116             :                       XLogRecPtr PrevRecPtr, XLogRecord *record,
    1117             :                       bool randAccess)
    1118             : {
    1119      177966 :     if (record->xl_tot_len < SizeOfXLogRecord)
    1120             :     {
    1121           0 :         report_invalid_record(state,
    1122             :                               "invalid record length at %X/%X: wanted %u, got %u",
    1123           0 :                               LSN_FORMAT_ARGS(RecPtr),
    1124             :                               (uint32) SizeOfXLogRecord, record->xl_tot_len);
    1125           0 :         return false;
    1126             :     }
    1127      177966 :     if (!RmgrIdIsValid(record->xl_rmid))
    1128             :     {
    1129           0 :         report_invalid_record(state,
    1130             :                               "invalid resource manager ID %u at %X/%X",
    1131           0 :                               record->xl_rmid, LSN_FORMAT_ARGS(RecPtr));
    1132           0 :         return false;
    1133             :     }
    1134      177966 :     if (randAccess)
    1135             :     {
    1136             :         /*
    1137             :          * We can't exactly verify the prev-link, but surely it should be less
    1138             :          * than the record's own address.
    1139             :          */
    1140        5012 :         if (!(record->xl_prev < RecPtr))
    1141             :         {
    1142           0 :             report_invalid_record(state,
    1143             :                                   "record with incorrect prev-link %X/%X at %X/%X",
    1144           0 :                                   LSN_FORMAT_ARGS(record->xl_prev),
    1145           0 :                                   LSN_FORMAT_ARGS(RecPtr));
    1146           0 :             return false;
    1147             :         }
    1148             :     }
    1149             :     else
    1150             :     {
    1151             :         /*
    1152             :          * Record's prev-link should exactly match our previous location. This
    1153             :          * check guards against torn WAL pages where a stale but valid-looking
    1154             :          * WAL record starts on a sector boundary.
    1155             :          */
    1156      172954 :         if (record->xl_prev != PrevRecPtr)
    1157             :         {
    1158           0 :             report_invalid_record(state,
    1159             :                                   "record with incorrect prev-link %X/%X at %X/%X",
    1160           0 :                                   LSN_FORMAT_ARGS(record->xl_prev),
    1161           0 :                                   LSN_FORMAT_ARGS(RecPtr));
    1162           0 :             return false;
    1163             :         }
    1164             :     }
    1165             : 
    1166      177966 :     return true;
    1167             : }
    1168             : 
    1169             : 
    1170             : /*
    1171             :  * CRC-check an XLOG record.  We do not believe the contents of an XLOG
    1172             :  * record (other than to the minimal extent of computing the amount of
    1173             :  * data to read in) until we've checked the CRCs.
    1174             :  *
    1175             :  * We assume all of the record (that is, xl_tot_len bytes) has been read
    1176             :  * into memory at *record.  Also, ValidXLogRecordHeader() has accepted the
    1177             :  * record's header, which means in particular that xl_tot_len is at least
    1178             :  * SizeOfXLogRecord.
    1179             :  */
    1180             : static bool
    1181      177966 : ValidXLogRecord(XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
    1182             : {
    1183             :     pg_crc32c   crc;
    1184             : 
    1185             :     /* Calculate the CRC */
    1186      177966 :     INIT_CRC32C(crc);
    1187      177966 :     COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
    1188             :     /* include the record header last */
    1189      177966 :     COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
    1190      177966 :     FIN_CRC32C(crc);
    1191             : 
    1192      177966 :     if (!EQ_CRC32C(record->xl_crc, crc))
    1193             :     {
    1194           0 :         report_invalid_record(state,
    1195             :                               "incorrect resource manager data checksum in record at %X/%X",
    1196           0 :                               LSN_FORMAT_ARGS(recptr));
    1197           0 :         return false;
    1198             :     }
    1199             : 
    1200      177966 :     return true;
    1201             : }
    1202             : 
    1203             : /*
    1204             :  * Validate a page header.
    1205             :  *
    1206             :  * Check if 'phdr' is valid as the header of the XLog page at position
    1207             :  * 'recptr'.
    1208             :  */
    1209             : bool
    1210       10830 : XLogReaderValidatePageHeader(XLogReaderState *state, XLogRecPtr recptr,
    1211             :                              char *phdr)
    1212             : {
    1213             :     XLogRecPtr  recaddr;
    1214             :     XLogSegNo   segno;
    1215             :     int32       offset;
    1216       10830 :     XLogPageHeader hdr = (XLogPageHeader) phdr;
    1217             : 
    1218             :     Assert((recptr % XLOG_BLCKSZ) == 0);
    1219             : 
    1220       10830 :     XLByteToSeg(recptr, segno, state->segcxt.ws_segsize);
    1221       10830 :     offset = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
    1222             : 
    1223       10830 :     XLogSegNoOffsetToRecPtr(segno, offset, state->segcxt.ws_segsize, recaddr);
    1224             : 
    1225       10830 :     if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
    1226             :     {
    1227             :         char        fname[MAXFNAMELEN];
    1228             : 
    1229           0 :         XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
    1230             : 
    1231           0 :         report_invalid_record(state,
    1232             :                               "invalid magic number %04X in WAL segment %s, offset %u",
    1233           0 :                               hdr->xlp_magic,
    1234             :                               fname,
    1235             :                               offset);
    1236           0 :         return false;
    1237             :     }
    1238             : 
    1239       10830 :     if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
    1240             :     {
    1241             :         char        fname[MAXFNAMELEN];
    1242             : 
    1243           0 :         XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
    1244             : 
    1245           0 :         report_invalid_record(state,
    1246             :                               "invalid info bits %04X in WAL segment %s, offset %u",
    1247           0 :                               hdr->xlp_info,
    1248             :                               fname,
    1249             :                               offset);
    1250           0 :         return false;
    1251             :     }
    1252             : 
    1253       10830 :     if (hdr->xlp_info & XLP_LONG_HEADER)
    1254             :     {
    1255         102 :         XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
    1256             : 
    1257         102 :         if (state->system_identifier &&
    1258           0 :             longhdr->xlp_sysid != state->system_identifier)
    1259             :         {
    1260           0 :             report_invalid_record(state,
    1261             :                                   "WAL file is from different database system: WAL file database system identifier is %llu, pg_control database system identifier is %llu",
    1262           0 :                                   (unsigned long long) longhdr->xlp_sysid,
    1263           0 :                                   (unsigned long long) state->system_identifier);
    1264           0 :             return false;
    1265             :         }
    1266         102 :         else if (longhdr->xlp_seg_size != state->segcxt.ws_segsize)
    1267             :         {
    1268           0 :             report_invalid_record(state,
    1269             :                                   "WAL file is from different database system: incorrect segment size in page header");
    1270           0 :             return false;
    1271             :         }
    1272         102 :         else if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
    1273             :         {
    1274           0 :             report_invalid_record(state,
    1275             :                                   "WAL file is from different database system: incorrect XLOG_BLCKSZ in page header");
    1276           0 :             return false;
    1277             :         }
    1278             :     }
    1279       10728 :     else if (offset == 0)
    1280             :     {
    1281             :         char        fname[MAXFNAMELEN];
    1282             : 
    1283           0 :         XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
    1284             : 
    1285             :         /* hmm, first page of file doesn't have a long header? */
    1286           0 :         report_invalid_record(state,
    1287             :                               "invalid info bits %04X in WAL segment %s, offset %u",
    1288           0 :                               hdr->xlp_info,
    1289             :                               fname,
    1290             :                               offset);
    1291           0 :         return false;
    1292             :     }
    1293             : 
    1294             :     /*
    1295             :      * Check that the address on the page agrees with what we expected. This
    1296             :      * check typically fails when an old WAL segment is recycled, and hasn't
    1297             :      * yet been overwritten with new data yet.
    1298             :      */
    1299       10830 :     if (hdr->xlp_pageaddr != recaddr)
    1300             :     {
    1301             :         char        fname[MAXFNAMELEN];
    1302             : 
    1303           0 :         XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
    1304             : 
    1305           0 :         report_invalid_record(state,
    1306             :                               "unexpected pageaddr %X/%X in WAL segment %s, offset %u",
    1307           0 :                               LSN_FORMAT_ARGS(hdr->xlp_pageaddr),
    1308             :                               fname,
    1309             :                               offset);
    1310           0 :         return false;
    1311             :     }
    1312             : 
    1313             :     /*
    1314             :      * Since child timelines are always assigned a TLI greater than their
    1315             :      * immediate parent's TLI, we should never see TLI go backwards across
    1316             :      * successive pages of a consistent WAL sequence.
    1317             :      *
    1318             :      * Sometimes we re-read a segment that's already been (partially) read. So
    1319             :      * we only verify TLIs for pages that are later than the last remembered
    1320             :      * LSN.
    1321             :      */
    1322       10830 :     if (recptr > state->latestPagePtr)
    1323             :     {
    1324        8434 :         if (hdr->xlp_tli < state->latestPageTLI)
    1325             :         {
    1326             :             char        fname[MAXFNAMELEN];
    1327             : 
    1328           0 :             XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
    1329             : 
    1330           0 :             report_invalid_record(state,
    1331             :                                   "out-of-sequence timeline ID %u (after %u) in WAL segment %s, offset %u",
    1332             :                                   hdr->xlp_tli,
    1333             :                                   state->latestPageTLI,
    1334             :                                   fname,
    1335             :                                   offset);
    1336           0 :             return false;
    1337             :         }
    1338             :     }
    1339       10830 :     state->latestPagePtr = recptr;
    1340       10830 :     state->latestPageTLI = hdr->xlp_tli;
    1341             : 
    1342       10830 :     return true;
    1343             : }
    1344             : 
    1345             : /*
    1346             :  * Forget about an error produced by XLogReaderValidatePageHeader().
    1347             :  */
    1348             : void
    1349           0 : XLogReaderResetError(XLogReaderState *state)
    1350             : {
    1351           0 :     state->errormsg_buf[0] = '\0';
    1352           0 :     state->errormsg_deferred = false;
    1353           0 : }
    1354             : 
    1355             : /*
    1356             :  * Find the first record with an lsn >= RecPtr.
    1357             :  *
    1358             :  * This is different from XLogBeginRead() in that RecPtr doesn't need to point
    1359             :  * to a valid record boundary.  Useful for checking whether RecPtr is a valid
    1360             :  * xlog address for reading, and to find the first valid address after some
    1361             :  * address when dumping records for debugging purposes.
    1362             :  *
    1363             :  * This positions the reader, like XLogBeginRead(), so that the next call to
    1364             :  * XLogReadRecord() will read the next valid record.
    1365             :  */
    1366             : XLogRecPtr
    1367           0 : XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr)
    1368             : {
    1369             :     XLogRecPtr  tmpRecPtr;
    1370           0 :     XLogRecPtr  found = InvalidXLogRecPtr;
    1371             :     XLogPageHeader header;
    1372             :     char       *errormsg;
    1373             : 
    1374             :     Assert(!XLogRecPtrIsInvalid(RecPtr));
    1375             : 
    1376             :     /* Make sure ReadPageInternal() can't return XLREAD_WOULDBLOCK. */
    1377           0 :     state->nonblocking = false;
    1378             : 
    1379             :     /*
    1380             :      * skip over potential continuation data, keeping in mind that it may span
    1381             :      * multiple pages
    1382             :      */
    1383           0 :     tmpRecPtr = RecPtr;
    1384             :     while (true)
    1385           0 :     {
    1386             :         XLogRecPtr  targetPagePtr;
    1387             :         int         targetRecOff;
    1388             :         uint32      pageHeaderSize;
    1389             :         int         readLen;
    1390             : 
    1391             :         /*
    1392             :          * Compute targetRecOff. It should typically be equal or greater than
    1393             :          * short page-header since a valid record can't start anywhere before
    1394             :          * that, except when caller has explicitly specified the offset that
    1395             :          * falls somewhere there or when we are skipping multi-page
    1396             :          * continuation record. It doesn't matter though because
    1397             :          * ReadPageInternal() is prepared to handle that and will read at
    1398             :          * least short page-header worth of data
    1399             :          */
    1400           0 :         targetRecOff = tmpRecPtr % XLOG_BLCKSZ;
    1401             : 
    1402             :         /* scroll back to page boundary */
    1403           0 :         targetPagePtr = tmpRecPtr - targetRecOff;
    1404             : 
    1405             :         /* Read the page containing the record */
    1406           0 :         readLen = ReadPageInternal(state, targetPagePtr, targetRecOff);
    1407           0 :         if (readLen < 0)
    1408           0 :             goto err;
    1409             : 
    1410           0 :         header = (XLogPageHeader) state->readBuf;
    1411             : 
    1412           0 :         pageHeaderSize = XLogPageHeaderSize(header);
    1413             : 
    1414             :         /* make sure we have enough data for the page header */
    1415           0 :         readLen = ReadPageInternal(state, targetPagePtr, pageHeaderSize);
    1416           0 :         if (readLen < 0)
    1417           0 :             goto err;
    1418             : 
    1419             :         /* skip over potential continuation data */
    1420           0 :         if (header->xlp_info & XLP_FIRST_IS_CONTRECORD)
    1421             :         {
    1422             :             /*
    1423             :              * If the length of the remaining continuation data is more than
    1424             :              * what can fit in this page, the continuation record crosses over
    1425             :              * this page. Read the next page and try again. xlp_rem_len in the
    1426             :              * next page header will contain the remaining length of the
    1427             :              * continuation data
    1428             :              *
    1429             :              * Note that record headers are MAXALIGN'ed
    1430             :              */
    1431           0 :             if (MAXALIGN(header->xlp_rem_len) >= (XLOG_BLCKSZ - pageHeaderSize))
    1432           0 :                 tmpRecPtr = targetPagePtr + XLOG_BLCKSZ;
    1433             :             else
    1434             :             {
    1435             :                 /*
    1436             :                  * The previous continuation record ends in this page. Set
    1437             :                  * tmpRecPtr to point to the first valid record
    1438             :                  */
    1439           0 :                 tmpRecPtr = targetPagePtr + pageHeaderSize
    1440           0 :                     + MAXALIGN(header->xlp_rem_len);
    1441           0 :                 break;
    1442             :             }
    1443             :         }
    1444             :         else
    1445             :         {
    1446           0 :             tmpRecPtr = targetPagePtr + pageHeaderSize;
    1447           0 :             break;
    1448             :         }
    1449             :     }
    1450             : 
    1451             :     /*
    1452             :      * we know now that tmpRecPtr is an address pointing to a valid XLogRecord
    1453             :      * because either we're at the first record after the beginning of a page
    1454             :      * or we just jumped over the remaining data of a continuation.
    1455             :      */
    1456           0 :     XLogBeginRead(state, tmpRecPtr);
    1457           0 :     while (XLogReadRecord(state, &errormsg) != NULL)
    1458             :     {
    1459             :         /* past the record we've found, break out */
    1460           0 :         if (RecPtr <= state->ReadRecPtr)
    1461             :         {
    1462             :             /* Rewind the reader to the beginning of the last record. */
    1463           0 :             found = state->ReadRecPtr;
    1464           0 :             XLogBeginRead(state, found);
    1465           0 :             return found;
    1466             :         }
    1467             :     }
    1468             : 
    1469           0 : err:
    1470           0 :     XLogReaderInvalReadState(state);
    1471             : 
    1472           0 :     return InvalidXLogRecPtr;
    1473             : }
    1474             : 
    1475             : /*
    1476             :  * Helper function to ease writing of XLogRoutine->page_read callbacks.
    1477             :  * If this function is used, caller must supply a segment_open callback in
    1478             :  * 'state', as that is used here.
    1479             :  *
    1480             :  * Read 'count' bytes into 'buf', starting at location 'startptr', from WAL
    1481             :  * fetched from timeline 'tli'.
    1482             :  *
    1483             :  * Returns true if succeeded, false if an error occurs, in which case
    1484             :  * 'errinfo' receives error details.
    1485             :  *
    1486             :  * XXX probably this should be improved to suck data directly from the
    1487             :  * WAL buffers when possible.
    1488             :  */
    1489             : bool
    1490           0 : WALRead(XLogReaderState *state,
    1491             :         char *buf, XLogRecPtr startptr, Size count, TimeLineID tli,
    1492             :         WALReadError *errinfo)
    1493             : {
    1494             :     char       *p;
    1495             :     XLogRecPtr  recptr;
    1496             :     Size        nbytes;
    1497             : 
    1498           0 :     p = buf;
    1499           0 :     recptr = startptr;
    1500           0 :     nbytes = count;
    1501             : 
    1502           0 :     while (nbytes > 0)
    1503             :     {
    1504             :         uint32      startoff;
    1505             :         int         segbytes;
    1506             :         int         readbytes;
    1507             : 
    1508           0 :         startoff = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
    1509             : 
    1510             :         /*
    1511             :          * If the data we want is not in a segment we have open, close what we
    1512             :          * have (if anything) and open the next one, using the caller's
    1513             :          * provided openSegment callback.
    1514             :          */
    1515           0 :         if (state->seg.ws_file < 0 ||
    1516           0 :             !XLByteInSeg(recptr, state->seg.ws_segno, state->segcxt.ws_segsize) ||
    1517           0 :             tli != state->seg.ws_tli)
    1518             :         {
    1519             :             XLogSegNo   nextSegNo;
    1520             : 
    1521           0 :             if (state->seg.ws_file >= 0)
    1522           0 :                 state->routine.segment_close(state);
    1523             : 
    1524           0 :             XLByteToSeg(recptr, nextSegNo, state->segcxt.ws_segsize);
    1525           0 :             state->routine.segment_open(state, nextSegNo, &tli);
    1526             : 
    1527             :             /* This shouldn't happen -- indicates a bug in segment_open */
    1528             :             Assert(state->seg.ws_file >= 0);
    1529             : 
    1530             :             /* Update the current segment info. */
    1531           0 :             state->seg.ws_tli = tli;
    1532           0 :             state->seg.ws_segno = nextSegNo;
    1533             :         }
    1534             : 
    1535             :         /* How many bytes are within this segment? */
    1536           0 :         if (nbytes > (state->segcxt.ws_segsize - startoff))
    1537           0 :             segbytes = state->segcxt.ws_segsize - startoff;
    1538             :         else
    1539           0 :             segbytes = nbytes;
    1540             : 
    1541             : #ifndef FRONTEND
    1542             :         pgstat_report_wait_start(WAIT_EVENT_WAL_READ);
    1543             : #endif
    1544             : 
    1545             :         /* Reset errno first; eases reporting non-errno-affecting errors */
    1546           0 :         errno = 0;
    1547           0 :         readbytes = pg_pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
    1548             : 
    1549             : #ifndef FRONTEND
    1550             :         pgstat_report_wait_end();
    1551             : #endif
    1552             : 
    1553           0 :         if (readbytes <= 0)
    1554             :         {
    1555           0 :             errinfo->wre_errno = errno;
    1556           0 :             errinfo->wre_req = segbytes;
    1557           0 :             errinfo->wre_read = readbytes;
    1558           0 :             errinfo->wre_off = startoff;
    1559           0 :             errinfo->wre_seg = state->seg;
    1560           0 :             return false;
    1561             :         }
    1562             : 
    1563             :         /* Update state for read */
    1564           0 :         recptr += readbytes;
    1565           0 :         nbytes -= readbytes;
    1566           0 :         p += readbytes;
    1567             :     }
    1568             : 
    1569           0 :     return true;
    1570             : }
    1571             : 
    1572             : /* ----------------------------------------
    1573             :  * Functions for decoding the data and block references in a record.
    1574             :  * ----------------------------------------
    1575             :  */
    1576             : 
    1577             : /*
    1578             :  * Private function to reset the state, forgetting all decoded records, if we
    1579             :  * are asked to move to a new read position.
    1580             :  */
    1581             : static void
    1582        5012 : ResetDecoder(XLogReaderState *state)
    1583             : {
    1584             :     DecodedXLogRecord *r;
    1585             : 
    1586             :     /* Reset the decoded record queue, freeing any oversized records. */
    1587        9946 :     while ((r = state->decode_queue_head) != NULL)
    1588             :     {
    1589        4934 :         state->decode_queue_head = r->next;
    1590        4934 :         if (r->oversized)
    1591           0 :             pfree(r);
    1592             :     }
    1593        5012 :     state->decode_queue_tail = NULL;
    1594        5012 :     state->decode_queue_head = NULL;
    1595        5012 :     state->record = NULL;
    1596             : 
    1597             :     /* Reset the decode buffer to empty. */
    1598        5012 :     state->decode_buffer_tail = state->decode_buffer;
    1599        5012 :     state->decode_buffer_head = state->decode_buffer;
    1600             : 
    1601             :     /* Clear error state. */
    1602        5012 :     state->errormsg_buf[0] = '\0';
    1603        5012 :     state->errormsg_deferred = false;
    1604        5012 : }
    1605             : 
    1606             : /*
    1607             :  * Compute the maximum possible amount of padding that could be required to
    1608             :  * decode a record, given xl_tot_len from the record's header.  This is the
    1609             :  * amount of output buffer space that we need to decode a record, though we
    1610             :  * might not finish up using it all.
    1611             :  *
    1612             :  * This computation is pessimistic and assumes the maximum possible number of
    1613             :  * blocks, due to lack of better information.
    1614             :  */
    1615             : size_t
    1616      177966 : DecodeXLogRecordRequiredSpace(size_t xl_tot_len)
    1617             : {
    1618      177966 :     size_t      size = 0;
    1619             : 
    1620             :     /* Account for the fixed size part of the decoded record struct. */
    1621      177966 :     size += offsetof(DecodedXLogRecord, blocks[0]);
    1622             :     /* Account for the flexible blocks array of maximum possible size. */
    1623      177966 :     size += sizeof(DecodedBkpBlock) * (XLR_MAX_BLOCK_ID + 1);
    1624             :     /* Account for all the raw main and block data. */
    1625      177966 :     size += xl_tot_len;
    1626             :     /* We might insert padding before main_data. */
    1627      177966 :     size += (MAXIMUM_ALIGNOF - 1);
    1628             :     /* We might insert padding before each block's data. */
    1629      177966 :     size += (MAXIMUM_ALIGNOF - 1) * (XLR_MAX_BLOCK_ID + 1);
    1630             :     /* We might insert padding at the end. */
    1631      177966 :     size += (MAXIMUM_ALIGNOF - 1);
    1632             : 
    1633      177966 :     return size;
    1634             : }
    1635             : 
    1636             : /*
    1637             :  * Decode a record.  "decoded" must point to a MAXALIGNed memory area that has
    1638             :  * space for at least DecodeXLogRecordRequiredSpace(record) bytes.  On
    1639             :  * success, decoded->size contains the actual space occupied by the decoded
    1640             :  * record, which may turn out to be less.
    1641             :  *
    1642             :  * Only decoded->oversized member must be initialized already, and will not be
    1643             :  * modified.  Other members will be initialized as required.
    1644             :  *
    1645             :  * On error, a human-readable error message is returned in *errormsg, and
    1646             :  * the return value is false.
    1647             :  */
    1648             : bool
    1649      177966 : DecodeXLogRecord(XLogReaderState *state,
    1650             :                  DecodedXLogRecord *decoded,
    1651             :                  XLogRecord *record,
    1652             :                  XLogRecPtr lsn,
    1653             :                  char **errormsg)
    1654             : {
    1655             :     /*
    1656             :      * read next _size bytes from record buffer, but check for overrun first.
    1657             :      */
    1658             : #define COPY_HEADER_FIELD(_dst, _size)          \
    1659             :     do {                                        \
    1660             :         if (remaining < _size)                   \
    1661             :             goto shortdata_err;                 \
    1662             :         memcpy(_dst, ptr, _size);               \
    1663             :         ptr += _size;                           \
    1664             :         remaining -= _size;                     \
    1665             :     } while(0)
    1666             : 
    1667             :     char       *ptr;
    1668             :     char       *out;
    1669             :     uint32      remaining;
    1670             :     uint32      datatotal;
    1671      177966 :     RelFileLocator *rlocator = NULL;
    1672             :     uint8       block_id;
    1673             : 
    1674      177966 :     decoded->header = *record;
    1675      177966 :     decoded->lsn = lsn;
    1676      177966 :     decoded->next = NULL;
    1677      177966 :     decoded->record_origin = InvalidRepOriginId;
    1678      177966 :     decoded->toplevel_xid = InvalidTransactionId;
    1679      177966 :     decoded->main_data = NULL;
    1680      177966 :     decoded->main_data_len = 0;
    1681      177966 :     decoded->max_block_id = -1;
    1682      177966 :     ptr = (char *) record;
    1683      177966 :     ptr += SizeOfXLogRecord;
    1684      177966 :     remaining = record->xl_tot_len - SizeOfXLogRecord;
    1685             : 
    1686             :     /* Decode the headers */
    1687      177966 :     datatotal = 0;
    1688      352706 :     while (remaining > datatotal)
    1689             :     {
    1690      341316 :         COPY_HEADER_FIELD(&block_id, sizeof(uint8));
    1691             : 
    1692      341316 :         if (block_id == XLR_BLOCK_ID_DATA_SHORT)
    1693             :         {
    1694             :             /* XLogRecordDataHeaderShort */
    1695             :             uint8       main_data_len;
    1696             : 
    1697      166544 :             COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
    1698             : 
    1699      166544 :             decoded->main_data_len = main_data_len;
    1700      166544 :             datatotal += main_data_len;
    1701      166544 :             break;              /* by convention, the main data fragment is
    1702             :                                  * always last */
    1703             :         }
    1704      174772 :         else if (block_id == XLR_BLOCK_ID_DATA_LONG)
    1705             :         {
    1706             :             /* XLogRecordDataHeaderLong */
    1707             :             uint32      main_data_len;
    1708             : 
    1709          32 :             COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
    1710          32 :             decoded->main_data_len = main_data_len;
    1711          32 :             datatotal += main_data_len;
    1712          32 :             break;              /* by convention, the main data fragment is
    1713             :                                  * always last */
    1714             :         }
    1715      174740 :         else if (block_id == XLR_BLOCK_ID_ORIGIN)
    1716             :         {
    1717           0 :             COPY_HEADER_FIELD(&decoded->record_origin, sizeof(RepOriginId));
    1718             :         }
    1719      174740 :         else if (block_id == XLR_BLOCK_ID_TOPLEVEL_XID)
    1720             :         {
    1721           0 :             COPY_HEADER_FIELD(&decoded->toplevel_xid, sizeof(TransactionId));
    1722             :         }
    1723      174740 :         else if (block_id <= XLR_MAX_BLOCK_ID)
    1724             :         {
    1725             :             /* XLogRecordBlockHeader */
    1726             :             DecodedBkpBlock *blk;
    1727             :             uint8       fork_flags;
    1728             : 
    1729             :             /* mark any intervening block IDs as not in use */
    1730      174740 :             for (int i = decoded->max_block_id + 1; i < block_id; ++i)
    1731           0 :                 decoded->blocks[i].in_use = false;
    1732             : 
    1733      174740 :             if (block_id <= decoded->max_block_id)
    1734             :             {
    1735           0 :                 report_invalid_record(state,
    1736             :                                       "out-of-order block_id %u at %X/%X",
    1737             :                                       block_id,
    1738           0 :                                       LSN_FORMAT_ARGS(state->ReadRecPtr));
    1739           0 :                 goto err;
    1740             :             }
    1741      174740 :             decoded->max_block_id = block_id;
    1742             : 
    1743      174740 :             blk = &decoded->blocks[block_id];
    1744      174740 :             blk->in_use = true;
    1745      174740 :             blk->apply_image = false;
    1746             : 
    1747      174740 :             COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
    1748      174740 :             blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
    1749      174740 :             blk->flags = fork_flags;
    1750      174740 :             blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
    1751      174740 :             blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
    1752             : 
    1753      174740 :             blk->prefetch_buffer = InvalidBuffer;
    1754             : 
    1755      174740 :             COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
    1756             :             /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
    1757      174740 :             if (blk->has_data && blk->data_len == 0)
    1758             :             {
    1759           0 :                 report_invalid_record(state,
    1760             :                                       "BKPBLOCK_HAS_DATA set, but no data included at %X/%X",
    1761           0 :                                       LSN_FORMAT_ARGS(state->ReadRecPtr));
    1762           0 :                 goto err;
    1763             :             }
    1764      174740 :             if (!blk->has_data && blk->data_len != 0)
    1765             :             {
    1766           0 :                 report_invalid_record(state,
    1767             :                                       "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%X",
    1768           0 :                                       (unsigned int) blk->data_len,
    1769           0 :                                       LSN_FORMAT_ARGS(state->ReadRecPtr));
    1770           0 :                 goto err;
    1771             :             }
    1772      174740 :             datatotal += blk->data_len;
    1773             : 
    1774      174740 :             if (blk->has_image)
    1775             :             {
    1776       11472 :                 COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
    1777       11472 :                 COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
    1778       11472 :                 COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
    1779             : 
    1780       11472 :                 blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
    1781             : 
    1782       11472 :                 if (BKPIMAGE_COMPRESSED(blk->bimg_info))
    1783             :                 {
    1784           0 :                     if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
    1785           0 :                         COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
    1786             :                     else
    1787           0 :                         blk->hole_length = 0;
    1788             :                 }
    1789             :                 else
    1790       11472 :                     blk->hole_length = BLCKSZ - blk->bimg_len;
    1791       11472 :                 datatotal += blk->bimg_len;
    1792             : 
    1793             :                 /*
    1794             :                  * cross-check that hole_offset > 0, hole_length > 0 and
    1795             :                  * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
    1796             :                  */
    1797       11472 :                 if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
    1798       11340 :                     (blk->hole_offset == 0 ||
    1799       11340 :                      blk->hole_length == 0 ||
    1800       11340 :                      blk->bimg_len == BLCKSZ))
    1801             :                 {
    1802           0 :                     report_invalid_record(state,
    1803             :                                           "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%X",
    1804           0 :                                           (unsigned int) blk->hole_offset,
    1805           0 :                                           (unsigned int) blk->hole_length,
    1806           0 :                                           (unsigned int) blk->bimg_len,
    1807           0 :                                           LSN_FORMAT_ARGS(state->ReadRecPtr));
    1808           0 :                     goto err;
    1809             :                 }
    1810             : 
    1811             :                 /*
    1812             :                  * cross-check that hole_offset == 0 and hole_length == 0 if
    1813             :                  * the HAS_HOLE flag is not set.
    1814             :                  */
    1815       11472 :                 if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
    1816         132 :                     (blk->hole_offset != 0 || blk->hole_length != 0))
    1817             :                 {
    1818           0 :                     report_invalid_record(state,
    1819             :                                           "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%X",
    1820           0 :                                           (unsigned int) blk->hole_offset,
    1821           0 :                                           (unsigned int) blk->hole_length,
    1822           0 :                                           LSN_FORMAT_ARGS(state->ReadRecPtr));
    1823           0 :                     goto err;
    1824             :                 }
    1825             : 
    1826             :                 /*
    1827             :                  * Cross-check that bimg_len < BLCKSZ if it is compressed.
    1828             :                  */
    1829       11472 :                 if (BKPIMAGE_COMPRESSED(blk->bimg_info) &&
    1830           0 :                     blk->bimg_len == BLCKSZ)
    1831             :                 {
    1832           0 :                     report_invalid_record(state,
    1833             :                                           "BKPIMAGE_COMPRESSED set, but block image length %u at %X/%X",
    1834           0 :                                           (unsigned int) blk->bimg_len,
    1835           0 :                                           LSN_FORMAT_ARGS(state->ReadRecPtr));
    1836           0 :                     goto err;
    1837             :                 }
    1838             : 
    1839             :                 /*
    1840             :                  * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE is
    1841             :                  * set nor COMPRESSED().
    1842             :                  */
    1843       11472 :                 if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
    1844         132 :                     !BKPIMAGE_COMPRESSED(blk->bimg_info) &&
    1845         132 :                     blk->bimg_len != BLCKSZ)
    1846             :                 {
    1847           0 :                     report_invalid_record(state,
    1848             :                                           "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_COMPRESSED set, but block image length is %u at %X/%X",
    1849           0 :                                           (unsigned int) blk->data_len,
    1850           0 :                                           LSN_FORMAT_ARGS(state->ReadRecPtr));
    1851           0 :                     goto err;
    1852             :                 }
    1853             :             }
    1854      174740 :             if (!(fork_flags & BKPBLOCK_SAME_REL))
    1855             :             {
    1856      174068 :                 COPY_HEADER_FIELD(&blk->rlocator, sizeof(RelFileLocator));
    1857      174068 :                 rlocator = &blk->rlocator;
    1858             :             }
    1859             :             else
    1860             :             {
    1861         672 :                 if (rlocator == NULL)
    1862             :                 {
    1863           0 :                     report_invalid_record(state,
    1864             :                                           "BKPBLOCK_SAME_REL set but no previous rel at %X/%X",
    1865           0 :                                           LSN_FORMAT_ARGS(state->ReadRecPtr));
    1866           0 :                     goto err;
    1867             :                 }
    1868             : 
    1869         672 :                 blk->rlocator = *rlocator;
    1870             :             }
    1871      174740 :             COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
    1872             :         }
    1873             :         else
    1874             :         {
    1875           0 :             report_invalid_record(state,
    1876             :                                   "invalid block_id %u at %X/%X",
    1877           0 :                                   block_id, LSN_FORMAT_ARGS(state->ReadRecPtr));
    1878           0 :             goto err;
    1879             :         }
    1880             :     }
    1881             : 
    1882      177966 :     if (remaining != datatotal)
    1883           0 :         goto shortdata_err;
    1884             : 
    1885             :     /*
    1886             :      * Ok, we've parsed the fragment headers, and verified that the total
    1887             :      * length of the payload in the fragments is equal to the amount of data
    1888             :      * left.  Copy the data of each fragment to contiguous space after the
    1889             :      * blocks array, inserting alignment padding before the data fragments so
    1890             :      * they can be cast to struct pointers by REDO routines.
    1891             :      */
    1892      177966 :     out = ((char *) decoded) +
    1893      177966 :         offsetof(DecodedXLogRecord, blocks) +
    1894      177966 :         sizeof(decoded->blocks[0]) * (decoded->max_block_id + 1);
    1895             : 
    1896             :     /* block data first */
    1897      352706 :     for (block_id = 0; block_id <= decoded->max_block_id; block_id++)
    1898             :     {
    1899      174740 :         DecodedBkpBlock *blk = &decoded->blocks[block_id];
    1900             : 
    1901      174740 :         if (!blk->in_use)
    1902           0 :             continue;
    1903             : 
    1904             :         Assert(blk->has_image || !blk->apply_image);
    1905             : 
    1906      174740 :         if (blk->has_image)
    1907             :         {
    1908             :             /* no need to align image */
    1909       11472 :             blk->bkp_image = out;
    1910       11472 :             memcpy(out, ptr, blk->bimg_len);
    1911       11472 :             ptr += blk->bimg_len;
    1912       11472 :             out += blk->bimg_len;
    1913             :         }
    1914      174740 :         if (blk->has_data)
    1915             :         {
    1916       81780 :             out = (char *) MAXALIGN(out);
    1917       81780 :             blk->data = out;
    1918       81780 :             memcpy(blk->data, ptr, blk->data_len);
    1919       81780 :             ptr += blk->data_len;
    1920       81780 :             out += blk->data_len;
    1921             :         }
    1922             :     }
    1923             : 
    1924             :     /* and finally, the main data */
    1925      177966 :     if (decoded->main_data_len > 0)
    1926             :     {
    1927      166576 :         out = (char *) MAXALIGN(out);
    1928      166576 :         decoded->main_data = out;
    1929      166576 :         memcpy(decoded->main_data, ptr, decoded->main_data_len);
    1930      166576 :         ptr += decoded->main_data_len;
    1931      166576 :         out += decoded->main_data_len;
    1932             :     }
    1933             : 
    1934             :     /* Report the actual size we used. */
    1935      177966 :     decoded->size = MAXALIGN(out - (char *) decoded);
    1936             :     Assert(DecodeXLogRecordRequiredSpace(record->xl_tot_len) >=
    1937             :            decoded->size);
    1938             : 
    1939      177966 :     return true;
    1940             : 
    1941           0 : shortdata_err:
    1942           0 :     report_invalid_record(state,
    1943             :                           "record with invalid length at %X/%X",
    1944           0 :                           LSN_FORMAT_ARGS(state->ReadRecPtr));
    1945           0 : err:
    1946           0 :     *errormsg = state->errormsg_buf;
    1947             : 
    1948           0 :     return false;
    1949             : }
    1950             : 
    1951             : /*
    1952             :  * Returns information about the block that a block reference refers to.
    1953             :  *
    1954             :  * This is like XLogRecGetBlockTagExtended, except that the block reference
    1955             :  * must exist and there's no access to prefetch_buffer.
    1956             :  */
    1957             : void
    1958           0 : XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id,
    1959             :                    RelFileLocator *rlocator, ForkNumber *forknum,
    1960             :                    BlockNumber *blknum)
    1961             : {
    1962           0 :     if (!XLogRecGetBlockTagExtended(record, block_id, rlocator, forknum,
    1963             :                                     blknum, NULL))
    1964             :     {
    1965             : #ifndef FRONTEND
    1966             :         elog(ERROR, "could not locate backup block with ID %d in WAL record",
    1967             :              block_id);
    1968             : #else
    1969           0 :         pg_fatal("could not locate backup block with ID %d in WAL record",
    1970             :                  block_id);
    1971             : #endif
    1972             :     }
    1973           0 : }
    1974             : 
    1975             : /*
    1976             :  * Returns information about the block that a block reference refers to,
    1977             :  * optionally including the buffer that the block may already be in.
    1978             :  *
    1979             :  * If the WAL record contains a block reference with the given ID, *rlocator,
    1980             :  * *forknum, *blknum and *prefetch_buffer are filled in (if not NULL), and
    1981             :  * returns true.  Otherwise returns false.
    1982             :  */
    1983             : bool
    1984      171036 : XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id,
    1985             :                            RelFileLocator *rlocator, ForkNumber *forknum,
    1986             :                            BlockNumber *blknum,
    1987             :                            Buffer *prefetch_buffer)
    1988             : {
    1989             :     DecodedBkpBlock *bkpb;
    1990             : 
    1991      171036 :     if (!XLogRecHasBlockRef(record, block_id))
    1992           0 :         return false;
    1993             : 
    1994      171036 :     bkpb = &record->record->blocks[block_id];
    1995      171036 :     if (rlocator)
    1996      171036 :         *rlocator = bkpb->rlocator;
    1997      171036 :     if (forknum)
    1998      171036 :         *forknum = bkpb->forknum;
    1999      171036 :     if (blknum)
    2000      171036 :         *blknum = bkpb->blkno;
    2001      171036 :     if (prefetch_buffer)
    2002           0 :         *prefetch_buffer = bkpb->prefetch_buffer;
    2003      171036 :     return true;
    2004             : }
    2005             : 
    2006             : /*
    2007             :  * Returns the data associated with a block reference, or NULL if there is
    2008             :  * no data (e.g. because a full-page image was taken instead). The returned
    2009             :  * pointer points to a MAXALIGNed buffer.
    2010             :  */
    2011             : char *
    2012           0 : XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
    2013             : {
    2014             :     DecodedBkpBlock *bkpb;
    2015             : 
    2016           0 :     if (block_id > record->record->max_block_id ||
    2017           0 :         !record->record->blocks[block_id].in_use)
    2018           0 :         return NULL;
    2019             : 
    2020           0 :     bkpb = &record->record->blocks[block_id];
    2021             : 
    2022           0 :     if (!bkpb->has_data)
    2023             :     {
    2024           0 :         if (len)
    2025           0 :             *len = 0;
    2026           0 :         return NULL;
    2027             :     }
    2028             :     else
    2029             :     {
    2030           0 :         if (len)
    2031           0 :             *len = bkpb->data_len;
    2032           0 :         return bkpb->data;
    2033             :     }
    2034             : }
    2035             : 
    2036             : /*
    2037             :  * Restore a full-page image from a backup block attached to an XLOG record.
    2038             :  *
    2039             :  * Returns true if a full-page image is restored, and false on failure with
    2040             :  * an error to be consumed by the caller.
    2041             :  */
    2042             : bool
    2043           0 : RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
    2044             : {
    2045             :     DecodedBkpBlock *bkpb;
    2046             :     char       *ptr;
    2047             :     PGAlignedBlock tmp;
    2048             : 
    2049           0 :     if (block_id > record->record->max_block_id ||
    2050           0 :         !record->record->blocks[block_id].in_use)
    2051             :     {
    2052           0 :         report_invalid_record(record,
    2053             :                               "could not restore image at %X/%X with invalid block %d specified",
    2054           0 :                               LSN_FORMAT_ARGS(record->ReadRecPtr),
    2055             :                               block_id);
    2056           0 :         return false;
    2057             :     }
    2058           0 :     if (!record->record->blocks[block_id].has_image)
    2059             :     {
    2060           0 :         report_invalid_record(record, "could not restore image at %X/%X with invalid state, block %d",
    2061           0 :                               LSN_FORMAT_ARGS(record->ReadRecPtr),
    2062             :                               block_id);
    2063           0 :         return false;
    2064             :     }
    2065             : 
    2066           0 :     bkpb = &record->record->blocks[block_id];
    2067           0 :     ptr = bkpb->bkp_image;
    2068             : 
    2069           0 :     if (BKPIMAGE_COMPRESSED(bkpb->bimg_info))
    2070             :     {
    2071             :         /* If a backup block image is compressed, decompress it */
    2072           0 :         bool        decomp_success = true;
    2073             : 
    2074           0 :         if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_PGLZ) != 0)
    2075             :         {
    2076           0 :             if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
    2077           0 :                                 BLCKSZ - bkpb->hole_length, true) < 0)
    2078           0 :                 decomp_success = false;
    2079             :         }
    2080           0 :         else if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_LZ4) != 0)
    2081             :         {
    2082             : #ifdef USE_LZ4
    2083           0 :             if (LZ4_decompress_safe(ptr, tmp.data,
    2084           0 :                                     bkpb->bimg_len, BLCKSZ - bkpb->hole_length) <= 0)
    2085           0 :                 decomp_success = false;
    2086             : #else
    2087             :             report_invalid_record(record, "could not restore image at %X/%X compressed with %s not supported by build, block %d",
    2088             :                                   LSN_FORMAT_ARGS(record->ReadRecPtr),
    2089             :                                   "LZ4",
    2090             :                                   block_id);
    2091             :             return false;
    2092             : #endif
    2093             :         }
    2094           0 :         else if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_ZSTD) != 0)
    2095             :         {
    2096             : #ifdef USE_ZSTD
    2097             :             size_t      decomp_result = ZSTD_decompress(tmp.data,
    2098             :                                                         BLCKSZ - bkpb->hole_length,
    2099             :                                                         ptr, bkpb->bimg_len);
    2100             : 
    2101             :             if (ZSTD_isError(decomp_result))
    2102             :                 decomp_success = false;
    2103             : #else
    2104           0 :             report_invalid_record(record, "could not restore image at %X/%X compressed with %s not supported by build, block %d",
    2105           0 :                                   LSN_FORMAT_ARGS(record->ReadRecPtr),
    2106             :                                   "zstd",
    2107             :                                   block_id);
    2108           0 :             return false;
    2109             : #endif
    2110             :         }
    2111             :         else
    2112             :         {
    2113           0 :             report_invalid_record(record, "could not restore image at %X/%X compressed with unknown method, block %d",
    2114           0 :                                   LSN_FORMAT_ARGS(record->ReadRecPtr),
    2115             :                                   block_id);
    2116           0 :             return false;
    2117             :         }
    2118             : 
    2119           0 :         if (!decomp_success)
    2120             :         {
    2121           0 :             report_invalid_record(record, "could not decompress image at %X/%X, block %d",
    2122           0 :                                   LSN_FORMAT_ARGS(record->ReadRecPtr),
    2123             :                                   block_id);
    2124           0 :             return false;
    2125             :         }
    2126             : 
    2127           0 :         ptr = tmp.data;
    2128             :     }
    2129             : 
    2130             :     /* generate page, taking into account hole if necessary */
    2131           0 :     if (bkpb->hole_length == 0)
    2132             :     {
    2133           0 :         memcpy(page, ptr, BLCKSZ);
    2134             :     }
    2135             :     else
    2136             :     {
    2137           0 :         memcpy(page, ptr, bkpb->hole_offset);
    2138             :         /* must zero-fill the hole */
    2139           0 :         MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
    2140           0 :         memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
    2141           0 :                ptr + bkpb->hole_offset,
    2142           0 :                BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
    2143             :     }
    2144             : 
    2145           0 :     return true;
    2146             : }
    2147             : 
    2148             : #ifndef FRONTEND
    2149             : 
    2150             : /*
    2151             :  * Extract the FullTransactionId from a WAL record.
    2152             :  */
    2153             : FullTransactionId
    2154             : XLogRecGetFullXid(XLogReaderState *record)
    2155             : {
    2156             :     TransactionId xid,
    2157             :                 next_xid;
    2158             :     uint32      epoch;
    2159             : 
    2160             :     /*
    2161             :      * This function is only safe during replay, because it depends on the
    2162             :      * replay state.  See AdvanceNextFullTransactionIdPastXid() for more.
    2163             :      */
    2164             :     Assert(AmStartupProcess() || !IsUnderPostmaster);
    2165             : 
    2166             :     xid = XLogRecGetXid(record);
    2167             :     next_xid = XidFromFullTransactionId(ShmemVariableCache->nextXid);
    2168             :     epoch = EpochFromFullTransactionId(ShmemVariableCache->nextXid);
    2169             : 
    2170             :     /*
    2171             :      * If xid is numerically greater than next_xid, it has to be from the last
    2172             :      * epoch.
    2173             :      */
    2174             :     if (unlikely(xid > next_xid))
    2175             :         --epoch;
    2176             : 
    2177             :     return FullTransactionIdFromEpochAndXid(epoch, xid);
    2178             : }
    2179             : 
    2180             : #endif

Generated by: LCOV version 1.14