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 XLogRecGetTopXid(decoder)   ((decoder)->toplevel_xid)
 
#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 310 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 304 of file xlogreader.h.

Referenced by xlog_redo(), and XLogDumpDisplayRecord().

◆ XLogRecGetRmid

◆ XLogRecGetTopXid

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

Definition at line 309 of file xlogreader.h.

Referenced by LogicalDecodingProcessRecord().

◆ XLogRecGetTotalLen

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

Definition at line 303 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 1177 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, InvalidTransactionId, 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, XLogReaderState::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 XLogInsertRecord(), and XLogReadRecord().

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

1552 {
1553  DecodedBkpBlock *bkpb;
1554  char *ptr;
1555  PGAlignedBlock tmp;
1556 
1557  if (!record->blocks[block_id].in_use)
1558  return false;
1559  if (!record->blocks[block_id].has_image)
1560  return false;
1561 
1562  bkpb = &record->blocks[block_id];
1563  ptr = bkpb->bkp_image;
1564 
1565  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1566  {
1567  /* If a backup block image is compressed, decompress it */
1568  if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
1569  BLCKSZ - bkpb->hole_length, true) < 0)
1570  {
1571  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1572  (uint32) (record->ReadRecPtr >> 32),
1573  (uint32) record->ReadRecPtr,
1574  block_id);
1575  return false;
1576  }
1577  ptr = tmp.data;
1578  }
1579 
1580  /* generate page, taking into account hole if necessary */
1581  if (bkpb->hole_length == 0)
1582  {
1583  memcpy(page, ptr, BLCKSZ);
1584  }
1585  else
1586  {
1587  memcpy(page, ptr, bkpb->hole_offset);
1588  /* must zero-fill the hole */
1589  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1590  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1591  ptr + bkpb->hole_offset,
1592  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1593  }
1594 
1595  return true;
1596 }
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:949
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
char data[BLCKSZ]
Definition: c.h:1082
uint16 hole_length
Definition: xlogreader.h:136
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
unsigned int uint32
Definition: c.h:374
#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:197

◆ WALRead()

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

Definition at line 1063 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().

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

◆ 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:117
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1014
#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:215
void pfree(void *pointer)
Definition: mcxt.c:1057
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:214
char * errormsg_buf
Definition: xlogreader.h:254
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:250
WALOpenSegment seg
Definition: xlogreader.h:215
void pfree(void *pointer)
Definition: mcxt.c:1057
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:221
char * errormsg_buf
Definition: xlogreader.h:254
char * main_data
Definition: xlogreader.h:188
XLogReaderRoutine routine
Definition: xlogreader.h:152
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:197

◆ XLogReaderValidatePageHeader()

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

Definition at line 787 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().

