PostgreSQL Source Code  git master
xlogreader.c File Reference
#include "postgres.h"
#include <unistd.h>
#include "access/transam.h"
#include "access/xlog_internal.h"
#include "access/xlogreader.h"
#include "access/xlogrecord.h"
#include "catalog/pg_control.h"
#include "common/pg_lzcompress.h"
#include "replication/origin.h"
#include "miscadmin.h"
#include "pgstat.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 WALRead (char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, WALOpenSegment *seg, WALSegmentContext *segcxt, WALSegmentOpen openSegment, WALReadError *errinfo)
 
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:667

Referenced by DecodeXLogRecord().

◆ MAX_ERRORMSG_LEN

#define MAX_ERRORMSG_LEN   1000

Definition at line 49 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 166 of file xlogreader.c.

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

Referenced by XLogReaderAllocate(), and XLogReadRecord().

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

◆ DecodeXLogRecord()

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

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

1145 {
1146  /*
1147  * read next _size bytes from record buffer, but check for overrun first.
1148  */
1149 #define COPY_HEADER_FIELD(_dst, _size) \
1150  do { \
1151  if (remaining < _size) \
1152  goto shortdata_err; \
1153  memcpy(_dst, ptr, _size); \
1154  ptr += _size; \
1155  remaining -= _size; \
1156  } while(0)
1157 
1158  char *ptr;
1159  uint32 remaining;
1160  uint32 datatotal;
1161  RelFileNode *rnode = NULL;
1162  uint8 block_id;
1163 
1164  ResetDecoder(state);
1165 
1166  state->decoded_record = record;
1168 
1169  ptr = (char *) record;
1170  ptr += SizeOfXLogRecord;
1171  remaining = record->xl_tot_len - SizeOfXLogRecord;
1172 
1173  /* Decode the headers */
1174  datatotal = 0;
1175  while (remaining > datatotal)
1176  {
1177  COPY_HEADER_FIELD(&block_id, sizeof(uint8));
1178 
1179  if (block_id == XLR_BLOCK_ID_DATA_SHORT)
1180  {
1181  /* XLogRecordDataHeaderShort */
1182  uint8 main_data_len;
1183 
1184  COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
1185 
1186  state->main_data_len = main_data_len;
1187  datatotal += main_data_len;
1188  break; /* by convention, the main data fragment is
1189  * always last */
1190  }
1191  else if (block_id == XLR_BLOCK_ID_DATA_LONG)
1192  {
1193  /* XLogRecordDataHeaderLong */
1194  uint32 main_data_len;
1195 
1196  COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
1197  state->main_data_len = main_data_len;
1198  datatotal += main_data_len;
1199  break; /* by convention, the main data fragment is
1200  * always last */
1201  }
1202  else if (block_id == XLR_BLOCK_ID_ORIGIN)
1203  {
1204  COPY_HEADER_FIELD(&state->record_origin, sizeof(RepOriginId));
1205  }
1206  else if (block_id <= XLR_MAX_BLOCK_ID)
1207  {
1208  /* XLogRecordBlockHeader */
1209  DecodedBkpBlock *blk;
1210  uint8 fork_flags;
1211 
1212  if (block_id <= state->max_block_id)
1213  {
1214  report_invalid_record(state,
1215  "out-of-order block_id %u at %X/%X",
1216  block_id,
1217  (uint32) (state->ReadRecPtr >> 32),
1218  (uint32) state->ReadRecPtr);
1219  goto err;
1220  }
1221  state->max_block_id = block_id;
1222 
1223  blk = &state->blocks[block_id];
1224  blk->in_use = true;
1225  blk->apply_image = false;
1226 
1227  COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
1228  blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
1229  blk->flags = fork_flags;
1230  blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
1231  blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
1232 
1233  COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
1234  /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
1235  if (blk->has_data && blk->data_len == 0)
1236  {
1237  report_invalid_record(state,
1238  "BKPBLOCK_HAS_DATA set, but no data included at %X/%X",
1239  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1240  goto err;
1241  }
1242  if (!blk->has_data && blk->data_len != 0)
1243  {
1244  report_invalid_record(state,
1245  "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%X",
1246  (unsigned int) blk->data_len,
1247  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1248  goto err;
1249  }
1250  datatotal += blk->data_len;
1251 
1252  if (blk->has_image)
1253  {
1254  COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
1255  COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
1256  COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
1257 
1258  blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
1259 
1260  if (blk->bimg_info & BKPIMAGE_IS_COMPRESSED)
1261  {
1262  if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
1263  COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
1264  else
1265  blk->hole_length = 0;
1266  }
1267  else
1268  blk->hole_length = BLCKSZ - blk->bimg_len;
1269  datatotal += blk->bimg_len;
1270 
1271  /*
1272  * cross-check that hole_offset > 0, hole_length > 0 and
1273  * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
1274  */
1275  if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1276  (blk->hole_offset == 0 ||
1277  blk->hole_length == 0 ||
1278  blk->bimg_len == BLCKSZ))
1279  {
1280  report_invalid_record(state,
1281  "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%X",
1282  (unsigned int) blk->hole_offset,
1283  (unsigned int) blk->hole_length,
1284  (unsigned int) blk->bimg_len,
1285  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1286  goto err;
1287  }
1288 
1289  /*
1290  * cross-check that hole_offset == 0 and hole_length == 0 if
1291  * the HAS_HOLE flag is not set.
1292  */
1293  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1294  (blk->hole_offset != 0 || blk->hole_length != 0))
1295  {
1296  report_invalid_record(state,
1297  "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%X",
1298  (unsigned int) blk->hole_offset,
1299  (unsigned int) blk->hole_length,
1300  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1301  goto err;
1302  }
1303 
1304  /*
1305  * cross-check that bimg_len < BLCKSZ if the IS_COMPRESSED
1306  * flag is set.
1307  */
1308  if ((blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1309  blk->bimg_len == BLCKSZ)
1310  {
1311  report_invalid_record(state,
1312  "BKPIMAGE_IS_COMPRESSED set, but block image length %u at %X/%X",
1313  (unsigned int) blk->bimg_len,
1314  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1315  goto err;
1316  }
1317 
1318  /*
1319  * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE nor
1320  * IS_COMPRESSED flag is set.
1321  */
1322  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1323  !(blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1324  blk->bimg_len != BLCKSZ)
1325  {
1326  report_invalid_record(state,
1327  "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_IS_COMPRESSED set, but block image length is %u at %X/%X",
1328  (unsigned int) blk->data_len,
1329  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1330  goto err;
1331  }
1332  }
1333  if (!(fork_flags & BKPBLOCK_SAME_REL))
1334  {
1335  COPY_HEADER_FIELD(&blk->rnode, sizeof(RelFileNode));
1336  rnode = &blk->rnode;
1337  }
1338  else
1339  {
1340  if (rnode == NULL)
1341  {
1342  report_invalid_record(state,
1343  "BKPBLOCK_SAME_REL set but no previous rel at %X/%X",
1344  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1345  goto err;
1346  }
1347 
1348  blk->rnode = *rnode;
1349  }
1350  COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
1351  }
1352  else
1353  {
1354  report_invalid_record(state,
1355  "invalid block_id %u at %X/%X",
1356  block_id,
1357  (uint32) (state->ReadRecPtr >> 32),
1358  (uint32) state->ReadRecPtr);
1359  goto err;
1360  }
1361  }
1362 
1363  if (remaining != datatotal)
1364  goto shortdata_err;
1365 
1366  /*
1367  * Ok, we've parsed the fragment headers, and verified that the total
1368  * length of the payload in the fragments is equal to the amount of data
1369  * left. Copy the data of each fragment to a separate buffer.
1370  *
1371  * We could just set up pointers into readRecordBuf, but we want to align
1372  * the data for the convenience of the callers. Backup images are not
1373  * copied, however; they don't need alignment.
1374  */
1375 
1376  /* block data first */
1377  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1378  {
1379  DecodedBkpBlock *blk = &state->blocks[block_id];
1380 
1381  if (!blk->in_use)
1382  continue;
1383 
1384  Assert(blk->has_image || !blk->apply_image);
1385 
1386  if (blk->has_image)
1387  {
1388  blk->bkp_image = ptr;
1389  ptr += blk->bimg_len;
1390  }
1391  if (blk->has_data)
1392  {
1393  if (!blk->data || blk->data_len > blk->data_bufsz)
1394  {
1395  if (blk->data)
1396  pfree(blk->data);
1397 
1398  /*
1399  * Force the initial request to be BLCKSZ so that we don't
1400  * waste time with lots of trips through this stanza as a
1401  * result of WAL compression.
1402  */
1403  blk->data_bufsz = MAXALIGN(Max(blk->data_len, BLCKSZ));
1404  blk->data = palloc(blk->data_bufsz);
1405  }
1406  memcpy(blk->data, ptr, blk->data_len);
1407  ptr += blk->data_len;
1408  }
1409  }
1410 
1411  /* and finally, the main data */
1412  if (state->main_data_len > 0)
1413  {
1414  if (!state->main_data || state->main_data_len > state->main_data_bufsz)
1415  {
1416  if (state->main_data)
1417  pfree(state->main_data);
1418 
1419  /*
1420  * main_data_bufsz must be MAXALIGN'ed. In many xlog record
1421  * types, we omit trailing struct padding on-disk to save a few
1422  * bytes; but compilers may generate accesses to the xlog struct
1423  * that assume that padding bytes are present. If the palloc
1424  * request is not large enough to include such padding bytes then
1425  * we'll get valgrind complaints due to otherwise-harmless fetches
1426  * of the padding bytes.
1427  *
1428  * In addition, force the initial request to be reasonably large
1429  * so that we don't waste time with lots of trips through this
1430  * stanza. BLCKSZ / 2 seems like a good compromise choice.
1431  */
1432  state->main_data_bufsz = MAXALIGN(Max(state->main_data_len,
1433  BLCKSZ / 2));
1434  state->main_data = palloc(state->main_data_bufsz);
1435  }
1436  memcpy(state->main_data, ptr, state->main_data_len);
1437  ptr += state->main_data_len;
1438  }
1439 
1440  return true;
1441 
1442 shortdata_err:
1443  report_invalid_record(state,
1444  "record with invalid length at %X/%X",
1445  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1446 err:
1447  *errormsg = state->errormsg_buf;
1448 
1449  return false;
1450 }
int remaining
Definition: informix.c:667
BlockNumber blkno
Definition: xlogreader.h:66
#define BKPIMAGE_HAS_HOLE
Definition: xlogrecord.h:146
#define XLR_BLOCK_ID_DATA_LONG
Definition: xlogrecord.h:224
uint16 hole_offset
Definition: xlogreader.h:75
unsigned char uint8
Definition: c.h:357
uint16 RepOriginId
Definition: xlogdefs.h:58
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:56
uint32 BlockNumber
Definition: block.h:31
uint16 bimg_len
Definition: xlogreader.h:77
unsigned short uint16
Definition: c.h:358
void pfree(void *pointer)
Definition: mcxt.c:1056
uint16 hole_length
Definition: xlogreader.h:76
uint32 xl_tot_len
Definition: xlogrecord.h:43
uint32 main_data_len
Definition: xlogreader.h:145
#define BKPIMAGE_APPLY
Definition: xlogrecord.h:148
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:131
XLogRecord * decoded_record
Definition: xlogreader.h:142
#define COPY_HEADER_FIELD(_dst, _size)
unsigned int uint32
Definition: c.h:359
#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:65
uint16 data_len
Definition: xlogreader.h:83
#define Max(x, y)
Definition: c.h:905
#define Assert(condition)
Definition: c.h:739
uint16 data_bufsz
Definition: xlogreader.h:84
#define XLR_BLOCK_ID_ORIGIN
Definition: xlogrecord.h:225
#define MAXALIGN(LEN)
Definition: c.h:692
#define InvalidRepOriginId
Definition: origin.h:33
char * bkp_image
Definition: xlogreader.h:74
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:223
uint32 main_data_bufsz
Definition: xlogreader.h:146
#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:144
RelFileNode rnode
Definition: xlogreader.h:64
RepOriginId record_origin
Definition: xlogreader.h:148
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1119
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:151
#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, XLogReaderState::segoff, SizeOfXLogShortPHD, 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->segoff && 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->segoff = targetPageOff;
632  state->readLen = readLen;
633 
634  return readLen;
635 
636 err:
638  return -1;
639 }
XLogPageReadCB read_page
Definition: xlogreader.h:114
#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:169
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:359
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
XLogRecPtr currRecPtr
Definition: xlogreader.h:180
#define Max(x, y)
Definition: c.h:905
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:739
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
WALSegmentContext segcxt
Definition: xlogreader.h:168
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 56 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().

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

◆ ResetDecoder()

static void ResetDecoder ( XLogReaderState state)
static

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

1120 {
1121  int block_id;
1122 
1123  state->decoded_record = NULL;
1124 
1125  state->main_data_len = 0;
1126 
1127  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1128  {
1129  state->blocks[block_id].in_use = false;
1130  state->blocks[block_id].has_image = false;
1131  state->blocks[block_id].has_data = false;
1132  state->blocks[block_id].apply_image = false;
1133  }
1134  state->max_block_id = -1;
1135 }
uint32 main_data_len
Definition: xlogreader.h:145
XLogRecord * decoded_record
Definition: xlogreader.h:142
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:151

◆ RestoreBlockImage()

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

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

1514 {
1515  DecodedBkpBlock *bkpb;
1516  char *ptr;
1517  PGAlignedBlock tmp;
1518 
1519  if (!record->blocks[block_id].in_use)
1520  return false;
1521  if (!record->blocks[block_id].has_image)
1522  return false;
1523 
1524  bkpb = &record->blocks[block_id];
1525  ptr = bkpb->bkp_image;
1526 
1527  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1528  {
1529  /* If a backup block image is compressed, decompress it */
1530  if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
1531  BLCKSZ - bkpb->hole_length, true) < 0)
1532  {
1533  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1534  (uint32) (record->ReadRecPtr >> 32),
1535  (uint32) record->ReadRecPtr,
1536  block_id);
1537  return false;
1538  }
1539  ptr = tmp.data;
1540  }
1541 
1542  /* generate page, taking into account hole if necessary */
1543  if (bkpb->hole_length == 0)
1544  {
1545  memcpy(page, ptr, BLCKSZ);
1546  }
1547  else
1548  {
1549  memcpy(page, ptr, bkpb->hole_offset);
1550  /* must zero-fill the hole */
1551  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1552  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1553  ptr + bkpb->hole_offset,
1554  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1555  }
1556 
1557  return true;
1558 }
uint16 hole_offset
Definition: xlogreader.h:75
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:56
#define MemSet(start, val, len)
Definition: c.h:962
uint16 bimg_len
Definition: xlogreader.h:77
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
char data[BLCKSZ]
Definition: c.h:1091
uint16 hole_length
Definition: xlogreader.h:76
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:131
unsigned int uint32
Definition: c.h:359
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
char * bkp_image
Definition: xlogreader.h:74
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:151

◆ 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:56
unsigned int uint32
Definition: c.h:359
#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:662

◆ 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:56
RmgrId xl_rmid
Definition: xlogrecord.h:47
uint32 xl_tot_len
Definition: xlogrecord.h:43
unsigned int uint32
Definition: c.h:359
#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 209 of file xlogreader.c.

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

Referenced by XLogReaderAllocate().

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

◆ WALRead()

bool WALRead ( char *  buf,
XLogRecPtr  startptr,
Size  count,
TimeLineID  tli,
WALOpenSegment seg,
WALSegmentContext segcxt,
WALSegmentOpen  openSegment,
WALReadError errinfo 
)

Definition at line 1033 of file xlogreader.c.

References buf, close, pg_pread(), pgstat_report_wait_end(), pgstat_report_wait_start(), startptr, WAIT_EVENT_WAL_READ, WALReadError::wre_errno, WALReadError::wre_off, WALReadError::wre_read, WALReadError::wre_req, WALReadError::wre_seg, WALOpenSegment::ws_file, WALOpenSegment::ws_segno, WALSegmentContext::ws_segsize, WALOpenSegment::ws_tli, XLByteInSeg, XLByteToSeg, and XLogSegmentOffset.

Referenced by logical_read_xlog_page(), read_local_xlog_page(), WALDumpReadPage(), and XLogSendPhysical().

1036 {
1037  char *p;
1038  XLogRecPtr recptr;
1039  Size nbytes;
1040 
1041  p = buf;
1042  recptr = startptr;
1043  nbytes = count;
1044 
1045  while (nbytes > 0)
1046  {
1047  uint32 startoff;
1048  int segbytes;
1049  int readbytes;
1050 
1051  startoff = XLogSegmentOffset(recptr, segcxt->ws_segsize);
1052 
1053  /*
1054  * If the data we want is not in a segment we have open, close what we
1055  * have (if anything) and open the next one, using the caller's
1056  * provided openSegment callback.
1057  */
1058  if (seg->ws_file < 0 ||
1059  !XLByteInSeg(recptr, seg->ws_segno, segcxt->ws_segsize) ||
1060  tli != seg->ws_tli)
1061  {
1062  XLogSegNo nextSegNo;
1063 
1064  if (seg->ws_file >= 0)
1065  close(seg->ws_file);
1066 
1067  XLByteToSeg(recptr, nextSegNo, segcxt->ws_segsize);
1068  seg->ws_file = openSegment(nextSegNo, segcxt, &tli);
1069 
1070  /* Update the current segment info. */
1071  seg->ws_tli = tli;
1072  seg->ws_segno = nextSegNo;
1073  }
1074 
1075  /* How many bytes are within this segment? */
1076  if (nbytes > (segcxt->ws_segsize - startoff))
1077  segbytes = segcxt->ws_segsize - startoff;
1078  else
1079  segbytes = nbytes;
1080 
1081 #ifndef FRONTEND
1083 #endif
1084 
1085  /* Reset errno first; eases reporting non-errno-affecting errors */
1086  errno = 0;
1087  readbytes = pg_pread(seg->ws_file, p, segbytes, (off_t) startoff);
1088 
1089 #ifndef FRONTEND
1091 #endif
1092 
1093  if (readbytes <= 0)
1094  {
1095  errinfo->wre_errno = errno;
1096  errinfo->wre_req = segbytes;
1097  errinfo->wre_read = readbytes;
1098  errinfo->wre_off = startoff;
1099  errinfo->wre_seg = *seg;
1100  return false;
1101  }
1102 
1103  /* Update state for read */
1104  recptr += readbytes;
1105  nbytes -= readbytes;
1106  p += readbytes;
1107  }
1108 
1109  return true;
1110 }
WALOpenSegment wre_seg
Definition: xlogreader.h:264
ssize_t pg_pread(int fd, void *buf, size_t nbyte, off_t offset)
Definition: pread.c:27
#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes)
static char * buf
Definition: pg_test_fsync.c:67
uint64 XLogSegNo
Definition: xlogdefs.h:41
XLogSegNo ws_segno
Definition: xlogreader.h:38
unsigned int uint32
Definition: c.h:359
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1342
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
size_t Size
Definition: c.h:467
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1318
TimeLineID ws_tli
Definition: xlogreader.h:39
#define close(a)
Definition: win32.h:12
static XLogRecPtr startptr
Definition: basebackup.c:117
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ XLogReaderAllocate()

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

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

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

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)

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

136 {
137  int block_id;
138 
139  for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
140  {
141  if (state->blocks[block_id].data)
142  pfree(state->blocks[block_id].data);
143  }
144  if (state->main_data)
145  pfree(state->main_data);
146 
147  pfree(state->errormsg_buf);
148  if (state->readRecordBuf)
149  pfree(state->readRecordBuf);
150  pfree(state->readBuf);
151  pfree(state);
152 }
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:144
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:151

◆ XLogReaderInvalReadState()

static void XLogReaderInvalReadState ( XLogReaderState state)
static

Definition at line 645 of file xlogreader.c.

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

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

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

◆ 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:56
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
signed int int32
Definition: c.h:347
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:74
WALOpenSegment seg
Definition: xlogreader.h:169
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:359
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:739
#define XLP_LONG_HEADER
Definition: xlog_internal.h:79
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
TimeLineID ws_tli
Definition: xlogreader.h:39
uint64 system_identifier
Definition: xlogreader.h:120
WALSegmentContext segcxt
Definition: xlogreader.h:168
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 238 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().

239 {
240  XLogRecord *record;
241  XLogRecPtr targetPagePtr;
242  bool randAccess;
243  uint32 len,
244  total_len;
245  uint32 targetRecOff;
246  uint32 pageHeaderSize;
247  bool gotheader;
248  int readOff;
249 
250  /*
251  * randAccess indicates whether to verify the previous-record pointer of
252  * the record we're reading. We only do this if we're reading
253  * sequentially, which is what we initially assume.
254  */
255  randAccess = false;
256 
257  /* reset error state */
258  *errormsg = NULL;
259  state->errormsg_buf[0] = '\0';
260 
261  ResetDecoder(state);
262 
263  if (RecPtr == InvalidXLogRecPtr)
264  {
265  /* No explicit start point; read the record after the one we just read */
266  RecPtr = state->EndRecPtr;
267 
268  if (state->ReadRecPtr == InvalidXLogRecPtr)
269  randAccess = true;
270 
271  /*
272  * RecPtr is pointing to end+1 of the previous WAL record. If we're
273  * at a page boundary, no more records can fit on the current page. We
274  * must skip over the page header, but we can't do that until we've
275  * read in the page, since the header size is variable.
276  */
277  }
278  else
279  {
280  /*
281  * Caller supplied a position to start at.
282  *
283  * In this case, the passed-in record pointer should already be
284  * pointing to a valid record starting position.
285  */
286  Assert(XRecOffIsValid(RecPtr));
287  randAccess = true;
288  }
289 
290  state->currRecPtr = RecPtr;
291 
292  targetPagePtr = RecPtr - (RecPtr % XLOG_BLCKSZ);
293  targetRecOff = RecPtr % XLOG_BLCKSZ;
294 
295  /*
296  * Read the page containing the record into state->readBuf. Request enough
297  * byte to cover the whole record header, or at least the part of it that
298  * fits on the same page.
299  */
300  readOff = ReadPageInternal(state, 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:911
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:56
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:1144
static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
Definition: xlogreader.c:546
XLogRecPtr EndRecPtr
Definition: xlogreader.h:132
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:166
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:131
unsigned int uint32
Definition: c.h:359
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:739
#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:692
#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:168
char * errormsg_buf
Definition: xlogreader.h:208
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1119

◆ XLogRecGetBlockData()

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

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

1485 {
1486  DecodedBkpBlock *bkpb;
1487 
1488  if (!record->blocks[block_id].in_use)
1489  return NULL;
1490 
1491  bkpb = &record->blocks[block_id];
1492 
1493  if (!bkpb->has_data)
1494  {
1495  if (len)
1496  *len = 0;
1497  return NULL;
1498  }
1499  else
1500  {
1501  if (len)
1502  *len = bkpb->data_len;
1503  return bkpb->data;
1504  }
1505 }
uint16 data_len
Definition: xlogreader.h:83
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:151

◆ XLogRecGetBlockTag()

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

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

1462 {
1463  DecodedBkpBlock *bkpb;
1464 
1465  if (!record->blocks[block_id].in_use)
1466  return false;
1467 
1468  bkpb = &record->blocks[block_id];
1469  if (rnode)
1470  *rnode = bkpb->rnode;
1471  if (forknum)
1472  *forknum = bkpb->forknum;
1473  if (blknum)
1474  *blknum = bkpb->blkno;
1475  return true;
1476 }
BlockNumber blkno
Definition: xlogreader.h:66
ForkNumber forknum
Definition: xlogreader.h:65
RelFileNode rnode
Definition: xlogreader.h:64
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:151

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 1566 of file xlogreader.c.

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

1567 {
1568  TransactionId xid,
1569  next_xid;
1570  uint32 epoch;
1571 
1572  /*
1573  * This function is only safe during replay, because it depends on the
1574  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1575  */
1577 
1578  xid = XLogRecGetXid(record);
1581 
1582  /*
1583  * If xid is numerically greater than next_xid, it has to be from the
1584  * last epoch.
1585  */
1586  if (unlikely(xid > next_xid))
1587  --epoch;
1588 
1589  return FullTransactionIdFromEpochAndXid(epoch, xid);
1590 }
#define AmStartupProcess()
Definition: miscadmin.h:412
uint32 TransactionId
Definition: c.h:514
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:359
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:281
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:739
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