PostgreSQL Source Code  git master
xlogreader.c File Reference
#include "postgres.h"
#include "access/transam.h"
#include "access/xlogrecord.h"
#include "access/xlog_internal.h"
#include "access/xlogreader.h"
#include "catalog/pg_control.h"
#include "common/pg_lzcompress.h"
#include "replication/origin.h"
#include "miscadmin.h"
#include "utils/memutils.h"
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 void report_invalid_record (XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
 
static void static bool allocate_recordbuf (XLogReaderState *state, uint32 reclength)
 
static int ReadPageInternal (XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
 
static void XLogReaderInvalReadState (XLogReaderState *state)
 
static bool ValidXLogRecordHeader (XLogReaderState *state, XLogRecPtr RecPtr, XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess)
 
static bool ValidXLogRecord (XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
 
static void ResetDecoder (XLogReaderState *state)
 
XLogReaderStateXLogReaderAllocate (int wal_segment_size, const char *waldir, XLogPageReadCB pagereadfunc, void *private_data)
 
void XLogReaderFree (XLogReaderState *state)
 
void WALOpenSegmentInit (WALOpenSegment *seg, WALSegmentContext *segcxt, int segsize, const char *waldir)
 
XLogRecordXLogReadRecord (XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
 
bool XLogReaderValidatePageHeader (XLogReaderState *state, XLogRecPtr recptr, char *phdr)
 
bool DecodeXLogRecord (XLogReaderState *state, XLogRecord *record, char **errormsg)
 
bool XLogRecGetBlockTag (XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
 
char * XLogRecGetBlockData (XLogReaderState *record, uint8 block_id, Size *len)
 
bool RestoreBlockImage (XLogReaderState *record, uint8 block_id, char *page)
 
FullTransactionId XLogRecGetFullXid (XLogReaderState *record)
 

Macro Definition Documentation

◆ COPY_HEADER_FIELD

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

Referenced by DecodeXLogRecord().

◆ MAX_ERRORMSG_LEN

#define MAX_ERRORMSG_LEN   1000

Definition at line 47 of file xlogreader.c.

Referenced by report_invalid_record(), and XLogReaderAllocate().

Function Documentation

◆ allocate_recordbuf()

static bool allocate_recordbuf ( XLogReaderState state,
uint32  reclength 
)
static

Definition at line 164 of file xlogreader.c.

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

Referenced by XLogReaderAllocate(), and XLogReadRecord().

165 {
166  uint32 newSize = reclength;
167 
168  newSize += XLOG_BLCKSZ - (newSize % XLOG_BLCKSZ);
169  newSize = Max(newSize, 5 * Max(BLCKSZ, XLOG_BLCKSZ));
170 
171 #ifndef FRONTEND
172 
173  /*
174  * Note that in much unlucky circumstances, the random data read from a
175  * recycled segment can cause this routine to be called with a size
176  * causing a hard failure at allocation. For a standby, this would cause
177  * the instance to stop suddenly with a hard failure, preventing it to
178  * retry fetching WAL from one of its sources which could allow it to move
179  * on with replay without a manual restart. If the data comes from a past
180  * recycled segment and is still valid, then the allocation may succeed
181  * but record checks are going to fail so this would be short-lived. If
182  * the allocation fails because of a memory shortage, then this is not a
183  * hard failure either per the guarantee given by MCXT_ALLOC_NO_OOM.
184  */
185  if (!AllocSizeIsValid(newSize))
186  return false;
187 
188 #endif
189 
190  if (state->readRecordBuf)
191  pfree(state->readRecordBuf);
192  state->readRecordBuf =
193  (char *) palloc_extended(newSize, MCXT_ALLOC_NO_OOM);
194  if (state->readRecordBuf == NULL)
195  {
196  state->readRecordBufSize = 0;
197  return false;
198  }
199  state->readRecordBufSize = newSize;
200  return true;
201 }
char * readRecordBuf
Definition: xlogreader.h:204
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1013
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
void pfree(void *pointer)
Definition: mcxt.c:1056
unsigned int uint32
Definition: c.h:358
#define AllocSizeIsValid(size)
Definition: memutils.h:42
uint32 readRecordBufSize
Definition: xlogreader.h:205
#define Max(x, y)
Definition: c.h:898

◆ DecodeXLogRecord()

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

Definition at line 1051 of file xlogreader.c.

References DecodedBkpBlock::apply_image, Assert, DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, DecodedBkpBlock::bkp_image, BKPBLOCK_FORK_MASK, BKPBLOCK_HAS_DATA, BKPBLOCK_HAS_IMAGE, BKPBLOCK_SAME_REL, BKPIMAGE_APPLY, BKPIMAGE_HAS_HOLE, BKPIMAGE_IS_COMPRESSED, DecodedBkpBlock::blkno, XLogReaderState::blocks, COPY_HEADER_FIELD, DecodedBkpBlock::data, DecodedBkpBlock::data_bufsz, DecodedBkpBlock::data_len, XLogReaderState::decoded_record, XLogReaderState::errormsg_buf, DecodedBkpBlock::flags, DecodedBkpBlock::forknum, DecodedBkpBlock::has_data, DecodedBkpBlock::has_image, DecodedBkpBlock::hole_length, DecodedBkpBlock::hole_offset, DecodedBkpBlock::in_use, InvalidRepOriginId, XLogReaderState::main_data, XLogReaderState::main_data_bufsz, XLogReaderState::main_data_len, Max, XLogReaderState::max_block_id, MAXALIGN, palloc(), pfree(), XLogReaderState::ReadRecPtr, XLogReaderState::record_origin, remaining, report_invalid_record(), ResetDecoder(), DecodedBkpBlock::rnode, SizeOfXLogRecord, XLogRecord::xl_tot_len, XLR_BLOCK_ID_DATA_LONG, XLR_BLOCK_ID_DATA_SHORT, XLR_BLOCK_ID_ORIGIN, and XLR_MAX_BLOCK_ID.

Referenced by XLogInsertRecord(), and XLogReadRecord().

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

◆ ReadPageInternal()

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

Definition at line 546 of file xlogreader.c.

References Assert, XLogReaderState::currRecPtr, Max, XLogReaderState::read_page, XLogReaderState::readBuf, XLogReaderState::readLen, readLen, XLogReaderState::seg, XLogReaderState::segcxt, SizeOfXLogShortPHD, WALOpenSegment::ws_off, WALOpenSegment::ws_segno, WALSegmentContext::ws_segsize, XLByteToSeg, XLogPageHeaderSize, XLogReaderInvalReadState(), XLogReaderValidatePageHeader(), and XLogSegmentOffset.

Referenced by XLogReaderValidatePageHeader(), and XLogReadRecord().

547 {
548  int readLen;
549  uint32 targetPageOff;
550  XLogSegNo targetSegNo;
551  XLogPageHeader hdr;
552 
553  Assert((pageptr % XLOG_BLCKSZ) == 0);
554 
555  XLByteToSeg(pageptr, targetSegNo, state->segcxt.ws_segsize);
556  targetPageOff = XLogSegmentOffset(pageptr, state->segcxt.ws_segsize);
557 
558  /* check whether we have all the requested data already */
559  if (targetSegNo == state->seg.ws_segno &&
560  targetPageOff == state->seg.ws_off && reqLen <= state->readLen)
561  return state->readLen;
562 
563  /*
564  * Data is not in our buffer.
565  *
566  * Every time we actually read the page, even if we looked at parts of it
567  * before, we need to do verification as the read_page callback might now
568  * be rereading data from a different source.
569  *
570  * Whenever switching to a new WAL segment, we read the first page of the
571  * file and validate its header, even if that's not where the target
572  * record is. This is so that we can check the additional identification
573  * info that is present in the first page's "long" header.
574  */
575  if (targetSegNo != state->seg.ws_segno && targetPageOff != 0)
576  {
577  XLogRecPtr targetSegmentPtr = pageptr - targetPageOff;
578 
579  readLen = state->read_page(state, targetSegmentPtr, XLOG_BLCKSZ,
580  state->currRecPtr,
581  state->readBuf);
582  if (readLen < 0)
583  goto err;
584 
585  /* we can be sure to have enough WAL available, we scrolled back */
586  Assert(readLen == XLOG_BLCKSZ);
587 
588  if (!XLogReaderValidatePageHeader(state, targetSegmentPtr,
589  state->readBuf))
590  goto err;
591  }
592 
593  /*
594  * First, read the requested data length, but at least a short page header
595  * so that we can validate it.
596  */
597  readLen = state->read_page(state, pageptr, Max(reqLen, SizeOfXLogShortPHD),
598  state->currRecPtr,
599  state->readBuf);
600  if (readLen < 0)
601  goto err;
602 
603  Assert(readLen <= XLOG_BLCKSZ);
604 
605  /* Do we have enough data to check the header length? */
606  if (readLen <= SizeOfXLogShortPHD)
607  goto err;
608 
609  Assert(readLen >= reqLen);
610 
611  hdr = (XLogPageHeader) state->readBuf;
612 
613  /* still not enough */
614  if (readLen < XLogPageHeaderSize(hdr))
615  {
616  readLen = state->read_page(state, pageptr, XLogPageHeaderSize(hdr),
617  state->currRecPtr,
618  state->readBuf);
619  if (readLen < 0)
620  goto err;
621  }
622 
623  /*
624  * Now that we know we have the full header, validate it.
625  */
626  if (!XLogReaderValidatePageHeader(state, pageptr, (char *) hdr))
627  goto err;
628 
629  /* update read state information */
630  state->seg.ws_segno = targetSegNo;
631  state->seg.ws_off = targetPageOff;
632  state->readLen = readLen;
633 
634  return readLen;
635 
636 err:
638  return -1;
639 }
XLogPageReadCB read_page
Definition: xlogreader.h:115
#define XLogPageHeaderSize(hdr)
Definition: xlog_internal.h:85
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
bool XLogReaderValidatePageHeader(XLogReaderState *state, XLogRecPtr recptr, char *phdr)
Definition: xlogreader.c:757
WALOpenSegment seg
Definition: xlogreader.h:170
static void XLogReaderInvalReadState(XLogReaderState *state)
Definition: xlogreader.c:645
uint64 XLogSegNo
Definition: xlogdefs.h:41
XLogSegNo ws_segno
Definition: xlogreader.h:38
unsigned int uint32
Definition: c.h:358
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
XLogRecPtr currRecPtr
Definition: xlogreader.h:180
#define Max(x, y)
Definition: c.h:898
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:732
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
WALSegmentContext segcxt
Definition: xlogreader.h:169
uint32 ws_off
Definition: xlogreader.h:39
static uint32 readLen
Definition: xlog.c:793
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ report_invalid_record()

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

Definition at line 54 of file xlogreader.c.

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

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

55 {
56  va_list args;
57 
58  fmt = _(fmt);
59 
60  va_start(args, fmt);
61  vsnprintf(state->errormsg_buf, MAX_ERRORMSG_LEN, fmt, args);
62  va_end(args);
63 }
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:47
#define vsnprintf
Definition: port.h:191
char * errormsg_buf
Definition: xlogreader.h:208
#define _(x)
Definition: elog.c:84

◆ ResetDecoder()

static void ResetDecoder ( XLogReaderState state)
static

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

1027 {
1028  int block_id;
1029 
1030  state->decoded_record = NULL;
1031 
1032  state->main_data_len = 0;
1033 
1034  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1035  {
1036  state->blocks[block_id].in_use = false;
1037  state->blocks[block_id].has_image = false;
1038  state->blocks[block_id].has_data = false;
1039  state->blocks[block_id].apply_image = false;
1040  }
1041  state->max_block_id = -1;
1042 }
uint32 main_data_len
Definition: xlogreader.h:146
XLogRecord * decoded_record
Definition: xlogreader.h:143
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:152

◆ RestoreBlockImage()

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

Definition at line 1420 of file xlogreader.c.

References DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, DecodedBkpBlock::bkp_image, BKPIMAGE_IS_COMPRESSED, XLogReaderState::blocks, PGAlignedBlock::data, DecodedBkpBlock::has_image, DecodedBkpBlock::hole_length, DecodedBkpBlock::hole_offset, DecodedBkpBlock::in_use, MemSet, pglz_decompress(), XLogReaderState::ReadRecPtr, and report_invalid_record().

Referenced by checkXLogConsistency(), and XLogReadBufferForRedoExtended().

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

◆ ValidXLogRecord()

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

Definition at line 728 of file xlogreader.c.

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

Referenced by XLogReadRecord().

729 {
730  pg_crc32c crc;
731 
732  /* Calculate the CRC */
733  INIT_CRC32C(crc);
734  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
735  /* include the record header last */
736  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
737  FIN_CRC32C(crc);
738 
739  if (!EQ_CRC32C(record->xl_crc, crc))
740  {
741  report_invalid_record(state,
742  "incorrect resource manager data checksum in record at %X/%X",
743  (uint32) (recptr >> 32), (uint32) recptr);
744  return false;
745  }
746 
747  return true;
748 }
#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:54
unsigned int uint32
Definition: c.h:358
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:94
#define offsetof(type, field)
Definition: c.h:655

◆ ValidXLogRecordHeader()

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

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

662 {
663  if (record->xl_tot_len < SizeOfXLogRecord)
664  {
665  report_invalid_record(state,
666  "invalid record length at %X/%X: wanted %u, got %u",
667  (uint32) (RecPtr >> 32), (uint32) RecPtr,
668  (uint32) SizeOfXLogRecord, record->xl_tot_len);
669  return false;
670  }
671  if (record->xl_rmid > RM_MAX_ID)
672  {
673  report_invalid_record(state,
674  "invalid resource manager ID %u at %X/%X",
675  record->xl_rmid, (uint32) (RecPtr >> 32),
676  (uint32) RecPtr);
677  return false;
678  }
679  if (randAccess)
680  {
681  /*
682  * We can't exactly verify the prev-link, but surely it should be less
683  * than the record's own address.
684  */
685  if (!(record->xl_prev < RecPtr))
686  {
687  report_invalid_record(state,
688  "record with incorrect prev-link %X/%X at %X/%X",
689  (uint32) (record->xl_prev >> 32),
690  (uint32) record->xl_prev,
691  (uint32) (RecPtr >> 32), (uint32) RecPtr);
692  return false;
693  }
694  }
695  else
696  {
697  /*
698  * Record's prev-link should exactly match our previous location. This
699  * check guards against torn WAL pages where a stale but valid-looking
700  * WAL record starts on a sector boundary.
701  */
702  if (record->xl_prev != PrevRecPtr)
703  {
704  report_invalid_record(state,
705  "record with incorrect prev-link %X/%X at %X/%X",
706  (uint32) (record->xl_prev >> 32),
707  (uint32) record->xl_prev,
708  (uint32) (RecPtr >> 32), (uint32) RecPtr);
709  return false;
710  }
711  }
712 
713  return true;
714 }
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:54
RmgrId xl_rmid
Definition: xlogrecord.h:47
uint32 xl_tot_len
Definition: xlogrecord.h:43
unsigned int uint32
Definition: c.h:358
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define RM_MAX_ID
Definition: rmgr.h:33

◆ WALOpenSegmentInit()

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

Definition at line 207 of file xlogreader.c.

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

Referenced by XLogReaderAllocate().

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

◆ XLogReaderAllocate()

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

Definition at line 71 of file xlogreader.c.

References allocate_recordbuf(), XLogReaderState::errormsg_buf, XLogReaderState::max_block_id, MAX_ERRORMSG_LEN, MCXT_ALLOC_NO_OOM, MCXT_ALLOC_ZERO, palloc_extended(), pfree(), XLogReaderState::private_data, XLogReaderState::read_page, XLogReaderState::readBuf, XLogReaderState::seg, XLogReaderState::segcxt, and WALOpenSegmentInit().

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

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

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)

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

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

◆ XLogReaderInvalReadState()

static void XLogReaderInvalReadState ( XLogReaderState state)
static

Definition at line 645 of file xlogreader.c.

References XLogReaderState::readLen, XLogReaderState::seg, WALOpenSegment::ws_off, and WALOpenSegment::ws_segno.

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

646 {
647  state->seg.ws_segno = 0;
648  state->seg.ws_off = 0;
649  state->readLen = 0;
650 }
WALOpenSegment seg
Definition: xlogreader.h:170
XLogSegNo ws_segno
Definition: xlogreader.h:38
uint32 ws_off
Definition: xlogreader.h:39

◆ XLogReaderValidatePageHeader()

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

Definition at line 757 of file xlogreader.c.

References Assert, XLogReaderState::EndRecPtr, header(), InvalidXLogRecPtr, XLogReaderState::latestPagePtr, XLogReaderState::latestPageTLI, MAXALIGN, MAXFNAMELEN, XLogReaderState::readBuf, readLen, ReadPageInternal(), XLogReaderState::ReadRecPtr, ReadRecPtr, report_invalid_record(), XLogReaderState::seg, XLogReaderState::segcxt, XLogReaderState::system_identifier, WALSegmentContext::ws_segsize, WALOpenSegment::ws_tli, XLByteToSeg, XLOG_PAGE_MAGIC, XLogFileName, XLogPageHeaderSize, XLogReaderInvalReadState(), XLogReadRecord(), XLogRecPtrIsInvalid, XLogSegmentOffset, XLogSegNoOffsetToRecPtr, XLP_ALL_FLAGS, XLP_FIRST_IS_CONTRECORD, XLogPageHeaderData::xlp_info, XLP_LONG_HEADER, XLogPageHeaderData::xlp_magic, XLogPageHeaderData::xlp_pageaddr, XLogPageHeaderData::xlp_rem_len, XLogLongPageHeaderData::xlp_seg_size, XLogLongPageHeaderData::xlp_sysid, XLogPageHeaderData::xlp_tli, and XLogLongPageHeaderData::xlp_xlog_blcksz.

Referenced by ReadPageInternal(), and XLogPageRead().

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

◆ XLogReadRecord()

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

Definition at line 237 of file xlogreader.c.

References allocate_recordbuf(), Assert, XLogReaderState::currRecPtr, DecodeXLogRecord(), XLogReaderState::EndRecPtr, XLogReaderState::errormsg_buf, InvalidXLogRecPtr, MAXALIGN, Min, XLogReaderState::readBuf, readOff, ReadPageInternal(), XLogReaderState::readRecordBuf, XLogReaderState::readRecordBufSize, XLogReaderState::ReadRecPtr, report_invalid_record(), ResetDecoder(), XLogReaderState::segcxt, SizeOfXLogRecord, SizeOfXLogShortPHD, ValidXLogRecord(), ValidXLogRecordHeader(), WALSegmentContext::ws_segsize, XLogRecord::xl_info, XLogRecord::xl_rmid, XLogRecord::xl_tot_len, XLOG_SWITCH, XLogPageHeaderSize, XLogReaderInvalReadState(), XLogSegmentOffset, XLP_FIRST_IS_CONTRECORD, XLogPageHeaderData::xlp_info, XLogPageHeaderData::xlp_rem_len, XLR_INFO_MASK, and XRecOffIsValid.

Referenced by DecodingContextFindStartpoint(), extractPageMap(), findLastCheckpoint(), main(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), readOneRecord(), ReadRecord(), XLogReaderValidatePageHeader(), XlogReadTwoPhaseData(), and XLogSendLogical().

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

◆ XLogRecGetBlockData()

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

Definition at line 1391 of file xlogreader.c.

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

Referenced by _bt_restore_meta(), brin_xlog_insert_update(), brin_xlog_samepage_update(), btree_desc(), btree_xlog_insert(), btree_xlog_newroot(), btree_xlog_split(), btree_xlog_vacuum(), DecodeInsert(), DecodeMultiInsert(), DecodeUpdate(), generic_redo(), gin_desc(), ginRedoInsert(), ginRedoInsertListPage(), ginRedoUpdateMetapage(), ginRedoVacuumDataLeafPage(), gistRedoPageSplitRecord(), gistRedoPageUpdateRecord(), hash_xlog_add_ovfl_page(), hash_xlog_delete(), hash_xlog_insert(), hash_xlog_move_page_contents(), hash_xlog_split_allocate_page(), hash_xlog_squeeze_page(), heap_xlog_clean(), heap_xlog_freeze_page(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_multi_insert(), and heap_xlog_update().

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

◆ XLogRecGetBlockTag()

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

Definition at line 1367 of file xlogreader.c.

References DecodedBkpBlock::blkno, XLogReaderState::blocks, DecodedBkpBlock::forknum, DecodedBkpBlock::in_use, and DecodedBkpBlock::rnode.

Referenced by brin_xlog_revmap_extend(), btree_xlog_delete(), btree_xlog_split(), btree_xlog_vacuum(), checkXLogConsistency(), DecodeDelete(), DecodeInsert(), DecodeMultiInsert(), DecodeSpecConfirm(), DecodeUpdate(), extractPageInfo(), gistRedoDeleteRecord(), gistRedoPageSplitRecord(), hash_xlog_add_ovfl_page(), hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), hash_xlog_vacuum_one_page(), heap_xlog_clean(), heap_xlog_delete(), heap_xlog_freeze_page(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_update(), heap_xlog_visible(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), spgRedoVacuumRedirect(), xlog_redo(), XLogDumpDisplayRecord(), and XLogReadBufferForRedoExtended().

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

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 1473 of file xlogreader.c.

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

1474 {
1475  TransactionId xid,
1476  next_xid;
1477  uint32 epoch;
1478 
1479  /*
1480  * This function is only safe during replay, because it depends on the
1481  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1482  */
1484 
1485  xid = XLogRecGetXid(record);
1488 
1489  /*
1490  * If xid is numerically greater than next_xid, it has to be from the
1491  * last epoch.
1492  */
1493  if (unlikely(xid > next_xid))
1494  --epoch;
1495 
1496  return FullTransactionIdFromEpochAndXid(epoch, xid);
1497 }
#define AmStartupProcess()
Definition: miscadmin.h:412
uint32 TransactionId
Definition: c.h:507
FullTransactionId nextFullXid
Definition: transam.h:164
#define XidFromFullTransactionId(x)
Definition: transam.h:48
bool IsUnderPostmaster
Definition: globals.c:109
VariableCache ShmemVariableCache
Definition: varsup.c:34
unsigned int uint32
Definition: c.h:358
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:244
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:732
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:65
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
#define unlikely(x)
Definition: c.h:208