PostgreSQL Source Code git master
Loading...
Searching...
No Matches
xlogreader.h File Reference
#include "access/transam.h"
#include "access/xlogrecord.h"
#include "storage/buf.h"
Include dependency graph for xlogreader.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  WALOpenSegment
 
struct  WALSegmentContext
 
struct  XLogReaderRoutine
 
struct  DecodedBkpBlock
 
struct  DecodedXLogRecord
 
struct  XLogReaderState
 
struct  WALReadError
 

Macros

#define XL_ROUTINE(...)   &(XLogReaderRoutine){__VA_ARGS__}
 
#define XLogRecGetTotalLen(decoder)   ((decoder)->record->header.xl_tot_len)
 
#define XLogRecGetPrev(decoder)   ((decoder)->record->header.xl_prev)
 
#define XLogRecGetInfo(decoder)   ((decoder)->record->header.xl_info)
 
#define XLogRecGetRmid(decoder)   ((decoder)->record->header.xl_rmid)
 
#define XLogRecGetXid(decoder)   ((decoder)->record->header.xl_xid)
 
#define XLogRecGetOrigin(decoder)   ((decoder)->record->record_origin)
 
#define XLogRecGetTopXid(decoder)   ((decoder)->record->toplevel_xid)
 
#define XLogRecGetData(decoder)   ((decoder)->record->main_data)
 
#define XLogRecGetDataLen(decoder)   ((decoder)->record->main_data_len)
 
#define XLogRecHasAnyBlockRefs(decoder)   ((decoder)->record->max_block_id >= 0)
 
#define XLogRecMaxBlockId(decoder)   ((decoder)->record->max_block_id)
 
#define XLogRecGetBlock(decoder, i)   (&(decoder)->record->blocks[(i)])
 
#define XLogRecHasBlockRef(decoder, block_id)
 
#define XLogRecHasBlockImage(decoder, block_id)    ((decoder)->record->blocks[block_id].has_image)
 
#define XLogRecBlockImageApply(decoder, block_id)    ((decoder)->record->blocks[block_id].apply_image)
 
#define XLogRecHasBlockData(decoder, block_id)    ((decoder)->record->blocks[block_id].has_data)
 

Typedefs

typedef struct WALOpenSegment WALOpenSegment
 
typedef struct WALSegmentContext WALSegmentContext
 
typedef struct XLogReaderState XLogReaderState
 
typedef int(* XLogPageReadCB) (XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf)
 
typedef void(* WALSegmentOpenCB) (XLogReaderState *xlogreader, XLogSegNo nextSegNo, TimeLineID *tli_p)
 
typedef void(* WALSegmentCloseCB) (XLogReaderState *xlogreader)
 
typedef struct XLogReaderRoutine XLogReaderRoutine
 
typedef struct DecodedXLogRecord DecodedXLogRecord
 
typedef enum XLogPageReadResult XLogPageReadResult
 
typedef struct WALReadError WALReadError
 

Enumerations

enum  XLogPageReadResult { XLREAD_SUCCESS = 0 , XLREAD_FAIL = -1 , XLREAD_WOULDBLOCK = -2 }
 

Functions

static bool XLogReaderHasQueuedRecordOrError (XLogReaderState *state)
 
XLogReaderStateXLogReaderAllocate (int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
 
void XLogReaderFree (XLogReaderState *state)
 
void XLogReaderSetDecodeBuffer (XLogReaderState *state, void *buffer, size_t size)
 
void XLogBeginRead (XLogReaderState *state, XLogRecPtr RecPtr)
 
XLogRecPtr XLogFindNextRecord (XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
 
struct XLogRecordXLogReadRecord (XLogReaderState *state, char **errormsg)
 
DecodedXLogRecordXLogNextRecord (XLogReaderState *state, char **errormsg)
 
XLogRecPtr XLogReleasePreviousRecord (XLogReaderState *state)
 
DecodedXLogRecordXLogReadAhead (XLogReaderState *state, bool nonblocking)
 
bool XLogReaderValidatePageHeader (XLogReaderState *state, XLogRecPtr recptr, char *phdr)
 
void XLogReaderResetError (XLogReaderState *state)
 
bool WALRead (XLogReaderState *state, char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, WALReadError *errinfo)
 
size_t DecodeXLogRecordRequiredSpace (size_t xl_tot_len)
 
bool DecodeXLogRecord (XLogReaderState *state, DecodedXLogRecord *decoded, XLogRecord *record, XLogRecPtr lsn, char **errormsg)
 
FullTransactionId XLogRecGetFullXid (XLogReaderState *record)
 
bool RestoreBlockImage (XLogReaderState *record, uint8 block_id, char *page)
 
charXLogRecGetBlockData (XLogReaderState *record, uint8 block_id, Size *len)
 
void XLogRecGetBlockTag (XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
 
bool XLogRecGetBlockTagExtended (XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)
 

Macro Definition Documentation

◆ XL_ROUTINE

#define XL_ROUTINE (   ...)    &(XLogReaderRoutine){__VA_ARGS__}

Definition at line 117 of file xlogreader.h.

◆ XLogRecBlockImageApply

#define XLogRecBlockImageApply (   decoder,
  block_id 
)     ((decoder)->record->blocks[block_id].apply_image)

Definition at line 425 of file xlogreader.h.

◆ XLogRecGetBlock

#define XLogRecGetBlock (   decoder,
  i 
)    (&(decoder)->record->blocks[(i)])

Definition at line 419 of file xlogreader.h.

◆ XLogRecGetData

#define XLogRecGetData (   decoder)    ((decoder)->record->main_data)

Definition at line 415 of file xlogreader.h.

◆ XLogRecGetDataLen

#define XLogRecGetDataLen (   decoder)    ((decoder)->record->main_data_len)

Definition at line 416 of file xlogreader.h.

◆ XLogRecGetInfo

#define XLogRecGetInfo (   decoder)    ((decoder)->record->header.xl_info)

Definition at line 410 of file xlogreader.h.

◆ XLogRecGetOrigin

#define XLogRecGetOrigin (   decoder)    ((decoder)->record->record_origin)

Definition at line 413 of file xlogreader.h.

◆ XLogRecGetPrev

#define XLogRecGetPrev (   decoder)    ((decoder)->record->header.xl_prev)

Definition at line 409 of file xlogreader.h.

◆ XLogRecGetRmid

#define XLogRecGetRmid (   decoder)    ((decoder)->record->header.xl_rmid)

Definition at line 411 of file xlogreader.h.

◆ XLogRecGetTopXid

#define XLogRecGetTopXid (   decoder)    ((decoder)->record->toplevel_xid)

Definition at line 414 of file xlogreader.h.

◆ XLogRecGetTotalLen

#define XLogRecGetTotalLen (   decoder)    ((decoder)->record->header.xl_tot_len)

Definition at line 408 of file xlogreader.h.

◆ XLogRecGetXid

#define XLogRecGetXid (   decoder)    ((decoder)->record->header.xl_xid)

Definition at line 412 of file xlogreader.h.

◆ XLogRecHasAnyBlockRefs

#define XLogRecHasAnyBlockRefs (   decoder)    ((decoder)->record->max_block_id >= 0)

Definition at line 417 of file xlogreader.h.

◆ XLogRecHasBlockData

#define XLogRecHasBlockData (   decoder,
  block_id 
)     ((decoder)->record->blocks[block_id].has_data)

