PostgreSQL Source Code git master
Data Structures | Macros | Typedefs | Enumerations | Functions
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 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 struct WALReadError  WALReadError
 

Enumerations

 

Functions

 
XLogReaderStateXLogReaderAllocate (int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
 
 
void  XLogReaderSetDecodeBuffer (XLogReaderState *state, void *buffer, size_t size)
 
 
 
struct XLogRecordXLogReadRecord (XLogReaderState *state, char **errormsg)
 
 
 
 
 
 
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)
 
 
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 ( XLogReaderStatestate,
DecodedXLogRecorddecoded,
XLogRecordrecord,
XLogRecPtr  lsn,
char **  errormsg 
)

Definition at line 1682 of file xlogreader.c.

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

1650{
1651 size_t size = 0;
1652
1653 /* Account for the fixed size part of the decoded record struct. */
1654 size += offsetof(DecodedXLogRecord, blocks[0]);
1655 /* Account for the flexible blocks array of maximum possible size. */
1656 size += sizeof(DecodedBkpBlock) * (XLR_MAX_BLOCK_ID + 1);
1657 /* Account for all the raw main and block data. */
1658 size += xl_tot_len;
1659 /* We might insert padding before main_data. */
1660 size += (MAXIMUM_ALIGNOF - 1);
1661 /* We might insert padding before each block's data. */
1662 size += (MAXIMUM_ALIGNOF - 1) * (XLR_MAX_BLOCK_ID + 1);
1663 /* We might insert padding at the end. */
1664 size += (MAXIMUM_ALIGNOF - 1);
1665
1666 return size;
1667}

References XLR_MAX_BLOCK_ID.

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

RestoreBlockImage()

bool RestoreBlockImage ( XLogReaderStaterecord,
uint8  block_id,
char *  page 
)

Definition at line 2076 of file xlogreader.c.

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

Definition at line 1514 of file xlogreader.c.

