55 int segsize, const
char *waldir);
58 #define MAX_ERRORMSG_LEN 1000
64 #define DEFAULT_DECODE_BUFFER_SIZE (64 * 1024)
81 state->errormsg_deferred =
true;
94 state->decode_buffer = buffer;
96 state->decode_buffer_tail = buffer;
97 state->decode_buffer_head = buffer;
118 state->routine = *routine;
140 state->private_data = private_data;
144 if (!
state->errormsg_buf)
150 state->errormsg_buf[0] =
'\0';
163 if (
state->seg.ws_file != -1)
166 if (
state->decode_buffer &&
state->free_decode_buffer)
170 if (
state->readRecordBuf)
192 uint32 newSize = reclength;
194 newSize += XLOG_BLCKSZ - (newSize % XLOG_BLCKSZ);
195 newSize =
Max(newSize, 5 *
Max(BLCKSZ, XLOG_BLCKSZ));
197 if (
state->readRecordBuf)
200 state->readRecordBufSize = newSize;
208 int segsize,
const char *waldir)
238 state->EndRecPtr = RecPtr;
239 state->NextRecPtr = RecPtr;
261 record =
state->record;
264 state->record = NULL;
268 if (
state->decode_queue_tail == record)
269 state->decode_queue_tail = NULL;
280 Assert(
state->decode_buffer_head == (
char *) record);
287 record = record->next;
288 while (
unlikely(record && record->oversized))
289 record = record->next;
294 state->decode_buffer_head = (
char *) record;
304 state->decode_buffer_head =
state->decode_buffer;
305 state->decode_buffer_tail =
state->decode_buffer;
330 if (
state->decode_queue_head == NULL)
333 if (
state->errormsg_deferred)
335 if (
state->errormsg_buf[0] !=
'\0')
336 *errormsg =
state->errormsg_buf;
337 state->errormsg_deferred =
false;
369 return state->record;
446 if (
state->decode_buffer_size == 0)
449 state->decode_buffer_head =
state->decode_buffer;
450 state->decode_buffer_tail =
state->decode_buffer;
451 state->free_decode_buffer =
true;
455 if (
state->decode_buffer_tail >=
state->decode_buffer_head)
458 if (required_space <=
459 state->decode_buffer_size -
460 (
state->decode_buffer_tail -
state->decode_buffer))
476 else if (required_space <
477 state->decode_buffer_head -
state->decode_buffer)
498 state->decode_buffer_head -
state->decode_buffer_tail)
519 decoded =
palloc(required_space);
552 state->errormsg_buf[0] =
'\0';
558 RecPtr =
state->NextRecPtr;
585 state->nonblocking = nonblocking;
586 state->currRecPtr = RecPtr;
589 targetPagePtr = RecPtr - (RecPtr % XLOG_BLCKSZ);
590 targetRecOff = RecPtr % XLOG_BLCKSZ;
609 if (targetRecOff == 0)
614 RecPtr += pageHeaderSize;
615 targetRecOff = pageHeaderSize;
617 else if (targetRecOff < pageHeaderSize)
621 pageHeaderSize, targetRecOff);
626 targetRecOff == pageHeaderSize)
669 "invalid record length at %X/%X: expected at least %u, got %u",
686 if (decoded == NULL && nonblocking)
696 len = XLOG_BLCKSZ - RecPtr % XLOG_BLCKSZ;
711 Assert(
state->readRecordBufSize >= XLOG_BLCKSZ * 2);
715 memcpy(
state->readRecordBuf,
716 state->readBuf + RecPtr % XLOG_BLCKSZ,
len);
717 buffer =
state->readRecordBuf +
len;
723 targetPagePtr += XLOG_BLCKSZ;
749 state->overwrittenRecPtr = RecPtr;
750 RecPtr = targetPagePtr;
758 "there is no contrecord flag at %X/%X",
771 "invalid contrecord length %u (expected %lld) at %X/%X",
773 ((
long long) total_len) - gotlen,
787 contdata = (
char *)
state->readBuf + pageHeaderSize;
788 len = XLOG_BLCKSZ - pageHeaderSize;
794 pageHeaderSize +
len);
796 memcpy(buffer, (
char *) contdata,
len);
816 if (total_len >
state->readRecordBufSize)
818 char save_copy[XLOG_BLCKSZ * 2];
825 Assert(gotlen <= state->readRecordBufSize);
826 memcpy(save_copy,
state->readRecordBuf, gotlen);
828 memcpy(
state->readRecordBuf, save_copy, gotlen);
829 buffer =
state->readRecordBuf + gotlen;
831 }
while (gotlen < total_len);
839 state->DecodeRecPtr = RecPtr;
840 state->NextRecPtr = targetPagePtr + pageHeaderSize
847 Min(targetRecOff + total_len, XLOG_BLCKSZ));
859 state->DecodeRecPtr = RecPtr;
865 if (record->
xl_rmid == RM_XLOG_ID &&
869 state->NextRecPtr +=
state->segcxt.ws_segsize - 1;
900 if ((
char *) decoded ==
state->decode_buffer)
903 state->decode_buffer_tail += decoded->
size;
908 if (
state->decode_queue_tail)
909 state->decode_queue_tail->
next = decoded;
910 state->decode_queue_tail = decoded;
911 if (!
state->decode_queue_head)
912 state->decode_queue_head = decoded;
928 state->abortedRecPtr = RecPtr;
929 state->missingContrecPtr = targetPagePtr;
936 state->errormsg_deferred =
true;
970 if (
state->errormsg_deferred)
977 return state->decode_queue_tail;
1007 Assert((pageptr % XLOG_BLCKSZ) == 0);
1013 if (targetSegNo ==
state->seg.ws_segno &&
1014 targetPageOff ==
state->segoff && reqLen <= state->
readLen)
1015 return state->readLen;
1036 if (targetSegNo !=
state->seg.ws_segno && targetPageOff != 0)
1038 XLogRecPtr targetSegmentPtr = pageptr - targetPageOff;
1097 state->seg.ws_segno = targetSegNo;
1098 state->segoff = targetPageOff;
1115 state->seg.ws_segno = 0;
1134 "invalid record length at %X/%X: expected at least %u, got %u",
1142 "invalid resource manager ID %u at %X/%X",
1152 if (!(record->
xl_prev < RecPtr))
1155 "record with incorrect prev-link %X/%X at %X/%X",
1168 if (record->
xl_prev != PrevRecPtr)
1171 "record with incorrect prev-link %X/%X at %X/%X",
1209 "incorrect resource manager data checksum in record at %X/%X",
1231 Assert((recptr % XLOG_BLCKSZ) == 0);
1243 "invalid magic number %04X in WAL segment %s, LSN %X/%X, offset %u",
1258 "invalid info bits %04X in WAL segment %s, LSN %X/%X, offset %u",
1270 if (
state->system_identifier &&
1274 "WAL file is from different database system: WAL file database system identifier is %llu, pg_control database system identifier is %llu",
1275 (
unsigned long long) longhdr->
xlp_sysid,
1276 (
unsigned long long)
state->system_identifier);
1282 "WAL file is from different database system: incorrect segment size in page header");
1288 "WAL file is from different database system: incorrect XLOG_BLCKSZ in page header");
1292 else if (offset == 0)
1300 "invalid info bits %04X in WAL segment %s, LSN %X/%X, offset %u",
1320 "unexpected pageaddr %X/%X in WAL segment %s, LSN %X/%X, offset %u",
1337 if (recptr >
state->latestPagePtr)
1346 "out-of-sequence timeline ID %u (after %u) in WAL segment %s, LSN %X/%X, offset %u",
1348 state->latestPageTLI,
1355 state->latestPagePtr = recptr;
1367 state->errormsg_buf[0] =
'\0';
1368 state->errormsg_deferred =
false;
1393 state->nonblocking =
false;
1416 targetRecOff = tmpRecPtr % XLOG_BLCKSZ;
1419 targetPagePtr = tmpRecPtr - targetRecOff;
1448 tmpRecPtr = targetPagePtr + XLOG_BLCKSZ;
1455 tmpRecPtr = targetPagePtr + pageHeaderSize
1462 tmpRecPtr = targetPagePtr + pageHeaderSize;
1476 if (RecPtr <= state->ReadRecPtr)
1479 found =
state->ReadRecPtr;
1528 if (
state->seg.ws_file < 0 ||
1530 tli !=
state->seg.ws_tli)
1534 if (
state->seg.ws_file >= 0)
1538 state->routine.segment_open(
state, nextSegNo, &tli);
1544 state->seg.ws_tli = tli;
1545 state->seg.ws_segno = nextSegNo;
1549 if (nbytes > (
state->segcxt.ws_segsize - startoff))
1550 segbytes =
state->segcxt.ws_segsize - startoff;
1560 readbytes =
pg_pread(
state->seg.ws_file, p, segbytes, (off_t) startoff);
1577 recptr += readbytes;
1578 nbytes -= readbytes;
1600 while ((r =
state->decode_queue_head) != NULL)
1606 state->decode_queue_tail = NULL;
1607 state->decode_queue_head = NULL;
1608 state->record = NULL;
1611 state->decode_buffer_tail =
state->decode_buffer;
1612 state->decode_buffer_head =
state->decode_buffer;
1615 state->errormsg_buf[0] =
'\0';
1616 state->errormsg_deferred =
false;
1640 size += (MAXIMUM_ALIGNOF - 1);
1644 size += (MAXIMUM_ALIGNOF - 1);
1671 #define COPY_HEADER_FIELD(_dst, _size) \
1673 if (remaining < _size) \
1674 goto shortdata_err; \
1675 memcpy(_dst, ptr, _size); \
1677 remaining -= _size; \
1687 decoded->
header = *record;
1689 decoded->
next = NULL;
1695 ptr = (
char *) record;
1708 uint8 main_data_len;
1713 datatotal += main_data_len;
1724 datatotal += main_data_len;
1746 if (block_id <= decoded->max_block_id)
1749 "out-of-order block_id %u at %X/%X",
1756 blk = &decoded->
blocks[block_id];
1762 blk->
flags = fork_flags;
1773 "BKPBLOCK_HAS_DATA set, but no data included at %X/%X",
1780 "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%X",
1816 "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%X",
1832 "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%X",
1846 "BKPIMAGE_COMPRESSED set, but block image length %u at %X/%X",
1861 "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_COMPRESSED set, but block image length is %u at %X/%X",
1874 if (rlocator == NULL)
1877 "BKPBLOCK_SAME_REL set but no previous rel at %X/%X",
1889 "invalid block_id %u at %X/%X",
1905 out = ((
char *) decoded) +
1910 for (block_id = 0; block_id <= decoded->
max_block_id; block_id++)
1956 "record with invalid length at %X/%X",
1959 *errormsg =
state->errormsg_buf;
1979 elog(
ERROR,
"could not locate backup block with ID %d in WAL record",
1982 pg_fatal(
"could not locate backup block with ID %d in WAL record",
2013 *blknum = bkpb->
blkno;
2014 if (prefetch_buffer)
2066 "could not restore image at %X/%X with invalid block %d specified",
2085 bool decomp_success =
true;
2091 decomp_success =
false;
2096 if (LZ4_decompress_safe(ptr, tmp.
data,
2098 decomp_success =
false;
2100 report_invalid_record(record,
"could not restore image at %X/%X compressed with %s not supported by build, block %d",
2110 size_t decomp_result = ZSTD_decompress(tmp.
data,
2114 if (ZSTD_isError(decomp_result))
2115 decomp_success =
false;
2117 report_invalid_record(record,
"could not restore image at %X/%X compressed with %s not supported by build, block %d",
2126 report_invalid_record(record,
"could not restore image at %X/%X compressed with unknown method, block %d",
2132 if (!decomp_success)
2146 memcpy(page, ptr, BLCKSZ);
#define Assert(condition)
#define pg_attribute_printf(f, a)
#define MemSet(start, val, len)
void err(int eval, const char *fmt,...)
#define MCXT_ALLOC_NO_OOM
if(TABLE==NULL||TABLE_index==NULL)
static void const char * fmt
void pfree(void *pointer)
void * palloc_extended(Size size, int flags)
#define AmStartupProcess()
#define InvalidRepOriginId
#define COMP_CRC32C(crc, data, len)
#define EQ_CRC32C(c1, c2)
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
#define RmgrIdIsValid(rmid)
static pg_noinline void Size size
struct DecodedXLogRecord * next
TransactionId toplevel_xid
RepOriginId record_origin
DecodedBkpBlock blocks[FLEXIBLE_ARRAY_MEMBER]
FullTransactionId nextXid
DecodedXLogRecord * record
#define InvalidTransactionId
#define EpochFromFullTransactionId(x)
#define XidFromFullTransactionId(x)
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
TransamVariablesData * TransamVariables
static void pgstat_report_wait_start(uint32 wait_event_info)
static void pgstat_report_wait_end(void)
static const unsigned __int64 epoch
#define XLP_FIRST_IS_CONTRECORD
XLogLongPageHeaderData * XLogLongPageHeader
#define XLP_FIRST_IS_OVERWRITE_CONTRECORD
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
XLogPageHeaderData * XLogPageHeader
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
#define XRecOffIsValid(xlrp)
#define SizeOfXLogShortPHD
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
#define XLogPageHeaderSize(hdr)
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
#define LSN_FORMAT_ARGS(lsn)
#define XLogRecPtrIsInvalid(r)
#define InvalidXLogRecPtr
bool XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)
static XLogPageReadResult XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking)
void XLogReaderSetDecodeBuffer(XLogReaderState *state, void *buffer, size_t size)
static void WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt, int segsize, const char *waldir)
static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
static void static void allocate_recordbuf(XLogReaderState *state, uint32 reclength)
bool WALRead(XLogReaderState *state, char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, WALReadError *errinfo)
DecodedXLogRecord * XLogNextRecord(XLogReaderState *state, char **errormsg)
void XLogReaderResetError(XLogReaderState *state)
static void XLogReaderInvalReadState(XLogReaderState *state)
#define COPY_HEADER_FIELD(_dst, _size)
bool XLogReaderValidatePageHeader(XLogReaderState *state, XLogRecPtr recptr, char *phdr)
FullTransactionId XLogRecGetFullXid(XLogReaderState *record)
void XLogReaderFree(XLogReaderState *state)
void XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
DecodedXLogRecord * XLogReadAhead(XLogReaderState *state, bool nonblocking)
XLogReaderState * XLogReaderAllocate(int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
static void ResetDecoder(XLogReaderState *state)
bool DecodeXLogRecord(XLogReaderState *state, DecodedXLogRecord *decoded, XLogRecord *record, XLogRecPtr lsn, char **errormsg)
static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
#define DEFAULT_DECODE_BUFFER_SIZE
size_t DecodeXLogRecordRequiredSpace(size_t xl_tot_len)
XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr)
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
bool RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
static DecodedXLogRecord * XLogReadRecordAlloc(XLogReaderState *state, size_t xl_tot_len, bool allow_oversized)
XLogRecPtr XLogReleasePreviousRecord(XLogReaderState *state)
static bool ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr, XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess)
static bool XLogReaderHasQueuedRecordOrError(XLogReaderState *state)
#define XLogRecGetXid(decoder)
#define XLogRecHasBlockRef(decoder, block_id)
#define BKPIMAGE_COMPRESS_ZSTD
#define BKPBLOCK_FORK_MASK
#define BKPBLOCK_HAS_DATA
#define BKPIMAGE_HAS_HOLE
#define XLR_BLOCK_ID_DATA_LONG
#define BKPIMAGE_COMPRESS_LZ4
#define BKPIMAGE_COMPRESSED(info)
#define XLR_BLOCK_ID_TOPLEVEL_XID
#define XLR_BLOCK_ID_DATA_SHORT
#define BKPBLOCK_SAME_REL
#define BKPIMAGE_COMPRESS_PGLZ
#define XLR_BLOCK_ID_ORIGIN
#define BKPBLOCK_HAS_IMAGE