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)
 
static void WALOpenSegmentInit (WALOpenSegment *seg, WALSegmentContext *segcxt, int segsize, const char *waldir)
 
XLogReaderStateXLogReaderAllocate (int wal_segment_size, const char *waldir, XLogReaderRoutine *routine, void *private_data)
 
void XLogReaderFree (XLogReaderState *state)
 
void XLogBeginRead (XLogReaderState *state, XLogRecPtr RecPtr)
 
XLogRecordXLogReadRecord (XLogReaderState *state, char **errormsg)
 
bool XLogReaderValidatePageHeader (XLogReaderState *state, XLogRecPtr recptr, char *phdr)
 
bool WALRead (XLogReaderState *state, char *buf, XLogRecPtr startptr, Size count, TimeLineID tli, 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 51 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 173 of file xlogreader.c.

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

Referenced by XLogReaderAllocate(), and XLogReadRecord().

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

◆ DecodeXLogRecord()

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

Definition at line 1173 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, InvalidTransactionId, LSN_FORMAT_ARGS, 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, XLogReaderState::toplevel_xid, XLogRecord::xl_tot_len, XLR_BLOCK_ID_DATA_LONG, XLR_BLOCK_ID_DATA_SHORT, XLR_BLOCK_ID_ORIGIN, XLR_BLOCK_ID_TOPLEVEL_XID, and XLR_MAX_BLOCK_ID.

Referenced by XLogInsertRecord(), and XLogReadRecord().

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

◆ ReadPageInternal()

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

Definition at line 575 of file xlogreader.c.

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

Referenced by XLogReaderValidatePageHeader(), and XLogReadRecord().

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

◆ report_invalid_record()

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

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

59 {
60  va_list args;
61 
62  fmt = _(fmt);
63 
64  va_start(args, fmt);
65  vsnprintf(state->errormsg_buf, MAX_ERRORMSG_LEN, fmt, args);
66  va_end(args);
67 }
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:51
#define vsnprintf
Definition: port.h:215
char * errormsg_buf
Definition: xlogreader.h:254
#define _(x)
Definition: elog.c:89

◆ ResetDecoder()

static void ResetDecoder ( XLogReaderState state)
static

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

1149 {
1150  int block_id;
1151 
1152  state->decoded_record = NULL;
1153 
1154  state->main_data_len = 0;
1155 
1156  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1157  {
1158  state->blocks[block_id].in_use = false;
1159  state->blocks[block_id].has_image = false;
1160  state->blocks[block_id].has_data = false;
1161  state->blocks[block_id].apply_image = false;
1162  }
1163  state->max_block_id = -1;
1164 }
uint32 main_data_len
Definition: xlogreader.h:189
XLogRecord * decoded_record
Definition: xlogreader.h:186
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:197

◆ RestoreBlockImage()

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

Definition at line 1544 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, LSN_FORMAT_ARGS, MemSet, pglz_decompress(), XLogReaderState::ReadRecPtr, and report_invalid_record().

Referenced by checkXLogConsistency(), and XLogReadBufferForRedoExtended().

1545 {
1546  DecodedBkpBlock *bkpb;
1547  char *ptr;
1548  PGAlignedBlock tmp;
1549 
1550  if (!record->blocks[block_id].in_use)
1551  return false;
1552  if (!record->blocks[block_id].has_image)
1553  return false;
1554 
1555  bkpb = &record->blocks[block_id];
1556  ptr = bkpb->bkp_image;
1557 
1558  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1559  {
1560  /* If a backup block image is compressed, decompress it */
1561  if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
1562  BLCKSZ - bkpb->hole_length, true) < 0)
1563  {
1564  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1565  LSN_FORMAT_ARGS(record->ReadRecPtr),
1566  block_id);
1567  return false;
1568  }
1569  ptr = tmp.data;
1570  }
1571 
1572  /* generate page, taking into account hole if necessary */
1573  if (bkpb->hole_length == 0)
1574  {
1575  memcpy(page, ptr, BLCKSZ);
1576  }
1577  else
1578  {
1579  memcpy(page, ptr, bkpb->hole_offset);
1580  /* must zero-fill the hole */
1581  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1582  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1583  ptr + bkpb->hole_offset,
1584  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1585  }
1586 
1587  return true;
1588 }
uint16 hole_offset
Definition: xlogreader.h:135
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:58
#define MemSet(start, val, len)
Definition: c.h:1008
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
char data[BLCKSZ]
Definition: c.h:1141
uint16 hole_length
Definition: xlogreader.h:136
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
char * bkp_image
Definition: xlogreader.h:134
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:197

