PostgreSQL Source Code git master
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)
 
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)
 
char * XLogRecGetBlockData (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))

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

typedef struct WALReadError WALReadError

◆ WALSegmentCloseCB

typedef void(* WALSegmentCloseCB) (XLogReaderState *xlogreader)

Definition at line 70 of file xlogreader.h.

◆ WALSegmentContext

◆ WALSegmentOpenCB

typedef void(* WALSegmentOpenCB) (XLogReaderState *xlogreader, XLogSegNo nextSegNo, TimeLineID *tli_p)

Definition at line 67 of file xlogreader.h.

◆ XLogPageReadCB

typedef int(* XLogPageReadCB) (XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf)

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 
)

Definition at line 1672 of file xlogreader.c.

1677{
1678 /*
1679 * read next _size bytes from record buffer, but check for overrun first.
1680 */
1681#define COPY_HEADER_FIELD(_dst, _size) \
1682 do { \
1683 if (remaining < _size) \
1684 goto shortdata_err; \
1685 memcpy(_dst, ptr, _size); \
1686 ptr += _size; \
1687 remaining -= _size; \
1688 } while(0)
1689
1690 char *ptr;
1691 char *out;
1693 uint32 datatotal;
1694 RelFileLocator *rlocator = NULL;
1695 uint8 block_id;
1696
1697 decoded->header = *record;
1698 decoded->lsn = lsn;
1699 decoded->next = NULL;
1702 decoded->main_data = NULL;
1703 decoded->main_data_len = 0;
1704 decoded->max_block_id = -1;
1705 ptr = (char *) record;
1706 ptr += SizeOfXLogRecord;
1708
1709 /* Decode the headers */
1710 datatotal = 0;
1711 while (remaining > datatotal)
1712 {
1713 COPY_HEADER_FIELD(&block_id, sizeof(uint8));
1714
1715 if (block_id == XLR_BLOCK_ID_DATA_SHORT)
1716 {
1717 /* XLogRecordDataHeaderShort */
1718 uint8 main_data_len;
1719
1720 COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
1721
1722 decoded->main_data_len = main_data_len;
1723 datatotal += main_data_len;
1724 break; /* by convention, the main data fragment is
1725 * always last */
1726 }
1727 else if (block_id == XLR_BLOCK_ID_DATA_LONG)
1728 {
1729 /* XLogRecordDataHeaderLong */
1730 uint32 main_data_len;
1731
1732 COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
1733 decoded->main_data_len = main_data_len;
1734 datatotal += main_data_len;
1735 break; /* by convention, the main data fragment is
1736 * always last */
1737 }
1738 else if (block_id == XLR_BLOCK_ID_ORIGIN)
1739 {
1740 COPY_HEADER_FIELD(&decoded->record_origin, sizeof(RepOriginId));
1741 }
1742 else if (block_id == XLR_BLOCK_ID_TOPLEVEL_XID)
1743 {
1744 COPY_HEADER_FIELD(&decoded->toplevel_xid, sizeof(TransactionId));
1745 }
1746 else if (block_id <= XLR_MAX_BLOCK_ID)
1747 {
1748 /* XLogRecordBlockHeader */
1749 DecodedBkpBlock *blk;
1750 uint8 fork_flags;
1751
1752 /* mark any intervening block IDs as not in use */
1753 for (int i = decoded->max_block_id + 1; i < block_id; ++i)
1754 decoded->blocks[i].in_use = false;
1755
1756 if (block_id <= decoded->max_block_id)
1757 {
1759 "out-of-order block_id %u at %X/%X",
1760 block_id,
1761 LSN_FORMAT_ARGS(state->ReadRecPtr));
1762 goto err;
1763 }
1764 decoded->max_block_id = block_id;
1765
1766 blk = &decoded->blocks[block_id];
1767 blk->in_use = true;
1768 blk->apply_image = false;
1769
1770 COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
1771 blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
1772 blk->flags = fork_flags;
1773 blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
1774 blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
1775
1777
1778 COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
1779 /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
1780 if (blk->has_data && blk->data_len == 0)
1781 {
1783 "BKPBLOCK_HAS_DATA set, but no data included at %X/%X",
1784 LSN_FORMAT_ARGS(state->ReadRecPtr));
1785 goto err;
1786 }
1787 if (!blk->has_data && blk->data_len != 0)
1788 {
1790 "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%X",
1791 (unsigned int) blk->data_len,
1792 LSN_FORMAT_ARGS(state->ReadRecPtr));
1793 goto err;
1794 }
1795 datatotal += blk->data_len;
1796
1797 if (blk->has_image)
1798 {
1799 COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
1800 COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
1801 COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
1802
1803 blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
1804
1806 {
1807 if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
1808 COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
1809 else
1810 blk->hole_length = 0;
1811 }
1812 else
1813 blk->hole_length = BLCKSZ - blk->bimg_len;
1814 datatotal += blk->bimg_len;
1815
1816 /*
1817 * cross-check that hole_offset > 0, hole_length > 0 and
1818 * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
1819 */
1820 if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1821 (blk->hole_offset == 0 ||
1822 blk->hole_length == 0 ||
1823 blk->bimg_len == BLCKSZ))
1824 {
1826 "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%X",
1827 (unsigned int) blk->hole_offset,
1828 (unsigned int) blk->hole_length,
1829 (unsigned int) blk->bimg_len,
1830 LSN_FORMAT_ARGS(state->ReadRecPtr));
1831 goto err;
1832 }
1833
1834 /*
1835 * cross-check that hole_offset == 0 and hole_length == 0 if
1836 * the HAS_HOLE flag is not set.
1837 */
1838 if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1839 (blk->hole_offset != 0 || blk->hole_length != 0))
1840 {
1842 "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%X",
1843 (unsigned int) blk->hole_offset,
1844 (unsigned int) blk->hole_length,
1845 LSN_FORMAT_ARGS(state->ReadRecPtr));
1846 goto err;
1847 }
1848
1849 /*
1850 * Cross-check that bimg_len < BLCKSZ if it is compressed.
1851 */
1852 if (BKPIMAGE_COMPRESSED(blk->bimg_info) &&
1853 blk->bimg_len == BLCKSZ)
1854 {
1856 "BKPIMAGE_COMPRESSED set, but block image length %u at %X/%X",
1857 (unsigned int) blk->bimg_len,
1858 LSN_FORMAT_ARGS(state->ReadRecPtr));
1859 goto err;
1860 }
1861
1862 /*
1863 * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE is
1864 * set nor COMPRESSED().
1865 */
1866 if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1868 blk->bimg_len != BLCKSZ)
1869 {
1871 "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_COMPRESSED set, but block image length is %u at %X/%X",
1872 (unsigned int) blk->data_len,
1873 LSN_FORMAT_ARGS(state->ReadRecPtr));
1874 goto err;
1875 }
1876 }
1877 if (!(fork_flags & BKPBLOCK_SAME_REL))
1878 {
1880 rlocator = &blk->rlocator;
1881 }
1882 else
1883 {
1884 if (rlocator == NULL)
1885 {
1887 "BKPBLOCK_SAME_REL set but no previous rel at %X/%X",
1888 LSN_FORMAT_ARGS(state->ReadRecPtr));
1889 goto err;
1890 }
1891
1892 blk->rlocator = *rlocator;
1893 }
1894 COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
1895 }
1896 else
1897 {
1899 "invalid block_id %u at %X/%X",
1900 block_id, LSN_FORMAT_ARGS(state->ReadRecPtr));
1901 goto err;
1902 }
1903 }
1904
1905 if (remaining != datatotal)
1906 goto shortdata_err;
1907
1908 /*
1909 * Ok, we've parsed the fragment headers, and verified that the total
1910 * length of the payload in the fragments is equal to the amount of data
1911 * left. Copy the data of each fragment to contiguous space after the
1912 * blocks array, inserting alignment padding before the data fragments so
1913 * they can be cast to struct pointers by REDO routines.
1914 */
1915 out = ((char *) decoded) +
1916 offsetof(DecodedXLogRecord, blocks) +
1917 sizeof(decoded->blocks[0]) * (decoded->max_block_id + 1);
1918
1919 /* block data first */
1920 for (block_id = 0; block_id <= decoded->max_block_id; block_id++)
1921 {
1922 DecodedBkpBlock *blk = &decoded->blocks[block_id];
1923
1924 if (!blk->in_use)
1925 continue;
1926
1927 Assert(blk->has_image || !blk->apply_image);
1928
1929 if (blk->has_image)
1930 {
1931 /* no need to align image */
1932 blk->bkp_image = out;
1933 memcpy(out, ptr, blk->bimg_len);
1934 ptr += blk->bimg_len;
1935 out += blk->bimg_len;
1936 }
1937 if (blk->has_data)
1938 {
1939 out = (char *) MAXALIGN(out);
1940 blk->data = out;
1941 memcpy(blk->data, ptr, blk->data_len);
1942 ptr += blk->data_len;
1943 out += blk->data_len;
1944 }
1945 }
1946
1947 /* and finally, the main data */
1948 if (decoded->main_data_len > 0)
1949 {
1950 out = (char *) MAXALIGN(out);
1951 decoded->main_data = out;
1952 memcpy(decoded->main_data, ptr, decoded->main_data_len);
1953 ptr += decoded->main_data_len;
1954 out += decoded->main_data_len;
1955 }
1956
1957 /* Report the actual size we used. */
1958 decoded->size = MAXALIGN(out - (char *) decoded);
1960 decoded->size);
1961
1962 return true;
1963
1964shortdata_err:
1966 "record with invalid length at %X/%X",
1967 LSN_FORMAT_ARGS(state->ReadRecPtr));
1968err:
1969 *errormsg = state->errormsg_buf;
1970
1971 return false;
1972}
uint32 BlockNumber
Definition: block.h:31
#define InvalidBuffer
Definition: buf.h:25
#define MAXALIGN(LEN)
Definition: c.h:768
uint8_t uint8
Definition: c.h:486
#define Assert(condition)
Definition: c.h:815
uint16_t uint16
Definition: c.h:487
uint32_t uint32
Definition: c.h:488
uint32 TransactionId
Definition: c.h:609
void err(int eval, const char *fmt,...)
Definition: err.c:43
int remaining
Definition: informix.c:692
int i
Definition: isn.c:72
#define InvalidRepOriginId
Definition: origin.h:33
uint16 hole_length
Definition: xlogreader.h:140
char * bkp_image
Definition: xlogreader.h:138
Buffer prefetch_buffer
Definition: xlogreader.h:130
RelFileLocator rlocator
Definition: xlogreader.h:125
BlockNumber blkno
Definition: xlogreader.h:127
ForkNumber forknum
Definition: xlogreader.h:126
uint16 hole_offset
Definition: xlogreader.h:139
XLogRecord header
Definition: xlogreader.h:166
struct DecodedXLogRecord * next
Definition: xlogreader.h:161
TransactionId toplevel_xid
Definition: xlogreader.h:168
uint32 main_data_len
Definition: xlogreader.h:170
RepOriginId record_origin
Definition: xlogreader.h:167
DecodedBkpBlock blocks[FLEXIBLE_ARRAY_MEMBER]
Definition: xlogreader.h:172
XLogRecPtr lsn
Definition: xlogreader.h:164
uint32 xl_tot_len
Definition: xlogrecord.h:43
Definition: regguts.h:323
#define InvalidTransactionId
Definition: transam.h:31
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
uint16 RepOriginId
Definition: xlogdefs.h:65
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:72
#define COPY_HEADER_FIELD(_dst, _size)
size_t DecodeXLogRecordRequiredSpace(size_t xl_tot_len)
Definition: xlogreader.c:1639
#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 DecodedBkpBlock::apply_image, Assert, DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, DecodedBkpBlock::bkp_image, BKPBLOCK_FORK_MASK, BKPBLOCK_HAS_DATA, BKPBLOCK_HAS_IMAGE, BKPBLOCK_SAME_REL, BKPIMAGE_APPLY, BKPIMAGE_COMPRESSED, BKPIMAGE_HAS_HOLE, DecodedBkpBlock::blkno, DecodedXLogRecord::blocks, COPY_HEADER_FIELD, DecodedBkpBlock::data, DecodedBkpBlock::data_len, DecodeXLogRecordRequiredSpace(), err(), DecodedBkpBlock::flags, DecodedBkpBlock::forknum, DecodedBkpBlock::has_data, DecodedBkpBlock::has_image, DecodedXLogRecord::header, DecodedBkpBlock::hole_length, DecodedBkpBlock::hole_offset, i, DecodedBkpBlock::in_use, InvalidBuffer, InvalidRepOriginId, InvalidTransactionId, DecodedXLogRecord::lsn, LSN_FORMAT_ARGS, DecodedXLogRecord::main_data, DecodedXLogRecord::main_data_len, DecodedXLogRecord::max_block_id, MAXALIGN, DecodedXLogRecord::next, DecodedBkpBlock::prefetch_buffer, DecodedXLogRecord::record_origin, remaining, report_invalid_record(), DecodedBkpBlock::rlocator, DecodedXLogRecord::size, SizeOfXLogRecord, DecodedXLogRecord::toplevel_xid, 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)