Definition at line 427 of file xlogreader.h.

◆ XLogRecHasBlockImage

#define XLogRecHasBlockImage (   decoder,
  block_id 
)     ((decoder)->record->blocks[block_id].has_image)

Definition at line 423 of file xlogreader.h.

◆ XLogRecHasBlockRef

#define XLogRecHasBlockRef (   decoder,
  block_id 
)
Value:
(((decoder)->record->max_block_id >= (block_id)) && \
((decoder)->record->blocks[block_id].in_use))
static int fb(int x)

Definition at line 420 of file xlogreader.h.

◆ XLogRecMaxBlockId

#define XLogRecMaxBlockId (   decoder)    ((decoder)->record->max_block_id)

Definition at line 418 of file xlogreader.h.

Typedef Documentation

◆ DecodedXLogRecord

◆ WALOpenSegment

◆ WALReadError

◆ WALSegmentCloseCB

typedef void(* WALSegmentCloseCB) (XLogReaderState *xlogreader)

Definition at line 70 of file xlogreader.h.

◆ WALSegmentContext

◆ WALSegmentOpenCB

Definition at line 67 of file xlogreader.h.

◆ XLogPageReadCB

Definition at line 62 of file xlogreader.h.

◆ XLogPageReadResult

◆ XLogReaderRoutine

◆ XLogReaderState

Definition at line 59 of file xlogreader.h.

Enumeration Type Documentation

◆ XLogPageReadResult

Enumerator
XLREAD_SUCCESS 
XLREAD_FAIL 
XLREAD_WOULDBLOCK 

Definition at line 349 of file xlogreader.h.