◆ ValidXLogRecord()

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

Definition at line 754 of file xlogreader.c.

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

Referenced by XLogReadRecord().

755 {
756  pg_crc32c crc;
757 
758  /* Calculate the CRC */
759  INIT_CRC32C(crc);
760  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
761  /* include the record header last */
762  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
763  FIN_CRC32C(crc);
764 
765  if (!EQ_CRC32C(record->xl_crc, crc))
766  {
767  report_invalid_record(state,
768  "incorrect resource manager data checksum in record at %X/%X",
769  LSN_FORMAT_ARGS(recptr));
770  return false;
771  }
772 
773  return true;
774 }
#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:58
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#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:727

◆ ValidXLogRecordHeader()

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

Definition at line 688 of file xlogreader.c.

References LSN_FORMAT_ARGS, report_invalid_record(), RM_MAX_ID, SizeOfXLogRecord, XLogRecord::xl_prev, XLogRecord::xl_rmid, and XLogRecord::xl_tot_len.

Referenced by XLogReadRecord().

691 {
692  if (record->xl_tot_len < SizeOfXLogRecord)
693  {
694  report_invalid_record(state,
695  "invalid record length at %X/%X: wanted %u, got %u",
696  LSN_FORMAT_ARGS(RecPtr),
697  (uint32) SizeOfXLogRecord, record->xl_tot_len);
698  return false;
699  }
700  if (record->xl_rmid > RM_MAX_ID)
701  {
702  report_invalid_record(state,
703  "invalid resource manager ID %u at %X/%X",
704  record->xl_rmid, LSN_FORMAT_ARGS(RecPtr));
705  return false;
706  }
707  if (randAccess)
708  {
709  /*
710  * We can't exactly verify the prev-link, but surely it should be less
711  * than the record's own address.
712  */
713  if (!(record->xl_prev < RecPtr))
714  {
715  report_invalid_record(state,
716  "record with incorrect prev-link %X/%X at %X/%X",
717  LSN_FORMAT_ARGS(record->xl_prev),
718  LSN_FORMAT_ARGS(RecPtr));
719  return false;
720  }
721  }
722  else
723  {
724  /*
725  * Record's prev-link should exactly match our previous location. This
726  * check guards against torn WAL pages where a stale but valid-looking
727  * WAL record starts on a sector boundary.
728  */
729  if (record->xl_prev != PrevRecPtr)
730  {
731  report_invalid_record(state,
732  "record with incorrect prev-link %X/%X at %X/%X",
733  LSN_FORMAT_ARGS(record->xl_prev),
734  LSN_FORMAT_ARGS(RecPtr));
735  return false;
736  }
737  }
738 
739  return true;
740 }
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:58
RmgrId xl_rmid
Definition: xlogrecord.h:47
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
uint32 xl_tot_len
Definition: xlogrecord.h:43
unsigned int uint32
Definition: c.h:441
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define RM_MAX_ID
Definition: rmgr.h:33

◆ WALOpenSegmentInit()

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

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

218 {
219  seg->ws_file = -1;
220  seg->ws_segno = 0;
221  seg->ws_tli = 0;
222 
223  segcxt->ws_segsize = segsize;
224  if (waldir)
225  snprintf(segcxt->ws_dir, MAXPGPATH, "%s", waldir);
226 }
char ws_dir[MAXPGPATH]
Definition: xlogreader.h:54
#define MAXPGPATH
XLogSegNo ws_segno
Definition: xlogreader.h:47
TimeLineID ws_tli
Definition: xlogreader.h:48
#define snprintf
Definition: port.h:216

