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

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

1620 {
1621  size_t size = 0;
1622 
1623  /* Account for the fixed size part of the decoded record struct. */
1624  size += offsetof(DecodedXLogRecord, blocks[0]);
1625  /* Account for the flexible blocks array of maximum possible size. */
1626  size += sizeof(DecodedBkpBlock) * (XLR_MAX_BLOCK_ID + 1);
1627  /* Account for all the raw main and block data. */
1628  size += xl_tot_len;
1629  /* We might insert padding before main_data. */
1630  size += (MAXIMUM_ALIGNOF - 1);
1631  /* We might insert padding before each block's data. */
1632  size += (MAXIMUM_ALIGNOF - 1) * (XLR_MAX_BLOCK_ID + 1);
1633  /* We might insert padding at the end. */
1634  size += (MAXIMUM_ALIGNOF - 1);
1635 
1636  return size;
1637 }

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

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

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

265 {
266  Assert(!XLogRecPtrIsInvalid(RecPtr));
267 
269 
270  /* Begin at the passed-in record pointer. */
271  state->EndRecPtr = RecPtr;
272  state->NextRecPtr = RecPtr;
273  state->ReadRecPtr = InvalidXLogRecPtr;
274  state->DecodeRecPtr = InvalidXLogRecPtr;
275 }
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1585

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

Referenced by DecodingContextFindStartpoint(), extractPageMap(), findLastCheckpoint(), 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 1370 of file xlogreader.c.

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

359 {
360  /* Release the last record returned by XLogNextRecord(). */
362 
363  if (state->decode_queue_head == NULL)
364  {
365  *errormsg = NULL;
366  if (state->errormsg_deferred)
367  {
368  if (state->errormsg_buf[0] != '\0')
369  *errormsg = state->errormsg_buf;
370  state->errormsg_deferred = false;
371  }
372 
373  /*
374  * state->EndRecPtr is expected to have been set by the last call to
375  * XLogBeginRead() or XLogNextRecord(), and is the location of the
376  * error.
377  */
378  Assert(!XLogRecPtrIsInvalid(state->EndRecPtr));
379 
380  return NULL;
381  }
382 
383  /*
384  * Record this as the most recent record returned, so that we'll release
385  * it next time. This also exposes it to the traditional
386  * XLogRecXXX(xlogreader) macros, which work with the decoder rather than
387  * the record for historical reasons.
388  */
389  state->record = state->decode_queue_head;
390 
391  /*
392  * Update the pointers to the beginning and one-past-the-end of this
393  * record, again for the benefit of historical code that expected the
394  * decoder to track this rather than accessing these fields of the record
395  * itself.
396  */
397  state->ReadRecPtr = state->record->lsn;
398  state->EndRecPtr = state->record->next_lsn;
399 
400  *errormsg = NULL;
401 
402  return state->record;
403 }
XLogRecPtr XLogReleasePreviousRecord(XLogReaderState *state)
Definition: xlogreader.c:282

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

Referenced by XLogPrefetcherReadRecord(), and XLogReadRecord().

◆ XLogReadAhead()

DecodedXLogRecord* XLogReadAhead ( XLogReaderState state,
bool  nonblocking 
)

Definition at line 955 of file xlogreader.c.

956 {
957  XLogPageReadResult result;
958 
959  if (state->errormsg_deferred)
960  return NULL;
961 
962  result = XLogDecodeNextRecord(state, nonblocking);
963  if (result == XLREAD_SUCCESS)
964  {
965  Assert(state->decode_queue_tail != NULL);
966  return state->decode_queue_tail;
967  }
968 
969  return NULL;
970 }
static XLogPageReadResult XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking)
Definition: xlogreader.c:535

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  */
158  if (!allocate_recordbuf(state, 0))
159  {
160  pfree(state->errormsg_buf);
161  pfree(state->readBuf);
162  pfree(state);
163  return NULL;
164  }
165 
166  return state;
167 }
#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 static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength)
Definition: xlogreader.c:197
static void WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt, int segsize, const char *waldir)
Definition: xlogreader.c:240
#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 170 of file xlogreader.c.

171 {
172  if (state->seg.ws_file != -1)
173  state->routine.segment_close(state);
174 
175  if (state->decode_buffer && state->free_decode_buffer)
176  pfree(state->decode_buffer);
177 
178  pfree(state->errormsg_buf);
179  if (state->readRecordBuf)
180  pfree(state->readRecordBuf);
181  pfree(state->readBuf);
182  pfree(state);
183 }

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

1353 {
1354  state->errormsg_buf[0] = '\0';
1355  state->errormsg_deferred = false;
1356 }

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

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