Definition at line 1639 of file xlogreader.c.

1640{
1641 size_t size = 0;
1642
1643 /* Account for the fixed size part of the decoded record struct. */
1644 size += offsetof(DecodedXLogRecord, blocks[0]);
1645 /* Account for the flexible blocks array of maximum possible size. */
1646 size += sizeof(DecodedBkpBlock) * (XLR_MAX_BLOCK_ID + 1);
1647 /* Account for all the raw main and block data. */
1648 size += xl_tot_len;
1649 /* We might insert padding before main_data. */
1650 size += (MAXIMUM_ALIGNOF - 1);
1651 /* We might insert padding before each block's data. */
1652 size += (MAXIMUM_ALIGNOF - 1) * (XLR_MAX_BLOCK_ID + 1);
1653 /* We might insert padding at the end. */
1654 size += (MAXIMUM_ALIGNOF - 1);
1655
1656 return size;
1657}
static pg_noinline void Size size
Definition: slab.c:607

References size, and XLR_MAX_BLOCK_ID.

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

◆ RestoreBlockImage()

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

Definition at line 2066 of file xlogreader.c.

2067{
2068 DecodedBkpBlock *bkpb;
2069 char *ptr;
2070 PGAlignedBlock tmp;
2071
2072 if (block_id > record->record->max_block_id ||
2073 !record->record->blocks[block_id].in_use)
2074 {
2075 report_invalid_record(record,
2076 "could not restore image at %X/%X with invalid block %d specified",
2077 LSN_FORMAT_ARGS(record->ReadRecPtr),
2078 block_id);
2079 return false;
2080 }
2081 if (!record->record->blocks[block_id].has_image)
2082 {
2083 report_invalid_record(record, "could not restore image at %X/%X with invalid state, block %d",
2084 LSN_FORMAT_ARGS(record->ReadRecPtr),
2085 block_id);
2086 return false;
2087 }
2088
2089 bkpb = &record->record->blocks[block_id];
2090 ptr = bkpb->bkp_image;
2091
2092 if (BKPIMAGE_COMPRESSED(bkpb->bimg_info))
2093 {
2094 /* If a backup block image is compressed, decompress it */
2095 bool decomp_success = true;
2096
2097 if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_PGLZ) != 0)
2098 {
2099 if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
2100 BLCKSZ - bkpb->hole_length, true) < 0)
2101 decomp_success = false;
2102 }
2103 else if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_LZ4) != 0)
2104 {
2105#ifdef USE_LZ4
2106 if (LZ4_decompress_safe(ptr, tmp.data,
2107 bkpb->bimg_len, BLCKSZ - bkpb->hole_length) <= 0)
2108 decomp_success = false;
2109#else
2110 report_invalid_record(record, "could not restore image at %X/%X compressed with %s not supported by build, block %d",
2111 LSN_FORMAT_ARGS(record->ReadRecPtr),
2112 "LZ4",
2113 block_id);
2114 return false;
2115#endif
2116 }
2117 else if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_ZSTD) != 0)
2118 {
2119#ifdef USE_ZSTD
2120 size_t decomp_result = ZSTD_decompress(tmp.data,
2121 BLCKSZ - bkpb->hole_length,
2122 ptr, bkpb->bimg_len);
2123
2124 if (ZSTD_isError(decomp_result))
2125 decomp_success = false;
2126#else
2127 report_invalid_record(record, "could not restore image at %X/%X compressed with %s not supported by build, block %d",
2128 LSN_FORMAT_ARGS(record->ReadRecPtr),
2129 "zstd",
2130 block_id);
2131 return false;
2132#endif
2133 }
2134 else
2135 {
2136 report_invalid_record(record, "could not restore image at %X/%X compressed with unknown method, block %d",
2137 LSN_FORMAT_ARGS(record->ReadRecPtr),
2138 block_id);
2139 return false;
2140 }
2141
2142 if (!decomp_success)
2143 {
2144 report_invalid_record(record, "could not decompress image at %X/%X, block %d",
2145 LSN_FORMAT_ARGS(record->ReadRecPtr),
2146 block_id);
2147 return false;
2148 }
2149
2150 ptr = tmp.data;
2151 }
2152
2153 /* generate page, taking into account hole if necessary */
2154 if (bkpb->hole_length == 0)
2155 {
2156 memcpy(page, ptr, BLCKSZ);
2157 }
2158 else
2159 {
2160 memcpy(page, ptr, bkpb->hole_offset);
2161 /* must zero-fill the hole */
2162 MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
2163 memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
2164 ptr + bkpb->hole_offset,
2165 BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
2166 }
2167
2168 return true;
2169}
#define MemSet(start, val, len)
Definition: c.h:977
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
DecodedXLogRecord * record
Definition: xlogreader.h:236
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:206
char data[BLCKSZ]
Definition: c.h:1076
#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::bimg_info, DecodedBkpBlock::bimg_len, DecodedBkpBlock::bkp_image, BKPIMAGE_COMPRESS_LZ4, BKPIMAGE_COMPRESS_PGLZ, BKPIMAGE_COMPRESS_ZSTD, BKPIMAGE_COMPRESSED, DecodedXLogRecord::blocks, PGAlignedBlock::data, DecodedBkpBlock::has_image, DecodedBkpBlock::hole_length, DecodedBkpBlock::hole_offset, DecodedBkpBlock::in_use, LSN_FORMAT_ARGS, DecodedXLogRecord::max_block_id, 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 
)