350{
351 XLREAD_SUCCESS = 0, /* record is successfully read */
352 XLREAD_FAIL = -1, /* failed during reading a record */
353 XLREAD_WOULDBLOCK = -2, /* nonblocking mode only, no data */
XLogPageReadResult
Definition xlogreader.h:350
@ XLREAD_WOULDBLOCK
Definition xlogreader.h:353
@ XLREAD_SUCCESS
Definition xlogreader.h:351
@ XLREAD_FAIL
Definition xlogreader.h:352

Function Documentation

◆ DecodeXLogRecord()

bool DecodeXLogRecord ( XLogReaderState state,
DecodedXLogRecord decoded,
XLogRecord record,
XLogRecPtr  lsn,
char **  errormsg 
)
extern

Definition at line 1701 of file xlogreader.c.

1706{
1707 /*
1708 * read next _size bytes from record buffer, but check for overrun first.
1709 */
1710#define COPY_HEADER_FIELD(_dst, _size) \
1711 do { \
1712 if (remaining < _size) \
1713 goto shortdata_err; \
1714 memcpy(_dst, ptr, _size); \
1715 ptr += _size; \
1716 remaining -= _size; \
1717 } while(0)
1718
1719 char *ptr;
1720 char *out;
1723 RelFileLocator *rlocator = NULL;
1725
1726 decoded->header = *record;
1727 decoded->lsn = lsn;
1728 decoded->next = NULL;
1729 decoded->record_origin = InvalidReplOriginId;
1730 decoded->toplevel_xid = InvalidTransactionId;
1731 decoded->main_data = NULL;
1732 decoded->main_data_len = 0;
1733 decoded->max_block_id = -1;
1734 ptr = (char *) record;
1735 ptr += SizeOfXLogRecord;
1737
1738 /* Decode the headers */
1739 datatotal = 0;
1740 while (remaining > datatotal)
1741 {
1742 COPY_HEADER_FIELD(&block_id, sizeof(uint8));
1743
1745 {
1746 /* XLogRecordDataHeaderShort */
1747 uint8 main_data_len;
1748
1749 COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
1750
1751 decoded->main_data_len = main_data_len;
1752 datatotal += main_data_len;
1753 break; /* by convention, the main data fragment is
1754 * always last */
1755 }
1756 else if (block_id == XLR_BLOCK_ID_DATA_LONG)
1757 {
1758 /* XLogRecordDataHeaderLong */
1759 uint32 main_data_len;
1760
1761 COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
1762 decoded->main_data_len = main_data_len;
1763 datatotal += main_data_len;
1764 break; /* by convention, the main data fragment is
1765 * always last */
1766 }
1767 else if (block_id == XLR_BLOCK_ID_ORIGIN)
1768 {
1769 COPY_HEADER_FIELD(&decoded->record_origin, sizeof(ReplOriginId));
1770 }
1772 {
1773 COPY_HEADER_FIELD(&decoded->toplevel_xid, sizeof(TransactionId));
1774 }
1775 else if (block_id <= XLR_MAX_BLOCK_ID)
1776 {
1777 /* XLogRecordBlockHeader */
1779 uint8 fork_flags;
1780
1781 /* mark any intervening block IDs as not in use */
1782 for (int i = decoded->max_block_id + 1; i < block_id; ++i)
1783 decoded->blocks[i].in_use = false;
1784
1785 if (block_id <= decoded->max_block_id)
1786 {
1788 "out-of-order block_id %u at %X/%08X",
1789 block_id,
1790 LSN_FORMAT_ARGS(state->ReadRecPtr));
1791 goto err;
1792 }
1793 decoded->max_block_id = block_id;
1794
1795 blk = &decoded->blocks[block_id];
1796 blk->in_use = true;
1797 blk->apply_image = false;
1798
1799 COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
1800 blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
1801 blk->flags = fork_flags;
1802 blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
1803 blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
1804
1805 blk->prefetch_buffer = InvalidBuffer;
1806
1807 COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
1808 /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
1809 if (blk->has_data && blk->data_len == 0)
1810 {
1812 "BKPBLOCK_HAS_DATA set, but no data included at %X/%08X",
1813 LSN_FORMAT_ARGS(state->ReadRecPtr));
1814 goto err;
1815 }
1816 if (!blk->has_data && blk->data_len != 0)
1817 {
1819 "BKPBLOCK_HAS_DATA not set, but data length is %d at %X/%08X",
1820 blk->data_len,
1821 LSN_FORMAT_ARGS(state->ReadRecPtr));
1822 goto err;
1823 }
1824 datatotal += blk->data_len;
1825
1826 if (blk->has_image)
1827 {
1828 COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
1829 COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
1830 COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
1831
1832 blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
1833
1834 if (BKPIMAGE_COMPRESSED(blk->bimg_info))
1835 {
1836 if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
1837 COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
1838 else
1839 blk->hole_length = 0;
1840 }
1841 else
1842 blk->hole_length = BLCKSZ - blk->bimg_len;
1843 datatotal += blk->bimg_len;
1844
1845 /*
1846 * cross-check that hole_offset > 0, hole_length > 0 and
1847 * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
1848 */
1849 if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1850 (blk->hole_offset == 0 ||
1851 blk->hole_length == 0 ||
1852 blk->bimg_len == BLCKSZ))
1853 {
1855 "BKPIMAGE_HAS_HOLE set, but hole offset %d length %d block image length %d at %X/%08X",
1856 blk->hole_offset,
1857 blk->hole_length,
1858 blk->bimg_len,
1859 LSN_FORMAT_ARGS(state->ReadRecPtr));
1860 goto err;
1861 }
1862
1863 /*
1864 * cross-check that hole_offset == 0 and hole_length == 0 if
1865 * the HAS_HOLE flag is not set.
1866 */
1867 if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1868 (blk->hole_offset != 0 || blk->hole_length != 0))
1869 {
1871 "BKPIMAGE_HAS_HOLE not set, but hole offset %d length %d at %X/%08X",
1872 blk->hole_offset,
1873 blk->hole_length,
1874 LSN_FORMAT_ARGS(state->ReadRecPtr));
1875 goto err;
1876 }
1877
1878 /*
1879 * Cross-check that bimg_len < BLCKSZ if it is compressed.
1880 */
1881 if (BKPIMAGE_COMPRESSED(blk->bimg_info) &&
1882 blk->bimg_len == BLCKSZ)
1883 {
1885 "BKPIMAGE_COMPRESSED set, but block image length %d at %X/%08X",
1886 blk->bimg_len,
1887 LSN_FORMAT_ARGS(state->ReadRecPtr));
1888 goto err;
1889 }
1890
1891 /*
1892 * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE is
1893 * set nor COMPRESSED().
1894 */
1895 if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1896 !BKPIMAGE_COMPRESSED(blk->bimg_info) &&
1897 blk->bimg_len != BLCKSZ)
1898 {
1900 "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_COMPRESSED set, but block image length is %d at %X/%08X",
1901 blk->data_len,
1902 LSN_FORMAT_ARGS(state->ReadRecPtr));
1903 goto err;
1904 }
1905 }
1906 if (!(fork_flags & BKPBLOCK_SAME_REL))
1907 {
1908 COPY_HEADER_FIELD(&blk->rlocator, sizeof(RelFileLocator));
1909 rlocator = &blk->rlocator;
1910 }
1911 else
1912 {
1913 if (rlocator == NULL)
1914 {
1916 "BKPBLOCK_SAME_REL set but no previous rel at %X/%08X",
1917 LSN_FORMAT_ARGS(state->ReadRecPtr));
1918 goto err;
1919 }
1920
1921 blk->rlocator = *rlocator;
1922 }
1923 COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
1924 }
1925 else
1926 {
1928 "invalid block_id %u at %X/%08X",
1929 block_id, LSN_FORMAT_ARGS(state->ReadRecPtr));
1930 goto err;
1931 }
1932 }
1933
1934 if (remaining != datatotal)
1935 goto shortdata_err;
1936
1937 /*
1938 * Ok, we've parsed the fragment headers, and verified that the total
1939 * length of the payload in the fragments is equal to the amount of data
1940 * left. Copy the data of each fragment to contiguous space after the
1941 * blocks array, inserting alignment padding before the data fragments so
1942 * they can be cast to struct pointers by REDO routines.
1943 */
1944 out = ((char *) decoded) +
1945 offsetof(DecodedXLogRecord, blocks) +
1946 sizeof(decoded->blocks[0]) * (decoded->max_block_id + 1);
1947
1948 /* block data first */
1949 for (block_id = 0; block_id <= decoded->max_block_id; block_id++)
1950 {
1951 DecodedBkpBlock *blk = &decoded->blocks[block_id];
1952
1953 if (!blk->in_use)
1954 continue;
1955
1956 Assert(blk->has_image || !blk->apply_image);
1957
1958 if (blk->has_image)
1959 {
1960 /* no need to align image */
1961 blk->bkp_image = out;
1962 memcpy(out, ptr, blk->bimg_len);
1963 ptr += blk->bimg_len;
1964 out += blk->bimg_len;
1965 }
1966 if (blk->has_data)
1967 {
1968 out = (char *) MAXALIGN(out);
1969 blk->data = out;
1970 memcpy(blk->data, ptr, blk->data_len);
1971 ptr += blk->data_len;
1972 out += blk->data_len;
1973 }
1974 }
1975
1976 /* and finally, the main data */
1977 if (decoded->main_data_len > 0)
1978 {
1979 out = (char *) MAXALIGN(out);
1980 decoded->main_data = out;
1981 memcpy(decoded->main_data, ptr, decoded->main_data_len);
1982 ptr += decoded->main_data_len;
1983 out += decoded->main_data_len;
1984 }
1985
1986 /* Report the actual size we used. */
1987 decoded->size = MAXALIGN(out - (char *) decoded);
1989 decoded->size);
1990
1991 return true;
1992
1995 "record with invalid length at %X/%08X",
1996 LSN_FORMAT_ARGS(state->ReadRecPtr));
1997err:
1998 *errormsg = state->errormsg_buf;
1999
2000 return false;
2001}
uint32 BlockNumber
Definition block.h:31
#define InvalidBuffer
Definition buf.h:25
#define MAXALIGN(LEN)
Definition c.h:896
uint8_t uint8
Definition c.h:622
#define Assert(condition)
Definition c.h:943
uint16_t uint16
Definition c.h:623
uint32_t uint32
Definition c.h:624
uint32 TransactionId
Definition c.h:736
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
void err(int eval, const char *fmt,...)
Definition err.c:43
int remaining
Definition informix.c:692
int i
Definition isn.c:77
#define InvalidReplOriginId
Definition origin.h:33
uint32 xl_tot_len
Definition xlogrecord.h:43
#define InvalidTransactionId
Definition transam.h:31
#define LSN_FORMAT_ARGS(lsn)
Definition xlogdefs.h:47
uint16 ReplOriginId
Definition xlogdefs.h:69
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition xlogreader.c:73
#define COPY_HEADER_FIELD(_dst, _size)
size_t DecodeXLogRecordRequiredSpace(size_t xl_tot_len)
#define BKPBLOCK_FORK_MASK
Definition xlogrecord.h:195
#define BKPBLOCK_HAS_DATA
Definition xlogrecord.h:198
#define BKPIMAGE_APPLY
Definition xlogrecord.h:158
#define BKPIMAGE_HAS_HOLE
Definition xlogrecord.h:157
#define XLR_BLOCK_ID_DATA_LONG
Definition xlogrecord.h:242
#define BKPIMAGE_COMPRESSED(info)
Definition xlogrecord.h:164
#define XLR_BLOCK_ID_TOPLEVEL_XID
Definition xlogrecord.h:244
#define XLR_BLOCK_ID_DATA_SHORT
Definition xlogrecord.h:241
#define XLR_MAX_BLOCK_ID
Definition xlogrecord.h:239
#define BKPBLOCK_SAME_REL
Definition xlogrecord.h:200
#define XLR_BLOCK_ID_ORIGIN
Definition xlogrecord.h:243
#define SizeOfXLogRecord
Definition xlogrecord.h:55
#define BKPBLOCK_HAS_IMAGE
Definition xlogrecord.h:197

References Assert, BKPBLOCK_FORK_MASK, BKPBLOCK_HAS_DATA, BKPBLOCK_HAS_IMAGE, BKPBLOCK_SAME_REL, BKPIMAGE_APPLY, BKPIMAGE_COMPRESSED, BKPIMAGE_HAS_HOLE, COPY_HEADER_FIELD, DecodeXLogRecordRequiredSpace(), err(), fb(), i, DecodedBkpBlock::in_use, InvalidBuffer, InvalidReplOriginId, InvalidTransactionId, LSN_FORMAT_ARGS, MAXALIGN, memcpy(), remaining, report_invalid_record(), SizeOfXLogRecord, XLogRecord::xl_tot_len, XLR_BLOCK_ID_DATA_LONG, XLR_BLOCK_ID_DATA_SHORT, XLR_BLOCK_ID_ORIGIN, XLR_BLOCK_ID_TOPLEVEL_XID, and XLR_MAX_BLOCK_ID.

