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)
 
void XLogBeginRead (XLogReaderState *state, XLogRecPtr RecPtr)
 
XLogRecordXLogReadRecord (XLogReaderState *state, 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:207
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:367
#define AllocSizeIsValid(size)
Definition: memutils.h:42
uint32 readRecordBufSize
Definition: xlogreader.h:208
#define Max(x, y)
Definition: c.h:914

◆ DecodeXLogRecord()

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

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

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

◆ ReadPageInternal()

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

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

569 {
570  int readLen;
571  uint32 targetPageOff;
572  XLogSegNo targetSegNo;
573  XLogPageHeader hdr;
574 
575  Assert((pageptr % XLOG_BLCKSZ) == 0);
576 
577  XLByteToSeg(pageptr, targetSegNo, state->segcxt.ws_segsize);
578  targetPageOff = XLogSegmentOffset(pageptr, state->segcxt.ws_segsize);
579 
580  /* check whether we have all the requested data already */
581  if (targetSegNo == state->seg.ws_segno &&
582  targetPageOff == state->segoff && reqLen <= state->readLen)
583  return state->readLen;
584 
585  /*
586  * Data is not in our buffer.
587  *
588  * Every time we actually read the page, even if we looked at parts of it
589  * before, we need to do verification as the read_page callback might now
590  * be rereading data from a different source.
591  *
592  * Whenever switching to a new WAL segment, we read the first page of the
593  * file and validate its header, even if that's not where the target
594  * record is. This is so that we can check the additional identification
595  * info that is present in the first page's "long" header.
596  */
597  if (targetSegNo != state->seg.ws_segno && targetPageOff != 0)
598  {
599  XLogRecPtr targetSegmentPtr = pageptr - targetPageOff;
600 
601  readLen = state->read_page(state, targetSegmentPtr, XLOG_BLCKSZ,
602  state->currRecPtr,
603  state->readBuf);
604  if (readLen < 0)
605  goto err;
606 
607  /* we can be sure to have enough WAL available, we scrolled back */
608  Assert(readLen == XLOG_BLCKSZ);
609 
610  if (!XLogReaderValidatePageHeader(state, targetSegmentPtr,
611  state->readBuf))
612  goto err;
613  }
614 
615  /*
616  * First, read the requested data length, but at least a short page header
617  * so that we can validate it.
618  */
619  readLen = state->read_page(state, pageptr, Max(reqLen, SizeOfXLogShortPHD),
620  state->currRecPtr,
621  state->readBuf);
622  if (readLen < 0)
623  goto err;
624 
625  Assert(readLen <= XLOG_BLCKSZ);
626 
627  /* Do we have enough data to check the header length? */
628  if (readLen <= SizeOfXLogShortPHD)
629  goto err;
630 
631  Assert(readLen >= reqLen);
632 
633  hdr = (XLogPageHeader) state->readBuf;
634 
635  /* still not enough */
636  if (readLen < XLogPageHeaderSize(hdr))
637  {
638  readLen = state->read_page(state, pageptr, XLogPageHeaderSize(hdr),
639  state->currRecPtr,
640  state->readBuf);
641  if (readLen < 0)
642  goto err;
643  }
644 
645  /*
646  * Now that we know we have the full header, validate it.
647  */
648  if (!XLogReaderValidatePageHeader(state, pageptr, (char *) hdr))
649  goto err;
650 
651  /* update read state information */
652  state->seg.ws_segno = targetSegNo;
653  state->segoff = targetPageOff;
654  state->readLen = readLen;
655 
656  return readLen;
657 
658 err:
660  return -1;
661 }
XLogPageReadCB read_page
Definition: xlogreader.h:116
#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:779
WALOpenSegment seg
Definition: xlogreader.h:172
static void XLogReaderInvalReadState(XLogReaderState *state)
Definition: xlogreader.c:667
uint64 XLogSegNo
Definition: xlogdefs.h:41
XLogSegNo ws_segno
Definition: xlogreader.h:40
unsigned int uint32
Definition: c.h:367
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
XLogRecPtr currRecPtr
Definition: xlogreader.h:183
#define Max(x, y)
Definition: c.h:914
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:738
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
WALSegmentContext segcxt
Definition: xlogreader.h:171
static uint32 readLen
Definition: xlog.c:796
#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:211
#define _(x)
Definition: elog.c:87

◆ ResetDecoder()

static void ResetDecoder ( XLogReaderState state)
static

Definition at line 1140 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(), XLogBeginRead(), and XLogReadRecord().

1141 {
1142  int block_id;
1143 
1144  state->decoded_record = NULL;
1145 
1146  state->main_data_len = 0;
1147 
1148  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1149  {
1150  state->blocks[block_id].in_use = false;
1151  state->blocks[block_id].has_image = false;
1152  state->blocks[block_id].has_data = false;
1153  state->blocks[block_id].apply_image = false;
1154  }
1155  state->max_block_id = -1;
1156 }
uint32 main_data_len
Definition: xlogreader.h:148
XLogRecord * decoded_record
Definition: xlogreader.h:145
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:154

◆ RestoreBlockImage()

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

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

1535 {
1536  DecodedBkpBlock *bkpb;
1537  char *ptr;
1538  PGAlignedBlock tmp;
1539 
1540  if (!record->blocks[block_id].in_use)
1541  return false;
1542  if (!record->blocks[block_id].has_image)
1543  return false;
1544 
1545  bkpb = &record->blocks[block_id];
1546  ptr = bkpb->bkp_image;
1547 
1548  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1549  {
1550  /* If a backup block image is compressed, decompress it */
1551  if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
1552  BLCKSZ - bkpb->hole_length, true) < 0)
1553  {
1554  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1555  (uint32) (record->ReadRecPtr >> 32),
1556  (uint32) record->ReadRecPtr,
1557  block_id);
1558  return false;
1559  }
1560  ptr = tmp.data;
1561  }
1562 
1563  /* generate page, taking into account hole if necessary */
1564  if (bkpb->hole_length == 0)
1565  {
1566  memcpy(page, ptr, BLCKSZ);
1567  }
1568  else
1569  {
1570  memcpy(page, ptr, bkpb->hole_offset);
1571  /* must zero-fill the hole */
1572  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1573  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1574  ptr + bkpb->hole_offset,
1575  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1576  }
1577 
1578  return true;
1579 }
uint16 hole_offset
Definition: xlogreader.h:77
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:971
uint16 bimg_len
Definition: xlogreader.h:79
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
char data[BLCKSZ]
Definition: c.h:1100
uint16 hole_length
Definition: xlogreader.h:78
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:134
unsigned int uint32
Definition: c.h:367
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
char * bkp_image
Definition: xlogreader.h:76
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:154