Definition at line 1504 of file xlogreader.c.

1507{
1508 char *p;
1509 XLogRecPtr recptr;
1510 Size nbytes;
1511#ifndef FRONTEND
1512 instr_time io_start;
1513#endif
1514
1515 p = buf;
1516 recptr = startptr;
1517 nbytes = count;
1518
1519 while (nbytes > 0)
1520 {
1521 uint32 startoff;
1522 int segbytes;
1523 int readbytes;
1524
1525 startoff = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
1526
1527 /*
1528 * If the data we want is not in a segment we have open, close what we
1529 * have (if anything) and open the next one, using the caller's
1530 * provided segment_open callback.
1531 */
1532 if (state->seg.ws_file < 0 ||
1533 !XLByteInSeg(recptr, state->seg.ws_segno, state->segcxt.ws_segsize) ||
1534 tli != state->seg.ws_tli)
1535 {
1536 XLogSegNo nextSegNo;
1537
1538 if (state->seg.ws_file >= 0)
1539 state->routine.segment_close(state);
1540
1541 XLByteToSeg(recptr, nextSegNo, state->segcxt.ws_segsize);
1542 state->routine.segment_open(state, nextSegNo, &tli);
1543
1544 /* This shouldn't happen -- indicates a bug in segment_open */
1545 Assert(state->seg.ws_file >= 0);
1546
1547 /* Update the current segment info. */
1548 state->seg.ws_tli = tli;
1549 state->seg.ws_segno = nextSegNo;
1550 }
1551
1552 /* How many bytes are within this segment? */
1553 if (nbytes > (state->segcxt.ws_segsize - startoff))
1554 segbytes = state->segcxt.ws_segsize - startoff;
1555 else
1556 segbytes = nbytes;
1557
1558#ifndef FRONTEND
1559 /* Measure I/O timing when reading segment */
1561
1562 pgstat_report_wait_start(WAIT_EVENT_WAL_READ);
1563#endif
1564
1565 /* Reset errno first; eases reporting non-errno-affecting errors */
1566 errno = 0;
1567 readbytes = pg_pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
1568
1569#ifndef FRONTEND
1571
1573 io_start, 1, readbytes);
1574#endif
1575
1576 if (readbytes <= 0)
1577 {
1578 errinfo->wre_errno = errno;
1579 errinfo->wre_req = segbytes;
1580 errinfo->wre_read = readbytes;
1581 errinfo->wre_off = startoff;
1582 errinfo->wre_seg = state->seg;
1583 return false;
1584 }
1585
1586 /* Update state for read */
1587 recptr += readbytes;
1588 nbytes -= readbytes;
1589 p += readbytes;
1590 }
1591
1592 return true;
1593}
bool track_io_timing
Definition: bufmgr.c:143
size_t Size
Definition: c.h:562
static char * buf
Definition: pg_test_fsync.c:72
@ IOOBJECT_WAL
Definition: pgstat.h:277
@ IOCONTEXT_NORMAL
Definition: pgstat.h:287
@ IOOP_READ
Definition: pgstat.h:313
instr_time pgstat_prepare_io_time(bool track_io_guc)
Definition: pgstat_io.c:90
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:120
#define pg_pread
Definition: port.h:226
WALOpenSegment wre_seg
Definition: xlogreader.h:388
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:85
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101
#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:48

