LCOV - differential code coverage report
Current view: top level - src/backend/access/transam - xlogreader.c (source / functions) Coverage Total Hit UNC UBC CBC DUB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 79.6 % 765 609 2 154 609 2
Current Date: 2024-04-14 14:21:10 Functions: 96.6 % 29 28 1 28
Baseline: 16@8cea358b128 Branches: 70.4 % 476 335 141 335
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (120,180] days: 77.8 % 9 7 2 7
(180,240] days: 100.0 % 24 24 24
(240..) days: 79.0 % 732 578 154 578
Function coverage date bins:
(240..) days: 96.6 % 29 28 1 28
Branch coverage date bins:
(120,180] days: 83.3 % 6 5 1 5
(180,240] days: 73.1 % 26 19 7 19
(240..) days: 70.0 % 444 311 133 311

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

Generated by: LCOV version 2.1-beta2-3-g6141622