789 {
790  XLogRecPtr recaddr;
791  XLogSegNo segno;
792  int32 offset;
793  XLogPageHeader hdr = (XLogPageHeader) phdr;
794 
795  Assert((recptr % XLOG_BLCKSZ) == 0);
796 
797  XLByteToSeg(recptr, segno, state->segcxt.ws_segsize);
798  offset = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
799 
800  XLogSegNoOffsetToRecPtr(segno, offset, state->segcxt.ws_segsize, recaddr);
801 
802  if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
803  {
804  char fname[MAXFNAMELEN];
805 
806  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
807 
808  report_invalid_record(state,
809  "invalid magic number %04X in log segment %s, offset %u",
810  hdr->xlp_magic,
811  fname,
812  offset);
813  return false;
814  }
815 
816  if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
817  {
818  char fname[MAXFNAMELEN];
819 
820  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
821 
822  report_invalid_record(state,
823  "invalid info bits %04X in log segment %s, offset %u",
824  hdr->xlp_info,
825  fname,
826  offset);
827  return false;
828  }
829 
830  if (hdr->xlp_info & XLP_LONG_HEADER)
831  {
832  XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
833 
834  if (state->system_identifier &&
835  longhdr->xlp_sysid != state->system_identifier)
836  {
837  report_invalid_record(state,
838  "WAL file is from different database system: WAL file database system identifier is %llu, pg_control database system identifier is %llu",
839  (unsigned long long) longhdr->xlp_sysid,
840  (unsigned long long) state->system_identifier);
841  return false;
842  }
843  else if (longhdr->xlp_seg_size != state->segcxt.ws_segsize)
844  {
845  report_invalid_record(state,
846  "WAL file is from different database system: incorrect segment size in page header");
847  return false;
848  }
849  else if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
850  {
851  report_invalid_record(state,
852  "WAL file is from different database system: incorrect XLOG_BLCKSZ in page header");
853  return false;
854  }
855  }
856  else if (offset == 0)
857  {
858  char fname[MAXFNAMELEN];
859 
860  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
861 
862  /* hmm, first page of file doesn't have a long header? */
863  report_invalid_record(state,
864  "invalid info bits %04X in log segment %s, offset %u",
865  hdr->xlp_info,
866  fname,
867  offset);
868  return false;
869  }
870 
871  /*
872  * Check that the address on the page agrees with what we expected. This
873  * check typically fails when an old WAL segment is recycled, and hasn't
874  * yet been overwritten with new data yet.
875  */
876  if (hdr->xlp_pageaddr != recaddr)
877  {
878  char fname[MAXFNAMELEN];
879 
880  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
881 
882  report_invalid_record(state,
883  "unexpected pageaddr %X/%X in log segment %s, offset %u",
884  (uint32) (hdr->xlp_pageaddr >> 32), (uint32) hdr->xlp_pageaddr,
885  fname,
886  offset);
887  return false;
888  }
889 
890  /*
891  * Since child timelines are always assigned a TLI greater than their
892  * immediate parent's TLI, we should never see TLI go backwards across
893  * successive pages of a consistent WAL sequence.
894  *
895  * Sometimes we re-read a segment that's already been (partially) read. So
896  * we only verify TLIs for pages that are later than the last remembered
897  * LSN.
898  */
899  if (recptr > state->latestPagePtr)
900  {
901  if (hdr->xlp_tli < state->latestPageTLI)
902  {
903  char fname[MAXFNAMELEN];
904 
905  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
906 
907  report_invalid_record(state,
908  "out-of-sequence timeline ID %u (after %u) in log segment %s, offset %u",
909  hdr->xlp_tli,
910  state->latestPageTLI,
911  fname,
912  offset);
913  return false;
914  }
915  }
916  state->latestPagePtr = recptr;
917  state->latestPageTLI = hdr->xlp_tli;
918 
919  return true;
920 }
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:58
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:54
signed int int32
Definition: c.h:362
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:71
WALOpenSegment seg
Definition: xlogreader.h:215
XLogRecPtr latestPagePtr
Definition: xlogreader.h:222
#define XLOG_PAGE_MAGIC
Definition: xlog_internal.h:34
uint64 XLogSegNo
Definition: xlogdefs.h:41
unsigned int uint32
Definition: c.h:374
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:80
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
#define XLP_LONG_HEADER
Definition: xlog_internal.h:76
#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:214
TimeLineID latestPageTLI
Definition: xlogreader.h:223
#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 (expected %lld) at %X/%X",
468  pageHeader->xlp_rem_len,
469  ((long long) total_len) - gotlen,
470  (uint32) (RecPtr >> 32), (uint32) RecPtr);
471  goto err;
472  }
473 
474  /* Append the continuation from this page to the buffer */
475  pageHeaderSize = XLogPageHeaderSize(pageHeader);
476 
477  if (readOff < pageHeaderSize)
478  readOff = ReadPageInternal(state, targetPagePtr,
479  pageHeaderSize);
480 
481  Assert(pageHeaderSize <= readOff);
482 
483  contdata = (char *) state->readBuf + pageHeaderSize;
484  len = XLOG_BLCKSZ - pageHeaderSize;
485  if (pageHeader->xlp_rem_len < len)
486  len = pageHeader->xlp_rem_len;
487 
488  if (readOff < pageHeaderSize + len)
489  readOff = ReadPageInternal(state, targetPagePtr,
490  pageHeaderSize + len);
491 
492  memcpy(buffer, (char *) contdata, len);
493  buffer += len;
494  gotlen += len;
495 
496  /* If we just reassembled the record header, validate it. */
497  if (!gotheader)
498  {
499  record = (XLogRecord *) state->readRecordBuf;
500  if (!ValidXLogRecordHeader(state, RecPtr, state->ReadRecPtr,
501  record, randAccess))
502  goto err;
503  gotheader = true;
504  }
505  } while (gotlen < total_len);
506 
507  Assert(gotheader);
508 
509  record = (XLogRecord *) state->readRecordBuf;
510  if (!ValidXLogRecord(state, record, RecPtr))
511  goto err;
512 
513  pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
514  state->ReadRecPtr = RecPtr;
515  state->EndRecPtr = targetPagePtr + pageHeaderSize
516  + MAXALIGN(pageHeader->xlp_rem_len);
517  }
518  else
519  {
520  /* Wait for the record data to become available */
521  readOff = ReadPageInternal(state, targetPagePtr,
522  Min(targetRecOff + total_len, XLOG_BLCKSZ));
523  if (readOff < 0)
524  goto err;
525 
526  /* Record does not cross a page boundary */
527  if (!ValidXLogRecord(state, record, RecPtr))
528  goto err;
529 
530  state->EndRecPtr = RecPtr + MAXALIGN(total_len);
531 
532  state->ReadRecPtr = RecPtr;
533  }
534 
535  /*
536  * Special processing if it's an XLOG SWITCH record
537  */
538  if (record->xl_rmid == RM_XLOG_ID &&
539  (record->xl_info & ~XLR_INFO_MASK) == XLOG_SWITCH)
540  {
541  /* Pretend it extends to end of segment */
542  state->EndRecPtr += state->segcxt.ws_segsize - 1;
543  state->EndRecPtr -= XLogSegmentOffset(state->EndRecPtr, state->segcxt.ws_segsize);
544  }
545 
546  if (DecodeXLogRecord(state, record, errormsg))
547  return record;
548  else
549  return NULL;
550 
551 err:
552 
553  /*
554  * Invalidate the read state. We might read from a different source after
555  * failure.
556  */
558 
559  if (state->errormsg_buf[0] != '\0')
560  *errormsg = state->errormsg_buf;
561 
562  return NULL;
563 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
char * readRecordBuf
Definition: xlogreader.h:250
#define XLogPageHeaderSize(hdr)
Definition: xlog_internal.h:82
#define Min(x, y)
Definition: c.h:927
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:54
bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record, char **errormsg)
Definition: xlogreader.c:1177
static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
Definition: xlogreader.c:576
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
static uint32 readOff
Definition: xlog.c:814
static void XLogReaderInvalReadState(XLogReaderState *state)
Definition: xlogreader.c:675
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:374
uint32 readRecordBufSize
Definition: xlogreader.h:251
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
XLogRecPtr currRecPtr
Definition: xlogreader.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
static bool ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr, XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess)
Definition: xlogreader.c:689
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:52
uint8 xl_info
Definition: xlogrecord.h:46
#define XLP_FIRST_IS_CONTRECORD
Definition: xlog_internal.h:74
#define MAXALIGN(LEN)
Definition: c.h:698
#define XLOG_SWITCH
Definition: pg_control.h:71
static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
Definition: xlogreader.c:758
#define XRecOffIsValid(xlrp)
WALSegmentContext segcxt
Definition: xlogreader.h:214
char * errormsg_buf
Definition: xlogreader.h:254
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1152

