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  DecodedBkpBlock
 
struct  XLogReaderState
 
struct  WALReadError
 

Macros

#define XLogRecGetTotalLen(decoder)   ((decoder)->decoded_record->xl_tot_len)
 
#define XLogRecGetPrev(decoder)   ((decoder)->decoded_record->xl_prev)
 
#define XLogRecGetInfo(decoder)   ((decoder)->decoded_record->xl_info)
 
#define XLogRecGetRmid(decoder)   ((decoder)->decoded_record->xl_rmid)
 
#define XLogRecGetXid(decoder)   ((decoder)->decoded_record->xl_xid)
 
#define XLogRecGetOrigin(decoder)   ((decoder)->record_origin)
 
#define XLogRecGetData(decoder)   ((decoder)->main_data)
 
#define XLogRecGetDataLen(decoder)   ((decoder)->main_data_len)
 
#define XLogRecHasAnyBlockRefs(decoder)   ((decoder)->max_block_id >= 0)
 
#define XLogRecHasBlockRef(decoder, block_id)   ((decoder)->blocks[block_id].in_use)
 
#define XLogRecHasBlockImage(decoder, block_id)   ((decoder)->blocks[block_id].has_image)
 
#define XLogRecBlockImageApply(decoder, block_id)   ((decoder)->blocks[block_id].apply_image)
 

Typedefs

typedef struct WALOpenSegment WALOpenSegment
 
typedef struct WALSegmentContext WALSegmentContext
 
typedef struct XLogReaderState XLogReaderState
 
typedef int(* XLogPageReadCB) (XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf)
 
typedef int(* WALSegmentOpen) (XLogSegNo nextSegNo, WALSegmentContext *segcxt, TimeLineID *tli_p)
 
typedef struct WALReadError WALReadError
 

Functions

XLogReaderStateXLogReaderAllocate (int wal_segment_size, const char *waldir, XLogPageReadCB pagereadfunc, void *private_data)
 
void XLogReaderFree (XLogReaderState *state)
 
void WALOpenSegmentInit (WALOpenSegment *seg, WALSegmentContext *segcxt, int segsize, const char *waldir)
 
struct XLogRecordXLogReadRecord (XLogReaderState *state, XLogRecPtr recptr, char **errormsg)
 
bool XLogReaderValidatePageHeader (XLogReaderState *state, XLogRecPtr recptr, char *phdr)
 
bool WALRead (char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, WALOpenSegment *seg, WALSegmentContext *segcxt, WALSegmentOpen openSegment, 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

◆ XLogRecBlockImageApply

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

◆ XLogRecGetData

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

Definition at line 283 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_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 278 of file xlogreader.h.

Referenced by xlog_redo(), and XLogDumpDisplayRecord().

◆ XLogRecGetRmid

◆ XLogRecGetTotalLen

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

Definition at line 277 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

◆ WALSegmentContext

◆ WALSegmentOpen

typedef int(* WALSegmentOpen) (XLogSegNo nextSegNo, WALSegmentContext *segcxt, TimeLineID *tli_p)

Definition at line 235 of file xlogreader.h.

◆ XLogPageReadCB

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

Definition at line 52 of file xlogreader.h.

◆ XLogReaderState

Definition at line 49 of file xlogreader.h.

Function Documentation

◆ DecodeXLogRecord()

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

Definition at line 1144 of file xlogreader.c.

References DecodedBkpBlock::apply_image, Assert, DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, DecodedBkpBlock::bkp_image, BKPBLOCK_FORK_MASK, BKPBLOCK_HAS_DATA, BKPBLOCK_HAS_IMAGE, BKPBLOCK_SAME_REL, BKPIMAGE_APPLY, BKPIMAGE_HAS_HOLE, BKPIMAGE_IS_COMPRESSED, DecodedBkpBlock::blkno, XLogReaderState::blocks, COPY_HEADER_FIELD, DecodedBkpBlock::data, DecodedBkpBlock::data_bufsz, DecodedBkpBlock::data_len, XLogReaderState::decoded_record, XLogReaderState::errormsg_buf, DecodedBkpBlock::flags, DecodedBkpBlock::forknum, DecodedBkpBlock::has_data, DecodedBkpBlock::has_image, DecodedBkpBlock::hole_length, DecodedBkpBlock::hole_offset, DecodedBkpBlock::in_use, InvalidRepOriginId, XLogReaderState::main_data, XLogReaderState::main_data_bufsz, XLogReaderState::main_data_len, Max, XLogReaderState::max_block_id, MAXALIGN, palloc(), pfree(), XLogReaderState::ReadRecPtr, XLogReaderState::record_origin, remaining, report_invalid_record(), ResetDecoder(), DecodedBkpBlock::rnode, SizeOfXLogRecord, XLogRecord::xl_tot_len, XLR_BLOCK_ID_DATA_LONG, XLR_BLOCK_ID_DATA_SHORT, XLR_BLOCK_ID_ORIGIN, and XLR_MAX_BLOCK_ID.

Referenced by XLogInsertRecord(), and XLogReadRecord().

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

◆ RestoreBlockImage()

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

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

1514 {
1515  DecodedBkpBlock *bkpb;
1516  char *ptr;
1517  PGAlignedBlock tmp;
1518 
1519  if (!record->blocks[block_id].in_use)
1520  return false;
1521  if (!record->blocks[block_id].has_image)
1522  return false;
1523 
1524  bkpb = &record->blocks[block_id];
1525  ptr = bkpb->bkp_image;
1526 
1527  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1528  {
1529  /* If a backup block image is compressed, decompress it */
1530  if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
1531  BLCKSZ - bkpb->hole_length, true) < 0)
1532  {
1533  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1534  (uint32) (record->ReadRecPtr >> 32),
1535  (uint32) record->ReadRecPtr,
1536  block_id);
1537  return false;
1538  }
1539  ptr = tmp.data;
1540  }
1541 
1542  /* generate page, taking into account hole if necessary */
1543  if (bkpb->hole_length == 0)
1544  {
1545  memcpy(page, ptr, BLCKSZ);
1546  }
1547  else
1548  {
1549  memcpy(page, ptr, bkpb->hole_offset);
1550  /* must zero-fill the hole */
1551  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1552  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1553  ptr + bkpb->hole_offset,
1554  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1555  }
1556 
1557  return true;
1558 }
uint16 hole_offset
Definition: xlogreader.h:75
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:56
#define MemSet(start, val, len)
Definition: c.h:962
uint16 bimg_len
Definition: xlogreader.h:77
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
char data[BLCKSZ]
Definition: c.h:1091
uint16 hole_length
Definition: xlogreader.h:76
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:131
unsigned int uint32
Definition: c.h:359
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
char * bkp_image
Definition: xlogreader.h:74
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:151

