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
 

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)
 

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 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 246 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 241 of file xlogreader.h.

Referenced by xlog_redo(), and XLogDumpDisplayRecord().

◆ XLogRecGetRmid

◆ XLogRecGetTotalLen

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

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

◆ WALSegmentContext

◆ XLogPageReadCB

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

Definition at line 53 of file xlogreader.h.

◆ XLogReaderState

Definition at line 50 of file xlogreader.h.

Function Documentation

◆ DecodeXLogRecord()

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

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

1051 {
1052  /*
1053  * read next _size bytes from record buffer, but check for overrun first.
1054  */
1055 #define COPY_HEADER_FIELD(_dst, _size) \
1056  do { \
1057  if (remaining < _size) \
1058  goto shortdata_err; \
1059  memcpy(_dst, ptr, _size); \
1060  ptr += _size; \
1061  remaining -= _size; \
1062  } while(0)
1063 
1064  char *ptr;
1065  uint32 remaining;
1066  uint32 datatotal;
1067  RelFileNode *rnode = NULL;
1068  uint8 block_id;
1069 
1070  ResetDecoder(state);
1071 
1072  state->decoded_record = record;
1074 
1075  ptr = (char *) record;
1076  ptr += SizeOfXLogRecord;
1077  remaining = record->xl_tot_len - SizeOfXLogRecord;
1078 
1079  /* Decode the headers */
1080  datatotal = 0;
1081  while (remaining > datatotal)
1082  {
1083  COPY_HEADER_FIELD(&block_id, sizeof(uint8));
1084 
1085  if (block_id == XLR_BLOCK_ID_DATA_SHORT)
1086  {
1087  /* XLogRecordDataHeaderShort */
1088  uint8 main_data_len;
1089 
1090  COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
1091 
1092  state->main_data_len = main_data_len;
1093  datatotal += main_data_len;
1094  break; /* by convention, the main data fragment is
1095  * always last */
1096  }
1097  else if (block_id == XLR_BLOCK_ID_DATA_LONG)
1098  {
1099  /* XLogRecordDataHeaderLong */
1100  uint32 main_data_len;
1101 
1102  COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
1103  state->main_data_len = main_data_len;
1104  datatotal += main_data_len;
1105  break; /* by convention, the main data fragment is
1106  * always last */
1107  }
1108  else if (block_id == XLR_BLOCK_ID_ORIGIN)
1109  {
1110  COPY_HEADER_FIELD(&state->record_origin, sizeof(RepOriginId));
1111  }
1112  else if (block_id <= XLR_MAX_BLOCK_ID)
1113  {
1114  /* XLogRecordBlockHeader */
1115  DecodedBkpBlock *blk;
1116  uint8 fork_flags;
1117 
1118  if (block_id <= state->max_block_id)
1119  {
1120  report_invalid_record(state,
1121  "out-of-order block_id %u at %X/%X",
1122  block_id,
1123  (uint32) (state->ReadRecPtr >> 32),
1124  (uint32) state->ReadRecPtr);
1125  goto err;
1126  }
1127  state->max_block_id = block_id;
1128 
1129  blk = &state->blocks[block_id];
1130  blk->in_use = true;
1131  blk->apply_image = false;
1132 
1133  COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
1134  blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
1135  blk->flags = fork_flags;
1136  blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
1137  blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
1138 
1139  COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
1140  /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
1141  if (blk->has_data && blk->data_len == 0)
1142  {
1143  report_invalid_record(state,
1144  "BKPBLOCK_HAS_DATA set, but no data included at %X/%X",
1145  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1146  goto err;
1147  }
1148  if (!blk->has_data && blk->data_len != 0)
1149  {
1150  report_invalid_record(state,
1151  "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%X",
1152  (unsigned int) blk->data_len,
1153  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1154  goto err;
1155  }
1156  datatotal += blk->data_len;
1157 
1158  if (blk->has_image)
1159  {
1160  COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
1161  COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
1162  COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
1163 
1164  blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
1165 
1166  if (blk->bimg_info & BKPIMAGE_IS_COMPRESSED)
1167  {
1168  if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
1169  COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
1170  else
1171  blk->hole_length = 0;
1172  }
1173  else
1174  blk->hole_length = BLCKSZ - blk->bimg_len;
1175  datatotal += blk->bimg_len;
1176 
1177  /*
1178  * cross-check that hole_offset > 0, hole_length > 0 and
1179  * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
1180  */
1181  if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1182  (blk->hole_offset == 0 ||
1183  blk->hole_length == 0 ||
1184  blk->bimg_len == BLCKSZ))
1185  {
1186  report_invalid_record(state,
1187  "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%X",
1188  (unsigned int) blk->hole_offset,
1189  (unsigned int) blk->hole_length,
1190  (unsigned int) blk->bimg_len,
1191  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1192  goto err;
1193  }
1194 
1195  /*
1196  * cross-check that hole_offset == 0 and hole_length == 0 if
1197  * the HAS_HOLE flag is not set.
1198  */
1199  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1200  (blk->hole_offset != 0 || blk->hole_length != 0))
1201  {
1202  report_invalid_record(state,
1203  "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%X",
1204  (unsigned int) blk->hole_offset,
1205  (unsigned int) blk->hole_length,
1206  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1207  goto err;
1208  }
1209 
1210  /*
1211  * cross-check that bimg_len < BLCKSZ if the IS_COMPRESSED
1212  * flag is set.
1213  */
1214  if ((blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1215  blk->bimg_len == BLCKSZ)
1216  {
1217  report_invalid_record(state,
1218  "BKPIMAGE_IS_COMPRESSED set, but block image length %u at %X/%X",
1219  (unsigned int) blk->bimg_len,
1220  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1221  goto err;
1222  }
1223 
1224  /*
1225  * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE nor
1226  * IS_COMPRESSED flag is set.
1227  */
1228  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1229  !(blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1230  blk->bimg_len != BLCKSZ)
1231  {
1232  report_invalid_record(state,
1233  "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_IS_COMPRESSED set, but block image length is %u at %X/%X",
1234  (unsigned int) blk->data_len,
1235  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1236  goto err;
1237  }
1238  }
1239  if (!(fork_flags & BKPBLOCK_SAME_REL))
1240  {
1241  COPY_HEADER_FIELD(&blk->rnode, sizeof(RelFileNode));
1242  rnode = &blk->rnode;
1243  }
1244  else
1245  {
1246  if (rnode == NULL)
1247  {
1248  report_invalid_record(state,
1249  "BKPBLOCK_SAME_REL set but no previous rel at %X/%X",
1250  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1251  goto err;
1252  }
1253 
1254  blk->rnode = *rnode;
1255  }
1256  COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
1257  }
1258  else
1259  {
1260  report_invalid_record(state,
1261  "invalid block_id %u at %X/%X",
1262  block_id,
1263  (uint32) (state->ReadRecPtr >> 32),
1264  (uint32) state->ReadRecPtr);
1265  goto err;
1266  }
1267  }
1268 
1269  if (remaining != datatotal)
1270  goto shortdata_err;
1271 
1272  /*
1273  * Ok, we've parsed the fragment headers, and verified that the total
1274  * length of the payload in the fragments is equal to the amount of data
1275  * left. Copy the data of each fragment to a separate buffer.
1276  *
1277  * We could just set up pointers into readRecordBuf, but we want to align
1278  * the data for the convenience of the callers. Backup images are not
1279  * copied, however; they don't need alignment.
1280  */
1281 
1282  /* block data first */
1283  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1284  {
1285  DecodedBkpBlock *blk = &state->blocks[block_id];
1286 
1287  if (!blk->in_use)
1288  continue;
1289 
1290  Assert(blk->has_image || !blk->apply_image);
1291 
1292  if (blk->has_image)
1293  {
1294  blk->bkp_image = ptr;
1295  ptr += blk->bimg_len;
1296  }
1297  if (blk->has_data)
1298  {
1299  if (!blk->data || blk->data_len > blk->data_bufsz)
1300  {
1301  if (blk->data)
1302  pfree(blk->data);
1303 
1304  /*
1305  * Force the initial request to be BLCKSZ so that we don't
1306  * waste time with lots of trips through this stanza as a
1307  * result of WAL compression.
1308  */
1309  blk->data_bufsz = MAXALIGN(Max(blk->data_len, BLCKSZ));
1310  blk->data = palloc(blk->data_bufsz);
1311  }
1312  memcpy(blk->data, ptr, blk->data_len);
1313  ptr += blk->data_len;
1314  }
1315  }
1316 
1317  /* and finally, the main data */
1318  if (state->main_data_len > 0)
1319  {
1320  if (!state->main_data || state->main_data_len > state->main_data_bufsz)
1321  {
1322  if (state->main_data)
1323  pfree(state->main_data);
1324 
1325  /*
1326  * main_data_bufsz must be MAXALIGN'ed. In many xlog record
1327  * types, we omit trailing struct padding on-disk to save a few
1328  * bytes; but compilers may generate accesses to the xlog struct
1329  * that assume that padding bytes are present. If the palloc
1330  * request is not large enough to include such padding bytes then
1331  * we'll get valgrind complaints due to otherwise-harmless fetches
1332  * of the padding bytes.
1333  *
1334  * In addition, force the initial request to be reasonably large
1335  * so that we don't waste time with lots of trips through this
1336  * stanza. BLCKSZ / 2 seems like a good compromise choice.
1337  */
1338  state->main_data_bufsz = MAXALIGN(Max(state->main_data_len,
1339  BLCKSZ / 2));
1340  state->main_data = palloc(state->main_data_bufsz);
1341  }
1342  memcpy(state->main_data, ptr, state->main_data_len);
1343  ptr += state->main_data_len;
1344  }
1345 
1346  return true;
1347 
1348 shortdata_err:
1349  report_invalid_record(state,
1350  "record with invalid length at %X/%X",
1351  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1352 err:
1353  *errormsg = state->errormsg_buf;
1354 
1355  return false;
1356 }
int remaining
Definition: informix.c:687
BlockNumber blkno
Definition: xlogreader.h:67
#define BKPIMAGE_HAS_HOLE
Definition: xlogrecord.h:146
#define XLR_BLOCK_ID_DATA_LONG
Definition: xlogrecord.h:224
uint16 hole_offset
Definition: xlogreader.h:76
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:53
uint32 BlockNumber
Definition: block.h:31
uint16 bimg_len
Definition: xlogreader.h:78
unsigned short uint16
Definition: c.h:358
void pfree(void *pointer)
Definition: mcxt.c:1056
uint16 hole_length
Definition: xlogreader.h:77
uint32 xl_tot_len
Definition: xlogrecord.h:43
uint32 main_data_len
Definition: xlogreader.h:146
#define BKPIMAGE_APPLY
Definition: xlogrecord.h:148
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:132
XLogRecord * decoded_record
Definition: xlogreader.h:143
#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:66
uint16 data_len
Definition: xlogreader.h:84
#define Max(x, y)
Definition: c.h:899
#define Assert(condition)
Definition: c.h:733
uint16 data_bufsz
Definition: xlogreader.h:85
#define XLR_BLOCK_ID_ORIGIN
Definition: xlogrecord.h:225
#define MAXALIGN(LEN)
Definition: c.h:686
#define InvalidRepOriginId
Definition: origin.h:33
char * bkp_image
Definition: xlogreader.h:75
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:223
uint32 main_data_bufsz
Definition: xlogreader.h:147
#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:145
RelFileNode rnode
Definition: xlogreader.h:65
RepOriginId record_origin
Definition: xlogreader.h:149
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1025
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:152
#define BKPBLOCK_HAS_DATA
Definition: xlogrecord.h:181

◆ RestoreBlockImage()

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

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

1420 {
1421  DecodedBkpBlock *bkpb;
1422  char *ptr;
1423  PGAlignedBlock tmp;
1424 
1425  if (!record->blocks[block_id].in_use)
1426  return false;
1427  if (!record->blocks[block_id].has_image)
1428  return false;
1429 
1430  bkpb = &record->blocks[block_id];
1431  ptr = bkpb->bkp_image;
1432 
1433  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1434  {
1435  /* If a backup block image is compressed, decompress it */
1436  if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
1437  BLCKSZ - bkpb->hole_length, true) < 0)
1438  {
1439  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1440  (uint32) (record->ReadRecPtr >> 32),
1441  (uint32) record->ReadRecPtr,
1442  block_id);
1443  return false;
1444  }
1445  ptr = tmp.data;
1446  }
1447 
1448  /* generate page, taking into account hole if necessary */
1449  if (bkpb->hole_length == 0)
1450  {
1451  memcpy(page, ptr, BLCKSZ);
1452  }
1453  else
1454  {
1455  memcpy(page, ptr, bkpb->hole_offset);
1456  /* must zero-fill the hole */
1457  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1458  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1459  ptr + bkpb->hole_offset,
1460  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1461  }
1462 
1463  return true;
1464 }
uint16 hole_offset
Definition: xlogreader.h:76
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:53
#define MemSet(start, val, len)
Definition: c.h:956
uint16 bimg_len
Definition: xlogreader.h:78
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
char data[BLCKSZ]
Definition: c.h:1085
uint16 hole_length
Definition: xlogreader.h:77
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:132
unsigned int uint32
Definition: c.h:359
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
char * bkp_image
Definition: xlogreader.h:75
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:152

◆ WALOpenSegmentInit()

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

Definition at line 206 of file xlogreader.c.

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

Referenced by XLogReaderAllocate().

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

◆ XLogReaderAllocate()

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

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

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

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)

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

133 {
134  int block_id;
135 
136  for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
137  {
138  if (state->blocks[block_id].data)
139  pfree(state->blocks[block_id].data);
140  }
141  if (state->main_data)
142  pfree(state->main_data);
143 
144  pfree(state->errormsg_buf);
145  if (state->readRecordBuf)
146  pfree(state->readRecordBuf);
147  pfree(state->readBuf);
148  pfree(state);
149 }
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:145
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:152

◆ XLogReaderValidatePageHeader()

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

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

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

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

◆ XLogRecGetBlockData()

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

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

1391 {
1392  DecodedBkpBlock *bkpb;
1393 
1394  if (!record->blocks[block_id].in_use)
1395  return NULL;
1396 
1397  bkpb = &record->blocks[block_id];
1398 
1399  if (!bkpb->has_data)
1400  {
1401  if (len)
1402  *len = 0;
1403  return NULL;
1404  }
1405  else
1406  {
1407  if (len)
1408  *len = bkpb->data_len;
1409  return bkpb->data;
1410  }
1411 }
uint16 data_len
Definition: xlogreader.h:84
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:152

◆ XLogRecGetBlockTag()

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

Definition at line 1366 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(), btree_xlog_vacuum(), 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().

1368 {
1369  DecodedBkpBlock *bkpb;
1370 
1371  if (!record->blocks[block_id].in_use)
1372  return false;
1373 
1374  bkpb = &record->blocks[block_id];
1375  if (rnode)
1376  *rnode = bkpb->rnode;
1377  if (forknum)
1378  *forknum = bkpb->forknum;
1379  if (blknum)
1380  *blknum = bkpb->blkno;
1381  return true;
1382 }
BlockNumber blkno
Definition: xlogreader.h:67
ForkNumber forknum
Definition: xlogreader.h:66
RelFileNode rnode
Definition: xlogreader.h:65
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:152

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 1472 of file xlogreader.c.

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

1473 {
1474  TransactionId xid,
1475  next_xid;
1476  uint32 epoch;
1477 
1478  /*
1479  * This function is only safe during replay, because it depends on the
1480  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1481  */
1483 
1484  xid = XLogRecGetXid(record);
1487 
1488  /*
1489  * If xid is numerically greater than next_xid, it has to be from the
1490  * last epoch.
1491  */
1492  if (unlikely(xid > next_xid))
1493  --epoch;
1494 
1495  return FullTransactionIdFromEpochAndXid(epoch, xid);
1496 }
#define AmStartupProcess()
Definition: miscadmin.h:412
uint32 TransactionId
Definition: c.h:508
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:244
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:733
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