Referenced by XLogDecodeNextRecord(), and XLogInsertRecord().

◆ DecodeXLogRecordRequiredSpace()

size_t DecodeXLogRecordRequiredSpace ( size_t  xl_tot_len)
extern

Definition at line 1668 of file xlogreader.c.

1669{
1670 size_t size = 0;
1671
1672 /* Account for the fixed size part of the decoded record struct. */
1673 size += offsetof(DecodedXLogRecord, blocks[0]);
1674 /* Account for the flexible blocks array of maximum possible size. */
1675 size += sizeof(DecodedBkpBlock) * (XLR_MAX_BLOCK_ID + 1);
1676 /* Account for all the raw main and block data. */
1677 size += xl_tot_len;
1678 /* We might insert padding before main_data. */
1679 size += (MAXIMUM_ALIGNOF - 1);
1680 /* We might insert padding before each block's data. */
1681 size += (MAXIMUM_ALIGNOF - 1) * (XLR_MAX_BLOCK_ID + 1);
1682 /* We might insert padding at the end. */
1683 size += (MAXIMUM_ALIGNOF - 1);
1684
1685 return size;
1686}

References fb(), and XLR_MAX_BLOCK_ID.

Referenced by DecodeXLogRecord(), InitXLogInsert(), XLogInsertRecord(), and XLogReadRecordAlloc().

◆ RestoreBlockImage()

bool RestoreBlockImage ( XLogReaderState record,
uint8  block_id,
char page 
)
extern

Definition at line 2095 of file xlogreader.c.

2096{
2098 char *ptr;
2099 PGAlignedBlock tmp;
2100
2101 if (block_id > record->record->max_block_id ||
2102 !record->record->blocks[block_id].in_use)
2103 {
2104 report_invalid_record(record,
2105 "could not restore image at %X/%08X with invalid block %d specified",
2106 LSN_FORMAT_ARGS(record->ReadRecPtr),
2107 block_id);
2108 return false;
2109 }
2110 if (!record->record->blocks[block_id].has_image)
2111 {
2112 report_invalid_record(record, "could not restore image at %X/%08X with invalid state, block %d",
2113 LSN_FORMAT_ARGS(record->ReadRecPtr),
2114 block_id);
2115 return false;
2116 }
2117
2118 bkpb = &record->record->blocks[block_id];
2119 ptr = bkpb->bkp_image;
2120
2121 if (BKPIMAGE_COMPRESSED(bkpb->bimg_info))
2122 {
2123 /* If a backup block image is compressed, decompress it */
2124 bool decomp_success = true;
2125
2126 if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_PGLZ) != 0)
2127 {
2128 if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
2129 BLCKSZ - bkpb->hole_length, true) < 0)
2130 decomp_success = false;
2131 }
2132 else if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_LZ4) != 0)
2133 {
2134#ifdef USE_LZ4
2135 if (LZ4_decompress_safe(ptr, tmp.data,
2136 bkpb->bimg_len, BLCKSZ - bkpb->hole_length) <= 0)
2137 decomp_success = false;
2138#else
2139 report_invalid_record(record, "could not restore image at %X/%08X compressed with %s not supported by build, block %d",
2140 LSN_FORMAT_ARGS(record->ReadRecPtr),
2141 "LZ4",
2142 block_id);
2143 return false;
2144#endif
2145 }
2146 else if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_ZSTD) != 0)
2147 {
2148#ifdef USE_ZSTD
2149 size_t decomp_result = ZSTD_decompress(tmp.data,
2150 BLCKSZ - bkpb->hole_length,
2151 ptr, bkpb->bimg_len);
2152
2154 decomp_success = false;
2155#else
2156 report_invalid_record(record, "could not restore image at %X/%08X compressed with %s not supported by build, block %d",
2157 LSN_FORMAT_ARGS(record->ReadRecPtr),
2158 "zstd",
2159 block_id);
2160 return false;
2161#endif
2162 }
2163 else
2164 {
2165 report_invalid_record(record, "could not restore image at %X/%08X compressed with unknown method, block %d",
2166 LSN_FORMAT_ARGS(record->ReadRecPtr),
2167 block_id);
2168 return false;
2169 }
2170
2171 if (!decomp_success)
2172 {
2173 report_invalid_record(record, "could not decompress image at %X/%08X, block %d",
2174 LSN_FORMAT_ARGS(record->ReadRecPtr),
2175 block_id);
2176 return false;
2177 }
2178
2179 ptr = tmp.data;
2180 }
2181
2182 /* generate page, taking into account hole if necessary */
2183 if (bkpb->hole_length == 0)
2184 {
2185 memcpy(page, ptr, BLCKSZ);
2186 }
2187 else
2188 {
2189 memcpy(page, ptr, bkpb->hole_offset);
2190 /* must zero-fill the hole */
2191 MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
2192 memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
2193 ptr + bkpb->hole_offset,
2194 BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
2195 }
2196
2197 return true;
2198}
#define MemSet(start, val, len)
Definition c.h:1107
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
DecodedBkpBlock blocks[FLEXIBLE_ARRAY_MEMBER]
Definition xlogreader.h:171
char data[BLCKSZ]
Definition c.h:1204
DecodedXLogRecord * record
Definition xlogreader.h:235
XLogRecPtr ReadRecPtr
Definition xlogreader.h:205
#define BKPIMAGE_COMPRESS_ZSTD
Definition xlogrecord.h:162
#define BKPIMAGE_COMPRESS_LZ4
Definition xlogrecord.h:161
#define BKPIMAGE_COMPRESS_PGLZ
Definition xlogrecord.h:160

References DecodedBkpBlock::bkp_image, BKPIMAGE_COMPRESS_LZ4, BKPIMAGE_COMPRESS_PGLZ, BKPIMAGE_COMPRESS_ZSTD, BKPIMAGE_COMPRESSED, DecodedXLogRecord::blocks, PGAlignedBlock::data, fb(), DecodedBkpBlock::has_image, DecodedBkpBlock::in_use, LSN_FORMAT_ARGS, DecodedXLogRecord::max_block_id, memcpy(), MemSet, pglz_decompress(), XLogReaderState::ReadRecPtr, XLogReaderState::record, and report_invalid_record().

Referenced by GetWALBlockInfo(), verifyBackupPageConsistency(), XLogReadBufferForRedoExtended(), and XLogRecordSaveFPWs().

◆ WALRead()

bool WALRead ( XLogReaderState state,
char buf,
XLogRecPtr  startptr,
Size  count,
TimeLineID  tli,
WALReadError errinfo 
)
extern

Definition at line 1533 of file xlogreader.c.

