PostgreSQL Source Code  git master
xlogreader.h File Reference
#include "access/transam.h"
#include "access/xlogrecord.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  XLogReaderState
 
struct  WALReadError
 

Macros

#define XL_ROUTINE(...)   &(XLogReaderRoutine){__VA_ARGS__}
 
#define XLogRecGetTotalLen(decoder)   ((decoder)->decoded_record->xl_tot_len)
 
#define XLogRecGetPrev(decoder)   ((decoder)->decoded_record->xl_prev)
 
#define XLogRecGetInfo(decoder)   ((decoder)->decoded_record->xl_info)
 
#define XLogRecGetRmid(decoder)   ((decoder)->decoded_record->xl_rmid)
 
#define XLogRecGetXid(decoder)   ((decoder)->decoded_record->xl_xid)
 
#define XLogRecGetOrigin(decoder)   ((decoder)->record_origin)
 
#define XLogRecGetData(decoder)   ((decoder)->main_data)
 
#define XLogRecGetDataLen(decoder)   ((decoder)->main_data_len)
 
#define XLogRecHasAnyBlockRefs(decoder)   ((decoder)->max_block_id >= 0)
 
#define XLogRecHasBlockRef(decoder, block_id)   ((decoder)->blocks[block_id].in_use)
 
#define XLogRecHasBlockImage(decoder, block_id)   ((decoder)->blocks[block_id].has_image)
 
#define XLogRecBlockImageApply(decoder, block_id)   ((decoder)->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 WALReadError WALReadError
 

Functions

XLogReaderStateXLogReaderAllocate (int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
 
XLogReaderRoutineLocalXLogReaderRoutine (void)
 
void XLogReaderFree (XLogReaderState *state)
 
void XLogBeginRead (XLogReaderState *state, XLogRecPtr RecPtr)
 
struct XLogRecordXLogReadRecord (XLogReaderState *state, char **errormsg)
 
bool XLogReaderValidatePageHeader (XLogReaderState *state, XLogRecPtr recptr, char *phdr)
 
bool WALRead (XLogReaderState *state, char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, WALReadError *errinfo)
 
bool DecodeXLogRecord (XLogReaderState *state, XLogRecord *record, char **errmsg)
 
FullTransactionId XLogRecGetFullXid (XLogReaderState *record)
 
bool RestoreBlockImage (XLogReaderState *record, uint8 block_id, char *page)
 
char * XLogRecGetBlockData (XLogReaderState *record, uint8 block_id, Size *len)
 
bool XLogRecGetBlockTag (XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
 

Macro Definition Documentation

◆ XL_ROUTINE

◆ XLogRecBlockImageApply

#define XLogRecBlockImageApply (   decoder,
  block_id 
)    ((decoder)->blocks[block_id].apply_image)

◆ XLogRecGetData

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

Definition at line 307 of file xlogreader.h.

Referenced by brin_desc(), brin_xlog_createidx(), brin_xlog_desummarize_page(), brin_xlog_insert(), brin_xlog_revmap_extend(), brin_xlog_samepage_update(), brin_xlog_update(), btree_desc(), btree_xlog_dedup(), btree_xlog_delete(), btree_xlog_insert(), btree_xlog_mark_page_halfdead(), btree_xlog_newroot(), btree_xlog_reuse_page(), btree_xlog_split(), btree_xlog_unlink_page(), btree_xlog_vacuum(), clog_desc(), clog_redo(), commit_ts_desc(), commit_ts_redo(), dbase_desc(), dbase_redo(), DecodeDelete(), DecodeHeap2Op(), DecodeInsert(), DecodeLogicalMsgOp(), DecodeMultiInsert(), DecodeStandbyOp(), DecodeTruncate(), DecodeUpdate(), DecodeXactOp(), findLastCheckpoint(), generic_desc(), getRecordTimestamp(), gin_desc(), ginRedoCreatePTree(), ginRedoDeleteListPages(), ginRedoDeletePage(), ginRedoInsert(), ginRedoInsertListPage(), ginRedoSplit(), ginRedoUpdateMetapage(), gist_desc(), gistRedoDeleteRecord(), gistRedoPageDelete(), gistRedoPageReuse(), gistRedoPageSplitRecord(), gistRedoPageUpdateRecord(), hash_desc(), hash_xlog_add_ovfl_page(), hash_xlog_delete(), hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), hash_xlog_insert(), hash_xlog_move_page_contents(), hash_xlog_split_allocate_page(), hash_xlog_split_complete(), hash_xlog_squeeze_page(), hash_xlog_update_meta_page(), hash_xlog_vacuum_one_page(), heap2_desc(), heap_desc(), heap_xlog_clean(), heap_xlog_cleanup_info(), heap_xlog_confirm(), heap_xlog_delete(), heap_xlog_freeze_page(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_logical_rewrite(), heap_xlog_multi_insert(), heap_xlog_update(), heap_xlog_visible(), logicalmsg_desc(), multixact_desc(), multixact_redo(), recoveryStopsAfter(), recoveryStopsBefore(), relmap_desc(), relmap_redo(), replorigin_desc(), replorigin_redo(), seq_desc(), seq_redo(), smgr_desc(), smgr_redo(), spg_desc(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), spgRedoSplitTuple(), spgRedoVacuumLeaf(), spgRedoVacuumRedirect(), spgRedoVacuumRoot(), standby_desc(), standby_redo(), StartupXLOG(), tblspc_desc(), tblspc_redo(), xact_desc(), xact_redo(), xlog_desc(), xlog_redo(), and XlogReadTwoPhaseData().

◆ XLogRecGetDataLen

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

◆ XLogRecGetInfo

◆ XLogRecGetOrigin

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

◆ XLogRecGetPrev

#define XLogRecGetPrev (   decoder)    ((decoder)->decoded_record->xl_prev)

Definition at line 302 of file xlogreader.h.

Referenced by xlog_redo(), and XLogDumpDisplayRecord().

◆ XLogRecGetRmid

◆ XLogRecGetTotalLen

#define XLogRecGetTotalLen (   decoder)    ((decoder)->decoded_record->xl_tot_len)

Definition at line 301 of file xlogreader.h.

Referenced by XLogDumpDisplayRecord(), and XLogDumpRecordLen().

◆ XLogRecGetXid

◆ XLogRecHasAnyBlockRefs

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

◆ XLogRecHasBlockImage

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

◆ XLogRecHasBlockRef

#define XLogRecHasBlockRef (   decoder,
  block_id 
)    ((decoder)->blocks[block_id].in_use)

Typedef Documentation

◆ WALOpenSegment

◆ WALReadError

typedef struct WALReadError WALReadError

◆ WALSegmentCloseCB

typedef void(* WALSegmentCloseCB) (XLogReaderState *xlogreader)

Definition at line 69 of file xlogreader.h.

◆ WALSegmentContext

◆ WALSegmentOpenCB

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

Definition at line 66 of file xlogreader.h.

◆ XLogPageReadCB

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