◆ WALRead()

bool WALRead ( XLogReaderState state,
char *  buf,
XLogRecPtr  startptr,
Size  count,
TimeLineID  tli,
WALReadError errinfo 
)

Definition at line 1059 of file xlogreader.c.

References Assert, buf, pg_pread(), pgstat_report_wait_end(), pgstat_report_wait_start(), XLogReaderState::routine, XLogReaderState::seg, XLogReaderState::segcxt, XLogReaderRoutine::segment_close, XLogReaderRoutine::segment_open, 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().

1062 {
1063  char *p;
1064  XLogRecPtr recptr;
1065  Size nbytes;
1066 
1067  p = buf;
1068  recptr = startptr;
1069  nbytes = count;
1070 
1071  while (nbytes > 0)
1072  {
1073  uint32 startoff;
1074  int segbytes;
1075  int readbytes;
1076 
1077  startoff = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
1078 
1079  /*
1080  * If the data we want is not in a segment we have open, close what we
1081  * have (if anything) and open the next one, using the caller's
1082  * provided openSegment callback.
1083  */
1084  if (state->seg.ws_file < 0 ||
1085  !XLByteInSeg(recptr, state->seg.ws_segno, state->segcxt.ws_segsize) ||
1086  tli != state->seg.ws_tli)
1087  {
1088  XLogSegNo nextSegNo;
1089 
1090  if (state->seg.ws_file >= 0)
1091  state->routine.segment_close(state);
1092 
1093  XLByteToSeg(recptr, nextSegNo, state->segcxt.ws_segsize);
1094  state->routine.segment_open(state, nextSegNo, &tli);
1095 
1096  /* This shouldn't happen -- indicates a bug in segment_open */
1097  Assert(state->seg.ws_file >= 0);
1098 
1099  /* Update the current segment info. */
1100  state->seg.ws_tli = tli;
1101  state->seg.ws_segno = nextSegNo;
1102  }
1103 
1104  /* How many bytes are within this segment? */
1105  if (nbytes > (state->segcxt.ws_segsize - startoff))
1106  segbytes = state->segcxt.ws_segsize - startoff;
1107  else
1108  segbytes = nbytes;
1109 
1110 #ifndef FRONTEND
1112 #endif
1113 
1114  /* Reset errno first; eases reporting non-errno-affecting errors */
1115  errno = 0;
1116  readbytes = pg_pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
1117 
1118 #ifndef FRONTEND
1120 #endif
1121 
1122  if (readbytes <= 0)
1123  {
1124  errinfo->wre_errno = errno;
1125  errinfo->wre_req = segbytes;
1126  errinfo->wre_read = readbytes;
1127  errinfo->wre_off = startoff;
1128  errinfo->wre_seg = state->seg;
1129  return false;
1130  }
1131 
1132  /* Update state for read */
1133  recptr += readbytes;
1134  nbytes -= readbytes;
1135  p += readbytes;
1136  }
1137 
1138  return true;
1139 }
WALOpenSegment wre_seg
Definition: xlogreader.h:291
WALSegmentCloseCB segment_close
Definition: xlogreader.h:113
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)
WALOpenSegment seg
Definition: xlogreader.h:215
static char * buf
Definition: pg_test_fsync.c:68
uint64 XLogSegNo
Definition: xlogdefs.h:48
XLogSegNo ws_segno
Definition: xlogreader.h:47
unsigned int uint32
Definition: c.h:441
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1512
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1488
TimeLineID ws_tli
Definition: xlogreader.h:48
WALSegmentContext segcxt
Definition: xlogreader.h:214
WALSegmentOpenCB segment_open
Definition: xlogreader.h:107
XLogReaderRoutine routine
Definition: xlogreader.h:152
static XLogRecPtr startptr
Definition: basebackup.c:116
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ XLogBeginRead()

void XLogBeginRead ( XLogReaderState state,
XLogRecPtr  RecPtr 
)

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