◆ ValidXLogRecord()

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

Definition at line 750 of file xlogreader.c.

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

Referenced by XLogReadRecord().

751 {
752  pg_crc32c crc;
753 
754  /* Calculate the CRC */
755  INIT_CRC32C(crc);
756  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
757  /* include the record header last */
758  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
759  FIN_CRC32C(crc);
760 
761  if (!EQ_CRC32C(record->xl_crc, crc))
762  {
763  report_invalid_record(state,
764  "incorrect resource manager data checksum in record at %X/%X",
765  (uint32) (recptr >> 32), (uint32) recptr);
766  return false;
767  }
768 
769  return true;
770 }
#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:367
#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:661

◆ ValidXLogRecordHeader()

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

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

684 {
685  if (record->xl_tot_len < SizeOfXLogRecord)
686  {
687  report_invalid_record(state,
688  "invalid record length at %X/%X: wanted %u, got %u",
689  (uint32) (RecPtr >> 32), (uint32) RecPtr,
690  (uint32) SizeOfXLogRecord, record->xl_tot_len);
691  return false;
692  }
693  if (record->xl_rmid > RM_MAX_ID)
694  {
695  report_invalid_record(state,
696  "invalid resource manager ID %u at %X/%X",
697  record->xl_rmid, (uint32) (RecPtr >> 32),
698  (uint32) RecPtr);
699  return false;
700  }
701  if (randAccess)
702  {
703  /*
704  * We can't exactly verify the prev-link, but surely it should be less
705  * than the record's own address.
706  */
707  if (!(record->xl_prev < RecPtr))
708  {
709  report_invalid_record(state,
710  "record with incorrect prev-link %X/%X at %X/%X",
711  (uint32) (record->xl_prev >> 32),
712  (uint32) record->xl_prev,
713  (uint32) (RecPtr >> 32), (uint32) RecPtr);
714  return false;
715  }
716  }
717  else
718  {
719  /*
720  * Record's prev-link should exactly match our previous location. This
721  * check guards against torn WAL pages where a stale but valid-looking
722  * WAL record starts on a sector boundary.
723  */
724  if (record->xl_prev != PrevRecPtr)
725  {
726  report_invalid_record(state,
727  "record with incorrect prev-link %X/%X at %X/%X",
728  (uint32) (record->xl_prev >> 32),
729  (uint32) record->xl_prev,
730  (uint32) (RecPtr >> 32), (uint32) RecPtr);
731  return false;
732  }
733  }
734 
735  return true;
736 }
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:367
#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:47
#define MAXPGPATH
XLogSegNo ws_segno
Definition: xlogreader.h:40
TimeLineID ws_tli
Definition: xlogreader.h:41
#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 1054 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().

