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)
 

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)
 
XLogReaderRoutineLocalXLogReaderRoutine (void)
 
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 426 of file xlogreader.h.

◆ XLogRecGetBlock

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

Definition at line 420 of file xlogreader.h.

◆ XLogRecGetData

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

Definition at line 416 of file xlogreader.h.

◆ XLogRecGetDataLen

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

Definition at line 417 of file xlogreader.h.

◆ XLogRecGetInfo

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

Definition at line 411 of file xlogreader.h.

◆ XLogRecGetOrigin

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

Definition at line 414 of file xlogreader.h.

◆ XLogRecGetPrev

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

Definition at line 410 of file xlogreader.h.

◆ XLogRecGetRmid

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

Definition at line 412 of file xlogreader.h.

◆ XLogRecGetTopXid

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

Definition at line 415 of file xlogreader.h.

◆ XLogRecGetTotalLen

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

Definition at line 409 of file xlogreader.h.

◆ XLogRecGetXid

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

Definition at line 413 of file xlogreader.h.

◆ XLogRecHasAnyBlockRefs

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

Definition at line 418 of file xlogreader.h.

◆ XLogRecHasBlockImage

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

Definition at line 424 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 421 of file xlogreader.h.

◆ XLogRecMaxBlockId

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

Definition at line 419 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 350 of file xlogreader.h.

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

Function Documentation

◆ DecodeXLogRecord()

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

Definition at line 1649 of file xlogreader.c.

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

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(), 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 1616 of file xlogreader.c.

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

References XLR_MAX_BLOCK_ID.

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

◆ LocalXLogReaderRoutine()

XLogReaderRoutine* LocalXLogReaderRoutine ( void  )

◆ RestoreBlockImage()

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

Definition at line 2043 of file xlogreader.c.

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

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

◆ WALRead()

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

Definition at line 1490 of file xlogreader.c.

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

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

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

References Assert(), header(), InvalidXLogRecPtr, MAXALIGN, readLen, ReadPageInternal(), XLogBeginRead(), XLogPageHeaderSize, XLogReaderInvalReadState(), XLogReadRecord(), XLogRecPtrIsInvalid, and XLP_FIRST_IS_CONTRECORD.

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

955 {
956  XLogPageReadResult result;
957 
958  if (state->errormsg_deferred)
959  return NULL;
960 
961  result = XLogDecodeNextRecord(state, nonblocking);
962  if (result == XLREAD_SUCCESS)
963  {
964  Assert(state->decode_queue_tail != NULL);
965  return state->decode_queue_tail;
966  }
967 
968  return NULL;
969 }
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:1252
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1209
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(), main(), 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 1349 of file xlogreader.c.

1350 {
1351  state->errormsg_buf[0] = '\0';
1352  state->errormsg_deferred = false;
1353 }

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

1212 {
1213  XLogRecPtr recaddr;
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  XLogSegNoOffsetToRecPtr(segno, offset, state->segcxt.ws_segsize, recaddr);
1224 
1225  if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
1226  {
1227  char fname[MAXFNAMELEN];
1228 
1229  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1230 
1232  "invalid magic number %04X in WAL segment %s, offset %u",
1233  hdr->xlp_magic,
1234  fname,
1235  offset);
1236  return false;
1237  }
1238 
1239  if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
1240  {
1241  char fname[MAXFNAMELEN];
1242 
1243  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1244 
1246  "invalid info bits %04X in WAL segment %s, offset %u",
1247  hdr->xlp_info,
1248  fname,
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, offset %u",
1288  hdr->xlp_info,
1289  fname,
1290  offset);
1291  return false;
1292  }
1293 
1294  /*
1295  * Check that the address on the page agrees with what we expected. This
1296  * check typically fails when an old WAL segment is recycled, and hasn't
1297  * yet been overwritten with new data yet.
1298  */
1299  if (hdr->xlp_pageaddr != recaddr)
1300  {
1301  char fname[MAXFNAMELEN];
1302 
1303  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1304 
1306  "unexpected pageaddr %X/%X in WAL segment %s, offset %u",
1308  fname,
1309  offset);
1310  return false;
1311  }
1312 
1313  /*
1314  * Since child timelines are always assigned a TLI greater than their
1315  * immediate parent's TLI, we should never see TLI go backwards across
1316  * successive pages of a consistent WAL sequence.
1317  *
1318  * Sometimes we re-read a segment that's already been (partially) read. So
1319  * we only verify TLIs for pages that are later than the last remembered
1320  * LSN.
1321  */
1322  if (recptr > state->latestPagePtr)
1323  {
1324  if (hdr->xlp_tli < state->latestPageTLI)
1325  {
1326  char fname[MAXFNAMELEN];
1327 
1328  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1329 
1331  "out-of-sequence timeline ID %u (after %u) in WAL segment %s, offset %u",
1332  hdr->xlp_tli,
1333  state->latestPageTLI,
1334  fname,
1335  offset);
1336  return false;
1337  }
1338  }
1339  state->latestPagePtr = recptr;
1340  state->latestPageTLI = hdr->xlp_tli;
1341 
1342  return true;
1343 }
signed int int32
Definition: c.h:430
TimeLineID xlp_tli
Definition: xlog_internal.h:40
XLogRecPtr xlp_pageaddr
Definition: xlog_internal.h:41
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:71
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
#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, XLogSegNoOffsetToRecPtr, 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:954
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 2012 of file xlogreader.c.

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

1961 {
1962  if (!XLogRecGetBlockTagExtended(record, block_id, rlocator, forknum,
1963  blknum, NULL))
1964  {
1965 #ifndef FRONTEND
1966  elog(ERROR, "could not locate backup block with ID %d in WAL record",
1967  block_id);
1968 #else
1969  pg_fatal("could not locate backup block with ID %d in WAL record",
1970  block_id);
1971 #endif
1972  }
1973 }
#define ERROR
Definition: elog.h:35
#define pg_fatal(...)
bool XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)
Definition: xlogreader.c:1984

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

1988 {
1989  DecodedBkpBlock *bkpb;
1990 
1991  if (!XLogRecHasBlockRef(record, block_id))
1992  return false;
1993 
1994  bkpb = &record->record->blocks[block_id];
1995  if (rlocator)
1996  *rlocator = bkpb->rlocator;
1997  if (forknum)
1998  *forknum = bkpb->forknum;
1999  if (blknum)
2000  *blknum = bkpb->blkno;
2001  if (prefetch_buffer)
2002  *prefetch_buffer = bkpb->prefetch_buffer;
2003  return true;
2004 }
#define XLogRecHasBlockRef(decoder, block_id)
Definition: xlogreader.h:421

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

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

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 2154 of file xlogreader.c.

2155 {
2156  TransactionId xid,
2157  next_xid;
2158  uint32 epoch;
2159 
2160  /*
2161  * This function is only safe during replay, because it depends on the
2162  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
2163  */
2165 
2166  xid = XLogRecGetXid(record);
2169 
2170  /*
2171  * If xid is numerically greater than next_xid, it has to be from the last
2172  * epoch.
2173  */
2174  if (unlikely(xid > next_xid))
2175  --epoch;
2176 
2178 }
#define unlikely(x)
Definition: c.h:295
bool IsUnderPostmaster
Definition: globals.c:113
#define AmStartupProcess()
Definition: miscadmin.h:440
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:413

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