241 {
242  Assert(!XLogRecPtrIsInvalid(RecPtr));
243 
244  ResetDecoder(state);
245 
246  /* Begin at the passed-in record pointer. */
247  state->EndRecPtr = RecPtr;
248  state->ReadRecPtr = InvalidXLogRecPtr;
249 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define Assert(condition)
Definition: c.h:804
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1148

◆ XLogReaderAllocate()

XLogReaderState* XLogReaderAllocate ( int  wal_segment_size,
const char *  waldir,
XLogReaderRoutine routine,
void *  private_data 
)

Definition at line 75 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::readBuf, XLogReaderState::routine, XLogReaderState::seg, XLogReaderState::segcxt, and WALOpenSegmentInit().

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

77 {
79 
80  state = (XLogReaderState *)
83  if (!state)
84  return NULL;
85 
86  /* initialize caller-provided support functions */
87  state->routine = *routine;
88 
89  state->max_block_id = -1;
90 
91  /*
92  * Permanently allocate readBuf. We do it this way, rather than just
93  * making a static array, for two reasons: (1) no need to waste the
94  * storage in most instantiations of the backend; (2) a static char array
95  * isn't guaranteed to have any particular alignment, whereas
96  * palloc_extended() will provide MAXALIGN'd storage.
97  */
98  state->readBuf = (char *) palloc_extended(XLOG_BLCKSZ,
100  if (!state->readBuf)
101  {
102  pfree(state);
103  return NULL;
104  }
105 
106  /* Initialize segment info. */
107  WALOpenSegmentInit(&state->seg, &state->segcxt, wal_segment_size,
108  waldir);
109 
110  /* system_identifier initialized to zeroes above */
111  state->private_data = private_data;
112  /* ReadRecPtr, EndRecPtr and readLen initialized to zeroes above */
115  if (!state->errormsg_buf)
116  {
117  pfree(state->readBuf);
118  pfree(state);
119  return NULL;
120  }
121  state->errormsg_buf[0] = '\0';
122 
123  /*
124  * Allocate an initial readRecordBuf of minimal size, which can later be
125  * enlarged if necessary.
126  */
127  if (!allocate_recordbuf(state, 0))
128  {
129  pfree(state->errormsg_buf);
130  pfree(state->readBuf);
131  pfree(state);
132  return NULL;
133  }
134 
135  return state;
136 }
static void WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt, int segsize, const char *waldir)
Definition: xlogreader.c:216
int wal_segment_size
Definition: xlog.c:118
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1014
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
void * private_data
Definition: xlogreader.h:168
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:51
WALOpenSegment seg
Definition: xlogreader.h:215
void pfree(void *pointer)
Definition: mcxt.c:1057
static void static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength)
Definition: xlogreader.c:173
Definition: regguts.h:317
#define MCXT_ALLOC_ZERO
Definition: fe_memutils.h:19
WALSegmentContext segcxt
Definition: xlogreader.h:214
char * errormsg_buf
Definition: xlogreader.h:254
XLogReaderRoutine routine
Definition: xlogreader.h:152

◆ XLogReaderFree()

void XLogReaderFree ( XLogReaderState state)

Definition at line 139 of file xlogreader.c.

References XLogReaderState::blocks, DecodedBkpBlock::data, XLogReaderState::errormsg_buf, XLogReaderState::main_data, pfree(), XLogReaderState::readBuf, XLogReaderState::readRecordBuf, XLogReaderState::routine, XLogReaderState::seg, XLogReaderRoutine::segment_close, WALOpenSegment::ws_file, and XLR_MAX_BLOCK_ID.

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

140 {
141  int block_id;
142 
143  if (state->seg.ws_file != -1)
144  state->routine.segment_close(state);
145 
146  for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
147  {
148  if (state->blocks[block_id].data)
149  pfree(state->blocks[block_id].data);
150  }
151  if (state->main_data)
152  pfree(state->main_data);
153 
154  pfree(state->errormsg_buf);
155  if (state->readRecordBuf)
156  pfree(state->readRecordBuf);
157  pfree(state->readBuf);
158  pfree(state);
159 }
WALSegmentCloseCB segment_close
Definition: xlogreader.h:113
char * readRecordBuf
Definition: xlogreader.h:250
WALOpenSegment seg
Definition: xlogreader.h:215
void pfree(void *pointer)
Definition: mcxt.c:1057
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:221
char * errormsg_buf
Definition: xlogreader.h:254
char * main_data
Definition: xlogreader.h:188
XLogReaderRoutine routine
Definition: xlogreader.h:152
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:197