Definition at line 61 of file xlogreader.h.

◆ XLogReaderRoutine

◆ XLogReaderState

Definition at line 58 of file xlogreader.h.

Function Documentation

◆ DecodeXLogRecord()

bool DecodeXLogRecord ( XLogReaderState state,
XLogRecord record,
char **  errmsg 
)

Definition at line 1176 of file xlogreader.c.

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_HAS_HOLE, BKPIMAGE_IS_COMPRESSED, DecodedBkpBlock::blkno, XLogReaderState::blocks, COPY_HEADER_FIELD, DecodedBkpBlock::data, DecodedBkpBlock::data_bufsz, DecodedBkpBlock::data_len, XLogReaderState::decoded_record, XLogReaderState::errormsg_buf, DecodedBkpBlock::flags, DecodedBkpBlock::forknum, DecodedBkpBlock::has_data, DecodedBkpBlock::has_image, DecodedBkpBlock::hole_length, DecodedBkpBlock::hole_offset, DecodedBkpBlock::in_use, InvalidRepOriginId, XLogReaderState::main_data, XLogReaderState::main_data_bufsz, XLogReaderState::main_data_len, Max, XLogReaderState::max_block_id, MAXALIGN, palloc(), pfree(), XLogReaderState::ReadRecPtr, XLogReaderState::record_origin, remaining, report_invalid_record(), ResetDecoder(), DecodedBkpBlock::rnode, SizeOfXLogRecord, XLogRecord::xl_tot_len, XLR_BLOCK_ID_DATA_LONG, XLR_BLOCK_ID_DATA_SHORT, XLR_BLOCK_ID_ORIGIN, and XLR_MAX_BLOCK_ID.

Referenced by XLogInsertRecord(), and XLogReadRecord().

