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)
 
void XLogBeginRead (XLogReaderState *state, XLogRecPtr RecPtr)
 
struct XLogRecordXLogReadRecord (XLogReaderState *state, 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 288 of file xlogreader.h.

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

◆ XLogRecGetDataLen

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

◆ XLogRecGetInfo

◆ XLogRecGetOrigin

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

◆ XLogRecGetPrev

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

Definition at line 283 of file xlogreader.h.

Referenced by xlog_redo(), and XLogDumpDisplayRecord().

◆ XLogRecGetRmid

◆ XLogRecGetTotalLen

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

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

◆ XLogPageReadCB

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

Definition at line 54 of file xlogreader.h.

◆ XLogReaderState

Definition at line 51 of file xlogreader.h.

Function Documentation

◆ DecodeXLogRecord()

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

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

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

◆ RestoreBlockImage()

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

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

1538 {
1539  DecodedBkpBlock *bkpb;
1540  char *ptr;
1541  PGAlignedBlock tmp;
1542 
1543  if (!record->blocks[block_id].in_use)
1544  return false;
1545  if (!record->blocks[block_id].has_image)
1546  return false;
1547 
1548  bkpb = &record->blocks[block_id];
1549  ptr = bkpb->bkp_image;
1550 
1551  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1552  {
1553  /* If a backup block image is compressed, decompress it */
1554  if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
1555  BLCKSZ - bkpb->hole_length, true) < 0)
1556  {
1557  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1558  (uint32) (record->ReadRecPtr >> 32),
1559  (uint32) record->ReadRecPtr,
1560  block_id);
1561  return false;
1562  }
1563  ptr = tmp.data;
1564  }
1565 
1566  /* generate page, taking into account hole if necessary */
1567  if (bkpb->hole_length == 0)
1568  {
1569  memcpy(page, ptr, BLCKSZ);
1570  }
1571  else
1572  {
1573  memcpy(page, ptr, bkpb->hole_offset);
1574  /* must zero-fill the hole */
1575  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1576  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1577  ptr + bkpb->hole_offset,
1578  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1579  }
1580 
1581  return true;
1582 }
uint16 hole_offset
Definition: xlogreader.h:77
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:971
uint16 bimg_len
Definition: xlogreader.h:79
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
char data[BLCKSZ]
Definition: c.h:1104
uint16 hole_length
Definition: xlogreader.h:78
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:134
unsigned int uint32
Definition: c.h:367
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
char * bkp_image
Definition: xlogreader.h:76
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:154

◆ WALOpenSegmentInit()

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

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

214 {
215  seg->ws_file = -1;
216  seg->ws_segno = 0;
217  seg->ws_tli = 0;
218 
219  segcxt->ws_segsize = segsize;
220  if (waldir)
221  snprintf(segcxt->ws_dir, MAXPGPATH, "%s", waldir);
222 }
char ws_dir[MAXPGPATH]
Definition: xlogreader.h:47
#define MAXPGPATH
XLogSegNo ws_segno
Definition: xlogreader.h:40
TimeLineID ws_tli
Definition: xlogreader.h:41
#define snprintf
Definition: port.h:193

◆ WALRead()

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

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

1060 {
1061  char *p;
1062  XLogRecPtr recptr;
1063  Size nbytes;
1064 
1065  p = buf;
1066  recptr = startptr;
1067  nbytes = count;
1068 
1069  while (nbytes > 0)
1070  {
1071  uint32 startoff;
1072  int segbytes;
1073  int readbytes;
1074 
1075  startoff = XLogSegmentOffset(recptr, segcxt->ws_segsize);
1076 
1077  /*
1078  * If the data we want is not in a segment we have open, close what we
1079  * have (if anything) and open the next one, using the caller's
1080  * provided openSegment callback.
1081  */
1082  if (seg->ws_file < 0 ||
1083  !XLByteInSeg(recptr, seg->ws_segno, segcxt->ws_segsize) ||
1084  tli != seg->ws_tli)
1085  {
1086  XLogSegNo nextSegNo;
1087 
1088  if (seg->ws_file >= 0)
1089  close(seg->ws_file);
1090 
1091  XLByteToSeg(recptr, nextSegNo, segcxt->ws_segsize);
1092  seg->ws_file = openSegment(nextSegNo, segcxt, &tli);
1093 
1094  /* Update the current segment info. */
1095  seg->ws_tli = tli;
1096  seg->ws_segno = nextSegNo;
1097  }
1098 
1099  /* How many bytes are within this segment? */
1100  if (nbytes > (segcxt->ws_segsize - startoff))
1101  segbytes = segcxt->ws_segsize - startoff;
1102  else
1103  segbytes = nbytes;
1104 
1105 #ifndef FRONTEND
1107 #endif
1108 
1109  /* Reset errno first; eases reporting non-errno-affecting errors */
1110  errno = 0;
1111  readbytes = pg_pread(seg->ws_file, p, segbytes, (off_t) startoff);
1112 
1113 #ifndef FRONTEND
1115 #endif
1116 
1117  if (readbytes <= 0)
1118  {
1119  errinfo->wre_errno = errno;
1120  errinfo->wre_req = segbytes;
1121  errinfo->wre_read = readbytes;
1122  errinfo->wre_off = startoff;
1123  errinfo->wre_seg = *seg;
1124  return false;
1125  }
1126 
1127  /* Update state for read */
1128  recptr += readbytes;
1129  nbytes -= readbytes;
1130  p += readbytes;
1131  }
1132 
1133  return true;
1134 }
WALOpenSegment wre_seg
Definition: xlogreader.h:269
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:40
unsigned int uint32
Definition: c.h:367
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1386
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
size_t Size
Definition: c.h:466
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1362
TimeLineID ws_tli
Definition: xlogreader.h:41
#define close(a)
Definition: win32.h:12
static XLogRecPtr startptr
Definition: basebackup.c:168
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ XLogBeginRead()