◆ XLogReaderInvalReadState()

static void XLogReaderInvalReadState ( XLogReaderState state)
static

Definition at line 674 of file xlogreader.c.

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

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

675 {
676  state->seg.ws_segno = 0;
677  state->segoff = 0;
678  state->readLen = 0;
679 }
WALOpenSegment seg
Definition: xlogreader.h:215
XLogSegNo ws_segno
Definition: xlogreader.h:47

◆ XLogReaderValidatePageHeader()

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

Definition at line 783 of file xlogreader.c.

References Assert, header(), InvalidXLogRecPtr, XLogReaderState::latestPagePtr, XLogReaderState::latestPageTLI, LSN_FORMAT_ARGS, 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().

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

◆ XLogReadRecord()

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

Definition at line 268 of file xlogreader.c.

References allocate_recordbuf(), Assert, XLogReaderState::currRecPtr, DecodeXLogRecord(), XLogReaderState::EndRecPtr, XLogReaderState::errormsg_buf, InvalidXLogRecPtr, LSN_FORMAT_ARGS, 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().

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

◆ XLogRecGetBlockData()

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

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

1516 {
1517  DecodedBkpBlock *bkpb;
1518 
1519  if (!record->blocks[block_id].in_use)
1520  return NULL;
1521 
1522  bkpb = &record->blocks[block_id];
1523 
1524  if (!bkpb->has_data)
1525  {
1526  if (len)
1527  *len = 0;
1528  return NULL;
1529  }
1530  else
1531  {
1532  if (len)
1533  *len = bkpb->data_len;
1534  return bkpb->data;
1535  }
1536 }
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:197

◆ XLogRecGetBlockTag()

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

Definition at line 1491 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_block_info(), XLogDumpDisplayRecord(), and XLogReadBufferForRedoExtended().

1493 {
1494  DecodedBkpBlock *bkpb;
1495 
1496  if (!record->blocks[block_id].in_use)
1497  return false;
1498 
1499  bkpb = &record->blocks[block_id];
1500  if (rnode)
1501  *rnode = bkpb->rnode;
1502  if (forknum)
1503  *forknum = bkpb->forknum;
1504  if (blknum)
1505  *blknum = bkpb->blkno;
1506  return true;
1507 }
BlockNumber blkno
Definition: xlogreader.h:126
ForkNumber forknum
Definition: xlogreader.h:125
RelFileNode rnode
Definition: xlogreader.h:124
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:197

◆ XLogRecGetFullXid()

FullTransactionId XLogRecGetFullXid ( XLogReaderState record)

Definition at line 1596 of file xlogreader.c.

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

1597 {
1598  TransactionId xid,
1599  next_xid;
1600  uint32 epoch;
1601 
1602  /*
1603  * This function is only safe during replay, because it depends on the
1604  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1605  */
1607 
1608  xid = XLogRecGetXid(record);
1611 
1612  /*
1613  * If xid is numerically greater than next_xid, it has to be from the last
1614  * epoch.
1615  */
1616  if (unlikely(xid > next_xid))
1617  --epoch;
1618 
1619  return FullTransactionIdFromEpochAndXid(epoch, xid);
1620 }
#define AmStartupProcess()
Definition: miscadmin.h:432
uint32 TransactionId
Definition: c.h:587
FullTransactionId nextXid
Definition: transam.h:213
#define XidFromFullTransactionId(x)
Definition: transam.h:48
bool IsUnderPostmaster
Definition: globals.c:110
VariableCache ShmemVariableCache
Definition: varsup.c:34
unsigned int uint32
Definition: c.h:441
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:804
static FullTransactionId FullTransactionIdFromEpochAndXid(uint32 epoch, TransactionId xid)
Definition: transam.h:71
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
#define unlikely(x)
Definition: c.h:273