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_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_prune(), heap_xlog_update(), heap_xlog_vacuum(), 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 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_COMPRESSED, BKPIMAGE_HAS_HOLE, 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, LSN_FORMAT_ARGS, 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().

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

◆ LocalXLogReaderRoutine()

XLogReaderRoutine* LocalXLogReaderRoutine ( void  )

◆ RestoreBlockImage()

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

Definition at line 1546 of file xlogreader.c.

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

Referenced by checkXLogConsistency(), and XLogReadBufferForRedoExtended().

1547 {
1548  DecodedBkpBlock *bkpb;
1549  char *ptr;
1550  PGAlignedBlock tmp;
1551 
1552  if (!record->blocks[block_id].in_use)
1553  return false;
1554  if (!record->blocks[block_id].has_image)
1555  return false;
1556 
1557  bkpb = &record->blocks[block_id];
1558  ptr = bkpb->bkp_image;
1559 
1560  if (BKPIMAGE_COMPRESSED(bkpb->bimg_info))
1561  {
1562  /* If a backup block image is compressed, decompress it */
1563  bool decomp_success = true;
1564 
1565  if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_PGLZ) != 0)
1566  {
1567  if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
1568  BLCKSZ - bkpb->hole_length, true) < 0)
1569  decomp_success = false;
1570  }
1571  else if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_LZ4) != 0)
1572  {
1573 #ifdef USE_LZ4
1574  if (LZ4_decompress_safe(ptr, tmp.data,
1575  bkpb->bimg_len, BLCKSZ - bkpb->hole_length) <= 0)
1576  decomp_success = false;
1577 #else
1578  report_invalid_record(record, "image at %X/%X compressed with %s not supported by build, block %d",
1579  LSN_FORMAT_ARGS(record->ReadRecPtr),
1580  "LZ4",
1581  block_id);
1582  return false;
1583 #endif
1584  }
1585  else
1586  {
1587  report_invalid_record(record, "image at %X/%X compressed with unknown method, block %d",
1588  LSN_FORMAT_ARGS(record->ReadRecPtr),
1589  block_id);
1590  return false;
1591  }
1592 
1593  if (!decomp_success)
1594  {
1595  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1596  LSN_FORMAT_ARGS(record->ReadRecPtr),
1597  block_id);
1598  return false;
1599  }
1600 
1601  ptr = tmp.data;
1602  }
1603 
1604  /* generate page, taking into account hole if necessary */
1605  if (bkpb->hole_length == 0)
1606  {
1607  memcpy(page, ptr, BLCKSZ);
1608  }
1609  else
1610  {
1611  memcpy(page, ptr, bkpb->hole_offset);
1612  /* must zero-fill the hole */
1613  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1614  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1615  ptr + bkpb->hole_offset,
1616  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1617  }
1618 
1619  return true;
1620 }
uint16 hole_offset
Definition: xlogreader.h:135
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:61
#define MemSet(start, val, len)
Definition: c.h:1008
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
char data[BLCKSZ]
Definition: c.h:1141
uint16 hole_length
Definition: xlogreader.h:136
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
char * bkp_image
Definition: xlogreader.h:134
#define BKPIMAGE_COMPRESS_LZ4
Definition: xlogrecord.h:151
#define BKPIMAGE_COMPRESS_PGLZ
Definition: xlogrecord.h:150
#define BKPIMAGE_COMPRESSED(info)
Definition: xlogrecord.h:152
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 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:291
WALSegmentCloseCB segment_close
Definition: xlogreader.h:113
static void pgstat_report_wait_end(void)
Definition: wait_event.h:278
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:48
XLogSegNo ws_segno
Definition: xlogreader.h:47
unsigned int uint32
Definition: c.h:441
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:262
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
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 243 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().

244 {
245  Assert(!XLogRecPtrIsInvalid(RecPtr));
246 
247  ResetDecoder(state);
248 
249  /* Begin at the passed-in record pointer. */
250  state->EndRecPtr = RecPtr;
251  state->ReadRecPtr = InvalidXLogRecPtr;
252 }
#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:804
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 78 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().

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