1177 {
1178  /*
1179  * read next _size bytes from record buffer, but check for overrun first.
1180  */
1181 #define COPY_HEADER_FIELD(_dst, _size) \
1182  do { \
1183  if (remaining < _size) \
1184  goto shortdata_err; \
1185  memcpy(_dst, ptr, _size); \
1186  ptr += _size; \
1187  remaining -= _size; \
1188  } while(0)
1189 
1190  char *ptr;
1191  uint32 remaining;
1192  uint32 datatotal;
1193  RelFileNode *rnode = NULL;
1194  uint8 block_id;
1195 
1196  ResetDecoder(state);
1197 
1198  state->decoded_record = record;
1200 
1201  ptr = (char *) record;
1202  ptr += SizeOfXLogRecord;
1203  remaining = record->xl_tot_len - SizeOfXLogRecord;
1204 
1205  /* Decode the headers */
1206  datatotal = 0;
1207  while (remaining > datatotal)
1208  {
1209  COPY_HEADER_FIELD(&block_id, sizeof(uint8));
1210 
1211  if (block_id == XLR_BLOCK_ID_DATA_SHORT)
1212  {
1213  /* XLogRecordDataHeaderShort */
1214  uint8 main_data_len;
1215 
1216  COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
1217 
1218  state->main_data_len = main_data_len;
1219  datatotal += main_data_len;
1220  break; /* by convention, the main data fragment is
1221  * always last */
1222  }
1223  else if (block_id == XLR_BLOCK_ID_DATA_LONG)
1224  {
1225  /* XLogRecordDataHeaderLong */
1226  uint32 main_data_len;
1227 
1228  COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
1229  state->main_data_len = main_data_len;
1230  datatotal += main_data_len;
1231  break; /* by convention, the main data fragment is
1232  * always last */
1233  }
1234  else if (block_id == XLR_BLOCK_ID_ORIGIN)
1235  {
1236  COPY_HEADER_FIELD(&state->record_origin, sizeof(RepOriginId));
1237  }
1238  else if (block_id <= XLR_MAX_BLOCK_ID)
1239  {
1240  /* XLogRecordBlockHeader */
1241  DecodedBkpBlock *blk;
1242  uint8 fork_flags;
1243 
1244  if (block_id <= state->max_block_id)
1245  {
1246  report_invalid_record(state,
1247  "out-of-order block_id %u at %X/%X",
1248  block_id,
1249  (uint32) (state->ReadRecPtr >> 32),
1250  (uint32) state->ReadRecPtr);
1251  goto err;
1252  }
1253  state->max_block_id = block_id;
1254 
1255  blk = &state->blocks[block_id];
1256  blk->in_use = true;
1257  blk->apply_image = false;
1258 
1259  COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
1260  blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
1261  blk->flags = fork_flags;
1262  blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
1263  blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
1264 
1265  COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
1266  /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
1267  if (blk->has_data && blk->data_len == 0)
1268  {
1269  report_invalid_record(state,
1270  "BKPBLOCK_HAS_DATA set, but no data included at %X/%X",
1271  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1272  goto err;
1273  }
1274  if (!blk->has_data && blk->data_len != 0)
1275  {
1276  report_invalid_record(state,
1277  "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%X",
1278  (unsigned int) blk->data_len,
1279  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1280  goto err;
1281  }
1282  datatotal += blk->data_len;
1283 
1284  if (blk->has_image)
1285  {
1286  COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
1287  COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
1288  COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
1289 
1290  blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
1291 
1292  if (blk->bimg_info & BKPIMAGE_IS_COMPRESSED)
1293  {
1294  if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
1295  COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
1296  else
1297  blk->hole_length = 0;
1298  }
1299  else
1300  blk->hole_length = BLCKSZ - blk->bimg_len;
1301  datatotal += blk->bimg_len;
1302 
1303  /*
1304  * cross-check that hole_offset > 0, hole_length > 0 and
1305  * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
1306  */
1307  if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1308  (blk->hole_offset == 0 ||
1309  blk->hole_length == 0 ||
1310  blk->bimg_len == BLCKSZ))
1311  {
1312  report_invalid_record(state,
1313  "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%X",
1314  (unsigned int) blk->hole_offset,
1315  (unsigned int) blk->hole_length,
1316  (unsigned int) blk->bimg_len,
1317  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1318  goto err;
1319  }
1320 
1321  /*
1322  * cross-check that hole_offset == 0 and hole_length == 0 if
1323  * the HAS_HOLE flag is not set.
1324  */
1325  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1326  (blk->hole_offset != 0 || blk->hole_length != 0))
1327  {
1328  report_invalid_record(state,
1329  "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%X",
1330  (unsigned int) blk->hole_offset,
1331  (unsigned int) blk->hole_length,
1332  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1333  goto err;
1334  }
1335 
1336  /*
1337  * cross-check that bimg_len < BLCKSZ if the IS_COMPRESSED
1338  * flag is set.
1339  */
1340  if ((blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1341  blk->bimg_len == BLCKSZ)
1342  {
1343  report_invalid_record(state,
1344  "BKPIMAGE_IS_COMPRESSED set, but block image length %u at %X/%X",
1345  (unsigned int) blk->bimg_len,
1346  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1347  goto err;
1348  }
1349 
1350  /*
1351  * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE nor
1352  * IS_COMPRESSED flag is set.
1353  */
1354  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1355  !(blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1356  blk->bimg_len != BLCKSZ)
1357  {
1358  report_invalid_record(state,
1359  "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_IS_COMPRESSED set, but block image length is %u at %X/%X",
1360  (unsigned int) blk->data_len,
1361  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1362  goto err;
1363  }
1364  }
1365  if (!(fork_flags & BKPBLOCK_SAME_REL))
1366  {
1367  COPY_HEADER_FIELD(&blk->rnode, sizeof(RelFileNode));
1368  rnode = &blk->rnode;
1369  }
1370  else
1371  {
1372  if (rnode == NULL)
1373  {
1374  report_invalid_record(state,
1375  "BKPBLOCK_SAME_REL set but no previous rel at %X/%X",
1376  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1377  goto err;
1378  }
1379 
1380  blk->rnode = *rnode;
1381  }
1382  COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
1383  }
1384  else
1385  {
1386  report_invalid_record(state,
1387  "invalid block_id %u at %X/%X",
1388  block_id,
1389  (uint32) (state->ReadRecPtr >> 32),
1390  (uint32) state->ReadRecPtr);
1391  goto err;
1392  }
1393  }
1394 
1395  if (remaining != datatotal)
1396  goto shortdata_err;
1397 
1398  /*
1399  * Ok, we've parsed the fragment headers, and verified that the total
1400  * length of the payload in the fragments is equal to the amount of data
1401  * left. Copy the data of each fragment to a separate buffer.
1402  *
1403  * We could just set up pointers into readRecordBuf, but we want to align
1404  * the data for the convenience of the callers. Backup images are not
1405  * copied, however; they don't need alignment.
1406  */
1407 
1408  /* block data first */
1409  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1410  {
1411  DecodedBkpBlock *blk = &state->blocks[block_id];
1412 
1413  if (!blk->in_use)
1414  continue;
1415 
1416  Assert(blk->has_image || !blk->apply_image);
1417 
1418  if (blk->has_image)
1419  {
1420  blk->bkp_image = ptr;
1421  ptr += blk->bimg_len;
1422  }
1423  if (blk->has_data)
1424  {
1425  if (!blk->data || blk->data_len > blk->data_bufsz)
1426  {
1427  if (blk->data)
1428  pfree(blk->data);
1429 
1430  /*
1431  * Force the initial request to be BLCKSZ so that we don't
1432  * waste time with lots of trips through this stanza as a
1433  * result of WAL compression.
1434  */
1435  blk->data_bufsz = MAXALIGN(Max(blk->data_len, BLCKSZ));
1436  blk->data = palloc(blk->data_bufsz);
1437  }
1438  memcpy(blk->data, ptr, blk->data_len);
1439  ptr += blk->data_len;
1440  }
1441  }
1442 
1443  /* and finally, the main data */
1444  if (state->main_data_len > 0)
1445  {
1446  if (!state->main_data || state->main_data_len > state->main_data_bufsz)
1447  {
1448  if (state->main_data)
1449  pfree(state->main_data);
1450 
1451  /*
1452  * main_data_bufsz must be MAXALIGN'ed. In many xlog record
1453  * types, we omit trailing struct padding on-disk to save a few
1454  * bytes; but compilers may generate accesses to the xlog struct
1455  * that assume that padding bytes are present. If the palloc
1456  * request is not large enough to include such padding bytes then
1457  * we'll get valgrind complaints due to otherwise-harmless fetches
1458  * of the padding bytes.
1459  *
1460  * In addition, force the initial request to be reasonably large
1461  * so that we don't waste time with lots of trips through this
1462  * stanza. BLCKSZ / 2 seems like a good compromise choice.
1463  */
1464  state->main_data_bufsz = MAXALIGN(Max(state->main_data_len,
1465  BLCKSZ / 2));
1466  state->main_data = palloc(state->main_data_bufsz);
1467  }
1468  memcpy(state->main_data, ptr, state->main_data_len);
1469  ptr += state->main_data_len;
1470  }
1471 
1472  return true;
1473 
1474 shortdata_err:
1475  report_invalid_record(state,
1476  "record with invalid length at %X/%X",
1477  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1478 err:
1479  *errormsg = state->errormsg_buf;
1480 
1481  return false;
1482 }
int remaining
Definition: informix.c:667
BlockNumber blkno
Definition: xlogreader.h:126
#define BKPIMAGE_HAS_HOLE
Definition: xlogrecord.h:146
#define XLR_BLOCK_ID_DATA_LONG
Definition: xlogrecord.h:224
uint16 hole_offset
Definition: xlogreader.h:135
unsigned char uint8
Definition: c.h:365
uint16 RepOriginId
Definition: xlogdefs.h:58
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:58
uint32 BlockNumber
Definition: block.h:31
unsigned short uint16
Definition: c.h:366
void pfree(void *pointer)
Definition: mcxt.c:1056
uint16 hole_length
Definition: xlogreader.h:136
uint32 xl_tot_len
Definition: xlogrecord.h:43
uint32 main_data_len
Definition: xlogreader.h:189
#define BKPIMAGE_APPLY
Definition: xlogrecord.h:148
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
XLogRecord * decoded_record
Definition: xlogreader.h:186
#define COPY_HEADER_FIELD(_dst, _size)
unsigned int uint32
Definition: c.h:367
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:221
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define BKPBLOCK_SAME_REL
Definition: xlogrecord.h:183
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
#define BKPBLOCK_HAS_IMAGE
Definition: xlogrecord.h:180
ForkNumber forknum
Definition: xlogreader.h:125
#define Max(x, y)
Definition: c.h:914
#define Assert(condition)
Definition: c.h:738
uint16 data_bufsz
Definition: xlogreader.h:144
#define XLR_BLOCK_ID_ORIGIN
Definition: xlogrecord.h:225
#define MAXALIGN(LEN)
Definition: c.h:691
#define InvalidRepOriginId
Definition: origin.h:33
char * bkp_image
Definition: xlogreader.h:134
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:223
uint32 main_data_bufsz
Definition: xlogreader.h:190
#define BKPBLOCK_FORK_MASK
Definition: xlogrecord.h:178
void * palloc(Size size)
Definition: mcxt.c:949
char * errormsg_buf
Definition: xlogreader.h:252
char * main_data
Definition: xlogreader.h:188
RelFileNode rnode
Definition: xlogreader.h:124
RepOriginId record_origin
Definition: xlogreader.h:192
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1151
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:195
#define BKPBLOCK_HAS_DATA
Definition: xlogrecord.h:181

◆ LocalXLogReaderRoutine()

XLogReaderRoutine* LocalXLogReaderRoutine ( void  )

◆ RestoreBlockImage()

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

Definition at line 1545 of file xlogreader.c.

References DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, DecodedBkpBlock::bkp_image, BKPIMAGE_IS_COMPRESSED, XLogReaderState::blocks, PGAlignedBlock::data, DecodedBkpBlock::has_image, DecodedBkpBlock::hole_length, DecodedBkpBlock::hole_offset, DecodedBkpBlock::in_use, MemSet, pglz_decompress(), XLogReaderState::ReadRecPtr, and report_invalid_record().

Referenced by checkXLogConsistency(), and XLogReadBufferForRedoExtended().

1546 {
1547  DecodedBkpBlock *bkpb;
1548  char *ptr;
1549  PGAlignedBlock tmp;
1550 
1551  if (!record->blocks[block_id].in_use)
1552  return false;
1553  if (!record->blocks[block_id].has_image)
1554  return false;
1555 
1556  bkpb = &record->blocks[block_id];
1557  ptr = bkpb->bkp_image;
1558 
1559  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1560  {
1561  /* If a backup block image is compressed, decompress it */
1562  if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
1563  BLCKSZ - bkpb->hole_length, true) < 0)
1564  {
1565  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1566  (uint32) (record->ReadRecPtr >> 32),
1567  (uint32) record->ReadRecPtr,
1568  block_id);
1569  return false;
1570  }
1571  ptr = tmp.data;
1572  }
1573 
1574  /* generate page, taking into account hole if necessary */
1575  if (bkpb->hole_length == 0)
1576  {
1577  memcpy(page, ptr, BLCKSZ);
1578  }
1579  else
1580  {
1581  memcpy(page, ptr, bkpb->hole_offset);
1582  /* must zero-fill the hole */
1583  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1584  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1585  ptr + bkpb->hole_offset,
1586  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1587  }
1588 
1589  return true;
1590 }
uint16 hole_offset
Definition: xlogreader.h:135
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:58
#define MemSet(start, val, len)
Definition: c.h:971
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
char data[BLCKSZ]
Definition: c.h:1104
uint16 hole_length
Definition: xlogreader.h:136
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
unsigned int uint32
Definition: c.h:367
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
char * bkp_image
Definition: xlogreader.h:134
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:195

◆ WALRead()

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

Definition at line 1062 of file xlogreader.c.

References Assert, buf, pg_pread(), pgstat_report_wait_end(), pgstat_report_wait_start(), XLogReaderState::routine, XLogReaderState::seg, XLogReaderState::segcxt, XLogReaderRoutine::segment_close, XLogReaderRoutine::segment_open, startptr, WAIT_EVENT_WAL_READ, WALReadError::wre_errno, WALReadError::wre_off, WALReadError::wre_read, WALReadError::wre_req, WALReadError::wre_seg, WALOpenSegment::ws_file, WALOpenSegment::ws_segno, WALSegmentContext::ws_segsize, WALOpenSegment::ws_tli, XLByteInSeg, XLByteToSeg, and XLogSegmentOffset.

Referenced by logical_read_xlog_page(), read_local_xlog_page(), WALDumpReadPage(), and XLogSendPhysical().

1065 {
1066  char *p;
1067  XLogRecPtr recptr;
1068  Size nbytes;
1069 
1070  p = buf;
1071  recptr = startptr;
1072  nbytes = count;
1073 
1074  while (nbytes > 0)
1075  {
1076  uint32 startoff;
1077  int segbytes;
1078  int readbytes;
1079 
1080  startoff = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
1081 
1082  /*
1083  * If the data we want is not in a segment we have open, close what we
1084  * have (if anything) and open the next one, using the caller's
1085  * provided openSegment callback.
1086  */
1087  if (state->seg.ws_file < 0 ||
1088  !XLByteInSeg(recptr, state->seg.ws_segno, state->segcxt.ws_segsize) ||
1089  tli != state->seg.ws_tli)
1090  {
1091  XLogSegNo nextSegNo;
1092 
1093  if (state->seg.ws_file >= 0)
1094  state->routine.segment_close(state);
1095 
1096  XLByteToSeg(recptr, nextSegNo, state->segcxt.ws_segsize);
1097  state->routine.segment_open(state, nextSegNo, &tli);
1098 
1099  /* This shouldn't happen -- indicates a bug in segment_open */
1100  Assert(state->seg.ws_file >= 0);
1101 
1102  /* Update the current segment info. */
1103  state->seg.ws_tli = tli;
1104  state->seg.ws_segno = nextSegNo;
1105  }
1106 
1107  /* How many bytes are within this segment? */
1108  if (nbytes > (state->segcxt.ws_segsize - startoff))
1109  segbytes = state->segcxt.ws_segsize - startoff;
1110  else
1111  segbytes = nbytes;
1112 
1113 #ifndef FRONTEND
1115 #endif
1116 
1117  /* Reset errno first; eases reporting non-errno-affecting errors */
1118  errno = 0;
1119  readbytes = pg_pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
1120 
1121 #ifndef FRONTEND
1123 #endif
1124 
1125  if (readbytes <= 0)
1126  {
1127  errinfo->wre_errno = errno;
1128  errinfo->wre_req = segbytes;
1129  errinfo->wre_read = readbytes;
1130  errinfo->wre_off = startoff;
1131  errinfo->wre_seg = state->seg;
1132  return false;
1133  }
1134 
1135  /* Update state for read */
1136  recptr += readbytes;
1137  nbytes -= readbytes;
1138  p += readbytes;
1139  }
1140 
1141  return true;
1142 }
WALOpenSegment wre_seg
Definition: xlogreader.h:289
WALSegmentCloseCB segment_close
Definition: xlogreader.h:113
ssize_t pg_pread(int fd, void *buf, size_t nbyte, off_t offset)
Definition: pread.c:27
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
WALOpenSegment seg
Definition: xlogreader.h:213
static char * buf
Definition: pg_test_fsync.c:67
uint64 XLogSegNo
Definition: xlogdefs.h:41
XLogSegNo ws_segno
Definition: xlogreader.h:47
unsigned int uint32
Definition: c.h:367
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1381
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
size_t Size
Definition: c.h:466
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1357
TimeLineID ws_tli
Definition: xlogreader.h:48
WALSegmentContext segcxt
Definition: xlogreader.h:212
WALSegmentOpenCB segment_open
Definition: xlogreader.h:107
XLogReaderRoutine routine
Definition: xlogreader.h:152
static XLogRecPtr startptr
Definition: basebackup.c:116
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ XLogBeginRead()

void XLogBeginRead ( XLogReaderState state,
XLogRecPtr  RecPtr 
)

Definition at line 240 of file xlogreader.c.

References Assert, XLogReaderState::EndRecPtr, InvalidXLogRecPtr, XLogReaderState::ReadRecPtr, ResetDecoder(), and XLogRecPtrIsInvalid.

Referenced by DecodingContextFindStartpoint(), extractPageMap(), findLastCheckpoint(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), ReadCheckpointRecord(), readOneRecord(), StartLogicalReplication(), StartupXLOG(), XLogReaderValidatePageHeader(), and XlogReadTwoPhaseData().

241 {
242  Assert(!XLogRecPtrIsInvalid(RecPtr));
243 
244  ResetDecoder(state);
245 
246  /* Begin at the passed-in record pointer. */
247  state->EndRecPtr = RecPtr;
248  state->ReadRecPtr = InvalidXLogRecPtr;
249 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define Assert(condition)
Definition: c.h:738
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1151

◆ XLogReaderAllocate()

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

Definition at line 75 of file xlogreader.c.

References allocate_recordbuf(), XLogReaderState::errormsg_buf, XLogReaderState::max_block_id, MAX_ERRORMSG_LEN, MCXT_ALLOC_NO_OOM, MCXT_ALLOC_ZERO, palloc_extended(), pfree(), XLogReaderState::private_data, XLogReaderState::readBuf, XLogReaderState::routine, XLogReaderState::seg, XLogReaderState::segcxt, and WALOpenSegmentInit().

Referenced by extractPageMap(), findLastCheckpoint(), main(), readOneRecord(), StartReplication(), StartupDecodingContext(), StartupXLOG(), XLogInsertRecord(), and XlogReadTwoPhaseData().

77 {
79 
80  state = (XLogReaderState *)
83  if (!state)
84  return NULL;
85 
86  /* initialize caller-provided support functions */
87  state->routine = *routine;
88 
89  state->max_block_id = -1;
90 
91  /*
92  * Permanently allocate readBuf. We do it this way, rather than just
93  * making a static array, for two reasons: (1) no need to waste the
94  * storage in most instantiations of the backend; (2) a static char array
95  * isn't guaranteed to have any particular alignment, whereas
96  * palloc_extended() will provide MAXALIGN'd storage.
97  */
98  state->readBuf = (char *) palloc_extended(XLOG_BLCKSZ,
100  if (!state->readBuf)
101  {
102  pfree(state);
103  return NULL;
104  }
105 
106  /* Initialize segment info. */
107  WALOpenSegmentInit(&state->seg, &state->segcxt, wal_segment_size,
108  waldir);
109 
110  /* system_identifier initialized to zeroes above */
111  state->private_data = private_data;
112  /* ReadRecPtr, EndRecPtr and readLen initialized to zeroes above */
115  if (!state->errormsg_buf)
116  {
117  pfree(state->readBuf);
118  pfree(state);
119  return NULL;
120  }
121  state->errormsg_buf[0] = '\0';
122 
123  /*
124  * Allocate an initial readRecordBuf of minimal size, which can later be
125  * enlarged if necessary.
126  */
127  if (!allocate_recordbuf(state, 0))
128  {
129  pfree(state->errormsg_buf);
130  pfree(state->readBuf);
131  pfree(state);
132  return NULL;
133  }
134 
135  return state;
136 }
static void WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt, int segsize, const char *waldir)
Definition: xlogreader.c:216
int wal_segment_size
Definition: xlog.c:116
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1013
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
void * private_data
Definition: xlogreader.h:168
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:51
WALOpenSegment seg
Definition: xlogreader.h:213
void pfree(void *pointer)
Definition: mcxt.c:1056
static void static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength)
Definition: xlogreader.c:173
Definition: regguts.h:298
#define MCXT_ALLOC_ZERO
Definition: fe_memutils.h:19
WALSegmentContext segcxt
Definition: xlogreader.h:212
char * errormsg_buf
Definition: xlogreader.h:252
XLogReaderRoutine routine
Definition: xlogreader.h:152

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)

Definition at line 139 of file xlogreader.c.

References XLogReaderState::blocks, DecodedBkpBlock::data, XLogReaderState::errormsg_buf, XLogReaderState::main_data, pfree(), XLogReaderState::readBuf, XLogReaderState::readRecordBuf, XLogReaderState::routine, XLogReaderState::seg, XLogReaderRoutine::segment_close, WALOpenSegment::ws_file, and XLR_MAX_BLOCK_ID.

Referenced by extractPageMap(), findLastCheckpoint(), FreeDecodingContext(), main(), readOneRecord(), StartupXLOG(), and XlogReadTwoPhaseData().

140 {
141  int block_id;
142 
143  if (state->seg.ws_file != -1)
144  state->routine.segment_close(state);
145 
146  for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
147  {
148  if (state->blocks[block_id].data)
149  pfree(state->blocks[block_id].data);
150  }
151  if (state->main_data)
152  pfree(state->main_data);
153 
154  pfree(state->errormsg_buf);
155  if (state->readRecordBuf)
156  pfree(state->readRecordBuf);
157  pfree(state->readBuf);
158  pfree(state);
159 }
WALSegmentCloseCB segment_close
Definition: xlogreader.h:113
char * readRecordBuf
Definition: xlogreader.h:248
WALOpenSegment seg
Definition: xlogreader.h:213
void pfree(void *pointer)
Definition: mcxt.c:1056
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:221
char * errormsg_buf
Definition: xlogreader.h:252
char * main_data
Definition: xlogreader.h:188
XLogReaderRoutine routine
Definition: xlogreader.h:152
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:195

◆ XLogReaderValidatePageHeader()

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

Definition at line 786 of file xlogreader.c.

References Assert, header(), InvalidXLogRecPtr, XLogReaderState::latestPagePtr, XLogReaderState::latestPageTLI, MAXALIGN, MAXFNAMELEN, XLogReaderState::readBuf, readLen, ReadPageInternal(), XLogReaderState::ReadRecPtr, ReadRecPtr, report_invalid_record(), XLogReaderState::seg, XLogReaderState::segcxt, XLogReaderState::system_identifier, WALSegmentContext::ws_segsize, WALOpenSegment::ws_tli, XLByteToSeg, XLOG_PAGE_MAGIC, XLogBeginRead(), XLogFileName, XLogPageHeaderSize, XLogReaderInvalReadState(), XLogReadRecord(), XLogRecPtrIsInvalid, XLogSegmentOffset, XLogSegNoOffsetToRecPtr, XLP_ALL_FLAGS, XLP_FIRST_IS_CONTRECORD, XLogPageHeaderData::xlp_info, XLP_LONG_HEADER, XLogPageHeaderData::xlp_magic, XLogPageHeaderData::xlp_pageaddr, XLogPageHeaderData::xlp_rem_len, XLogLongPageHeaderData::xlp_seg_size, XLogLongPageHeaderData::xlp_sysid, XLogPageHeaderData::xlp_tli, and XLogLongPageHeaderData::xlp_xlog_blcksz.

Referenced by ReadPageInternal(), and XLogPageRead().

788 {
789  XLogRecPtr recaddr;
790  XLogSegNo segno;
791  int32 offset;
792  XLogPageHeader hdr = (XLogPageHeader) phdr;
793 
794  Assert((recptr % XLOG_BLCKSZ) == 0);
795 
796  XLByteToSeg(recptr, segno, state->segcxt.ws_segsize);
797  offset = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
798 
799  XLogSegNoOffsetToRecPtr(segno, offset, state->segcxt.ws_segsize, recaddr);
800 
801  if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
802  {
803  char fname[MAXFNAMELEN];
804 
805  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
806 
807  report_invalid_record(state,
808  "invalid magic number %04X in log segment %s, offset %u",
809  hdr->xlp_magic,
810  fname,
811  offset);
812  return false;
813  }
814 
815  if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
816  {
817  char fname[MAXFNAMELEN];
818 
819  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
820 
821  report_invalid_record(state,
822  "invalid info bits %04X in log segment %s, offset %u",
823  hdr->xlp_info,
824  fname,
825  offset);
826  return false;
827  }
828 
829  if (hdr->xlp_info & XLP_LONG_HEADER)
830  {
831  XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
832 
833  if (state->system_identifier &&
834  longhdr->xlp_sysid != state->system_identifier)
835  {
836  report_invalid_record(state,
837  "WAL file is from different database system: WAL file database system identifier is %llu, pg_control database system identifier is %llu",
838  (unsigned long long) longhdr->xlp_sysid,
839  (unsigned long long) state->system_identifier);
840  return false;
841  }
842  else if (longhdr->xlp_seg_size != state->segcxt.ws_segsize)
843  {
844  report_invalid_record(state,
845  "WAL file is from different database system: incorrect segment size in page header");
846  return false;
847  }
848  else if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
849  {
850  report_invalid_record(state,
851  "WAL file is from different database system: incorrect XLOG_BLCKSZ in page header");
852  return false;
853  }
854  }
855  else if (offset == 0)
856  {
857  char fname[MAXFNAMELEN];
858 
859  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
860 
861  /* hmm, first page of file doesn't have a long header? */
862  report_invalid_record(state,
863  "invalid info bits %04X in log segment %s, offset %u",
864  hdr->xlp_info,
865  fname,
866  offset);
867  return false;
868  }
869 
870  /*
871  * Check that the address on the page agrees with what we expected. This
872  * check typically fails when an old WAL segment is recycled, and hasn't
873  * yet been overwritten with new data yet.
874  */
875  if (hdr->xlp_pageaddr != recaddr)
876  {
877  char fname[MAXFNAMELEN];
878 
879  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
880 
881  report_invalid_record(state,
882  "unexpected pageaddr %X/%X in log segment %s, offset %u",
883  (uint32) (hdr->xlp_pageaddr >> 32), (uint32) hdr->xlp_pageaddr,
884  fname,
885  offset);
886  return false;
887  }
888 
889  /*
890  * Since child timelines are always assigned a TLI greater than their
891  * immediate parent's TLI, we should never see TLI go backwards across
892  * successive pages of a consistent WAL sequence.
893  *
894  * Sometimes we re-read a segment that's already been (partially) read. So
895  * we only verify TLIs for pages that are later than the last remembered
896  * LSN.
897  */
898  if (recptr > state->latestPagePtr)
899  {
900  if (hdr->xlp_tli < state->latestPageTLI)
901  {
902  char fname[MAXFNAMELEN];
903 
904  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
905 
906  report_invalid_record(state,
907  "out-of-sequence timeline ID %u (after %u) in log segment %s, offset %u",
908  hdr->xlp_tli,
909  state->latestPageTLI,
910  fname,
911  offset);
912  return false;
913  }
914  }
915  state->latestPagePtr = recptr;
916  state->latestPageTLI = hdr->xlp_tli;
917 
918  return true;
919 }
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:58
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
signed int int32
Definition: c.h:355
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:74
WALOpenSegment seg
Definition: xlogreader.h:213
XLogRecPtr latestPagePtr
Definition: xlogreader.h:220
#define XLOG_PAGE_MAGIC
Definition: xlog_internal.h:34
uint64 XLogSegNo
Definition: xlogdefs.h:41
unsigned int uint32
Definition: c.h:367
TimeLineID xlp_tli
Definition: xlog_internal.h:40
XLogRecPtr xlp_pageaddr
Definition: xlog_internal.h:41
#define MAXFNAMELEN
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
#define XLP_ALL_FLAGS
Definition: xlog_internal.h:83
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
#define XLP_LONG_HEADER
Definition: xlog_internal.h:79
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
TimeLineID ws_tli
Definition: xlogreader.h:48
uint64 system_identifier
Definition: xlogreader.h:163
WALSegmentContext segcxt
Definition: xlogreader.h:212
TimeLineID latestPageTLI
Definition: xlogreader.h:221
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ XLogReadRecord()

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

Definition at line 268 of file xlogreader.c.

References allocate_recordbuf(), Assert, XLogReaderState::currRecPtr, DecodeXLogRecord(), XLogReaderState::EndRecPtr, XLogReaderState::errormsg_buf, InvalidXLogRecPtr, MAXALIGN, Min, XLogReaderState::readBuf, readOff, ReadPageInternal(), XLogReaderState::readRecordBuf, XLogReaderState::readRecordBufSize, XLogReaderState::ReadRecPtr, report_invalid_record(), ResetDecoder(), XLogReaderState::segcxt, SizeOfXLogRecord, SizeOfXLogShortPHD, ValidXLogRecord(), ValidXLogRecordHeader(), WALSegmentContext::ws_segsize, XLogRecord::xl_info, XLogRecord::xl_rmid, XLogRecord::xl_tot_len, XLOG_SWITCH, XLogPageHeaderSize, XLogReaderInvalReadState(), XLogSegmentOffset, XLP_FIRST_IS_CONTRECORD, XLogPageHeaderData::xlp_info, XLogPageHeaderData::xlp_rem_len, XLR_INFO_MASK, and XRecOffIsValid.

Referenced by DecodingContextFindStartpoint(), extractPageMap(), findLastCheckpoint(), main(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), readOneRecord(), ReadRecord(), XLogReaderValidatePageHeader(), XlogReadTwoPhaseData(), and XLogSendLogical().

269 {
270  XLogRecPtr RecPtr;
271  XLogRecord *record;
272  XLogRecPtr targetPagePtr;
273  bool randAccess;
274  uint32 len,
275  total_len;
276  uint32 targetRecOff;
277  uint32 pageHeaderSize;
278  bool gotheader;
279  int readOff;
280 
281  /*
282  * randAccess indicates whether to verify the previous-record pointer of
283  * the record we're reading. We only do this if we're reading
284  * sequentially, which is what we initially assume.
285  */
286  randAccess = false;
287 
288  /* reset error state */
289  *errormsg = NULL;
290  state->errormsg_buf[0] = '\0';
291 
292  ResetDecoder(state);
293 
294  RecPtr = state->EndRecPtr;
295 
296  if (state->ReadRecPtr != InvalidXLogRecPtr)
297  {
298  /* read the record after the one we just read */
299 
300  /*
301  * EndRecPtr is pointing to end+1 of the previous WAL record. If
302  * we're at a page boundary, no more records can fit on the current
303  * page. We must skip over the page header, but we can't do that until
304  * we've read in the page, since the header size is variable.
305  */
306  }
307  else
308  {
309  /*
310  * Caller supplied a position to start at.
311  *
312  * In this case, EndRecPtr should already be pointing to a valid
313  * record starting position.
314  */
315  Assert(XRecOffIsValid(RecPtr));
316  randAccess = true;
317  }
318 
319  state->currRecPtr = RecPtr;
320 
321  targetPagePtr = RecPtr - (RecPtr % XLOG_BLCKSZ);
322  targetRecOff = RecPtr % XLOG_BLCKSZ;
323 
324  /*
325  * Read the page containing the record into state->readBuf. Request enough
326  * byte to cover the whole record header, or at least the part of it that
327  * fits on the same page.
328  */
329  readOff = ReadPageInternal(state, targetPagePtr,
330  Min(targetRecOff + SizeOfXLogRecord, XLOG_BLCKSZ));
331  if (readOff < 0)
332  goto err;
333 
334  /*
335  * ReadPageInternal always returns at least the page header, so we can
336  * examine it now.
337  */
338  pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
339  if (targetRecOff == 0)
340  {
341  /*
342  * At page start, so skip over page header.
343  */
344  RecPtr += pageHeaderSize;
345  targetRecOff = pageHeaderSize;
346  }
347  else if (targetRecOff < pageHeaderSize)
348  {
349  report_invalid_record(state, "invalid record offset at %X/%X",
350  (uint32) (RecPtr >> 32), (uint32) RecPtr);
351  goto err;
352  }
353 
354  if ((((XLogPageHeader) state->readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD) &&
355  targetRecOff == pageHeaderSize)
356  {
357  report_invalid_record(state, "contrecord is requested by %X/%X",
358  (uint32) (RecPtr >> 32), (uint32) RecPtr);
359  goto err;
360  }
361 
362  /* ReadPageInternal has verified the page header */
363  Assert(pageHeaderSize <= readOff);
364 
365  /*
366  * Read the record length.
367  *
368  * NB: Even though we use an XLogRecord pointer here, the whole record
369  * header might not fit on this page. xl_tot_len is the first field of the
370  * struct, so it must be on this page (the records are MAXALIGNed), but we
371  * cannot access any other fields until we've verified that we got the
372  * whole header.
373  */
374  record = (XLogRecord *) (state->readBuf + RecPtr % XLOG_BLCKSZ);
375  total_len = record->xl_tot_len;
376 
377  /*
378  * If the whole record header is on this page, validate it immediately.
379  * Otherwise do just a basic sanity check on xl_tot_len, and validate the
380  * rest of the header after reading it from the next page. The xl_tot_len
381  * check is necessary here to ensure that we enter the "Need to reassemble
382  * record" code path below; otherwise we might fail to apply
383  * ValidXLogRecordHeader at all.
384  */
385  if (targetRecOff <= XLOG_BLCKSZ - SizeOfXLogRecord)
386  {
387  if (!ValidXLogRecordHeader(state, RecPtr, state->ReadRecPtr, record,
388  randAccess))
389  goto err;
390  gotheader = true;
391  }
392  else
393  {
394  /* XXX: more validation should be done here */
395  if (total_len < SizeOfXLogRecord)
396  {
397  report_invalid_record(state,
398  "invalid record length at %X/%X: wanted %u, got %u",
399  (uint32) (RecPtr >> 32), (uint32) RecPtr,
400  (uint32) SizeOfXLogRecord, total_len);
401  goto err;
402  }
403  gotheader = false;
404  }
405 
406  len = XLOG_BLCKSZ - RecPtr % XLOG_BLCKSZ;
407  if (total_len > len)
408  {
409  /* Need to reassemble record */
410  char *contdata;
411  XLogPageHeader pageHeader;
412  char *buffer;
413  uint32 gotlen;
414 
415  /*
416  * Enlarge readRecordBuf as needed.
417  */
418  if (total_len > state->readRecordBufSize &&
419  !allocate_recordbuf(state, total_len))
420  {
421  /* We treat this as a "bogus data" condition */
422  report_invalid_record(state, "record length %u at %X/%X too long",
423  total_len,
424  (uint32) (RecPtr >> 32), (uint32) RecPtr);
425  goto err;
426  }
427 
428  /* Copy the first fragment of the record from the first page. */
429  memcpy(state->readRecordBuf,
430  state->readBuf + RecPtr % XLOG_BLCKSZ, len);
431  buffer = state->readRecordBuf + len;
432  gotlen = len;
433 
434  do
435  {
436  /* Calculate pointer to beginning of next page */
437  targetPagePtr += XLOG_BLCKSZ;
438 
439  /* Wait for the next page to become available */
440  readOff = ReadPageInternal(state, targetPagePtr,
441  Min(total_len - gotlen + SizeOfXLogShortPHD,
442  XLOG_BLCKSZ));
443 
444  if (readOff < 0)
445  goto err;
446 
447  Assert(SizeOfXLogShortPHD <= readOff);
448 
449  /* Check that the continuation on next page looks valid */
450  pageHeader = (XLogPageHeader) state->readBuf;
451  if (!(pageHeader->xlp_info & XLP_FIRST_IS_CONTRECORD))
452  {
453  report_invalid_record(state,
454  "there is no contrecord flag at %X/%X",
455  (uint32) (RecPtr >> 32), (uint32) RecPtr);
456  goto err;
457  }
458 
459  /*
460  * Cross-check that xlp_rem_len agrees with how much of the record
461  * we expect there to be left.
462  */
463  if (pageHeader->xlp_rem_len == 0 ||
464  total_len != (pageHeader->xlp_rem_len + gotlen))
465  {
466  report_invalid_record(state,
467  "invalid contrecord length %u at %X/%X",
468  pageHeader->xlp_rem_len,
469  (uint32) (RecPtr >> 32), (uint32) RecPtr);
470  goto err;
471  }
472 
473  /* Append the continuation from this page to the buffer */
474  pageHeaderSize = XLogPageHeaderSize(pageHeader);
475 
476  if (readOff < pageHeaderSize)
477  readOff = ReadPageInternal(state, targetPagePtr,
478  pageHeaderSize);
479 
480  Assert(pageHeaderSize <= readOff);
481 
482  contdata = (char *) state->readBuf + pageHeaderSize;
483  len = XLOG_BLCKSZ - pageHeaderSize;
484  if (pageHeader->xlp_rem_len < len)
485  len = pageHeader->xlp_rem_len;
486 
487  if (readOff < pageHeaderSize + len)
488  readOff = ReadPageInternal(state, targetPagePtr,
489  pageHeaderSize + len);
490 
491  memcpy(buffer, (char *) contdata, len);
492  buffer += len;
493  gotlen += len;
494 
495  /* If we just reassembled the record header, validate it. */
496  if (!gotheader)
497  {
498  record = (XLogRecord *) state->readRecordBuf;
499  if (!ValidXLogRecordHeader(state, RecPtr, state->ReadRecPtr,
500  record, randAccess))
501  goto err;
502  gotheader = true;
503  }
504  } while (gotlen < total_len);
505 
506  Assert(gotheader);
507 
508  record = (XLogRecord *) state->readRecordBuf;
509  if (!ValidXLogRecord(state, record, RecPtr))
510  goto err;
511 
512  pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
513  state->ReadRecPtr = RecPtr;
514  state->EndRecPtr = targetPagePtr + pageHeaderSize
515  + MAXALIGN(pageHeader->xlp_rem_len);
516  }
517  else
518  {
519  /* Wait for the record data to become available */
520  readOff = ReadPageInternal(state, targetPagePtr,
521  Min(targetRecOff + total_len, XLOG_BLCKSZ));
522  if (readOff < 0)
523  goto err;
524 
525  /* Record does not cross a page boundary */
526  if (!ValidXLogRecord(state, record, RecPtr))
527  goto err;
528 
529  state->EndRecPtr = RecPtr + MAXALIGN(total_len);
530 
531  state->ReadRecPtr = RecPtr;
532  }
533 
534  /*
535  * Special processing if it's an XLOG SWITCH record
536  */
537  if (record->xl_rmid == RM_XLOG_ID &&
538  (record->xl_info & ~XLR_INFO_MASK) == XLOG_SWITCH)
539  {
540  /* Pretend it extends to end of segment */
541  state->EndRecPtr += state->segcxt.ws_segsize - 1;
542  state->EndRecPtr -= XLogSegmentOffset(state->EndRecPtr, state->segcxt.ws_segsize);
543  }
544 
545  if (DecodeXLogRecord(state, record, errormsg))
546  return record;
547  else
548  return NULL;
549 
550 err:
551 
552  /*
553  * Invalidate the read state. We might read from a different source after
554  * failure.
555  */
557 
558  if (state->errormsg_buf[0] != '\0')
559  *errormsg = state->errormsg_buf;
560 
561  return NULL;
562 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
char * readRecordBuf
Definition: xlogreader.h:248
#define XLogPageHeaderSize(hdr)
Definition: xlog_internal.h:85
#define Min(x, y)
Definition: c.h:920
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:58
RmgrId xl_rmid
Definition: xlogrecord.h:47
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record, char **errormsg)
Definition: xlogreader.c:1176
static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
Definition: xlogreader.c:575
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
static uint32 readOff
Definition: xlog.c:816
static void XLogReaderInvalReadState(XLogReaderState *state)
Definition: xlogreader.c:674
uint32 xl_tot_len
Definition: xlogrecord.h:43
static void static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength)
Definition: xlogreader.c:173
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
unsigned int uint32
Definition: c.h:367
uint32 readRecordBufSize
Definition: xlogreader.h:249
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
XLogRecPtr currRecPtr
Definition: xlogreader.h:224
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
static bool ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr, XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess)
Definition: xlogreader.c:688
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
uint8 xl_info
Definition: xlogrecord.h:46
#define XLP_FIRST_IS_CONTRECORD
Definition: xlog_internal.h:77
#define MAXALIGN(LEN)
Definition: c.h:691
#define XLOG_SWITCH
Definition: pg_control.h:71
static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
Definition: xlogreader.c:757
#define XRecOffIsValid(xlrp)
WALSegmentContext segcxt
Definition: xlogreader.h:212
char * errormsg_buf
Definition: xlogreader.h:252
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1151

◆ XLogRecGetBlockData()

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

Definition at line 1516 of file xlogreader.c.

References XLogReaderState::blocks, DecodedBkpBlock::data, DecodedBkpBlock::data_len, DecodedBkpBlock::has_data, and DecodedBkpBlock::in_use.

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_clean(), heap_xlog_freeze_page(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_multi_insert(), and heap_xlog_update().

1517 {
1518  DecodedBkpBlock *bkpb;
1519 
1520  if (!record->blocks[block_id].in_use)
1521  return NULL;
1522 
1523  bkpb = &record->blocks[block_id];
1524 
1525  if (!bkpb->has_data)
1526  {
1527  if (len)
1528  *len = 0;
1529  return NULL;
1530  }
1531  else
1532  {
1533  if (len)
1534  *len = bkpb->data_len;
1535  return bkpb->data;
1536  }
1537 }
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:195

◆ XLogRecGetBlockTag()

bool XLogRecGetBlockTag ( XLogReaderState record,
uint8  block_id,
RelFileNode rnode,
ForkNumber forknum,
BlockNumber blknum 
)

Definition at line 1492 of file xlogreader.c.

References DecodedBkpBlock::blkno, XLogReaderState::blocks, DecodedBkpBlock::forknum, DecodedBkpBlock::in_use, and DecodedBkpBlock::rnode.

Referenced by brin_xlog_revmap_extend(), btree_xlog_delete(), btree_xlog_split(), checkXLogConsistency(), DecodeDelete(), DecodeInsert(), DecodeMultiInsert(), DecodeSpecConfirm(), DecodeUpdate(), extractPageInfo(), gistRedoDeleteRecord(), gistRedoPageSplitRecord(), hash_xlog_add_ovfl_page(), hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), hash_xlog_vacuum_one_page(), heap_xlog_clean(), heap_xlog_delete(), heap_xlog_freeze_page(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_update(), heap_xlog_visible(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), spgRedoVacuumRedirect(), xlog_redo(), XLogDumpDisplayRecord(), and XLogReadBufferForRedoExtended().

1494 {
1495  DecodedBkpBlock *bkpb;
1496 
1497  if (!record->blocks[block_id].in_use)
1498  return false;
1499 
1500  bkpb = &record->blocks[block_id];
1501  if (rnode)
1502  *rnode = bkpb->rnode;
1503  if (forknum)
1504  *forknum = bkpb->forknum;
1505  if (blknum)
1506  *blknum = bkpb->blkno;
1507  return true;
1508 }
BlockNumber blkno
Definition: xlogreader.h:126
ForkNumber forknum
Definition: xlogreader.h:125
RelFileNode rnode
Definition: xlogreader.h:124
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:195

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 1598 of file xlogreader.c.

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

1599 {
1600  TransactionId xid,
1601  next_xid;
1602  uint32 epoch;
1603 
1604  /*
1605  * This function is only safe during replay, because it depends on the
1606  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1607  */
1609 
1610  xid = XLogRecGetXid(record);
1613 
1614  /*
1615  * If xid is numerically greater than next_xid, it has to be from the last
1616  * epoch.
1617  */
1618  if (unlikely(xid > next_xid))
1619  --epoch;
1620 
1621  return FullTransactionIdFromEpochAndXid(epoch, xid);
1622 }
#define AmStartupProcess()
Definition: miscadmin.h:430
uint32 TransactionId
Definition: c.h:513
FullTransactionId nextFullXid
Definition: transam.h:178
#define XidFromFullTransactionId(x)
Definition: transam.h:48
bool IsUnderPostmaster
Definition: globals.c:109
VariableCache ShmemVariableCache
Definition: varsup.c:34
unsigned int uint32
Definition: c.h:367
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:305
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:738
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:69
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
#define unlikely(x)
Definition: c.h:206