1057 {
1058  char *p;
1059  XLogRecPtr recptr;
1060  Size nbytes;
1061 
1062  p = buf;
1063  recptr = startptr;
1064  nbytes = count;
1065 
1066  while (nbytes > 0)
1067  {
1068  uint32 startoff;
1069  int segbytes;
1070  int readbytes;
1071 
1072  startoff = XLogSegmentOffset(recptr, segcxt->ws_segsize);
1073 
1074  /*
1075  * If the data we want is not in a segment we have open, close what we
1076  * have (if anything) and open the next one, using the caller's
1077  * provided openSegment callback.
1078  */
1079  if (seg->ws_file < 0 ||
1080  !XLByteInSeg(recptr, seg->ws_segno, segcxt->ws_segsize) ||
1081  tli != seg->ws_tli)
1082  {
1083  XLogSegNo nextSegNo;
1084 
1085  if (seg->ws_file >= 0)
1086  close(seg->ws_file);
1087 
1088  XLByteToSeg(recptr, nextSegNo, segcxt->ws_segsize);
1089  seg->ws_file = openSegment(nextSegNo, segcxt, &tli);
1090 
1091  /* Update the current segment info. */
1092  seg->ws_tli = tli;
1093  seg->ws_segno = nextSegNo;
1094  }
1095 
1096  /* How many bytes are within this segment? */
1097  if (nbytes > (segcxt->ws_segsize - startoff))
1098  segbytes = segcxt->ws_segsize - startoff;
1099  else
1100  segbytes = nbytes;
1101 
1102 #ifndef FRONTEND
1104 #endif
1105 
1106  /* Reset errno first; eases reporting non-errno-affecting errors */
1107  errno = 0;
1108  readbytes = pg_pread(seg->ws_file, p, segbytes, (off_t) startoff);
1109 
1110 #ifndef FRONTEND
1112 #endif
1113 
1114  if (readbytes <= 0)
1115  {
1116  errinfo->wre_errno = errno;
1117  errinfo->wre_req = segbytes;
1118  errinfo->wre_read = readbytes;
1119  errinfo->wre_off = startoff;
1120  errinfo->wre_seg = *seg;
1121  return false;
1122  }
1123 
1124  /* Update state for read */
1125  recptr += readbytes;
1126  nbytes -= readbytes;
1127  p += readbytes;
1128  }
1129 
1130  return true;
1131 }
WALOpenSegment wre_seg
Definition: xlogreader.h:269
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:40
unsigned int uint32
Definition: c.h:367
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1344
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
size_t Size
Definition: c.h:466
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1320
TimeLineID ws_tli
Definition: xlogreader.h:41
#define close(a)
Definition: win32.h:12
static XLogRecPtr startptr
Definition: basebackup.c:116
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ XLogBeginRead()

void XLogBeginRead ( XLogReaderState state,
XLogRecPtr  RecPtr 
)

Definition at line 233 of file xlogreader.c.

References Assert, XLogReaderState::EndRecPtr, InvalidXLogRecPtr, XLogReaderState::ReadRecPtr, ResetDecoder(), and XLogRecPtrIsInvalid.