1536{
1537 char *p;
1539 Size nbytes;
1540#ifndef FRONTEND
1542#endif
1543
1544 p = buf;
1545 recptr = startptr;
1546 nbytes = count;
1547
1548 while (nbytes > 0)
1549 {
1551 int segbytes;
1552 int readbytes;
1553
1554 startoff = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
1555
1556 /*
1557 * If the data we want is not in a segment we have open, close what we
1558 * have (if anything) and open the next one, using the caller's
1559 * provided segment_open callback.
1560 */
1561 if (state->seg.ws_file < 0 ||
1562 !XLByteInSeg(recptr, state->seg.ws_segno, state->segcxt.ws_segsize) ||
1563 tli != state->seg.ws_tli)
1564 {
1566
1567 if (state->seg.ws_file >= 0)
1568 state->routine.segment_close(state);
1569
1570 XLByteToSeg(recptr, nextSegNo, state->segcxt.ws_segsize);
1571 state->routine.segment_open(state, nextSegNo, &tli);
1572
1573 /* This shouldn't happen -- indicates a bug in segment_open */
1574 Assert(state->seg.ws_file >= 0);
1575
1576 /* Update the current segment info. */
1577 state->seg.ws_tli = tli;
1578 state->seg.ws_segno = nextSegNo;
1579 }
1580
1581 /* How many bytes are within this segment? */
1582 if (nbytes > (state->segcxt.ws_segsize - startoff))
1583 segbytes = state->segcxt.ws_segsize - startoff;
1584 else
1585 segbytes = nbytes;
1586
1587#ifndef FRONTEND
1588 /* Measure I/O timing when reading segment */
1590
1592#endif
1593
1594 /* Reset errno first; eases reporting non-errno-affecting errors */
1595 errno = 0;
1596 readbytes = pg_pread(state->seg.ws_file, p, segbytes, (pgoff_t) startoff);
1597
1598#ifndef FRONTEND
1600
1602 io_start, 1, readbytes);
1603#endif
1604
1605 if (readbytes <= 0)
1606 {
1607 errinfo->wre_errno = errno;
1608 errinfo->wre_req = segbytes;
1609 errinfo->wre_read = readbytes;
1610 errinfo->wre_off = startoff;
1611 errinfo->wre_seg = state->seg;
1612 return false;
1613 }
1614
1615 /* Update state for read */
1616 recptr += readbytes;
1617 nbytes -= readbytes;
1618 p += readbytes;
1619 }
1620
1621 return true;
1622}
size_t Size
Definition c.h:689
static char buf[DEFAULT_XLOG_SEG_SIZE]
@ IOOBJECT_WAL
Definition pgstat.h:283
@ IOCONTEXT_NORMAL
Definition pgstat.h:293
@ IOOP_READ
Definition pgstat.h:319
instr_time pgstat_prepare_io_time(bool track_io_guc)
Definition pgstat_io.c:91
void pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op, instr_time start_time, uint32 cnt, uint64 bytes)
Definition pgstat_io.c:122
#define pg_pread
Definition port.h:247
off_t pgoff_t
Definition port.h:421
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition wait_event.h:67
static void pgstat_report_wait_end(void)
Definition wait_event.h:83
bool track_wal_io_timing
Definition xlog.c:144
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
uint64 XLogRecPtr
Definition xlogdefs.h:21
uint64 XLogSegNo
Definition xlogdefs.h:52

References Assert, buf, fb(), IOCONTEXT_NORMAL, IOOBJECT_WAL, IOOP_READ, pg_pread, pgstat_count_io_op_time(), pgstat_prepare_io_time(), pgstat_report_wait_end(), pgstat_report_wait_start(), track_wal_io_timing, XLByteInSeg, XLByteToSeg, and XLogSegmentOffset.

Referenced by logical_read_xlog_page(), read_local_xlog_page_guts(), summarizer_read_local_xlog_page(), WALDumpReadPage(), and XLogSendPhysical().

◆ XLogBeginRead()

void XLogBeginRead ( XLogReaderState state,
XLogRecPtr  RecPtr 
)
extern

Definition at line 233 of file xlogreader.c.

234{
236
238
239 /* Begin at the passed-in record pointer. */
240 state->EndRecPtr = RecPtr;
241 state->NextRecPtr = RecPtr;
242 state->ReadRecPtr = InvalidXLogRecPtr;
243 state->DecodeRecPtr = InvalidXLogRecPtr;
244}
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
#define InvalidXLogRecPtr
Definition xlogdefs.h:28
static void ResetDecoder(XLogReaderState *state)

References Assert, fb(), InvalidXLogRecPtr, ResetDecoder(), and XLogRecPtrIsValid.

Referenced by DecodingContextFindStartpoint(), extractPageMap(), findLastCheckpoint(), LogicalReplicationSlotCheckPendingWal(), LogicalSlotAdvanceAndCheckSnapState(), pg_logical_slot_get_changes_guts(), readOneRecord(), StartLogicalReplication(), SummarizeWAL(), XLogFindNextRecord(), XLogPrefetcherBeginRead(), and XlogReadTwoPhaseData().

◆ XLogFindNextRecord()

XLogRecPtr XLogFindNextRecord ( XLogReaderState state,
XLogRecPtr  RecPtr,
char **  errormsg 
)
extern

Definition at line 1401 of file xlogreader.c.

1402{
1405 XLogPageHeader header;
1406
1407 *errormsg = NULL;
1408
1410
1411 /* Make sure ReadPageInternal() can't return XLREAD_WOULDBLOCK. */
1412 state->nonblocking = false;
1413
1414 /*
1415 * skip over potential continuation data, keeping in mind that it may span
1416 * multiple pages
1417 */
1418 tmpRecPtr = RecPtr;
1419 while (true)
1420 {
1422 int targetRecOff;
1424 int readLen;
1425
1426 /*
1427 * Compute targetRecOff. It should typically be equal or greater than
1428 * short page-header since a valid record can't start anywhere before
1429 * that, except when caller has explicitly specified the offset that
1430 * falls somewhere there or when we are skipping multi-page
1431 * continuation record. It doesn't matter though because
1432 * ReadPageInternal() is prepared to handle that and will read at
1433 * least short page-header worth of data
1434 */
1436
1437 /* scroll back to page boundary */
1439
1440 /* Read the page containing the record */
1442 if (readLen < 0)
1443 goto err;
1444
1445 header = (XLogPageHeader) state->readBuf;
1446
1448
1449 /* make sure we have enough data for the page header */
1451 if (readLen < 0)
1452 goto err;
1453
1454 /* skip over potential continuation data */
1455 if (header->xlp_info & XLP_FIRST_IS_CONTRECORD)
1456 {
1457 /*
1458 * If the length of the remaining continuation data is more than
1459 * what can fit in this page, the continuation record crosses over
1460 * this page. Read the next page and try again. xlp_rem_len in the
1461 * next page header will contain the remaining length of the
1462 * continuation data
1463 *
1464 * Note that record headers are MAXALIGN'ed
1465 */
1466 if (MAXALIGN(header->xlp_rem_len) >= (XLOG_BLCKSZ - pageHeaderSize))
1468 else
1469 {
1470 /*
1471 * The previous continuation record ends in this page. Set
1472 * tmpRecPtr to point to the first valid record
1473 */
1475 + MAXALIGN(header->xlp_rem_len);
1476 break;
1477 }
1478 }
1479 else
1480 {
1482 break;
1483 }
1484 }
1485
1486 /*
1487 * we know now that tmpRecPtr is an address pointing to a valid XLogRecord
1488 * because either we're at the first record after the beginning of a page
1489 * or we just jumped over the remaining data of a continuation.
1490 */
1492 while (XLogReadRecord(state, errormsg) != NULL)
1493 {
1494 /* past the record we've found, break out */
1495 if (RecPtr <= state->ReadRecPtr)
1496 {
1497 /* Rewind the reader to the beginning of the last record. */
1498 found = state->ReadRecPtr;
1499 XLogBeginRead(state, found);
1500 return found;
1501 }
1502 }
1503
1504err:
1506
1507 /*
1508 * We may have reported errors due to invalid WAL header, propagate the
1509 * error message to the caller.
1510 */
1511 if (state->errormsg_deferred)
1512 {
1513 if (state->errormsg_buf[0] != '\0')
1514 *errormsg = state->errormsg_buf;
1515 state->errormsg_deferred = false;
1516 }
1517
1518 return InvalidXLogRecPtr;
1519}
#define XLP_FIRST_IS_CONTRECORD
XLogPageHeaderData * XLogPageHeader
#define XLogPageHeaderSize(hdr)
static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition xlogreader.c:391
static void XLogReaderInvalReadState(XLogReaderState *state)
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition xlogreader.c:233
static uint32 readLen

