PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 ValidXLogPageHeader (XLogReaderState *state, XLogRecPtr recptr, XLogPageHeader hdr)
 
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 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

#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().

#define MAX_ERRORMSG_LEN   1000

Definition at line 43 of file xlogreader.c.

Referenced by report_invalid_record(), and XLogReaderAllocate().

Function Documentation

static bool allocate_recordbuf ( XLogReaderState state,
uint32  reclength 
)
static

Definition at line 158 of file xlogreader.c.

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

Referenced by XLogReaderAllocate(), and XLogReadRecord().

159 {
160  uint32 newSize = reclength;
161 
162  newSize += XLOG_BLCKSZ - (newSize % XLOG_BLCKSZ);
163  newSize = Max(newSize, 5 * Max(BLCKSZ, XLOG_BLCKSZ));
164 
165  if (state->readRecordBuf)
166  pfree(state->readRecordBuf);
167  state->readRecordBuf =
168  (char *) palloc_extended(newSize, MCXT_ALLOC_NO_OOM);
169  if (state->readRecordBuf == NULL)
170  {
171  state->readRecordBufSize = 0;
172  return false;
173  }
174  state->readRecordBufSize = newSize;
175  return true;
176 }
char * readRecordBuf
Definition: xlogreader.h:189
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:908
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
void pfree(void *pointer)
Definition: mcxt.c:949
unsigned int uint32
Definition: c.h:258
uint32 readRecordBufSize
Definition: xlogreader.h:190
#define Max(x, y)
Definition: c.h:806
bool DecodeXLogRecord ( XLogReaderState state,
XLogRecord record,
char **  errormsg 
)

Definition at line 1014 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, XLogReaderState::max_block_id, 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().