Referenced by DecodingContextFindStartpoint(), extractPageMap(), findLastCheckpoint(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), ReadCheckpointRecord(), readOneRecord(), StartLogicalReplication(), StartupXLOG(), XLogReaderValidatePageHeader(), and XlogReadTwoPhaseData().

234 {
235  Assert(!XLogRecPtrIsInvalid(RecPtr));
236 
237  ResetDecoder(state);
238 
239  /* Begin at the passed-in record pointer. */
240  state->EndRecPtr = RecPtr;
241  state->ReadRecPtr = InvalidXLogRecPtr;
242 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr EndRecPtr
Definition: xlogreader.h:135
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:134
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define Assert(condition)
Definition: c.h:738
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1140

◆ 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:116
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:127
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:49
WALOpenSegment seg
Definition: xlogreader.h:172
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:171
char * errormsg_buf
Definition: xlogreader.h:211

◆ 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:207
void pfree(void *pointer)
Definition: mcxt.c:1056
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:221
char * errormsg_buf
Definition: xlogreader.h:211
char * main_data
Definition: xlogreader.h:147
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:154

◆ XLogReaderInvalReadState()

static void XLogReaderInvalReadState ( XLogReaderState state)
static

Definition at line 667 of file xlogreader.c.

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

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

668 {
669  state->seg.ws_segno = 0;
670  state->segoff = 0;
671  state->readLen = 0;
672 }
WALOpenSegment seg
Definition: xlogreader.h:172
XLogSegNo ws_segno
Definition: xlogreader.h:40

◆ XLogReaderValidatePageHeader()

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

Definition at line 779 of file xlogreader.c.

References Assert, 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, XLogBeginRead(), 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().

781 {
782  XLogRecPtr recaddr;
783  XLogSegNo segno;
784  int32 offset;
785  XLogPageHeader hdr = (XLogPageHeader) phdr;
786 
787  Assert((recptr % XLOG_BLCKSZ) == 0);
788 
789  XLByteToSeg(recptr, segno, state->segcxt.ws_segsize);
790  offset = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
791 
792  XLogSegNoOffsetToRecPtr(segno, offset, state->segcxt.ws_segsize, recaddr);
793 
794  if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
795  {
796  char fname[MAXFNAMELEN];
797 
798  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
799 
800  report_invalid_record(state,
801  "invalid magic number %04X in log segment %s, offset %u",
802  hdr->xlp_magic,
803  fname,
804  offset);
805  return false;
806  }
807 
808  if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
809  {
810  char fname[MAXFNAMELEN];
811 
812  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
813 
814  report_invalid_record(state,
815  "invalid info bits %04X in log segment %s, offset %u",
816  hdr->xlp_info,
817  fname,
818  offset);
819  return false;
820  }
821 
822  if (hdr->xlp_info & XLP_LONG_HEADER)
823  {
824  XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
825 
826  if (state->system_identifier &&
827  longhdr->xlp_sysid != state->system_identifier)
828  {
829  report_invalid_record(state,
830  "WAL file is from different database system: WAL file database system identifier is %llu, pg_control database system identifier is %llu",
831  (unsigned long long) longhdr->xlp_sysid,
832  (unsigned long long) state->system_identifier);
833  return false;
834  }
835  else if (longhdr->xlp_seg_size != state->segcxt.ws_segsize)
836  {
837  report_invalid_record(state,
838  "WAL file is from different database system: incorrect segment size in page header");
839  return false;
840  }
841  else if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
842  {
843  report_invalid_record(state,
844  "WAL file is from different database system: incorrect XLOG_BLCKSZ in page header");
845  return false;
846  }
847  }
848  else if (offset == 0)
849  {
850  char fname[MAXFNAMELEN];
851 
852  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
853 
854  /* hmm, first page of file doesn't have a long header? */
855  report_invalid_record(state,
856  "invalid info bits %04X in log segment %s, offset %u",
857  hdr->xlp_info,
858  fname,
859  offset);
860  return false;
861  }
862 
863  /*
864  * Check that the address on the page agrees with what we expected. This
865  * check typically fails when an old WAL segment is recycled, and hasn't
866  * yet been overwritten with new data yet.
867  */
868  if (hdr->xlp_pageaddr != recaddr)
869  {
870  char fname[MAXFNAMELEN];
871 
872  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
873 
874  report_invalid_record(state,
875  "unexpected pageaddr %X/%X in log segment %s, offset %u",
876  (uint32) (hdr->xlp_pageaddr >> 32), (uint32) hdr->xlp_pageaddr,
877  fname,
878  offset);
879  return false;
880  }
881 
882  /*
883  * Since child timelines are always assigned a TLI greater than their
884  * immediate parent's TLI, we should never see TLI go backwards across
885  * successive pages of a consistent WAL sequence.
886  *
887  * Sometimes we re-read a segment that's already been (partially) read. So
888  * we only verify TLIs for pages that are later than the last remembered
889  * LSN.
890  */
891  if (recptr > state->latestPagePtr)
892  {
893  if (hdr->xlp_tli < state->latestPageTLI)
894  {
895  char fname[MAXFNAMELEN];
896 
897  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
898 
899  report_invalid_record(state,
900  "out-of-sequence timeline ID %u (after %u) in log segment %s, offset %u",
901  hdr->xlp_tli,
902  state->latestPageTLI,
903  fname,
904  offset);
905  return false;
906  }
907  }
908  state->latestPagePtr = recptr;
909  state->latestPageTLI = hdr->xlp_tli;
910 
911  return true;
912 }
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:355
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:74
WALOpenSegment seg
Definition: xlogreader.h:172
XLogRecPtr latestPagePtr
Definition: xlogreader.h:179
#define XLOG_PAGE_MAGIC
Definition: xlog_internal.h:34
uint64 XLogSegNo
Definition: xlogdefs.h:41
unsigned int uint32
Definition: c.h:367
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:738
#define XLP_LONG_HEADER
Definition: xlog_internal.h:79
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
TimeLineID ws_tli
Definition: xlogreader.h:41
uint64 system_identifier
Definition: xlogreader.h:122
WALSegmentContext segcxt
Definition: xlogreader.h:171
TimeLineID latestPageTLI
Definition: xlogreader.h:180
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ XLogReadRecord()

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

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

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

◆ XLogRecGetBlockData()

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

Definition at line 1505 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_dedup(), btree_xlog_delete(), 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().

1506 {
1507  DecodedBkpBlock *bkpb;
1508 
1509  if (!record->blocks[block_id].in_use)
1510  return NULL;
1511 
1512  bkpb = &record->blocks[block_id];
1513 
1514  if (!bkpb->has_data)
1515  {
1516  if (len)
1517  *len = 0;
1518  return NULL;
1519  }
1520  else
1521  {
1522  if (len)
1523  *len = bkpb->data_len;
1524  return bkpb->data;
1525  }
1526 }
uint16 data_len
Definition: xlogreader.h:85
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:154

◆ XLogRecGetBlockTag()

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

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

1483 {
1484  DecodedBkpBlock *bkpb;
1485 
1486  if (!record->blocks[block_id].in_use)
1487  return false;
1488 
1489  bkpb = &record->blocks[block_id];
1490  if (rnode)
1491  *rnode = bkpb->rnode;
1492  if (forknum)
1493  *forknum = bkpb->forknum;
1494  if (blknum)
1495  *blknum = bkpb->blkno;
1496  return true;
1497 }
BlockNumber blkno
Definition: xlogreader.h:68
ForkNumber forknum
Definition: xlogreader.h:67
RelFileNode rnode
Definition: xlogreader.h:66
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:154

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 1587 of file xlogreader.c.

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

1588 {
1589  TransactionId xid,
1590  next_xid;
1591  uint32 epoch;
1592 
1593  /*
1594  * This function is only safe during replay, because it depends on the
1595  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1596  */
1598 
1599  xid = XLogRecGetXid(record);
1602 
1603  /*
1604  * If xid is numerically greater than next_xid, it has to be from the
1605  * last epoch.
1606  */
1607  if (unlikely(xid > next_xid))
1608  --epoch;
1609 
1610  return FullTransactionIdFromEpochAndXid(epoch, xid);
1611 }
#define AmStartupProcess()
Definition: miscadmin.h:410
uint32 TransactionId
Definition: c.h:513
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:367
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:286
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:738
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:206