423 {
424  DecodedXLogRecord *decoded;
425 
426  /*
427  * Release last returned record, if there is one. We need to do this so
428  * that we can check for empty decode queue accurately.
429  */
431 
432  /*
433  * Call XLogReadAhead() in blocking mode to make sure there is something
434  * in the queue, though we don't use the result.
435  */
437  XLogReadAhead(state, false /* nonblocking */ );
438 
439  /* Consume the head record or error. */
440  decoded = XLogNextRecord(state, errormsg);
441  if (decoded)
442  {
443  /*
444  * This function returns a pointer to the record's header, not the
445  * actual decoded record. The caller will access the decoded record
446  * through the XLogRecGetXXX() macros, which reach the decoded
447  * recorded as xlogreader->record.
448  */
449  Assert(state->record == decoded);
450  return &decoded->header;
451  }
452 
453  return NULL;
454 }
DecodedXLogRecord * XLogNextRecord(XLogReaderState *state, char **errormsg)
Definition: xlogreader.c:358
DecodedXLogRecord * XLogReadAhead(XLogReaderState *state, bool nonblocking)
Definition: xlogreader.c:955
static bool XLogReaderHasQueuedRecordOrError(XLogReaderState *state)
Definition: xlogreader.h:325

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

Referenced by DecodingContextFindStartpoint(), extractPageMap(), findLastCheckpoint(), 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 2015 of file xlogreader.c.

2016 {
2017  DecodedBkpBlock *bkpb;
2018 
2019  if (block_id > record->record->max_block_id ||
2020  !record->record->blocks[block_id].in_use)
2021  return NULL;
2022 
2023  bkpb = &record->record->blocks[block_id];
2024 
2025  if (!bkpb->has_data)
2026  {
2027  if (len)
2028  *len = 0;
2029  return NULL;
2030  }
2031  else
2032  {
2033  if (len)
2034  *len = bkpb->data_len;
2035  return bkpb->data;
2036  }
2037 }
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 1961 of file xlogreader.c.

1964 {
1965  if (!XLogRecGetBlockTagExtended(record, block_id, rlocator, forknum,
1966  blknum, NULL))
1967  {
1968 #ifndef FRONTEND
1969  elog(ERROR, "could not locate backup block with ID %d in WAL record",
1970  block_id);
1971 #else
1972  pg_fatal("could not locate backup block with ID %d in WAL record",
1973  block_id);
1974 #endif
1975  }
1976 }
#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:1987

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

1991 {
1992  DecodedBkpBlock *bkpb;
1993 
1994  if (!XLogRecHasBlockRef(record, block_id))
1995  return false;
1996 
1997  bkpb = &record->record->blocks[block_id];
1998  if (rlocator)
1999  *rlocator = bkpb->rlocator;
2000  if (forknum)
2001  *forknum = bkpb->forknum;
2002  if (blknum)
2003  *blknum = bkpb->blkno;
2004  if (prefetch_buffer)
2005  *prefetch_buffer = bkpb->prefetch_buffer;
2006  return true;
2007 }
#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 2157 of file xlogreader.c.

2158 {
2159  TransactionId xid,
2160  next_xid;
2161  uint32 epoch;
2162 
2163  /*
2164  * This function is only safe during replay, because it depends on the
2165  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
2166  */
2168 
2169  xid = XLogRecGetXid(record);
2172 
2173  /*
2174  * If xid is numerically greater than next_xid, it has to be from the last
2175  * epoch.
2176  */
2177  if (unlikely(xid > next_xid))
2178  --epoch;
2179 
2181 }
#define unlikely(x)
Definition: c.h:295
bool IsUnderPostmaster
Definition: globals.c:113
#define AmStartupProcess()
Definition: miscadmin.h:452
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
VariableCache ShmemVariableCache
Definition: varsup.c:34
static const unsigned __int64 epoch
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:412

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

◆ XLogReleasePreviousRecord()

XLogRecPtr XLogReleasePreviousRecord ( XLogReaderState state)

Definition at line 282 of file xlogreader.c.

283 {
284  DecodedXLogRecord *record;
285  XLogRecPtr next_lsn;
286 
287  if (!state->record)
288  return InvalidXLogRecPtr;
289 
290  /*
291  * Remove it from the decoded record queue. It must be the oldest item
292  * decoded, decode_queue_head.
293  */
294  record = state->record;
295  next_lsn = record->next_lsn;
296  Assert(record == state->decode_queue_head);
297  state->record = NULL;
298  state->decode_queue_head = record->next;
299 
300  /* It might also be the newest item decoded, decode_queue_tail. */
301  if (state->decode_queue_tail == record)
302  state->decode_queue_tail = NULL;
303 
304  /* Release the space. */
305  if (unlikely(record->oversized))
306  {
307  /* It's not in the decode buffer, so free it to release space. */
308  pfree(record);
309  }
310  else
311  {
312  /* It must be the head (oldest) record in the decode buffer. */
313  Assert(state->decode_buffer_head == (char *) record);
314 
315  /*
316  * We need to update head to point to the next record that is in the
317  * decode buffer, if any, being careful to skip oversized ones
318  * (they're not in the decode buffer).
319  */
320  record = record->next;
321  while (unlikely(record && record->oversized))
322  record = record->next;
323 
324  if (record)
325  {
326  /* Adjust head to release space up to the next record. */
327  state->decode_buffer_head = (char *) record;
328  }
329  else
330  {
331  /*
332  * Otherwise we might as well just reset head and tail to the
333  * start of the buffer space, because we're empty. This means
334  * we'll keep overwriting the same piece of memory if we're not
335  * doing any prefetching.
336  */
337  state->decode_buffer_head = state->decode_buffer;
338  state->decode_buffer_tail = state->decode_buffer;
339  }
340  }
341 
342  return next_lsn;
343 }
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().