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

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

Referenced by XLogReaderAllocate(), and XLogReadRecord().

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

◆ DecodeXLogRecord()

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

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

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

◆ ReadPageInternal()

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

Definition at line 526 of file xlogreader.c.

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

Referenced by XLogReaderValidatePageHeader(), and XLogReadRecord().

527 {
528  int readLen;
529  uint32 targetPageOff;
530  XLogSegNo targetSegNo;
531  XLogPageHeader hdr;
532 
533  Assert((pageptr % XLOG_BLCKSZ) == 0);
534 
535  XLByteToSeg(pageptr, targetSegNo, state->wal_segment_size);
536  targetPageOff = XLogSegmentOffset(pageptr, state->wal_segment_size);
537 
538  /* check whether we have all the requested data already */
539  if (targetSegNo == state->readSegNo && targetPageOff == state->readOff &&
540  reqLen <= state->readLen)
541  return state->readLen;
542 
543  /*
544  * Data is not in our buffer.
545  *
546  * Every time we actually read the page, even if we looked at parts of it
547  * before, we need to do verification as the read_page callback might now
548  * be rereading data from a different source.
549  *
550  * Whenever switching to a new WAL segment, we read the first page of the
551  * file and validate its header, even if that's not where the target
552  * record is. This is so that we can check the additional identification
553  * info that is present in the first page's "long" header.
554  */
555  if (targetSegNo != state->readSegNo && targetPageOff != 0)
556  {
557  XLogRecPtr targetSegmentPtr = pageptr - targetPageOff;
558 
559  readLen = state->read_page(state, targetSegmentPtr, XLOG_BLCKSZ,
560  state->currRecPtr,
561  state->readBuf, &state->readPageTLI);
562  if (readLen < 0)
563  goto err;
564 
565  /* we can be sure to have enough WAL available, we scrolled back */
566  Assert(readLen == XLOG_BLCKSZ);
567 
568  if (!XLogReaderValidatePageHeader(state, targetSegmentPtr,
569  state->readBuf))
570  goto err;
571  }
572 
573  /*
574  * First, read the requested data length, but at least a short page header
575  * so that we can validate it.
576  */
577  readLen = state->read_page(state, pageptr, Max(reqLen, SizeOfXLogShortPHD),
578  state->currRecPtr,
579  state->readBuf, &state->readPageTLI);
580  if (readLen < 0)
581  goto err;
582 
583  Assert(readLen <= XLOG_BLCKSZ);
584 
585  /* Do we have enough data to check the header length? */
586  if (readLen <= SizeOfXLogShortPHD)
587  goto err;
588 
589  Assert(readLen >= reqLen);
590 
591  hdr = (XLogPageHeader) state->readBuf;
592 
593  /* still not enough */
594  if (readLen < XLogPageHeaderSize(hdr))
595  {
596  readLen = state->read_page(state, pageptr, XLogPageHeaderSize(hdr),
597  state->currRecPtr,
598  state->readBuf, &state->readPageTLI);
599  if (readLen < 0)
600  goto err;
601  }
602 
603  /*
604  * Now that we know we have the full header, validate it.
605  */
606  if (!XLogReaderValidatePageHeader(state, pageptr, (char *) hdr))
607  goto err;
608 
609  /* update read state information */
610  state->readSegNo = targetSegNo;
611  state->readOff = targetPageOff;
612  state->readLen = readLen;
613 
614  return readLen;
615 
616 err:
618  return -1;
619 }
XLogPageReadCB read_page
Definition: xlogreader.h:106
TimeLineID readPageTLI
Definition: xlogreader.h:162
#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:737
uint64 XLogSegNo
Definition: xlogdefs.h:41
unsigned int uint32
Definition: c.h:358
int wal_segment_size
Definition: xlogreader.h:83
void XLogReaderInvalReadState(XLogReaderState *state)
Definition: xlogreader.c:625
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
XLogRecPtr currRecPtr
Definition: xlogreader.h:172
#define Max(x, y)
Definition: c.h:898
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:732
XLogSegNo readSegNo
Definition: xlogreader.h:160
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
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 53 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().

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

◆ ResetDecoder()

static void ResetDecoder ( XLogReaderState state)
static

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