void XLogBeginRead ( XLogReaderState state,
XLogRecPtr  RecPtr 
)

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

237 {
238  Assert(!XLogRecPtrIsInvalid(RecPtr));
239 
240  ResetDecoder(state);
241 
242  /* Begin at the passed-in record pointer. */
243  state->EndRecPtr = RecPtr;
244  state->ReadRecPtr = InvalidXLogRecPtr;
245 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr EndRecPtr
Definition: xlogreader.h:135
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:134
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define Assert(condition)
Definition: c.h:738
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1143

◆ 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:116
int wal_segment_size
Definition: xlog.c:116
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:212
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
void * private_data
Definition: xlogreader.h:127
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:49
WALOpenSegment seg
Definition: xlogreader.h:172
void pfree(void *pointer)
Definition: mcxt.c:1056
static void static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength)
Definition: xlogreader.c:169
Definition: regguts.h:298
#define MCXT_ALLOC_ZERO
Definition: fe_memutils.h:19
WALSegmentContext segcxt
Definition: xlogreader.h:171
char * errormsg_buf
Definition: xlogreader.h:211

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)

Definition at line 135 of file xlogreader.c.

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

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

136 {
137  int block_id;
138 
139  if (state->seg.ws_file != -1)
140  close(state->seg.ws_file);
141 
142  for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
143  {
144  if (state->blocks[block_id].data)
145  pfree(state->blocks[block_id].data);
146  }
147  if (state->main_data)
148  pfree(state->main_data);
149 
150  pfree(state->errormsg_buf);
151  if (state->readRecordBuf)
152  pfree(state->readRecordBuf);
153  pfree(state->readBuf);
154  pfree(state);
155 }
char * readRecordBuf
Definition: xlogreader.h:207
WALOpenSegment seg
Definition: xlogreader.h:172
void pfree(void *pointer)
Definition: mcxt.c:1056
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:221
char * errormsg_buf
Definition: xlogreader.h:211
#define close(a)
Definition: win32.h:12
char * main_data
Definition: xlogreader.h:147
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:154

◆ XLogReaderValidatePageHeader()

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

Definition at line 782 of file xlogreader.c.

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

Referenced by ReadPageInternal(), and XLogPageRead().

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

◆ XLogReadRecord()

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

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

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

◆ XLogRecGetBlockData()

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

Definition at line 1508 of file xlogreader.c.

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

Referenced by _bt_restore_meta(), brin_xlog_insert_update(), brin_xlog_samepage_update(), btree_desc(), btree_xlog_dedup(), btree_xlog_delete(), btree_xlog_insert(), btree_xlog_newroot(), btree_xlog_split(), btree_xlog_vacuum(), DecodeInsert(), DecodeMultiInsert(), DecodeUpdate(), generic_redo(), gin_desc(), ginRedoInsert(), ginRedoInsertListPage(), ginRedoUpdateMetapage(), ginRedoVacuumDataLeafPage(), gistRedoPageSplitRecord(), gistRedoPageUpdateRecord(), hash_xlog_add_ovfl_page(), hash_xlog_delete(), hash_xlog_insert(), hash_xlog_move_page_contents(), hash_xlog_split_allocate_page(), hash_xlog_squeeze_page(), heap_xlog_clean(), heap_xlog_freeze_page(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_multi_insert(), and heap_xlog_update().

1509 {
1510  DecodedBkpBlock *bkpb;
1511 
1512  if (!record->blocks[block_id].in_use)
1513  return NULL;
1514 
1515  bkpb = &record->blocks[block_id];
1516 
1517  if (!bkpb->has_data)
1518  {
1519  if (len)
1520  *len = 0;
1521  return NULL;
1522  }
1523  else
1524  {
1525  if (len)
1526  *len = bkpb->data_len;
1527  return bkpb->data;
1528  }
1529 }
uint16 data_len
Definition: xlogreader.h:85
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:154

◆ XLogRecGetBlockTag()

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

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

1486 {
1487  DecodedBkpBlock *bkpb;
1488 
1489  if (!record->blocks[block_id].in_use)
1490  return false;
1491 
1492  bkpb = &record->blocks[block_id];
1493  if (rnode)
1494  *rnode = bkpb->rnode;
1495  if (forknum)
1496  *forknum = bkpb->forknum;
1497  if (blknum)
1498  *blknum = bkpb->blkno;
1499  return true;
1500 }
BlockNumber blkno
Definition: xlogreader.h:68
ForkNumber forknum
Definition: xlogreader.h:67
RelFileNode rnode
Definition: xlogreader.h:66
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:154

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 1590 of file xlogreader.c.

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

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