PostgreSQL Source Code  git master
xlogreader.c File Reference
Include dependency graph for xlogreader.c:

Go to the source code of this file.

Macros

#define MAX_ERRORMSG_LEN   1000
 
#define COPY_HEADER_FIELD(_dst, _size)
 

Functions

static bool allocate_recordbuf (XLogReaderState *state, uint32 reclength)
 
static bool ValidXLogRecordHeader (XLogReaderState *state, XLogRecPtr RecPtr, XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess)
 
static bool ValidXLogRecord (XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
 
static int ReadPageInternal (XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
 
static void report_invalid_record (XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
 
static void static void ResetDecoder (XLogReaderState *state)
 
XLogReaderStateXLogReaderAllocate (int wal_segment_size, XLogPageReadCB pagereadfunc, void *private_data)
 
void XLogReaderFree (XLogReaderState *state)
 
XLogRecordXLogReadRecord (XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
 
void XLogReaderInvalReadState (XLogReaderState *state)
 
bool XLogReaderValidatePageHeader (XLogReaderState *state, XLogRecPtr recptr, char *phdr)
 
bool DecodeXLogRecord (XLogReaderState *state, XLogRecord *record, char **errormsg)
 
bool XLogRecGetBlockTag (XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
 
char * XLogRecGetBlockData (XLogReaderState *record, uint8 block_id, Size *len)
 
bool RestoreBlockImage (XLogReaderState *record, uint8 block_id, char *page)
 

Macro Definition Documentation

◆ COPY_HEADER_FIELD

#define COPY_HEADER_FIELD (   _dst,
  _size 
)
Value:
do { \
if (remaining < _size) \
goto shortdata_err; \
memcpy(_dst, ptr, _size); \
ptr += _size; \
remaining -= _size; \
} while(0)
int remaining
Definition: informix.c:692

Referenced by DecodeXLogRecord().

◆ MAX_ERRORMSG_LEN

#define MAX_ERRORMSG_LEN   1000

Definition at line 41 of file xlogreader.c.

Referenced by report_invalid_record(), and XLogReaderAllocate().

Function Documentation

◆ allocate_recordbuf()

static bool allocate_recordbuf ( XLogReaderState state,
uint32  reclength 
)
static

Definition at line 156 of file xlogreader.c.

References Max, MCXT_ALLOC_NO_OOM, palloc_extended(), pfree(), XLogReaderState::readRecordBuf, and XLogReaderState::readRecordBufSize.

Referenced by XLogReaderAllocate(), and XLogReadRecord().

157 {
158  uint32 newSize = reclength;
159 
160  newSize += XLOG_BLCKSZ - (newSize % XLOG_BLCKSZ);
161  newSize = Max(newSize, 5 * Max(BLCKSZ, XLOG_BLCKSZ));
162 
163  if (state->readRecordBuf)
164  pfree(state->readRecordBuf);
165  state->readRecordBuf =
166  (char *) palloc_extended(newSize, MCXT_ALLOC_NO_OOM);
167  if (state->readRecordBuf == NULL)
168  {
169  state->readRecordBufSize = 0;
170  return false;
171  }
172  state->readRecordBufSize = newSize;
173  return true;
174 }
char * readRecordBuf
Definition: xlogreader.h:189
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:988
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
void pfree(void *pointer)
Definition: mcxt.c:1031
unsigned int uint32
Definition: c.h:325
uint32 readRecordBufSize
Definition: xlogreader.h:190
#define Max(x, y)
Definition: c.h:851

◆ DecodeXLogRecord()

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

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

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

◆ ReadPageInternal()

static int ReadPageInternal ( XLogReaderState state,
XLogRecPtr  pageptr,
int  reqLen 
)
static

Definition at line 503 of file xlogreader.c.

References Assert, XLogReaderState::currRecPtr, Max, XLogReaderState::read_page, XLogReaderState::readBuf, XLogReaderState::readLen, readLen, XLogReaderState::readOff, XLogReaderState::readPageTLI, XLogReaderState::readSegNo, SizeOfXLogShortPHD, XLogReaderState::wal_segment_size, XLByteToSeg, XLogPageHeaderSize, XLogReaderInvalReadState(), XLogReaderValidatePageHeader(), and XLogSegmentOffset.

Referenced by XLogReaderValidatePageHeader(), and XLogReadRecord().

504 {
505  int readLen;
506  uint32 targetPageOff;
507  XLogSegNo targetSegNo;
508  XLogPageHeader hdr;
509 
510  Assert((pageptr % XLOG_BLCKSZ) == 0);
511 
512  XLByteToSeg(pageptr, targetSegNo, state->wal_segment_size);
513  targetPageOff = XLogSegmentOffset(pageptr, state->wal_segment_size);
514 
515  /* check whether we have all the requested data already */
516  if (targetSegNo == state->readSegNo && targetPageOff == state->readOff &&
517  reqLen < state->readLen)
518  return state->readLen;
519 
520  /*
521  * Data is not in our buffer.
522  *
523  * Every time we actually read the page, even if we looked at parts of it
524  * before, we need to do verification as the read_page callback might now
525  * be rereading data from a different source.
526  *
527  * Whenever switching to a new WAL segment, we read the first page of the
528  * file and validate its header, even if that's not where the target
529  * record is. This is so that we can check the additional identification
530  * info that is present in the first page's "long" header.
531  */
532  if (targetSegNo != state->readSegNo && targetPageOff != 0)
533  {
534  XLogRecPtr targetSegmentPtr = pageptr - targetPageOff;
535 
536  readLen = state->read_page(state, targetSegmentPtr, XLOG_BLCKSZ,
537  state->currRecPtr,
538  state->readBuf, &state->readPageTLI);
539  if (readLen < 0)
540  goto err;
541 
542  /* we can be sure to have enough WAL available, we scrolled back */
543  Assert(readLen == XLOG_BLCKSZ);
544 
545  if (!XLogReaderValidatePageHeader(state, targetSegmentPtr,
546  state->readBuf))
547  goto err;
548  }
549 
550  /*
551  * First, read the requested data length, but at least a short page header
552  * so that we can validate it.
553  */
554  readLen = state->read_page(state, pageptr, Max(reqLen, SizeOfXLogShortPHD),
555  state->currRecPtr,
556  state->readBuf, &state->readPageTLI);
557  if (readLen < 0)
558  goto err;
559 
560  Assert(readLen <= XLOG_BLCKSZ);
561 
562  /* Do we have enough data to check the header length? */
563  if (readLen <= SizeOfXLogShortPHD)
564  goto err;
565 
566  Assert(readLen >= reqLen);
567 
568  hdr = (XLogPageHeader) state->readBuf;
569 
570  /* still not enough */
571  if (readLen < XLogPageHeaderSize(hdr))
572  {
573  readLen = state->read_page(state, pageptr, XLogPageHeaderSize(hdr),
574  state->currRecPtr,
575  state->readBuf, &state->readPageTLI);
576  if (readLen < 0)
577  goto err;
578  }
579 
580  /*
581  * Now that we know we have the full header, validate it.
582  */
583  if (!XLogReaderValidatePageHeader(state, pageptr, (char *) hdr))
584  goto err;
585 
586  /* update read state information */
587  state->readSegNo = targetSegNo;
588  state->readOff = targetPageOff;
589  state->readLen = readLen;
590 
591  return readLen;
592 
593 err:
595  return -1;
596 }
XLogPageReadCB read_page
Definition: xlogreader.h:102
TimeLineID readPageTLI
Definition: xlogreader.h:158
#define XLogPageHeaderSize(hdr)
Definition: xlog_internal.h:85
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
bool XLogReaderValidatePageHeader(XLogReaderState *state, XLogRecPtr recptr, char *phdr)
Definition: xlogreader.c:714
uint64 XLogSegNo
Definition: xlogdefs.h:34
unsigned int uint32
Definition: c.h:325
int wal_segment_size
Definition: xlogreader.h:79
void XLogReaderInvalReadState(XLogReaderState *state)
Definition: xlogreader.c:602
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
XLogRecPtr currRecPtr
Definition: xlogreader.h:168
#define Max(x, y)
Definition: c.h:851
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:699
XLogSegNo readSegNo
Definition: xlogreader.h:156
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
static uint32 readLen
Definition: xlog.c:789
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ report_invalid_record()

static void report_invalid_record ( XLogReaderState state,
const char *  fmt,
  ... 
)
static

Definition at line 48 of file xlogreader.c.

References _, generate_unaccent_rules::args, XLogReaderState::errormsg_buf, MAX_ERRORMSG_LEN, and vsnprintf().

Referenced by DecodeXLogRecord(), RestoreBlockImage(), ValidXLogRecord(), ValidXLogRecordHeader(), XLogReaderValidatePageHeader(), and XLogReadRecord().

49 {
50  va_list args;
51 
52  fmt = _(fmt);
53 
54  va_start(args, fmt);
55  vsnprintf(state->errormsg_buf, MAX_ERRORMSG_LEN, fmt, args);
56  va_end(args);
57 }
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:41
int int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
char * errormsg_buf
Definition: xlogreader.h:193
#define _(x)
Definition: elog.c:84

◆ ResetDecoder()

static void ResetDecoder ( XLogReaderState state)
static

Definition at line 994 of file xlogreader.c.

References DecodedBkpBlock::apply_image, XLogReaderState::blocks, XLogReaderState::decoded_record, DecodedBkpBlock::has_data, DecodedBkpBlock::has_image, DecodedBkpBlock::in_use, XLogReaderState::main_data_len, and XLogReaderState::max_block_id.

Referenced by DecodeXLogRecord(), and XLogReadRecord().

995 {
996  int block_id;
997 
998  state->decoded_record = NULL;
999 
1000  state->main_data_len = 0;
1001 
1002  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1003  {
1004  state->blocks[block_id].in_use = false;
1005  state->blocks[block_id].has_image = false;
1006  state->blocks[block_id].has_data = false;
1007  state->blocks[block_id].apply_image = false;
1008  }
1009  state->max_block_id = -1;
1010 }
uint32 main_data_len
Definition: xlogreader.h:133
XLogRecord * decoded_record
Definition: xlogreader.h:130
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139

◆ RestoreBlockImage()

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

Definition at line 1388 of file xlogreader.c.

References DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, DecodedBkpBlock::bkp_image, BKPIMAGE_IS_COMPRESSED, XLogReaderState::blocks, 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().

1389 {
1390  DecodedBkpBlock *bkpb;
1391  char *ptr;
1392  char tmp[BLCKSZ];
1393 
1394  if (!record->blocks[block_id].in_use)
1395  return false;
1396  if (!record->blocks[block_id].has_image)
1397  return false;
1398 
1399  bkpb = &record->blocks[block_id];
1400  ptr = bkpb->bkp_image;
1401 
1402  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1403  {
1404  /* If a backup block image is compressed, decompress it */
1405  if (pglz_decompress(ptr, bkpb->bimg_len, tmp,
1406  BLCKSZ - bkpb->hole_length) < 0)
1407  {
1408  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1409  (uint32) (record->ReadRecPtr >> 32),
1410  (uint32) record->ReadRecPtr,
1411  block_id);
1412  return false;
1413  }
1414  ptr = tmp;
1415  }
1416 
1417  /* generate page, taking into account hole if necessary */
1418  if (bkpb->hole_length == 0)
1419  {
1420  memcpy(page, ptr, BLCKSZ);
1421  }
1422  else
1423  {
1424  memcpy(page, ptr, bkpb->hole_offset);
1425  /* must zero-fill the hole */
1426  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1427  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1428  ptr + bkpb->hole_offset,
1429  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1430  }
1431 
1432  return true;
1433 }
uint16 hole_offset
Definition: xlogreader.h:57
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:48
#define MemSet(start, val, len)
Definition: c.h:908
uint16 bimg_len
Definition: xlogreader.h:59
uint16 hole_length
Definition: xlogreader.h:58
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:119
unsigned int uint32
Definition: c.h:325
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:148
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize)
char * bkp_image
Definition: xlogreader.h:56
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139

◆ ValidXLogRecord()

static bool ValidXLogRecord ( XLogReaderState state,
XLogRecord record,
XLogRecPtr  recptr 
)
static

Definition at line 685 of file xlogreader.c.

References COMP_CRC32C, EQ_CRC32C, FIN_CRC32C, INIT_CRC32C, offsetof, report_invalid_record(), and SizeOfXLogRecord.

Referenced by XLogReadRecord().

686 {
687  pg_crc32c crc;
688 
689  /* Calculate the CRC */
690  INIT_CRC32C(crc);
691  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
692  /* include the record header last */
693  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
694  FIN_CRC32C(crc);
695 
696  if (!EQ_CRC32C(record->xl_crc, crc))
697  {
698  report_invalid_record(state,
699  "incorrect resource manager data checksum in record at %X/%X",
700  (uint32) (recptr >> 32), (uint32) recptr);
701  return false;
702  }
703 
704  return true;
705 }
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
uint32 pg_crc32c
Definition: pg_crc32c.h:38
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:48
unsigned int uint32
Definition: c.h:325
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:94
#define offsetof(type, field)
Definition: c.h:622

◆ ValidXLogRecordHeader()

static bool ValidXLogRecordHeader ( XLogReaderState state,
XLogRecPtr  RecPtr,
XLogRecPtr  PrevRecPtr,
XLogRecord record,
bool  randAccess 
)
static

Definition at line 616 of file xlogreader.c.

References report_invalid_record(), RM_MAX_ID, SizeOfXLogRecord, XLogRecord::xl_prev, XLogRecord::xl_rmid, and XLogRecord::xl_tot_len.

Referenced by XLogReadRecord().

619 {
620  if (record->xl_tot_len < SizeOfXLogRecord)
621  {
622  report_invalid_record(state,
623  "invalid record length at %X/%X: wanted %u, got %u",
624  (uint32) (RecPtr >> 32), (uint32) RecPtr,
625  (uint32) SizeOfXLogRecord, record->xl_tot_len);
626  return false;
627  }
628  if (record->xl_rmid > RM_MAX_ID)
629  {
630  report_invalid_record(state,
631  "invalid resource manager ID %u at %X/%X",
632  record->xl_rmid, (uint32) (RecPtr >> 32),
633  (uint32) RecPtr);
634  return false;
635  }
636  if (randAccess)
637  {
638  /*
639  * We can't exactly verify the prev-link, but surely it should be less
640  * than the record's own address.
641  */
642  if (!(record->xl_prev < RecPtr))
643  {
644  report_invalid_record(state,
645  "record with incorrect prev-link %X/%X at %X/%X",
646  (uint32) (record->xl_prev >> 32),
647  (uint32) record->xl_prev,
648  (uint32) (RecPtr >> 32), (uint32) RecPtr);
649  return false;
650  }
651  }
652  else
653  {
654  /*
655  * Record's prev-link should exactly match our previous location. This
656  * check guards against torn WAL pages where a stale but valid-looking
657  * WAL record starts on a sector boundary.
658  */
659  if (record->xl_prev != PrevRecPtr)
660  {
661  report_invalid_record(state,
662  "record with incorrect prev-link %X/%X at %X/%X",
663  (uint32) (record->xl_prev >> 32),
664  (uint32) record->xl_prev,
665  (uint32) (RecPtr >> 32), (uint32) RecPtr);
666  return false;
667  }
668  }
669 
670  return true;
671 }
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:48
RmgrId xl_rmid
Definition: xlogrecord.h:47
uint32 xl_tot_len
Definition: xlogrecord.h:43
unsigned int uint32
Definition: c.h:325
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define RM_MAX_ID
Definition: rmgr.h:33

◆ XLogReaderAllocate()

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

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

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

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)

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

126 {
127  int block_id;
128 
129  for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
130  {
131  if (state->blocks[block_id].data)
132  pfree(state->blocks[block_id].data);
133  }
134  if (state->main_data)
135  pfree(state->main_data);
136 
137  pfree(state->errormsg_buf);
138  if (state->readRecordBuf)
139  pfree(state->readRecordBuf);
140  pfree(state->readBuf);
141  pfree(state);
142 }
char * readRecordBuf
Definition: xlogreader.h:189
void pfree(void *pointer)
Definition: mcxt.c:1031
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:222
char * errormsg_buf
Definition: xlogreader.h:193
char * main_data
Definition: xlogreader.h:132
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139

◆ XLogReaderInvalReadState()

void XLogReaderInvalReadState ( XLogReaderState state)

Definition at line 602 of file xlogreader.c.

References XLogReaderState::readLen, XLogReaderState::readOff, and XLogReaderState::readSegNo.

Referenced by ReadPageInternal(), XLogReaderValidatePageHeader(), and XLogReadRecord().

603 {
604  state->readSegNo = 0;
605  state->readOff = 0;
606  state->readLen = 0;
607 }
XLogSegNo readSegNo
Definition: xlogreader.h:156

◆ XLogReaderValidatePageHeader()

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

Definition at line 714 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(), snprintf(), XLogReaderState::system_identifier, UINT64_FORMAT, 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().

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

◆ XLogReadRecord()

XLogRecord* XLogReadRecord ( XLogReaderState state,
XLogRecPtr  RecPtr,
char **  errormsg 
)

Definition at line 193 of file xlogreader.c.

References allocate_recordbuf(), Assert, buffer, 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().

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

◆ XLogRecGetBlockData()

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

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

1360 {
1361  DecodedBkpBlock *bkpb;
1362 
1363  if (!record->blocks[block_id].in_use)
1364  return NULL;
1365 
1366  bkpb = &record->blocks[block_id];
1367 
1368  if (!bkpb->has_data)
1369  {
1370  if (len)
1371  *len = 0;
1372  return NULL;
1373  }
1374  else
1375  {
1376  if (len)
1377  *len = bkpb->data_len;
1378  return bkpb->data;
1379  }
1380 }
uint16 data_len
Definition: xlogreader.h:65
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139

◆ XLogRecGetBlockTag()

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

Definition at line 1335 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_delete_get_latestRemovedXid(), btree_xlog_split(), btree_xlog_vacuum(), checkXLogConsistency(), DecodeDelete(), DecodeInsert(), DecodeMultiInsert(), DecodeSpecConfirm(), DecodeUpdate(), extractPageInfo(), gistRedoPageSplitRecord(), hash_xlog_add_ovfl_page(), hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), hash_xlog_vacuum_get_latestRemovedXid(), 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().

1337 {
1338  DecodedBkpBlock *bkpb;
1339 
1340  if (!record->blocks[block_id].in_use)
1341  return false;
1342 
1343  bkpb = &record->blocks[block_id];
1344  if (rnode)
1345  *rnode = bkpb->rnode;
1346  if (forknum)
1347  *forknum = bkpb->forknum;
1348  if (blknum)
1349  *blknum = bkpb->blkno;
1350  return true;
1351 }
BlockNumber blkno
Definition: xlogreader.h:48
ForkNumber forknum
Definition: xlogreader.h:47
RelFileNode rnode
Definition: xlogreader.h:46
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139