References Assert, err(), fb(), InvalidXLogRecPtr, MAXALIGN, readLen, ReadPageInternal(), XLogBeginRead(), XLogPageHeaderSize, XLogReaderInvalReadState(), XLogReadRecord(), XLogRecPtrIsValid, XLP_FIRST_IS_CONTRECORD, XLogPageHeaderData::xlp_info, and XLogPageHeaderData::xlp_rem_len.

Referenced by InitXLogReaderState(), main(), and SummarizeWAL().

◆ XLogNextRecord()

DecodedXLogRecord * XLogNextRecord ( XLogReaderState state,
char **  errormsg 
)
extern

Definition at line 327 of file xlogreader.c.

328{
329 /* Release the last record returned by XLogNextRecord(). */
331
332 if (state->decode_queue_head == NULL)
333 {
334 *errormsg = NULL;
335 if (state->errormsg_deferred)
336 {
337 if (state->errormsg_buf[0] != '\0')
338 *errormsg = state->errormsg_buf;
339 state->errormsg_deferred = false;
340 }
341
342 /*
343 * state->EndRecPtr is expected to have been set by the last call to
344 * XLogBeginRead() or XLogNextRecord(), and is the location of the
345 * error.
346 */
347 Assert(XLogRecPtrIsValid(state->EndRecPtr));
348
349 return NULL;
350 }
351
352 /*
353 * Record this as the most recent record returned, so that we'll release
354 * it next time. This also exposes it to the traditional
355 * XLogRecXXX(xlogreader) macros, which work with the decoder rather than
356 * the record for historical reasons.
357 */
358 state->record = state->decode_queue_head;
359
360 /*
361 * Update the pointers to the beginning and one-past-the-end of this
362 * record, again for the benefit of historical code that expected the
363 * decoder to track this rather than accessing these fields of the record
364 * itself.
365 */
366 state->ReadRecPtr = state->record->lsn;
367 state->EndRecPtr = state->record->next_lsn;
368
369 *errormsg = NULL;
370
371 return state->record;
372}
XLogRecPtr XLogReleasePreviousRecord(XLogReaderState *state)
Definition xlogreader.c:251

References Assert, fb(), XLogRecPtrIsValid, and XLogReleasePreviousRecord().

Referenced by XLogPrefetcherReadRecord(), and XLogReadRecord().

◆ XLogReadAhead()

DecodedXLogRecord * XLogReadAhead ( XLogReaderState state,
bool  nonblocking 
)
extern

Definition at line 978 of file xlogreader.c.

979{
981
982 if (state->errormsg_deferred)
983 return NULL;
984
985 result = XLogDecodeNextRecord(state, nonblocking);
986 if (result == XLREAD_SUCCESS)
987 {
988 Assert(state->decode_queue_tail != NULL);
989 return state->decode_queue_tail;
990 }
991
992 return NULL;
993}
uint32 result
static XLogPageReadResult XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking)
Definition xlogreader.c:530

References Assert, fb(), result, XLogDecodeNextRecord(), and XLREAD_SUCCESS.

Referenced by XLogPrefetcherNextBlock(), and XLogReadRecord().

◆ XLogReaderAllocate()

XLogReaderState * XLogReaderAllocate ( int  wal_segment_size,
const char waldir,
XLogReaderRoutine routine,
void private_data 
)
extern

Definition at line 108 of file xlogreader.c.

110{
112
116 if (!state)
117 return NULL;
118
119 /* initialize caller-provided support functions */
120 state->routine = *routine;
121
122 /*
123 * Permanently allocate readBuf. We do it this way, rather than just
124 * making a static array, for two reasons: (1) no need to waste the
125 * storage in most instantiations of the backend; (2) a static char array
126 * isn't guaranteed to have any particular alignment, whereas
127 * palloc_extended() will provide MAXALIGN'd storage.
128 */
129 state->readBuf = (char *) palloc_extended(XLOG_BLCKSZ,
131 if (!state->readBuf)
132 {
133 pfree(state);
134 return NULL;
135 }
136
137 /* Initialize segment info. */
139 waldir);
140
141 /* system_identifier initialized to zeroes above */
142 state->private_data = private_data;
143 /* ReadRecPtr, EndRecPtr and readLen initialized to zeroes above */
144 state->errormsg_buf = palloc_extended(MAX_ERRORMSG_LEN + 1,
146 if (!state->errormsg_buf)
147 {
148 pfree(state->readBuf);
149 pfree(state);
150 return NULL;
151 }
152 state->errormsg_buf[0] = '\0';
153
154 /*
155 * Allocate an initial readRecordBuf of minimal size, which can later be
156 * enlarged if necessary.
157 */
159 return state;
160}
#define MCXT_ALLOC_ZERO
Definition fe_memutils.h:30
#define MCXT_ALLOC_NO_OOM
Definition fe_memutils.h:29
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc_extended(Size size, int flags)
Definition mcxt.c:1439
int wal_segment_size
Definition xlog.c:150
static void WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt, int segsize, const char *waldir)
Definition xlogreader.c:209
static void static void allocate_recordbuf(XLogReaderState *state, uint32 reclength)
Definition xlogreader.c:192
#define MAX_ERRORMSG_LEN
Definition xlogreader.c:60

References allocate_recordbuf(), fb(), MAX_ERRORMSG_LEN, MCXT_ALLOC_NO_OOM, MCXT_ALLOC_ZERO, palloc_extended(), pfree(), wal_segment_size, and WALOpenSegmentInit().

Referenced by extractPageMap(), findLastCheckpoint(), InitWalRecovery(), InitXLogReaderState(), main(), readOneRecord(), StartReplication(), StartupDecodingContext(), SummarizeWAL(), XLogInsertRecord(), and XlogReadTwoPhaseData().

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)
extern

Definition at line 163 of file xlogreader.c.

164{
165 if (state->seg.ws_file != -1)
166 state->routine.segment_close(state);
167
168 if (state->decode_buffer && state->free_decode_buffer)
169 pfree(state->decode_buffer);
170
171 pfree(state->errormsg_buf);
172 if (state->readRecordBuf)
173 pfree(state->readRecordBuf);
174 pfree(state->readBuf);
175 pfree(state);
176}