◆ XLogRecGetBlockData()

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

Definition at line 1522 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().

1523 {
1524  DecodedBkpBlock *bkpb;
1525 
1526  if (!record->blocks[block_id].in_use)
1527  return NULL;
1528 
1529  bkpb = &record->blocks[block_id];
1530 
1531  if (!bkpb->has_data)
1532  {
1533  if (len)
1534  *len = 0;
1535  return NULL;
1536  }
1537  else
1538  {
1539  if (len)
1540  *len = bkpb->data_len;
1541  return bkpb->data;
1542  }
1543 }
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:197

◆ XLogRecGetBlockTag()

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

Definition at line 1498 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().

1500 {
1501  DecodedBkpBlock *bkpb;
1502 
1503  if (!record->blocks[block_id].in_use)
1504  return false;
1505 
1506  bkpb = &record->blocks[block_id];
1507  if (rnode)
1508  *rnode = bkpb->rnode;
1509  if (forknum)
1510  *forknum = bkpb->forknum;
1511  if (blknum)
1512  *blknum = bkpb->blkno;
1513  return true;
1514 }
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:197

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 1604 of file xlogreader.c.

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

1605 {
1606  TransactionId xid,
1607  next_xid;
1608  uint32 epoch;
1609 
1610  /*
1611  * This function is only safe during replay, because it depends on the
1612  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1613  */
1615 
1616  xid = XLogRecGetXid(record);
1619 
1620  /*
1621  * If xid is numerically greater than next_xid, it has to be from the last
1622  * epoch.
1623  */
1624  if (unlikely(xid > next_xid))
1625  --epoch;
1626 
1627  return FullTransactionIdFromEpochAndXid(epoch, xid);
1628 }
#define AmStartupProcess()
Definition: miscadmin.h:431
uint32 TransactionId
Definition: c.h:520
FullTransactionId nextXid
Definition: transam.h:213
#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:374
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:745
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:71
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
#define unlikely(x)
Definition: c.h:206