143 {
144  int block_id;
145 
146  if (state->seg.ws_file != -1)
147  state->routine.segment_close(state);
148 
149  for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
150  {
151  if (state->blocks[block_id].data)
152  pfree(state->blocks[block_id].data);
153  }
154  if (state->main_data)
155  pfree(state->main_data);
156 
157  pfree(state->errormsg_buf);
158  if (state->readRecordBuf)
159  pfree(state->readRecordBuf);
160  pfree(state->readBuf);
161  pfree(state);
162 }
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:1169
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:225
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 786 of file xlogreader.c.

References Assert, header(), InvalidXLogRecPtr, XLogReaderState::latestPagePtr, XLogReaderState::latestPageTLI, LSN_FORMAT_ARGS, 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",
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:61
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:54
signed int int32
Definition: c.h:429
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:71
WALOpenSegment seg
Definition: xlogreader.h:215
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
XLogRecPtr latestPagePtr
Definition: xlogreader.h:222
#define XLOG_PAGE_MAGIC
Definition: xlog_internal.h:34
uint64 XLogSegNo
Definition: xlogdefs.h:48
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:804
#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 271 of file xlogreader.c.

References allocate_recordbuf(), Assert, XLogReaderState::currRecPtr, DecodeXLogRecord(), XLogReaderState::EndRecPtr, XLogReaderState::errormsg_buf, InvalidXLogRecPtr, LSN_FORMAT_ARGS, 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().

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

◆ XLogRecGetBlockData()

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

Definition at line 1517 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_freeze_page(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_multi_insert(), heap_xlog_prune(), heap_xlog_update(), and heap_xlog_vacuum().

1518 {
1519  DecodedBkpBlock *bkpb;
1520 
1521  if (!record->blocks[block_id].in_use)
1522  return NULL;
1523 
1524  bkpb = &record->blocks[block_id];
1525 
1526  if (!bkpb->has_data)
1527  {
1528  if (len)
1529  *len = 0;
1530  return NULL;
1531  }
1532  else
1533  {
1534  if (len)
1535  *len = bkpb->data_len;
1536  return bkpb->data;
1537  }
1538 }
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 1493 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_delete(), heap_xlog_freeze_page(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_prune(), heap_xlog_update(), heap_xlog_vacuum(), heap_xlog_visible(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), spgRedoVacuumRedirect(), xlog_block_info(), XLogDumpDisplayRecord(), and XLogReadBufferForRedoExtended().

1495 {
1496  DecodedBkpBlock *bkpb;
1497 
1498  if (!record->blocks[block_id].in_use)
1499  return false;
1500 
1501  bkpb = &record->blocks[block_id];
1502  if (rnode)
1503  *rnode = bkpb->rnode;
1504  if (forknum)
1505  *forknum = bkpb->forknum;
1506  if (blknum)
1507  *blknum = bkpb->blkno;
1508  return true;
1509 }
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 1628 of file xlogreader.c.

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

1629 {
1630  TransactionId xid,
1631  next_xid;
1632  uint32 epoch;
1633 
1634  /*
1635  * This function is only safe during replay, because it depends on the
1636  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1637  */
1639 
1640  xid = XLogRecGetXid(record);
1643 
1644  /*
1645  * If xid is numerically greater than next_xid, it has to be from the last
1646  * epoch.
1647  */
1648  if (unlikely(xid > next_xid))
1649  --epoch;
1650 
1651  return FullTransactionIdFromEpochAndXid(epoch, xid);
1652 }
#define AmStartupProcess()
Definition: miscadmin.h:445
uint32 TransactionId
Definition: c.h:587
FullTransactionId nextXid
Definition: transam.h:220
#define XidFromFullTransactionId(x)
Definition: transam.h:48
bool IsUnderPostmaster
Definition: globals.c:112
VariableCache ShmemVariableCache
Definition: varsup.c:34
unsigned int uint32
Definition: c.h:441
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:804
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:273