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 (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 156 of file xlogreader.c.

References Max, MCXT_ALLOC_NO_OOM, NULL, 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:184
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:909
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
void pfree(void *pointer)
Definition: mcxt.c:950
unsigned int uint32
Definition: c.h:268
uint32 readRecordBufSize
Definition: xlogreader.h:185
#define Max(x, y)
Definition: c.h:800
#define NULL
Definition: c.h:229
bool DecodeXLogRecord ( XLogReaderState state,
XLogRecord record,
char **  errormsg 
)

Definition at line 1012 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, NULL, 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().

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

Referenced by 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);
513  targetPageOff = (pageptr % XLogSegSize);
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  XLogPageHeader hdr;
535  XLogRecPtr targetSegmentPtr = pageptr - targetPageOff;
536 
537  readLen = state->read_page(state, targetSegmentPtr, XLOG_BLCKSZ,
538  state->currRecPtr,
539  state->readBuf, &state->readPageTLI);
540  if (readLen < 0)
541  goto err;
542 
543  /* we can be sure to have enough WAL available, we scrolled back */
544  Assert(readLen == XLOG_BLCKSZ);
545 
546  hdr = (XLogPageHeader) state->readBuf;
547 
548  if (!ValidXLogPageHeader(state, targetSegmentPtr, hdr))
549  goto err;
550  }
551 
552  /*
553  * First, read the requested data length, but at least a short page header
554  * so that we can validate it.
555  */
556  readLen = state->read_page(state, pageptr, Max(reqLen, SizeOfXLogShortPHD),
557  state->currRecPtr,
558  state->readBuf, &state->readPageTLI);
559  if (readLen < 0)
560  goto err;
561 
562  Assert(readLen <= XLOG_BLCKSZ);
563 
564  /* Do we have enough data to check the header length? */
565  if (readLen <= SizeOfXLogShortPHD)
566  goto err;
567 
568  Assert(readLen >= reqLen);
569 
570  hdr = (XLogPageHeader) state->readBuf;
571 
572  /* still not enough */
573  if (readLen < XLogPageHeaderSize(hdr))
574  {
575  readLen = state->read_page(state, pageptr, XLogPageHeaderSize(hdr),
576  state->currRecPtr,
577  state->readBuf, &state->readPageTLI);
578  if (readLen < 0)
579  goto err;
580  }
581 
582  /*
583  * Now that we know we have the full header, validate it.
584  */
585  if (!ValidXLogPageHeader(state, pageptr, hdr))
586  goto err;
587 
588  /* update read state information */
589  state->readSegNo = targetSegNo;
590  state->readOff = targetPageOff;
591  state->readLen = readLen;
592 
593  return readLen;
594 
595 err:
597  return -1;
598 }
#define XLogSegSize
Definition: xlog_internal.h:92
XLogPageReadCB read_page
Definition: xlogreader.h:97
TimeLineID readPageTLI
Definition: xlogreader.h:153
#define XLogPageHeaderSize(hdr)
Definition: xlog_internal.h:85
static bool ValidXLogPageHeader(XLogReaderState *state, XLogRecPtr recptr, XLogPageHeader hdr)
Definition: xlogreader.c:713
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
uint64 XLogSegNo
Definition: xlogdefs.h:34
unsigned int uint32
Definition: c.h:268
void XLogReaderInvalReadState(XLogReaderState *state)
Definition: xlogreader.c:604
XLogRecPtr currRecPtr
Definition: xlogreader.h:163
#define Max(x, y)
Definition: c.h:800
#define XLByteToSeg(xlrp, logSegNo)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
XLogSegNo readSegNo
Definition: xlogreader.h:151
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
static uint32 readLen
Definition: xlog.c:785
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:188
#define _(x)
Definition: elog.c:84
static void ResetDecoder ( XLogReaderState state)
static

Definition at line 987 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, XLogReaderState::max_block_id, and NULL.

Referenced by DecodeXLogRecord(), and XLogReadRecord().

988 {
989  int block_id;
990 
991  state->decoded_record = NULL;
992 
993  state->main_data_len = 0;
994 
995  for (block_id = 0; block_id <= state->max_block_id; block_id++)
996  {
997  state->blocks[block_id].in_use = false;
998  state->blocks[block_id].has_image = false;
999  state->blocks[block_id].has_data = false;
1000  state->blocks[block_id].apply_image = false;
1001  }
1002  state->max_block_id = -1;
1003 }
uint32 main_data_len
Definition: xlogreader.h:128
XLogRecord * decoded_record
Definition: xlogreader.h:125
#define NULL
Definition: c.h:229
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:134
bool RestoreBlockImage ( XLogReaderState record,
uint8  block_id,
char *  page 
)

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

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

Definition at line 713 of file xlogreader.c.