References Assert, buf, 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_io_timing, WALReadError::wre_errno, WALReadError::wre_off, WALReadError::wre_read, WALReadError::wre_req, WALReadError::wre_seg, 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 
)

Definition at line 232 of file xlogreader.c.

233{
234 Assert(!XLogRecPtrIsInvalid(RecPtr));
235
237
238 /* Begin at the passed-in record pointer. */
239 state->EndRecPtr = RecPtr;
240 state->NextRecPtr = RecPtr;
241 state->ReadRecPtr = InvalidXLogRecPtr;
242 state->DecodeRecPtr = InvalidXLogRecPtr;
243}
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1605

References Assert, InvalidXLogRecPtr, ResetDecoder(), and XLogRecPtrIsInvalid.

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

◆ XLogFindNextRecord()

XLogRecPtr XLogFindNextRecord ( XLogReaderState state,
XLogRecPtr  RecPtr 
)

Definition at line 1384 of file xlogreader.c.

1385{
1386 XLogRecPtr tmpRecPtr;
1388 XLogPageHeader header;
1389 char *errormsg;
1390
1391 Assert(!XLogRecPtrIsInvalid(RecPtr));
1392
1393 /* Make sure ReadPageInternal() can't return XLREAD_WOULDBLOCK. */
1394 state->nonblocking = false;
1395
1396 /*
1397 * skip over potential continuation data, keeping in mind that it may span
1398 * multiple pages
1399 */
1400 tmpRecPtr = RecPtr;
1401 while (true)
1402 {
1403 XLogRecPtr targetPagePtr;
1404 int targetRecOff;
1405 uint32 pageHeaderSize;
1406 int readLen;
1407
1408 /*
1409 * Compute targetRecOff. It should typically be equal or greater than
1410 * short page-header since a valid record can't start anywhere before
1411 * that, except when caller has explicitly specified the offset that
1412 * falls somewhere there or when we are skipping multi-page
1413 * continuation record. It doesn't matter though because
1414 * ReadPageInternal() is prepared to handle that and will read at
1415 * least short page-header worth of data
1416 */
1417 targetRecOff = tmpRecPtr % XLOG_BLCKSZ;
1418
1419 /* scroll back to page boundary */
1420 targetPagePtr = tmpRecPtr - targetRecOff;
1421
1422 /* Read the page containing the record */
1423 readLen = ReadPageInternal(state, targetPagePtr, targetRecOff);
1424 if (readLen < 0)
1425 goto err;
1426
1427 header = (XLogPageHeader) state->readBuf;
1428
1429 pageHeaderSize = XLogPageHeaderSize(header);
1430
1431 /* make sure we have enough data for the page header */
1432 readLen = ReadPageInternal(state, targetPagePtr, pageHeaderSize);
1433 if (readLen < 0)
1434 goto err;
1435
1436 /* skip over potential continuation data */
1437 if (header->xlp_info & XLP_FIRST_IS_CONTRECORD)
1438 {
1439 /*
1440 * If the length of the remaining continuation data is more than
1441 * what can fit in this page, the continuation record crosses over
1442 * this page. Read the next page and try again. xlp_rem_len in the
1443 * next page header will contain the remaining length of the
1444 * continuation data
1445 *
1446 * Note that record headers are MAXALIGN'ed
1447 */
1448 if (MAXALIGN(header->xlp_rem_len) >= (XLOG_BLCKSZ - pageHeaderSize))
1449 tmpRecPtr = targetPagePtr + XLOG_BLCKSZ;
1450 else
1451 {
1452 /*
1453 * The previous continuation record ends in this page. Set
1454 * tmpRecPtr to point to the first valid record
1455 */
1456 tmpRecPtr = targetPagePtr + pageHeaderSize
1457 + MAXALIGN(header->xlp_rem_len);
1458 break;
1459 }
1460 }
1461 else
1462 {
1463 tmpRecPtr = targetPagePtr + pageHeaderSize;
1464 break;
1465 }
1466 }
1467
1468 /*
1469 * we know now that tmpRecPtr is an address pointing to a valid XLogRecord
1470 * because either we're at the first record after the beginning of a page
1471 * or we just jumped over the remaining data of a continuation.
1472 */
1473 XLogBeginRead(state, tmpRecPtr);
1474 while (XLogReadRecord(state, &errormsg) != NULL)
1475 {
1476 /* past the record we've found, break out */
1477 if (RecPtr <= state->ReadRecPtr)
1478 {
1479 /* Rewind the reader to the beginning of the last record. */
1480 found = state->ReadRecPtr;
1481 XLogBeginRead(state, found);
1482 return found;
1483 }
1484 }
1485
1486err:
1488
1489 return InvalidXLogRecPtr;
1490}
#define XLP_FIRST_IS_CONTRECORD
Definition: xlog_internal.h:74
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:54
#define XLogPageHeaderSize(hdr)
Definition: xlog_internal.h:84
static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
Definition: xlogreader.c:1001
XLogRecord * XLogReadRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:390
static void XLogReaderInvalReadState(XLogReaderState *state)
Definition: xlogreader.c:1114
void XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
Definition: xlogreader.c:232
static uint32 readLen
Definition: xlogrecovery.c:234