References pfree().

Referenced by extractPageMap(), findLastCheckpoint(), FreeDecodingContext(), GetWALRecordsInfo(), GetWalStats(), main(), pg_get_wal_block_info(), pg_get_wal_record_info(), readOneRecord(), ShutdownWalRecovery(), SummarizeWAL(), and XlogReadTwoPhaseData().

◆ XLogReaderHasQueuedRecordOrError()

static bool XLogReaderHasQueuedRecordOrError ( XLogReaderState state)
inlinestatic

Definition at line 324 of file xlogreader.h.

325{
326 return (state->decode_queue_head != NULL) || state->errormsg_deferred;
327}

References fb().

Referenced by XLogPrefetcherNextBlock(), XLogPrefetcherReadRecord(), and XLogReadRecord().

◆ XLogReaderResetError()

void XLogReaderResetError ( XLogReaderState state)
extern

Definition at line 1377 of file xlogreader.c.

1378{
1379 state->errormsg_buf[0] = '\0';
1380 state->errormsg_deferred = false;
1381}

Referenced by XLogPageRead().

◆ XLogReaderSetDecodeBuffer()

void XLogReaderSetDecodeBuffer ( XLogReaderState state,
void buffer,
size_t  size 
)
extern

Definition at line 92 of file xlogreader.c.

93{
94 Assert(state->decode_buffer == NULL);
95
96 state->decode_buffer = buffer;
97 state->decode_buffer_size = size;
98 state->decode_buffer_tail = buffer;
99 state->decode_buffer_head = buffer;
100}

References Assert, and fb().

Referenced by InitWalRecovery().

◆ XLogReaderValidatePageHeader()

bool XLogReaderValidatePageHeader ( XLogReaderState state,
XLogRecPtr  recptr,
char phdr 
)
extern

Definition at line 1236 of file xlogreader.c.

1238{
1239 XLogSegNo segno;
1240 int32 offset;
1241 XLogPageHeader hdr = (XLogPageHeader) phdr;
1242
1243 Assert((recptr % XLOG_BLCKSZ) == 0);
1244
1245 XLByteToSeg(recptr, segno, state->segcxt.ws_segsize);
1246 offset = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
1247
1248 if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
1249 {
1250 char fname[MAXFNAMELEN];
1251
1252 XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1253
1255 "invalid magic number %04X in WAL segment %s, LSN %X/%08X, offset %u",
1256 hdr->xlp_magic,
1257 fname,
1259 offset);
1260 return false;
1261 }
1262
1263 if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
1264 {
1265 char fname[MAXFNAMELEN];
1266
1267 XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1268
1270 "invalid info bits %04X in WAL segment %s, LSN %X/%08X, offset %u",
1271 hdr->xlp_info,
1272 fname,
1274 offset);
1275 return false;
1276 }
1277
1278 if (hdr->xlp_info & XLP_LONG_HEADER)
1279 {
1281
1282 if (state->system_identifier &&
1283 longhdr->xlp_sysid != state->system_identifier)
1284 {
1286 "WAL file is from different database system: WAL file database system identifier is %" PRIu64 ", pg_control database system identifier is %" PRIu64,
1287 longhdr->xlp_sysid,
1288 state->system_identifier);
1289 return false;
1290 }
1291 else if (longhdr->xlp_seg_size != state->segcxt.ws_segsize)
1292 {
1294 "WAL file is from different database system: incorrect segment size in page header");
1295 return false;
1296 }
1297 else if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
1298 {
1300 "WAL file is from different database system: incorrect XLOG_BLCKSZ in page header");
1301 return false;
1302 }
1303 }
1304 else if (offset == 0)
1305 {
1306 char fname[MAXFNAMELEN];
1307
1308 XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1309
1310 /* hmm, first page of file doesn't have a long header? */
1312 "invalid info bits %04X in WAL segment %s, LSN %X/%08X, offset %u",
1313 hdr->xlp_info,
1314 fname,
1316 offset);
1317 return false;
1318 }
1319
1320 /*
1321 * Check that the address on the page agrees with what we expected. This
1322 * check typically fails when an old WAL segment is recycled, and hasn't
1323 * yet been overwritten with new data yet.
1324 */
1325 if (hdr->xlp_pageaddr != recptr)
1326 {
1327 char fname[MAXFNAMELEN];
1328
1329 XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1330
1332 "unexpected pageaddr %X/%08X in WAL segment %s, LSN %X/%08X, offset %u",
1334 fname,
1336 offset);
1337 return false;
1338 }
1339
1340 /*
1341 * Since child timelines are always assigned a TLI greater than their
1342 * immediate parent's TLI, we should never see TLI go backwards across
1343 * successive pages of a consistent WAL sequence.
1344 *
1345 * Sometimes we re-read a segment that's already been (partially) read. So
1346 * we only verify TLIs for pages that are later than the last remembered
1347 * LSN.
1348 */
1349 if (recptr > state->latestPagePtr)
1350 {
1351 if (hdr->xlp_tli < state->latestPageTLI)
1352 {
1353 char fname[MAXFNAMELEN];
1354
1355 XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1356
1358 "out-of-sequence timeline ID %u (after %u) in WAL segment %s, LSN %X/%08X, offset %u",
1359 hdr->xlp_tli,
1360 state->latestPageTLI,
1361 fname,
1363 offset);
1364 return false;
1365 }
1366 }
1367 state->latestPagePtr = recptr;
1368 state->latestPageTLI = hdr->xlp_tli;
1369
1370 return true;
1371}
int32_t int32
Definition c.h:620
XLogRecPtr xlp_pageaddr
XLogLongPageHeaderData * XLogLongPageHeader
#define MAXFNAMELEN
#define XLP_LONG_HEADER
#define XLP_ALL_FLAGS
#define XLOG_PAGE_MAGIC
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)

References Assert, fb(), LSN_FORMAT_ARGS, MAXFNAMELEN, report_invalid_record(), XLByteToSeg, XLOG_PAGE_MAGIC, XLogFileName(), XLogSegmentOffset, XLP_ALL_FLAGS, XLogPageHeaderData::xlp_info, XLP_LONG_HEADER, XLogPageHeaderData::xlp_magic, XLogPageHeaderData::xlp_pageaddr, and XLogPageHeaderData::xlp_tli.

Referenced by ReadPageInternal(), and XLogPageRead().

◆ XLogReadRecord()

struct XLogRecord * XLogReadRecord ( XLogReaderState state,
char **  errormsg 
)
extern

Definition at line 391 of file xlogreader.c.

392{
394
395 /*
396 * Release last returned record, if there is one. We need to do this so
397 * that we can check for empty decode queue accurately.
398 */
400
401 /*
402 * Call XLogReadAhead() in blocking mode to make sure there is something
403 * in the queue, though we don't use the result.
404 */
406 XLogReadAhead(state, false /* nonblocking */ );
407
408 /* Consume the head record or error. */
409 decoded = XLogNextRecord(state, errormsg);
410 if (decoded)
411 {
412 /*
413 * This function returns a pointer to the record's header, not the
414 * actual decoded record. The caller will access the decoded record
415 * through the XLogRecGetXXX() macros, which reach the decoded
416 * recorded as xlogreader->record.
417 */
418 Assert(state->record == decoded);
419 return &decoded->header;
420 }
421
422 return NULL;
423}
DecodedXLogRecord * XLogReadAhead(XLogReaderState *state, bool nonblocking)
Definition xlogreader.c:978
DecodedXLogRecord * XLogNextRecord(XLogReaderState *state, char **errormsg)
Definition xlogreader.c:327
static bool XLogReaderHasQueuedRecordOrError(XLogReaderState *state)
Definition xlogreader.h:324