1517{
1518 char *p;
1519 XLogRecPtr recptr;
1520 Size nbytes;
1521#ifndef FRONTEND
1522 instr_time io_start;
1523#endif
1524
1525 p = buf;
1526 recptr = startptr;
1527 nbytes = count;
1528
1529 while (nbytes > 0)
1530 {
1531 uint32 startoff;
1532 int segbytes;
1533 int readbytes;
1534
1535 startoff = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
1536
1537 /*
1538 * If the data we want is not in a segment we have open, close what we
1539 * have (if anything) and open the next one, using the caller's
1540 * provided segment_open callback.
1541 */
1542 if (state->seg.ws_file < 0 ||
1543 !XLByteInSeg(recptr, state->seg.ws_segno, state->segcxt.ws_segsize) ||
1544 tli != state->seg.ws_tli)
1545 {
1546 XLogSegNo nextSegNo;
1547
1548 if (state->seg.ws_file >= 0)
1549 state->routine.segment_close(state);
1550
1551 XLByteToSeg(recptr, nextSegNo, state->segcxt.ws_segsize);
1552 state->routine.segment_open(state, nextSegNo, &tli);
1553
1554 /* This shouldn't happen -- indicates a bug in segment_open */
1555 Assert(state->seg.ws_file >= 0);
1556
1557 /* Update the current segment info. */
1558 state->seg.ws_tli = tli;
1559 state->seg.ws_segno = nextSegNo;
1560 }
1561
1562 /* How many bytes are within this segment? */
1563 if (nbytes > (state->segcxt.ws_segsize - startoff))
1564 segbytes = state->segcxt.ws_segsize - startoff;
1565 else
1566 segbytes = nbytes;
1567
1568#ifndef FRONTEND
1569 /* Measure I/O timing when reading segment */
1571
1572 pgstat_report_wait_start(WAIT_EVENT_WAL_READ);
1573#endif
1574
1575 /* Reset errno first; eases reporting non-errno-affecting errors */
1576 errno = 0;
1577 readbytes = pg_pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
1578
1579#ifndef FRONTEND
1581
1583 io_start, 1, readbytes);
1584#endif
1585
1586 if (readbytes <= 0)
1587 {
1588 errinfo->wre_errno = errno;
1589 errinfo->wre_req = segbytes;
1590 errinfo->wre_read = readbytes;
1591 errinfo->wre_off = startoff;
1592 errinfo->wre_seg = state->seg;
1593 return false;
1594 }
1595
1596 /* Update state for read */
1597 recptr += readbytes;
1598 nbytes -= readbytes;
1599 p += readbytes;
1600 }
1601
1602 return true;
1603}
size_t Size
Definition: c.h:610
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: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:226
int wre_off
Definition: xlogreader.h:385
int wre_read
Definition: xlogreader.h:387
int wre_errno
Definition: xlogreader.h:384
int wre_req
Definition: xlogreader.h:386
WALOpenSegment wre_seg
Definition: xlogreader.h:388
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:69
static void pgstat_report_wait_end(void)
Definition: wait_event.h:85
bool track_wal_io_timing
Definition: xlog.c:138
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
Definition: xlog_internal.h:106
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
Definition: xlog_internal.h:117
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
Definition: xlog_internal.h:136
uint64 XLogRecPtr
Definition: xlogdefs.h:21
uint64 XLogSegNo
Definition: xlogdefs.h:51

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_wal_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 ( XLogReaderStatestate,
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:1615

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 ( XLogReaderStatestate,
XLogRecPtr  RecPtr 
)

Definition at line 1394 of file xlogreader.c.

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

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 ( XLogReaderStatestate,
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 ( XLogReaderStatestate,
bool  nonblocking 
)

Definition at line 977 of file xlogreader.c.

978{
979 XLogPageReadResult result;
980
981 if (state->errormsg_deferred)
982 return NULL;
983
984 result = XLogDecodeNextRecord(state, nonblocking);
985 if (result == XLREAD_SUCCESS)
986 {
987 Assert(state->decode_queue_tail != NULL);
988 return state->decode_queue_tail;
989 }
990
991 return NULL;
992}
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,
XLogReaderRoutineroutine,
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:1594
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1417
int wal_segment_size
Definition: xlog.c:144
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 ( XLogReaderStatestate )

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 ( XLogReaderStatestate )
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 ( XLogReaderStatestate )

Definition at line 1376 of file xlogreader.c.

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

Referenced by XLogPageRead().

XLogReaderSetDecodeBuffer()

void XLogReaderSetDecodeBuffer ( XLogReaderStatestate,
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().

Referenced by InitWalRecovery().

XLogReaderValidatePageHeader()

bool XLogReaderValidatePageHeader ( XLogReaderStatestate,
XLogRecPtr  recptr,
char *  phdr 
)

Definition at line 1235 of file xlogreader.c.

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

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 ( XLogReaderStatestate,
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:977
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 ( XLogReaderStaterecord,
uint8  block_id,
Sizelen 
)

Definition at line 2045 of file xlogreader.c.

2046{
2047 DecodedBkpBlock *bkpb;
2048
2049 if (block_id > record->record->max_block_id ||
2050 !record->record->blocks[block_id].in_use)
2051 return NULL;
2052
2053 bkpb = &record->record->blocks[block_id];
2054
2055 if (!bkpb->has_data)
2056 {
2057 if (len)
2058 *len = 0;
2059 return NULL;
2060 }
2061 else
2062 {
2063 if (len)
2064 *len = bkpb->data_len;
2065 return bkpb->data;
2066 }
2067}
const void size_t len

References DecodedXLogRecord::blocks, DecodedBkpBlock::data, DecodedBkpBlock::data_len, DecodedBkpBlock::has_data, DecodedBkpBlock::in_use, len, DecodedXLogRecord::max_block_id, and XLogReaderState::record.

Referenced by _bt_restore_meta(), brin_xlog_insert_update(), brin_xlog_samepage_update(), btree_desc(), btree_xlog_dedup(), btree_xlog_delete(), btree_xlog_insert(), btree_xlog_newroot(), btree_xlog_split(), btree_xlog_vacuum(), DecodeInsert(), DecodeMultiInsert(), DecodeUpdate(), generic_redo(), gin_desc(), ginRedoInsert(), ginRedoInsertListPage(), ginRedoUpdateMetapage(), ginRedoVacuumDataLeafPage(), gistRedoPageSplitRecord(), gistRedoPageUpdateRecord(), hash_xlog_add_ovfl_page(), hash_xlog_delete(), hash_xlog_insert(), hash_xlog_move_page_contents(), hash_xlog_split_allocate_page(), hash_xlog_squeeze_page(), heap2_desc(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_multi_insert(), heap_xlog_prune_freeze(), and heap_xlog_update().

XLogRecGetBlockTag()

void XLogRecGetBlockTag ( XLogReaderStaterecord,
uint8  block_id,
RelFileLocatorrlocator,
ForkNumberforknum,
BlockNumberblknum 
)

Definition at line 1991 of file xlogreader.c.

1994{
1995 if (!XLogRecGetBlockTagExtended(record, block_id, rlocator, forknum,
1996 blknum, NULL))
1997 {
1998#ifndef FRONTEND
1999 elog(ERROR, "could not locate backup block with ID %d in WAL record",
2000 block_id);
2001#else
2002 pg_fatal("could not locate backup block with ID %d in WAL record",
2003 block_id);
2004#endif
2005 }
2006}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define pg_fatal(...)
bool XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)
Definition: xlogreader.c:2017

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 ( XLogReaderStaterecord,
uint8  block_id,
RelFileLocatorrlocator,
ForkNumberforknum,
BlockNumberblknum,
Bufferprefetch_buffer 
)

Definition at line 2017 of file xlogreader.c.

2021{
2022 DecodedBkpBlock *bkpb;
2023
2024 if (!XLogRecHasBlockRef(record, block_id))
2025 return false;
2026
2027 bkpb = &record->record->blocks[block_id];
2028 if (rlocator)
2029 *rlocator = bkpb->rlocator;
2030 if (forknum)
2031 *forknum = bkpb->forknum;
2032 if (blknum)
2033 *blknum = bkpb->blkno;
2034 if (prefetch_buffer)
2035 *prefetch_buffer = bkpb->prefetch_buffer;
2036 return true;
2037}
#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 ( XLogReaderStaterecord )

Definition at line 2187 of file xlogreader.c.

2188{
2189 /*
2190 * This function is only safe during replay, because it depends on the
2191 * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
2192 */
2194
2196 XLogRecGetXid(record));
2197}
bool IsUnderPostmaster
Definition: globals.c:120
#define AmStartupProcess()
Definition: miscadmin.h:389
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 ( XLogReaderStatestate )

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:402
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().

AltStyle によって変換されたページ (->オリジナル) /