References Assert, err(), InvalidXLogRecPtr, MAXALIGN, readLen, ReadPageInternal(), XLogBeginRead(), XLogPageHeaderSize, XLogReaderInvalReadState(), XLogReadRecord(), XLogRecPtrIsInvalid, 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 
)

Definition at line 326 of file xlogreader.c.

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

References Assert, XLogRecPtrIsInvalid, and XLogReleasePreviousRecord().

Referenced by XLogPrefetcherReadRecord(), and XLogReadRecord().

◆ XLogReadAhead()

DecodedXLogRecord * XLogReadAhead ( XLogReaderState state,
bool  nonblocking 
)

Definition at line 967 of file xlogreader.c.

968{
969 XLogPageReadResult result;
970
971 if (state->errormsg_deferred)
972 return NULL;
973
974 result = XLogDecodeNextRecord(state, nonblocking);
975 if (result == XLREAD_SUCCESS)
976 {
977 Assert(state->decode_queue_tail != NULL);
978 return state->decode_queue_tail;
979 }
980
981 return NULL;
982}
static XLogPageReadResult XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking)
Definition: xlogreader.c:529

References Assert, XLogDecodeNextRecord(), and XLREAD_SUCCESS.

Referenced by XLogPrefetcherNextBlock(), and XLogReadRecord().