References Assert, fb(), XLogNextRecord(), XLogReadAhead(), XLogReaderHasQueuedRecordOrError(), and XLogReleasePreviousRecord().

Referenced by decode_concurrent_changes(), DecodingContextFindStartpoint(), extractPageMap(), findLastCheckpoint(), LogicalReplicationSlotCheckPendingWal(), LogicalSlotAdvanceAndCheckSnapState(), main(), pg_logical_slot_get_changes_guts(), ReadNextXLogRecord(), readOneRecord(), SummarizeWAL(), XLogFindNextRecord(), XlogReadTwoPhaseData(), and XLogSendLogical().

◆ XLogRecGetBlockData()

char * XLogRecGetBlockData ( XLogReaderState record,
uint8  block_id,
Size len 
)
extern

◆ XLogRecGetBlockTag()

void XLogRecGetBlockTag ( XLogReaderState record,
uint8  block_id,
RelFileLocator rlocator,
ForkNumber forknum,
BlockNumber blknum 
)
extern

Definition at line 2010 of file xlogreader.c.

2013{
2014 if (!XLogRecGetBlockTagExtended(record, block_id, rlocator, forknum,
2015 blknum, NULL))
2016 {
2017#ifndef FRONTEND
2018 elog(ERROR, "could not locate backup block with ID %d in WAL record",
2019 block_id);
2020#else
2021 pg_fatal("could not locate backup block with ID %d in WAL record",
2022 block_id);
2023#endif
2024 }
2025}
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define pg_fatal(...)
bool XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)

References elog, ERROR, fb(), pg_fatal, and XLogRecGetBlockTagExtended().

Referenced by brin_xlog_revmap_extend(), btree_xlog_delete(), btree_xlog_split(), DecodeDelete(), DecodeInsert(), DecodeMultiInsert(), DecodeSpecConfirm(), DecodeUpdate(), gistRedoDeleteRecord(), gistRedoPageSplitRecord(), hash_xlog_add_ovfl_page(), hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), hash_xlog_vacuum_one_page(), heap_xlog_delete(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_prune_freeze(), heap_xlog_update(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), and spgRedoVacuumRedirect().

◆ XLogRecGetBlockTagExtended()

bool XLogRecGetBlockTagExtended ( XLogReaderState record,
uint8  block_id,
RelFileLocator rlocator,
ForkNumber forknum,
BlockNumber blknum,
Buffer prefetch_buffer 
)
extern

Definition at line 2036 of file xlogreader.c.

2040{
2042
2043 if (!XLogRecHasBlockRef(record, block_id))
2044 return false;
2045
2046 bkpb = &record->record->blocks[block_id];
2047 if (rlocator)
2048 *rlocator = bkpb->rlocator;
2049 if (forknum)
2050 *forknum = bkpb->forknum;
2051 if (blknum)
2052 *blknum = bkpb->blkno;
2053 if (prefetch_buffer)
2054 *prefetch_buffer = bkpb->prefetch_buffer;
2055 return true;
2056}
#define XLogRecHasBlockRef(decoder, block_id)
Definition xlogreader.h:420

References DecodedXLogRecord::blocks, fb(), XLogReaderState::record, and XLogRecHasBlockRef.

Referenced by btree_xlog_split(), change_useless_for_repack(), extractPageInfo(), GetWALBlockInfo(), heap_xlog_update(), SummarizeWAL(), verifyBackupPageConsistency(), xlog_block_info(), XLogReadBufferForRedoExtended(), XLogRecGetBlockRefInfo(), XLogRecGetBlockTag(), XLogRecordMatchesRelationBlock(), and XLogRecordSaveFPWs().

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)
extern

Definition at line 2206 of file xlogreader.c.

2207{
2208 /*
2209 * This function is only safe during replay, because it depends on the
2210 * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
2211 */
2213
2215 XLogRecGetXid(record));
2216}
bool IsUnderPostmaster
Definition globals.c:122
#define AmStartupProcess()
Definition miscadmin.h:405
FullTransactionId nextXid
Definition transam.h:220
static FullTransactionId FullTransactionIdFromAllowableAt(FullTransactionId nextFullXid, TransactionId xid)
Definition transam.h:441
TransamVariablesData * TransamVariables
Definition varsup.c:37
#define XLogRecGetXid(decoder)
Definition xlogreader.h:412

References AmStartupProcess, Assert, FullTransactionIdFromAllowableAt(), IsUnderPostmaster, TransamVariablesData::nextXid, TransamVariables, and XLogRecGetXid.

◆ XLogReleasePreviousRecord()

XLogRecPtr XLogReleasePreviousRecord ( XLogReaderState state)
extern

Definition at line 251 of file xlogreader.c.

252{
253 DecodedXLogRecord *record;
254 XLogRecPtr next_lsn;
255
256 if (!state->record)
257 return InvalidXLogRecPtr;
258
259 /*
260 * Remove it from the decoded record queue. It must be the oldest item
261 * decoded, decode_queue_head.
262 */
263 record = state->record;
264 next_lsn = record->next_lsn;
265 Assert(record == state->decode_queue_head);
266 state->record = NULL;
267 state->decode_queue_head = record->next;
268
269 /* It might also be the newest item decoded, decode_queue_tail. */
270 if (state->decode_queue_tail == record)
271 state->decode_queue_tail = NULL;
272
273 /* Release the space. */
274 if (unlikely(record->oversized))
275 {
276 /* It's not in the decode buffer, so free it to release space. */
277 pfree(record);
278 }
279 else
280 {
281 /* It must be the head (oldest) record in the decode buffer. */
282 Assert(state->decode_buffer_head == (char *) record);
283
284 /*
285 * We need to update head to point to the next record that is in the
286 * decode buffer, if any, being careful to skip oversized ones
287 * (they're not in the decode buffer).
288 */
289 record = record->next;
290 while (unlikely(record && record->oversized))
291 record = record->next;
292
293 if (record)
294 {
295 /* Adjust head to release space up to the next record. */
296 state->decode_buffer_head = (char *) record;
297 }
298 else
299 {
300 /*
301 * Otherwise we might as well just reset head and tail to the
302 * start of the buffer space, because we're empty. This means
303 * we'll keep overwriting the same piece of memory if we're not
304 * doing any prefetching.
305 */
306 state->decode_buffer_head = state->decode_buffer;
307 state->decode_buffer_tail = state->decode_buffer;
308 }
309 }
310
311 return next_lsn;
312}
#define unlikely(x)
Definition c.h:438
XLogRecPtr next_lsn
Definition xlogreader.h:164
struct DecodedXLogRecord * next
Definition xlogreader.h:160

References Assert, fb(), InvalidXLogRecPtr, DecodedXLogRecord::next, DecodedXLogRecord::next_lsn, DecodedXLogRecord::oversized, pfree(), and unlikely.

Referenced by XLogNextRecord(), XLogPrefetcherReadRecord(), and XLogReadRecord().