1008 {
1009  int block_id;
1010 
1011  state->decoded_record = NULL;
1012 
1013  state->main_data_len = 0;
1014 
1015  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1016  {
1017  state->blocks[block_id].in_use = false;
1018  state->blocks[block_id].has_image = false;
1019  state->blocks[block_id].has_data = false;
1020  state->blocks[block_id].apply_image = false;
1021  }
1022  state->max_block_id = -1;
1023 }
uint32 main_data_len
Definition: xlogreader.h:137
XLogRecord * decoded_record
Definition: xlogreader.h:134
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:143

◆ RestoreBlockImage()

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

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

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

◆ ValidXLogRecord()

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

Definition at line 708 of file xlogreader.c.

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

Referenced by XLogReadRecord().

709 {
710  pg_crc32c crc;
711 
712  /* Calculate the CRC */
713  INIT_CRC32C(crc);
714  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
715  /* include the record header last */
716  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
717  FIN_CRC32C(crc);
718 
719  if (!EQ_CRC32C(record->xl_crc, crc))
720  {
721  report_invalid_record(state,
722  "incorrect resource manager data checksum in record at %X/%X",
723  (uint32) (recptr >> 32), (uint32) recptr);
724  return false;
725  }
726 
727  return true;
728 }
#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:53
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 639 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().

642 {
643  if (record->xl_tot_len < SizeOfXLogRecord)
644  {
645  report_invalid_record(state,
646  "invalid record length at %X/%X: wanted %u, got %u",
647  (uint32) (RecPtr >> 32), (uint32) RecPtr,
648  (uint32) SizeOfXLogRecord, record->xl_tot_len);
649  return false;
650  }
651  if (record->xl_rmid > RM_MAX_ID)
652  {
653  report_invalid_record(state,
654  "invalid resource manager ID %u at %X/%X",
655  record->xl_rmid, (uint32) (RecPtr >> 32),
656  (uint32) RecPtr);
657  return false;
658  }
659  if (randAccess)
660  {
661  /*
662  * We can't exactly verify the prev-link, but surely it should be less
663  * than the record's own address.
664  */
665  if (!(record->xl_prev < RecPtr))
666  {
667  report_invalid_record(state,
668  "record with incorrect prev-link %X/%X at %X/%X",
669  (uint32) (record->xl_prev >> 32),
670  (uint32) record->xl_prev,
671  (uint32) (RecPtr >> 32), (uint32) RecPtr);
672  return false;
673  }
674  }
675  else
676  {
677  /*
678  * Record's prev-link should exactly match our previous location. This
679  * check guards against torn WAL pages where a stale but valid-looking
680  * WAL record starts on a sector boundary.
681  */
682  if (record->xl_prev != PrevRecPtr)
683  {
684  report_invalid_record(state,
685  "record with incorrect prev-link %X/%X at %X/%X",
686  (uint32) (record->xl_prev >> 32),
687  (uint32) record->xl_prev,
688  (uint32) (RecPtr >> 32), (uint32) RecPtr);
689  return false;
690  }
691  }
692 
693  return true;
694 }
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:53
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

◆ XLogReaderAllocate()

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

Definition at line 70 of file xlogreader.c.

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

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

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

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)

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

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

◆ XLogReaderInvalReadState()

void XLogReaderInvalReadState ( XLogReaderState state)

Definition at line 625 of file xlogreader.c.

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

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

626 {
627  state->readSegNo = 0;
628  state->readOff = 0;
629  state->readLen = 0;
630 }
XLogSegNo readSegNo
Definition: xlogreader.h:160

◆ XLogReaderValidatePageHeader()

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

Definition at line 737 of file xlogreader.c.

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

Referenced by ReadPageInternal(), and XLogPageRead().

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

◆ XLogReadRecord()

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

Definition at line 217 of file xlogreader.c.

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

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

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

◆ XLogRecGetBlockData()

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

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

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

◆ XLogRecGetBlockTag()

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

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

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

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 1454 of file xlogreader.c.

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

1455 {
1456  TransactionId xid,
1457  next_xid;
1458  uint32 epoch;
1459 
1460  /*
1461  * This function is only safe during replay, because it depends on the
1462  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1463  */
1465 
1466  xid = XLogRecGetXid(record);
1469 
1470  /*
1471  * If xid is numerically greater than next_xid, it has to be from the
1472  * last epoch.
1473  */
1474  if (unlikely(xid > next_xid))
1475  --epoch;
1476 
1477  return FullTransactionIdFromEpochAndXid(epoch, xid);
1478 }
#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:235
#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