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  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 XLogReaderState XLogReaderState
 
typedef int(* XLogPageReadCB) (XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf, TimeLineID *pageTLI)
 

Functions

XLogReaderStateXLogReaderAllocate (int wal_segment_size, XLogPageReadCB pagereadfunc, void *private_data)
 
void XLogReaderFree (XLogReaderState *state)
 
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 233 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 228 of file xlogreader.h.

Referenced by xlog_redo(), and XLogDumpDisplayRecord().

◆ XLogRecGetRmid

◆ XLogRecGetTotalLen

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

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

◆ XLogPageReadCB

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

Definition at line 37 of file xlogreader.h.

◆ XLogReaderState

Definition at line 34 of file xlogreader.h.

Function Documentation

◆ DecodeXLogRecord()

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

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

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

◆ RestoreBlockImage()

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

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

1403 {
1404  DecodedBkpBlock *bkpb;
1405  char *ptr;
1406  PGAlignedBlock tmp;
1407 
1408  if (!record->blocks[block_id].in_use)
1409  return false;
1410  if (!record->blocks[block_id].has_image)
1411  return false;
1412 
1413  bkpb = &record->blocks[block_id];
1414  ptr = bkpb->bkp_image;
1415 
1416  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1417  {
1418  /* If a backup block image is compressed, decompress it */
1419  if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
1420  BLCKSZ - bkpb->hole_length, true) < 0)
1421  {
1422  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1423  (uint32) (record->ReadRecPtr >> 32),
1424  (uint32) record->ReadRecPtr,
1425  block_id);
1426  return false;
1427  }
1428  ptr = tmp.data;
1429  }
1430 
1431  /* generate page, taking into account hole if necessary */
1432  if (bkpb->hole_length == 0)
1433  {
1434  memcpy(page, ptr, BLCKSZ);
1435  }
1436  else
1437  {
1438  memcpy(page, ptr, bkpb->hole_offset);
1439  /* must zero-fill the hole */
1440  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1441  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1442  ptr + bkpb->hole_offset,
1443  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1444  }
1445 
1446  return true;
1447 }
uint16 hole_offset
Definition: xlogreader.h:61
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:54
#define MemSet(start, val, len)
Definition: c.h:955
uint16 bimg_len
Definition: xlogreader.h:63
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
char data[BLCKSZ]
Definition: c.h:1060
uint16 hole_length
Definition: xlogreader.h:62
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:123
unsigned int uint32
Definition: c.h:358
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
char * bkp_image
Definition: xlogreader.h:60
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:143

◆ XLogReaderAllocate()

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

Definition at line 71 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::wal_segment_size, and wal_segment_size.

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

73 {
75 
76  state = (XLogReaderState *)
79  if (!state)
80  return NULL;
81 
82  state->max_block_id = -1;
83 
84  /*
85  * Permanently allocate readBuf. We do it this way, rather than just
86  * making a static array, for two reasons: (1) no need to waste the
87  * storage in most instantiations of the backend; (2) a static char array
88  * isn't guaranteed to have any particular alignment, whereas
89  * palloc_extended() will provide MAXALIGN'd storage.
90  */
91  state->readBuf = (char *) palloc_extended(XLOG_BLCKSZ,
93  if (!state->readBuf)
94  {
95  pfree(state);
96  return NULL;
97  }
98 
100  state->read_page = pagereadfunc;
101  /* system_identifier initialized to zeroes above */
102  state->private_data = private_data;
103  /* ReadRecPtr and EndRecPtr initialized to zeroes above */
104  /* readSegNo, readOff, readLen, readPageTLI initialized to zeroes above */
107  if (!state->errormsg_buf)
108  {
109  pfree(state->readBuf);
110  pfree(state);
111  return NULL;
112  }
113  state->errormsg_buf[0] = '\0';
114 
115  /*
116  * Allocate an initial readRecordBuf of minimal size, which can later be
117  * enlarged if necessary.
118  */
119  if (!allocate_recordbuf(state, 0))
120  {
121  pfree(state->errormsg_buf);
122  pfree(state->readBuf);
123  pfree(state);
124  return NULL;
125  }
126 
127  return state;
128 }
XLogPageReadCB read_page
Definition: xlogreader.h:106
int wal_segment_size
Definition: xlog.c:112
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:988
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
void * private_data
Definition: xlogreader.h:117
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:47
void pfree(void *pointer)
Definition: mcxt.c:1031
static void static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength)
Definition: xlogreader.c:162
int wal_segment_size
Definition: xlogreader.h:83
Definition: regguts.h:298
#define MCXT_ALLOC_ZERO
Definition: fe_memutils.h:19
char * errormsg_buf
Definition: xlogreader.h:200

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)

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

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

◆ XLogReaderValidatePageHeader()

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

Definition at line 738 of file xlogreader.c.

References Assert, XLogReaderState::EndRecPtr, header(), InvalidXLogRecPtr, XLogReaderState::latestPagePtr, XLogReaderState::latestPageTLI, MAXALIGN, MAXFNAMELEN, XLogReaderState::readBuf, readLen, ReadPageInternal(), XLogReaderState::readPageTLI, XLogReaderState::ReadRecPtr, ReadRecPtr, report_invalid_record(), XLogReaderState::system_identifier, XLogReaderState::wal_segment_size, 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().

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

◆ XLogReadRecord()

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

Definition at line 218 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(), SizeOfXLogRecord, SizeOfXLogShortPHD, ValidXLogRecord(), ValidXLogRecordHeader(), XLogReaderState::wal_segment_size, 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().

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

◆ XLogRecGetBlockData()

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

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

1374 {
1375  DecodedBkpBlock *bkpb;
1376 
1377  if (!record->blocks[block_id].in_use)
1378  return NULL;
1379 
1380  bkpb = &record->blocks[block_id];
1381 
1382  if (!bkpb->has_data)
1383  {
1384  if (len)
1385  *len = 0;
1386  return NULL;
1387  }
1388  else
1389  {
1390  if (len)
1391  *len = bkpb->data_len;
1392  return bkpb->data;
1393  }
1394 }
uint16 data_len
Definition: xlogreader.h:69
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:143

◆ XLogRecGetBlockTag()

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

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

1351 {
1352  DecodedBkpBlock *bkpb;
1353 
1354  if (!record->blocks[block_id].in_use)
1355  return false;
1356 
1357  bkpb = &record->blocks[block_id];
1358  if (rnode)
1359  *rnode = bkpb->rnode;
1360  if (forknum)
1361  *forknum = bkpb->forknum;
1362  if (blknum)
1363  *blknum = bkpb->blkno;
1364  return true;
1365 }
BlockNumber blkno
Definition: xlogreader.h:52
ForkNumber forknum
Definition: xlogreader.h:51
RelFileNode rnode
Definition: xlogreader.h:50
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:143

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 1455 of file xlogreader.c.

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

1456 {
1457  TransactionId xid,
1458  next_xid;
1459  uint32 epoch;
1460 
1461  /*
1462  * This function is only safe during replay, because it depends on the
1463  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1464  */
1466 
1467  xid = XLogRecGetXid(record);
1470 
1471  /*
1472  * If xid is numerically greater than next_xid, it has to be from the
1473  * last epoch.
1474  */
1475  if (unlikely(xid > next_xid))
1476  --epoch;
1477 
1478  return FullTransactionIdFromEpochAndXid(epoch, xid);
1479 }
#define AmStartupProcess()
Definition: miscadmin.h:412
uint32 TransactionId
Definition: c.h:507
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:358
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:231
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:732
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