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 1 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 1667 of file xlogreader.c.

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

1635 {
1636  size_t size = 0;
1637 
1638  /* Account for the fixed size part of the decoded record struct. */
1639  size += offsetof(DecodedXLogRecord, blocks[0]);
1640  /* Account for the flexible blocks array of maximum possible size. */
1641  size += sizeof(DecodedBkpBlock) * (XLR_MAX_BLOCK_ID + 1);
1642  /* Account for all the raw main and block data. */
1643  size += xl_tot_len;
1644  /* We might insert padding before main_data. */
1645  size += (MAXIMUM_ALIGNOF - 1);
1646  /* We might insert padding before each block's data. */
1647  size += (MAXIMUM_ALIGNOF - 1) * (XLR_MAX_BLOCK_ID + 1);
1648  /* We might insert padding at the end. */
1649  size += (MAXIMUM_ALIGNOF - 1);
1650 
1651  return size;
1652 }

References XLR_MAX_BLOCK_ID.

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

◆ RestoreBlockImage()

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

Definition at line 2061 of file xlogreader.c.

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

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

◆ XLogBeginRead()

void XLogBeginRead ( XLogReaderState state,
XLogRecPtr  RecPtr 
)

Definition at line 233 of file xlogreader.c.

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

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

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

◆ XLogFindNextRecord()

XLogRecPtr XLogFindNextRecord ( XLogReaderState state,
XLogRecPtr  RecPtr 
)

Definition at line 1385 of file xlogreader.c.

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

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(), and main().

◆ XLogNextRecord()

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

Definition at line 327 of file xlogreader.c.

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

References Assert(), XLogRecPtrIsInvalid, and XLogReleasePreviousRecord().

Referenced by XLogPrefetcherReadRecord(), and XLogReadRecord().

◆ XLogReadAhead()

DecodedXLogRecord* XLogReadAhead ( XLogReaderState state,
bool  nonblocking 
)

Definition at line 968 of file xlogreader.c.

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

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 108 of file xlogreader.c.

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

References allocate_recordbuf(), 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(), XLogInsertRecord(), and XlogReadTwoPhaseData().

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)

Definition at line 163 of file xlogreader.c.

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

References pfree().

Referenced by extractPageMap(), findLastCheckpoint(), FreeDecodingContext(), GetWALRecordsInfo(), main(), pg_get_wal_block_info(), pg_get_wal_record_info(), readOneRecord(), ShutdownWalRecovery(), 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 1367 of file xlogreader.c.

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

Referenced by XLogPageRead().

◆ XLogReaderSetDecodeBuffer()

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

Definition at line 92 of file xlogreader.c.

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

References Assert().

Referenced by InitWalRecovery().

◆ XLogReaderValidatePageHeader()

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

Definition at line 1226 of file xlogreader.c.

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

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

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

Referenced by DecodingContextFindStartpoint(), extractPageMap(), findLastCheckpoint(), LogicalReplicationSlotHasPendingWal(), main(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), ReadNextXLogRecord(), readOneRecord(), XLogFindNextRecord(), XlogReadTwoPhaseData(), and XLogSendLogical().

◆ XLogRecGetBlockData()

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

Definition at line 2030 of file xlogreader.c.

2031 {
2032  DecodedBkpBlock *bkpb;
2033 
2034  if (block_id > record->record->max_block_id ||
2035  !record->record->blocks[block_id].in_use)
2036  return NULL;
2037 
2038  bkpb = &record->record->blocks[block_id];
2039 
2040  if (!bkpb->has_data)
2041  {
2042  if (len)
2043  *len = 0;
2044  return NULL;
2045  }
2046  else
2047  {
2048  if (len)
2049  *len = bkpb->data_len;
2050  return bkpb->data;
2051  }
2052 }
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_freeze_page(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_multi_insert(), heap_xlog_prune(), heap_xlog_update(), and heap_xlog_vacuum().

◆ XLogRecGetBlockTag()

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

Definition at line 1976 of file xlogreader.c.

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

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_freeze_page(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_prune(), heap_xlog_update(), heap_xlog_vacuum(), 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 2002 of file xlogreader.c.

2006 {
2007  DecodedBkpBlock *bkpb;
2008 
2009  if (!XLogRecHasBlockRef(record, block_id))
2010  return false;
2011 
2012  bkpb = &record->record->blocks[block_id];
2013  if (rlocator)
2014  *rlocator = bkpb->rlocator;
2015  if (forknum)
2016  *forknum = bkpb->forknum;
2017  if (blknum)
2018  *blknum = bkpb->blkno;
2019  if (prefetch_buffer)
2020  *prefetch_buffer = bkpb->prefetch_buffer;
2021  return true;
2022 }
#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(), verifyBackupPageConsistency(), xlog_block_info(), XLogReadBufferForRedoExtended(), XLogRecGetBlockRefInfo(), XLogRecGetBlockTag(), XLogRecordMatchesRelationBlock(), and XLogRecordSaveFPWs().

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 2172 of file xlogreader.c.

2173 {
2174  TransactionId xid,
2175  next_xid;
2176  uint32 epoch;
2177 
2178  /*
2179  * This function is only safe during replay, because it depends on the
2180  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
2181  */
2183 
2184  xid = XLogRecGetXid(record);
2187 
2188  /*
2189  * If xid is numerically greater than next_xid, it has to be from the last
2190  * epoch.
2191  */
2192  if (unlikely(xid > next_xid))
2193  --epoch;
2194 
2196 }
#define unlikely(x)
Definition: c.h:300
bool IsUnderPostmaster
Definition: globals.c:115
#define AmStartupProcess()
Definition: miscadmin.h:451
FullTransactionId nextXid
Definition: transam.h:220
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:71
TransamVariablesData * TransamVariables
Definition: varsup.c:34
static const unsigned __int64 epoch
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:412

References AmStartupProcess, Assert(), epoch, EpochFromFullTransactionId, FullTransactionIdFromEpochAndXid(), IsUnderPostmaster, TransamVariablesData::nextXid, TransamVariables, unlikely, XidFromFullTransactionId, and XLogRecGetXid.

◆ XLogReleasePreviousRecord()

XLogRecPtr XLogReleasePreviousRecord ( XLogReaderState state)

Definition at line 251 of file xlogreader.c.

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