◆ WALOpenSegmentInit()

void WALOpenSegmentInit ( WALOpenSegment seg,
WALSegmentContext segcxt,
int  segsize,
const char *  waldir 
)

Definition at line 209 of file xlogreader.c.

References MAXPGPATH, snprintf, WALSegmentContext::ws_dir, WALOpenSegment::ws_file, WALOpenSegment::ws_segno, WALSegmentContext::ws_segsize, and WALOpenSegment::ws_tli.

Referenced by XLogReaderAllocate().

211 {
212  seg->ws_file = -1;
213  seg->ws_segno = 0;
214  seg->ws_tli = 0;
215 
216  segcxt->ws_segsize = segsize;
217  if (waldir)
218  snprintf(segcxt->ws_dir, MAXPGPATH, "%s", waldir);
219 }
char ws_dir[MAXPGPATH]
Definition: xlogreader.h:45
#define MAXPGPATH
XLogSegNo ws_segno
Definition: xlogreader.h:38
TimeLineID ws_tli
Definition: xlogreader.h:39
#define snprintf
Definition: port.h:192

◆ WALRead()

bool WALRead ( char *  buf,
XLogRecPtr  startptr,
Size  count,
TimeLineID  tli,
WALOpenSegment seg,
WALSegmentContext segcxt,
WALSegmentOpen  openSegment,
WALReadError errinfo 
)

Definition at line 1033 of file xlogreader.c.

References buf, close, pg_pread(), pgstat_report_wait_end(), pgstat_report_wait_start(), 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().

1036 {
1037  char *p;
1038  XLogRecPtr recptr;
1039  Size nbytes;
1040 
1041  p = buf;
1042  recptr = startptr;
1043  nbytes = count;
1044 
1045  while (nbytes > 0)
1046  {
1047  uint32 startoff;
1048  int segbytes;
1049  int readbytes;
1050 
1051  startoff = XLogSegmentOffset(recptr, segcxt->ws_segsize);
1052 
1053  /*
1054  * If the data we want is not in a segment we have open, close what we
1055  * have (if anything) and open the next one, using the caller's
1056  * provided openSegment callback.
1057  */
1058  if (seg->ws_file < 0 ||
1059  !XLByteInSeg(recptr, seg->ws_segno, segcxt->ws_segsize) ||
1060  tli != seg->ws_tli)
1061  {
1062  XLogSegNo nextSegNo;
1063 
1064  if (seg->ws_file >= 0)
1065  close(seg->ws_file);
1066 
1067  XLByteToSeg(recptr, nextSegNo, segcxt->ws_segsize);
1068  seg->ws_file = openSegment(nextSegNo, segcxt, &tli);
1069 
1070  /* Update the current segment info. */
1071  seg->ws_tli = tli;
1072  seg->ws_segno = nextSegNo;
1073  }
1074 
1075  /* How many bytes are within this segment? */
1076  if (nbytes > (segcxt->ws_segsize - startoff))
1077  segbytes = segcxt->ws_segsize - startoff;
1078  else
1079  segbytes = nbytes;
1080 
1081 #ifndef FRONTEND
1083 #endif
1084 
1085  /* Reset errno first; eases reporting non-errno-affecting errors */
1086  errno = 0;
1087  readbytes = pg_pread(seg->ws_file, p, segbytes, (off_t) startoff);
1088 
1089 #ifndef FRONTEND
1091 #endif
1092 
1093  if (readbytes <= 0)
1094  {
1095  errinfo->wre_errno = errno;
1096  errinfo->wre_req = segbytes;
1097  errinfo->wre_read = readbytes;
1098  errinfo->wre_off = startoff;
1099  errinfo->wre_seg = *seg;
1100  return false;
1101  }
1102 
1103  /* Update state for read */
1104  recptr += readbytes;
1105  nbytes -= readbytes;
1106  p += readbytes;
1107  }
1108 
1109  return true;
1110 }
WALOpenSegment wre_seg
Definition: xlogreader.h:264
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)
static char * buf
Definition: pg_test_fsync.c:67
uint64 XLogSegNo
Definition: xlogdefs.h:41
XLogSegNo ws_segno
Definition: xlogreader.h:38
unsigned int uint32
Definition: c.h:359
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1344
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
size_t Size
Definition: c.h:467
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1320
TimeLineID ws_tli
Definition: xlogreader.h:39
#define close(a)
Definition: win32.h:12
static XLogRecPtr startptr
Definition: basebackup.c:116
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ XLogReaderAllocate()