◆ XLogReaderAllocate()

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

Definition at line 107 of file xlogreader.c.

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

References allocate_recordbuf(), 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)

Definition at line 162 of file xlogreader.c.

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

References pfree().

Referenced by extractPageMap(), findLastCheckpoint(), FreeDecodingContext(), GetWALRecordsInfo(), 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 325 of file xlogreader.h.

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

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

◆ XLogReaderResetError()

void XLogReaderResetError ( XLogReaderState state)

Definition at line 1366 of file xlogreader.c.

1367{
1368 state->errormsg_buf[0] = '\0';
1369 state->errormsg_deferred = false;
1370}

Referenced by XLogPageRead().

◆ XLogReaderSetDecodeBuffer()

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

Definition at line 91 of file xlogreader.c.

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

References Assert, and size.

Referenced by InitWalRecovery().

◆ XLogReaderValidatePageHeader()

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

Definition at line 1225 of file xlogreader.c.

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

References Assert, 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, XLogLongPageHeaderData::xlp_seg_size, XLogLongPageHeaderData::xlp_sysid, XLogPageHeaderData::xlp_tli, and XLogLongPageHeaderData::xlp_xlog_blcksz.

Referenced by ReadPageInternal(), and XLogPageRead().

◆ XLogReadRecord()

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