1015 {
1016  /*
1017  * read next _size bytes from record buffer, but check for overrun first.
1018  */
1019 #define COPY_HEADER_FIELD(_dst, _size) \
1020  do { \
1021  if (remaining < _size) \
1022  goto shortdata_err; \
1023  memcpy(_dst, ptr, _size); \
1024  ptr += _size; \
1025  remaining -= _size; \
1026  } while(0)
1027 
1028  char *ptr;
1029  uint32 remaining;
1030  uint32 datatotal;
1031  RelFileNode *rnode = NULL;
1032  uint8 block_id;
1033 
1034  ResetDecoder(state);
1035 
1036  state->decoded_record = record;
1038 
1039  ptr = (char *) record;
1040  ptr += SizeOfXLogRecord;
1041  remaining = record->xl_tot_len - SizeOfXLogRecord;
1042 
1043  /* Decode the headers */
1044  datatotal = 0;
1045  while (remaining > datatotal)
1046  {
1047  COPY_HEADER_FIELD(&block_id, sizeof(uint8));
1048 
1049  if (block_id == XLR_BLOCK_ID_DATA_SHORT)
1050  {
1051  /* XLogRecordDataHeaderShort */
1052  uint8 main_data_len;
1053 
1054  COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
1055 
1056  state->main_data_len = main_data_len;
1057  datatotal += main_data_len;
1058  break; /* by convention, the main data fragment is
1059  * always last */
1060  }
1061  else if (block_id == XLR_BLOCK_ID_DATA_LONG)
1062  {
1063  /* XLogRecordDataHeaderLong */
1064  uint32 main_data_len;
1065 
1066  COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
1067  state->main_data_len = main_data_len;
1068  datatotal += main_data_len;
1069  break; /* by convention, the main data fragment is
1070  * always last */
1071  }
1072  else if (block_id == XLR_BLOCK_ID_ORIGIN)
1073  {
1074  COPY_HEADER_FIELD(&state->record_origin, sizeof(RepOriginId));
1075  }
1076  else if (block_id <= XLR_MAX_BLOCK_ID)
1077  {
1078  /* XLogRecordBlockHeader */
1079  DecodedBkpBlock *blk;
1080  uint8 fork_flags;
1081 
1082  if (block_id <= state->max_block_id)
1083  {
1084  report_invalid_record(state,
1085  "out-of-order block_id %u at %X/%X",
1086  block_id,
1087  (uint32) (state->ReadRecPtr >> 32),
1088  (uint32) state->ReadRecPtr);
1089  goto err;
1090  }
1091  state->max_block_id = block_id;
1092 
1093  blk = &state->blocks[block_id];
1094  blk->in_use = true;
1095  blk->apply_image = false;
1096 
1097  COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
1098  blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
1099  blk->flags = fork_flags;
1100  blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
1101  blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
1102 
1103  COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
1104  /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
1105  if (blk->has_data && blk->data_len == 0)
1106  {
1107  report_invalid_record(state,
1108  "BKPBLOCK_HAS_DATA set, but no data included at %X/%X",
1109  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1110  goto err;
1111  }
1112  if (!blk->has_data && blk->data_len != 0)
1113  {
1114  report_invalid_record(state,
1115  "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%X",
1116  (unsigned int) blk->data_len,
1117  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1118  goto err;
1119  }
1120  datatotal += blk->data_len;
1121 
1122  if (blk->has_image)
1123  {
1124  COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
1125  COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
1126  COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
1127 
1128  blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
1129 
1130  if (blk->bimg_info & BKPIMAGE_IS_COMPRESSED)
1131  {
1132  if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
1133  COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
1134  else
1135  blk->hole_length = 0;
1136  }
1137  else
1138  blk->hole_length = BLCKSZ - blk->bimg_len;
1139  datatotal += blk->bimg_len;
1140 
1141  /*
1142  * cross-check that hole_offset > 0, hole_length > 0 and
1143  * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
1144  */
1145  if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1146  (blk->hole_offset == 0 ||
1147  blk->hole_length == 0 ||
1148  blk->bimg_len == BLCKSZ))
1149  {
1150  report_invalid_record(state,
1151  "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%X",
1152  (unsigned int) blk->hole_offset,
1153  (unsigned int) blk->hole_length,
1154  (unsigned int) blk->bimg_len,
1155  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1156  goto err;
1157  }
1158 
1159  /*
1160  * cross-check that hole_offset == 0 and hole_length == 0 if
1161  * the HAS_HOLE flag is not set.
1162  */
1163  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1164  (blk->hole_offset != 0 || blk->hole_length != 0))
1165  {
1166  report_invalid_record(state,
1167  "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%X",
1168  (unsigned int) blk->hole_offset,
1169  (unsigned int) blk->hole_length,
1170  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1171  goto err;
1172  }
1173 
1174  /*
1175  * cross-check that bimg_len < BLCKSZ if the IS_COMPRESSED
1176  * flag is set.
1177  */
1178  if ((blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1179  blk->bimg_len == BLCKSZ)
1180  {
1181  report_invalid_record(state,
1182  "BKPIMAGE_IS_COMPRESSED set, but block image length %u at %X/%X",
1183  (unsigned int) blk->bimg_len,
1184  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1185  goto err;
1186  }
1187 
1188  /*
1189  * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE nor
1190  * IS_COMPRESSED flag is set.
1191  */
1192  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1193  !(blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1194  blk->bimg_len != BLCKSZ)
1195  {
1196  report_invalid_record(state,
1197  "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_IS_COMPRESSED set, but block image length is %u at %X/%X",
1198  (unsigned int) blk->data_len,
1199  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1200  goto err;
1201  }
1202  }
1203  if (!(fork_flags & BKPBLOCK_SAME_REL))
1204  {
1205  COPY_HEADER_FIELD(&blk->rnode, sizeof(RelFileNode));
1206  rnode = &blk->rnode;
1207  }
1208  else
1209  {
1210  if (rnode == NULL)
1211  {
1212  report_invalid_record(state,
1213  "BKPBLOCK_SAME_REL set but no previous rel at %X/%X",
1214  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1215  goto err;
1216  }
1217 
1218  blk->rnode = *rnode;
1219  }
1220  COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
1221  }
1222  else
1223  {
1224  report_invalid_record(state,
1225  "invalid block_id %u at %X/%X",
1226  block_id,
1227  (uint32) (state->ReadRecPtr >> 32),
1228  (uint32) state->ReadRecPtr);
1229  goto err;
1230  }
1231  }
1232 
1233  if (remaining != datatotal)
1234  goto shortdata_err;
1235 
1236  /*
1237  * Ok, we've parsed the fragment headers, and verified that the total
1238  * length of the payload in the fragments is equal to the amount of data
1239  * left. Copy the data of each fragment to a separate buffer.
1240  *
1241  * We could just set up pointers into readRecordBuf, but we want to align
1242  * the data for the convenience of the callers. Backup images are not
1243  * copied, however; they don't need alignment.
1244  */
1245 
1246  /* block data first */
1247  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1248  {
1249  DecodedBkpBlock *blk = &state->blocks[block_id];
1250 
1251  if (!blk->in_use)
1252  continue;
1253 
1254  Assert(blk->has_image || !blk->apply_image);
1255 
1256  if (blk->has_image)
1257  {
1258  blk->bkp_image = ptr;
1259  ptr += blk->bimg_len;
1260  }
1261  if (blk->has_data)
1262  {
1263  if (!blk->data || blk->data_len > blk->data_bufsz)
1264  {
1265  if (blk->data)
1266  pfree(blk->data);
1267  blk->data_bufsz = blk->data_len;
1268  blk->data = palloc(blk->data_bufsz);
1269  }
1270  memcpy(blk->data, ptr, blk->data_len);
1271  ptr += blk->data_len;
1272  }
1273  }
1274 
1275  /* and finally, the main data */
1276  if (state->main_data_len > 0)
1277  {
1278  if (!state->main_data || state->main_data_len > state->main_data_bufsz)
1279  {
1280  if (state->main_data)
1281  pfree(state->main_data);
1282  state->main_data_bufsz = state->main_data_len;
1283  state->main_data = palloc(state->main_data_bufsz);
1284  }
1285  memcpy(state->main_data, ptr, state->main_data_len);
1286  ptr += state->main_data_len;
1287  }
1288 
1289  return true;
1290 
1291 shortdata_err:
1292  report_invalid_record(state,
1293  "record with invalid length at %X/%X",
1294  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1295 err:
1296  *errormsg = state->errormsg_buf;
1297 
1298  return false;
1299 }
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:256
uint16 RepOriginId
Definition: xlogdefs.h:51
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:50
uint32 BlockNumber
Definition: block.h:31
uint16 bimg_len
Definition: xlogreader.h:59
unsigned short uint16
Definition: c.h:257
void pfree(void *pointer)
Definition: mcxt.c:949
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:258
#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 Assert(condition)
Definition: c.h:681
uint16 data_bufsz
Definition: xlogreader.h:66
#define XLR_BLOCK_ID_ORIGIN
Definition: xlogrecord.h:226
#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:848
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:989
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139
#define BKPBLOCK_HAS_DATA
Definition: xlogrecord.h:182
static int ReadPageInternal ( XLogReaderState state,
XLogRecPtr  pageptr,
int  reqLen 
)
static

Definition at line 505 of file xlogreader.c.

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

Referenced by XLogReadRecord().

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

Definition at line 50 of file xlogreader.c.

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

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

51 {
52  va_list args;
53 
54  fmt = _(fmt);
55 
56  va_start(args, fmt);
57  vsnprintf(state->errormsg_buf, MAX_ERRORMSG_LEN, fmt, args);
58  va_end(args);
59 }
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:43
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
static void ResetDecoder ( XLogReaderState state)
static

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

990 {
991  int block_id;
992 
993  state->decoded_record = NULL;
994 
995  state->main_data_len = 0;
996 
997  for (block_id = 0; block_id <= state->max_block_id; block_id++)
998  {
999  state->blocks[block_id].in_use = false;
1000  state->blocks[block_id].has_image = false;
1001  state->blocks[block_id].has_data = false;
1002  state->blocks[block_id].apply_image = false;
1003  }
1004  state->max_block_id = -1;
1005 }
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
bool RestoreBlockImage ( XLogReaderState record,
uint8  block_id,
char *  page 
)

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

1363 {
1364  DecodedBkpBlock *bkpb;
1365  char *ptr;
1366  char tmp[BLCKSZ];
1367 
1368  if (!record->blocks[block_id].in_use)
1369  return false;
1370  if (!record->blocks[block_id].has_image)
1371  return false;
1372 
1373  bkpb = &record->blocks[block_id];
1374  ptr = bkpb->bkp_image;
1375 
1376  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1377  {
1378  /* If a backup block image is compressed, decompress it */
1379  if (pglz_decompress(ptr, bkpb->bimg_len, tmp,
1380  BLCKSZ - bkpb->hole_length) < 0)
1381  {
1382  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1383  (uint32) (record->ReadRecPtr >> 32),
1384  (uint32) record->ReadRecPtr,
1385  block_id);
1386  return false;
1387  }
1388  ptr = tmp;
1389  }
1390 
1391  /* generate page, taking into account hole if necessary */
1392  if (bkpb->hole_length == 0)
1393  {
1394  memcpy(page, ptr, BLCKSZ);
1395  }
1396  else
1397  {
1398  memcpy(page, ptr, bkpb->hole_offset);
1399  /* must zero-fill the hole */
1400  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1401  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1402  ptr + bkpb->hole_offset,
1403  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1404  }
1405 
1406  return true;
1407 }
uint16 hole_offset
Definition: xlogreader.h:57
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:50
#define MemSet(start, val, len)
Definition: c.h:863
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:258
#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
static bool ValidXLogPageHeader ( XLogReaderState state,
XLogRecPtr  recptr,
XLogPageHeader  hdr 
)
static

Definition at line 715 of file xlogreader.c.

References Assert, XLogReaderState::latestPagePtr, XLogReaderState::latestPageTLI, MAXFNAMELEN, XLogReaderState::readPageTLI, report_invalid_record(), snprintf(), XLogReaderState::system_identifier, UINT64_FORMAT, XLogReaderState::wal_segment_size, XLByteToSeg, XLOG_PAGE_MAGIC, XLogFileName, XLogSegmentOffset, XLogSegNoOffsetToRecPtr, XLP_ALL_FLAGS, XLogPageHeaderData::xlp_info, XLP_LONG_HEADER, XLogPageHeaderData::xlp_magic, XLogPageHeaderData::xlp_pageaddr, XLogLongPageHeaderData::xlp_seg_size, XLogLongPageHeaderData::xlp_sysid, XLogPageHeaderData::xlp_tli, and XLogLongPageHeaderData::xlp_xlog_blcksz.

Referenced by ReadPageInternal().

717 {
718  XLogRecPtr recaddr;
719  XLogSegNo segno;
720  int32 offset;
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  if (hdr->xlp_pageaddr != recaddr)
809  {
810  char fname[MAXFNAMELEN];
811 
812  XLogFileName(fname, state->readPageTLI, segno, state->wal_segment_size);
813 
814  report_invalid_record(state,
815  "unexpected pageaddr %X/%X in log segment %s, offset %u",
816  (uint32) (hdr->xlp_pageaddr >> 32), (uint32) hdr->xlp_pageaddr,
817  fname,
818  offset);
819  return false;
820  }
821 
822  /*
823  * Since child timelines are always assigned a TLI greater than their
824  * immediate parent's TLI, we should never see TLI go backwards across
825  * successive pages of a consistent WAL sequence.
826  *
827  * Sometimes we re-read a segment that's already been (partially) read. So
828  * we only verify TLIs for pages that are later than the last remembered
829  * LSN.
830  */
831  if (recptr > state->latestPagePtr)
832  {
833  if (hdr->xlp_tli < state->latestPageTLI)
834  {
835  char fname[MAXFNAMELEN];
836 
837  XLogFileName(fname, state->readPageTLI, segno, state->wal_segment_size);
838 
839  report_invalid_record(state,
840  "out-of-sequence timeline ID %u (after %u) in log segment %s, offset %u",
841  hdr->xlp_tli,
842  state->latestPageTLI,
843  fname,
844  offset);
845  return false;
846  }
847  }
848  state->latestPagePtr = recptr;
849  state->latestPageTLI = hdr->xlp_tli;
850 
851  return true;
852 }
TimeLineID readPageTLI
Definition: xlogreader.h:158
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:50
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
signed int int32
Definition: c.h:246
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:258
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:681
#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:301
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
static bool ValidXLogRecord ( XLogReaderState state,
XLogRecord record,
XLogRecPtr  recptr 
)
static

Definition at line 689 of file xlogreader.c.

References COMP_CRC32C, EQ_CRC32C, FIN_CRC32C, INIT_CRC32C, offsetof, report_invalid_record(), SizeOfXLogRecord, XLogRecord::xl_crc, and XLogRecord::xl_tot_len.

Referenced by XLogReadRecord().

690 {
691  pg_crc32c crc;
692 
693  /* Calculate the CRC */
694  INIT_CRC32C(crc);
695  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
696  /* include the record header last */
697  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
698  FIN_CRC32C(crc);
699 
700  if (!EQ_CRC32C(record->xl_crc, crc))
701  {
702  report_invalid_record(state,
703  "incorrect resource manager data checksum in record at %X/%X",
704  (uint32) (recptr >> 32), (uint32) recptr);
705  return false;
706  }
707 
708  return true;
709 }
#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:50
uint32 xl_tot_len
Definition: xlogrecord.h:43
unsigned int uint32
Definition: c.h:258
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
pg_crc32c xl_crc
Definition: xlogrecord.h:49
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:73
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:78
#define offsetof(type, field)
Definition: c.h:549
static bool ValidXLogRecordHeader ( XLogReaderState state,
XLogRecPtr  RecPtr,
XLogRecPtr  PrevRecPtr,
XLogRecord record,
bool  randAccess 
)
static

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

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

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

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

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

128 {
129  int block_id;
130 
131  for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
132  {
133  if (state->blocks[block_id].data)
134  pfree(state->blocks[block_id].data);
135  }
136  if (state->main_data)
137  pfree(state->main_data);
138 
139  pfree(state->errormsg_buf);
140  if (state->readRecordBuf)
141  pfree(state->readRecordBuf);
142  pfree(state->readBuf);
143  pfree(state);
144 }
char * readRecordBuf
Definition: xlogreader.h:189
void pfree(void *pointer)
Definition: mcxt.c:949
#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
void XLogReaderInvalReadState ( XLogReaderState state)

Definition at line 606 of file xlogreader.c.

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

Referenced by ReadPageInternal(), and XLogReadRecord().

607 {
608  state->readSegNo = 0;
609  state->readOff = 0;
610  state->readLen = 0;
611 }
XLogSegNo readSegNo
Definition: xlogreader.h:156
XLogRecord* XLogReadRecord ( XLogReaderState state,
XLogRecPtr  RecPtr,
char **  errormsg 
)

Definition at line 195 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_slot_get_changes_guts(), readOneRecord(), ReadRecord(), XlogReadTwoPhaseData(), and XLogSendLogical().

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

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

1334 {
1335  DecodedBkpBlock *bkpb;
1336 
1337  if (!record->blocks[block_id].in_use)
1338  return NULL;
1339 
1340  bkpb = &record->blocks[block_id];
1341 
1342  if (!bkpb->has_data)
1343  {
1344  if (len)
1345  *len = 0;
1346  return NULL;
1347  }
1348  else
1349  {
1350  if (len)
1351  *len = bkpb->data_len;
1352  return bkpb->data;
1353  }
1354 }
uint16 data_len
Definition: xlogreader.h:65
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139
bool XLogRecGetBlockTag ( XLogReaderState record,
uint8  block_id,
RelFileNode rnode,
ForkNumber forknum,
BlockNumber blknum 
)

Definition at line 1309 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(), XLogDumpDisplayRecord(), and XLogReadBufferForRedoExtended().

1311 {
1312  DecodedBkpBlock *bkpb;
1313 
1314  if (!record->blocks[block_id].in_use)
1315  return false;
1316 
1317  bkpb = &record->blocks[block_id];
1318  if (rnode)
1319  *rnode = bkpb->rnode;
1320  if (forknum)
1321  *forknum = bkpb->forknum;
1322  if (blknum)
1323  *blknum = bkpb->blkno;
1324  return true;
1325 }
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