XLogReaderState* XLogReaderAllocate ( int  wal_segment_size,
const char *  waldir,
XLogPageReadCB  pagereadfunc,
void *  private_data 
)

Definition at line 73 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::read_page, XLogReaderState::readBuf, XLogReaderState::seg, XLogReaderState::segcxt, and WALOpenSegmentInit().

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

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

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)

Definition at line 135 of file xlogreader.c.

References XLogReaderState::blocks, DecodedBkpBlock::data, XLogReaderState::errormsg_buf, XLogReaderState::main_data, pfree(), XLogReaderState::readBuf, XLogReaderState::readRecordBuf, and XLR_MAX_BLOCK_ID.

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

136 {
137  int block_id;
138 
139  for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
140  {
141  if (state->blocks[block_id].data)
142  pfree(state->blocks[block_id].data);
143  }
144  if (state->main_data)
145  pfree(state->main_data);
146 
147  pfree(state->errormsg_buf);
148  if (state->readRecordBuf)
149  pfree(state->readRecordBuf);
150  pfree(state->readBuf);
151  pfree(state);
152 }
char * readRecordBuf
Definition: xlogreader.h:204
void pfree(void *pointer)
Definition: mcxt.c:1056
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:221
char * errormsg_buf
Definition: xlogreader.h:208
char * main_data
Definition: xlogreader.h:144
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:151

◆ XLogReaderValidatePageHeader()

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

Definition at line 757 of file xlogreader.c.

References Assert, XLogReaderState::EndRecPtr, 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, 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().

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

◆ XLogReadRecord()

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

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

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

◆ XLogRecGetBlockData()

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

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

1485 {
1486  DecodedBkpBlock *bkpb;
1487 
1488  if (!record->blocks[block_id].in_use)
1489  return NULL;
1490 
1491  bkpb = &record->blocks[block_id];
1492 
1493  if (!bkpb->has_data)
1494  {
1495  if (len)
1496  *len = 0;
1497  return NULL;
1498  }
1499  else
1500  {
1501  if (len)
1502  *len = bkpb->data_len;
1503  return bkpb->data;
1504  }
1505 }
uint16 data_len
Definition: xlogreader.h:83
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:151

◆ XLogRecGetBlockTag()

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

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

1462 {
1463  DecodedBkpBlock *bkpb;
1464 
1465  if (!record->blocks[block_id].in_use)
1466  return false;
1467 
1468  bkpb = &record->blocks[block_id];
1469  if (rnode)
1470  *rnode = bkpb->rnode;
1471  if (forknum)
1472  *forknum = bkpb->forknum;
1473  if (blknum)
1474  *blknum = bkpb->blkno;
1475  return true;
1476 }
BlockNumber blkno
Definition: xlogreader.h:66
ForkNumber forknum
Definition: xlogreader.h:65
RelFileNode rnode
Definition: xlogreader.h:64
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:151

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 1566 of file xlogreader.c.

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

1567 {
1568  TransactionId xid,
1569  next_xid;
1570  uint32 epoch;
1571 
1572  /*
1573  * This function is only safe during replay, because it depends on the
1574  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1575  */
1577 
1578  xid = XLogRecGetXid(record);
1581 
1582  /*
1583  * If xid is numerically greater than next_xid, it has to be from the
1584  * last epoch.
1585  */
1586  if (unlikely(xid > next_xid))
1587  --epoch;
1588 
1589  return FullTransactionIdFromEpochAndXid(epoch, xid);
1590 }
#define AmStartupProcess()
Definition: miscadmin.h:410
uint32 TransactionId
Definition: c.h:514
FullTransactionId nextFullXid
Definition: transam.h:164
#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:359
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:281
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:739
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:65
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
#define unlikely(x)
Definition: c.h:208