Definition at line 390 of file xlogreader.c.

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

References Assert, DecodedXLogRecord::header, XLogNextRecord(), XLogReadAhead(), XLogReaderHasQueuedRecordOrError(), and XLogReleasePreviousRecord().

Referenced by DecodingContextFindStartpoint(), extractPageMap(), findLastCheckpoint(), LogicalReplicationSlotHasPendingWal(), 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 
)

◆ XLogRecGetBlockTag()

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

Definition at line 1981 of file xlogreader.c.

1984{
1985 if (!XLogRecGetBlockTagExtended(record, block_id, rlocator, forknum,
1986 blknum, NULL))
1987 {
1988#ifndef FRONTEND
1989 elog(ERROR, "could not locate backup block with ID %d in WAL record",
1990 block_id);
1991#else
1992 pg_fatal("could not locate backup block with ID %d in WAL record",
1993 block_id);
1994#endif
1995 }
1996}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define pg_fatal(...)
bool XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)
Definition: xlogreader.c:2007

References elog, ERROR, 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(), heap_xlog_visible(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), and spgRedoVacuumRedirect().

◆ XLogRecGetBlockTagExtended()

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

Definition at line 2007 of file xlogreader.c.

2011{
2012 DecodedBkpBlock *bkpb;
2013
2014 if (!XLogRecHasBlockRef(record, block_id))
2015 return false;
2016
2017 bkpb = &record->record->blocks[block_id];
2018 if (rlocator)
2019 *rlocator = bkpb->rlocator;
2020 if (forknum)
2021 *forknum = bkpb->forknum;
2022 if (blknum)
2023 *blknum = bkpb->blkno;
2024 if (prefetch_buffer)
2025 *prefetch_buffer = bkpb->prefetch_buffer;
2026 return true;
2027}
#define XLogRecHasBlockRef(decoder, block_id)
Definition: xlogreader.h:420

References DecodedBkpBlock::blkno, DecodedXLogRecord::blocks, DecodedBkpBlock::forknum, DecodedBkpBlock::prefetch_buffer, XLogReaderState::record, DecodedBkpBlock::rlocator, and XLogRecHasBlockRef.

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

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 2177 of file xlogreader.c.

2178{
2179 /*
2180 * This function is only safe during replay, because it depends on the
2181 * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
2182 */
2184
2186 XLogRecGetXid(record));
2187}
bool IsUnderPostmaster
Definition: globals.c:119
#define AmStartupProcess()
Definition: miscadmin.h:388
FullTransactionId nextXid
Definition: transam.h:220
static FullTransactionId FullTransactionIdFromAllowableAt(FullTransactionId nextFullXid, TransactionId xid)
Definition: transam.h:381
TransamVariablesData * TransamVariables
Definition: varsup.c:34
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:412

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

◆ XLogReleasePreviousRecord()

XLogRecPtr XLogReleasePreviousRecord ( XLogReaderState state)

Definition at line 250 of file xlogreader.c.

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

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

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