References Assert, XLogReaderState::latestPagePtr, XLogReaderState::latestPageTLI, MAXFNAMELEN, XLogReaderState::readPageTLI, report_invalid_record(), snprintf(), XLogReaderState::system_identifier, UINT64_FORMAT, XLByteToSeg, XLOG_PAGE_MAGIC, XLogFileName, XLogSegNoOffsetToRecPtr, XLogSegSize, 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().

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

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

688 {
689  pg_crc32c crc;
690 
691  /* Calculate the CRC */
692  INIT_CRC32C(crc);
693  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
694  /* include the record header last */
695  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
696  FIN_CRC32C(crc);
697 
698  if (!EQ_CRC32C(record->xl_crc, crc))
699  {
700  report_invalid_record(state,
701  "incorrect resource manager data checksum in record at %X/%X",
702  (uint32) (recptr >> 32), (uint32) recptr);
703  return false;
704  }
705 
706  return true;
707 }
#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:268
#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:555
static bool ValidXLogRecordHeader ( XLogReaderState state,
XLogRecPtr  RecPtr,
XLogRecPtr  PrevRecPtr,
XLogRecord record,
bool  randAccess 
)
static

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

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

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

68 {
70 
71  state = (XLogReaderState *)
74  if (!state)
75  return NULL;
76 
77  state->max_block_id = -1;
78 
79  /*
80  * Permanently allocate readBuf. We do it this way, rather than just
81  * making a static array, for two reasons: (1) no need to waste the
82  * storage in most instantiations of the backend; (2) a static char array
83  * isn't guaranteed to have any particular alignment, whereas
84  * palloc_extended() will provide MAXALIGN'd storage.
85  */
86  state->readBuf = (char *) palloc_extended(XLOG_BLCKSZ,
88  if (!state->readBuf)
89  {
90  pfree(state);
91  return NULL;
92  }
93 
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:97
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:909
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
void * private_data
Definition: xlogreader.h:108
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:43
void pfree(void *pointer)
Definition: mcxt.c:950
static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength)
Definition: xlogreader.c:156
#define NULL
Definition: c.h:229
Definition: regguts.h:298
#define MCXT_ALLOC_ZERO
Definition: fe_memutils.h:19
char * errormsg_buf
Definition: xlogreader.h:188
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:184
void pfree(void *pointer)
Definition: mcxt.c:950
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:222
char * errormsg_buf
Definition: xlogreader.h:188
char * main_data
Definition: xlogreader.h:127
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:134
void XLogReaderInvalReadState ( XLogReaderState state)

Definition at line 604 of file xlogreader.c.

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

Referenced by ReadPageInternal(), and XLogReadRecord().

605 {
606  state->readSegNo = 0;
607  state->readOff = 0;
608  state->readLen = 0;
609 }
XLogSegNo readSegNo
Definition: xlogreader.h:151
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, NULL, XLogReaderState::readBuf, readOff, ReadPageInternal(), XLogReaderState::readRecordBuf, XLogReaderState::readRecordBufSize, XLogReaderState::ReadRecPtr, report_invalid_record(), ResetDecoder(), SizeOfXLogRecord, SizeOfXLogShortPHD, ValidXLogRecord(), ValidXLogRecordHeader(), XLogRecord::xl_info, XLogRecord::xl_rmid, XLogRecord::xl_tot_len, XLOG_SWITCH, XLogPageHeaderSize, XLogReaderInvalReadState(), XLogSegSize, 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().

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 += XLogSegSize - 1;
470  state->EndRecPtr -= state->EndRecPtr % XLogSegSize;
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 XLogSegSize
Definition: xlog_internal.h:92
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
char * readRecordBuf
Definition: xlogreader.h:184
#define XLogPageHeaderSize(hdr)
Definition: xlog_internal.h:85
#define Min(x, y)
Definition: c.h:806
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:1012
static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
Definition: xlogreader.c:503
XLogRecPtr EndRecPtr
Definition: xlogreader.h:115
static uint32 readOff
Definition: xlog.c:784
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:114
unsigned int uint32
Definition: c.h:268
void XLogReaderInvalReadState(XLogReaderState *state)
Definition: xlogreader.c:604
uint32 readRecordBufSize
Definition: xlogreader.h:185
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
XLogRecPtr currRecPtr
Definition: xlogreader.h:163
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
static bool ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr, XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess)
Definition: xlogreader.c:618
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:588
#define XLOG_SWITCH
Definition: pg_control.h:70
static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
Definition: xlogreader.c:687
#define XRecOffIsValid(xlrp)
char * errormsg_buf
Definition: xlogreader.h:188
static void static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:987
char* XLogRecGetBlockData ( XLogReaderState record,
uint8  block_id,
Size len 
)

Definition at line 1331 of file xlogreader.c.

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

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

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

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

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