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: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:374
#define AllocSizeIsValid(size)
Definition: memutils.h:42
uint32 readRecordBufSize
Definition: xlogreader.h:251
#define Max(x, y)
Definition: c.h:921

◆ DecodeXLogRecord()

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

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

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

◆ ResetDecoder()

static void ResetDecoder ( XLogReaderState state)
static

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

1152 {
1153  int block_id;
1154 
1155  state->decoded_record = NULL;
1156 
1157  state->main_data_len = 0;
1158 
1159  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1160  {
1161  state->blocks[block_id].in_use = false;
1162  state->blocks[block_id].has_image = false;
1163  state->blocks[block_id].has_data = false;
1164  state->blocks[block_id].apply_image = false;
1165  }
1166  state->max_block_id = -1;
1167 }
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 1550 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().

1551 {
1552  DecodedBkpBlock *bkpb;
1553  char *ptr;
1554  PGAlignedBlock tmp;
1555 
1556  if (!record->blocks[block_id].in_use)
1557  return false;
1558  if (!record->blocks[block_id].has_image)
1559  return false;
1560 
1561  bkpb = &record->blocks[block_id];
1562  ptr = bkpb->bkp_image;
1563 
1564  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1565  {
1566  /* If a backup block image is compressed, decompress it */
1567  if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
1568  BLCKSZ - bkpb->hole_length, true) < 0)
1569  {
1570  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1571  (uint32) (record->ReadRecPtr >> 32),
1572  (uint32) record->ReadRecPtr,
1573  block_id);
1574  return false;
1575  }
1576  ptr = tmp.data;
1577  }
1578 
1579  /* generate page, taking into account hole if necessary */
1580  if (bkpb->hole_length == 0)
1581  {
1582  memcpy(page, ptr, BLCKSZ);
1583  }
1584  else
1585  {
1586  memcpy(page, ptr, bkpb->hole_offset);
1587  /* must zero-fill the hole */
1588  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1589  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1590  ptr + bkpb->hole_offset,
1591  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1592  }
1593 
1594  return true;
1595 }
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:949
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize, bool check_complete)
char data[BLCKSZ]
Definition: c.h:1082
uint16 hole_length
Definition: xlogreader.h:136
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:175
unsigned int uint32
Definition: c.h:374
#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 757 of file xlogreader.c.

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

Referenced by XLogReadRecord().

758 {
759  pg_crc32c crc;
760 
761  /* Calculate the CRC */
762  INIT_CRC32C(crc);
763  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
764  /* include the record header last */
765  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
766  FIN_CRC32C(crc);
767 
768  if (!EQ_CRC32C(record->xl_crc, crc))
769  {
770  report_invalid_record(state,
771  "incorrect resource manager data checksum in record at %X/%X",
772  (uint32) (recptr >> 32), (uint32) recptr);
773  return false;
774  }
775 
776  return true;
777 }
#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
unsigned int uint32
Definition: c.h:374
#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:668

◆ ValidXLogRecordHeader()

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

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

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  (uint32) (RecPtr >> 32), (uint32) 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, (uint32) (RecPtr >> 32),
705  (uint32) RecPtr);
706  return false;
707  }
708  if (randAccess)
709  {
710  /*
711  * We can't exactly verify the prev-link, but surely it should be less
712  * than the record's own address.
713  */
714  if (!(record->xl_prev < RecPtr))
715  {
716  report_invalid_record(state,
717  "record with incorrect prev-link %X/%X at %X/%X",
718  (uint32) (record->xl_prev >> 32),
719  (uint32) record->xl_prev,
720  (uint32) (RecPtr >> 32), (uint32) RecPtr);
721  return false;
722  }
723  }
724  else
725  {
726  /*
727  * Record's prev-link should exactly match our previous location. This
728  * check guards against torn WAL pages where a stale but valid-looking
729  * WAL record starts on a sector boundary.
730  */
731  if (record->xl_prev != PrevRecPtr)
732  {
733  report_invalid_record(state,
734  "record with incorrect prev-link %X/%X at %X/%X",
735  (uint32) (record->xl_prev >> 32),
736  (uint32) record->xl_prev,
737  (uint32) (RecPtr >> 32), (uint32) RecPtr);
738  return false;
739  }
740  }
741 
742  return true;
743 }
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
uint32 xl_tot_len
Definition: xlogrecord.h:43
unsigned int uint32
Definition: c.h:374
#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:193

◆ WALRead()

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

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

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

◆ 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:116
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:1013
#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:1056
static void static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength)
Definition: xlogreader.c:173
Definition: regguts.h:298
#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:1056
#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 786 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().

788 {
789  XLogRecPtr recaddr;
790  XLogSegNo segno;
791  int32 offset;
792  XLogPageHeader hdr = (XLogPageHeader) phdr;
793 
794  Assert((recptr % XLOG_BLCKSZ) == 0);
795 
796  XLByteToSeg(recptr, segno, state->segcxt.ws_segsize);
797  offset = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
798 
799  XLogSegNoOffsetToRecPtr(segno, offset, state->segcxt.ws_segsize, recaddr);
800 
801  if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
802  {
803  char fname[MAXFNAMELEN];
804 
805  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
806 
807  report_invalid_record(state,
808  "invalid magic number %04X in log segment %s, offset %u",
809  hdr->xlp_magic,
810  fname,
811  offset);
812  return false;
813  }
814 
815  if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
816  {
817  char fname[MAXFNAMELEN];
818 
819  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
820 
821  report_invalid_record(state,
822  "invalid info bits %04X in log segment %s, offset %u",
823  hdr->xlp_info,
824  fname,
825  offset);
826  return false;
827  }
828 
829  if (hdr->xlp_info & XLP_LONG_HEADER)
830  {
831  XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
832 
833  if (state->system_identifier &&
834  longhdr->xlp_sysid != state->system_identifier)
835  {
836  report_invalid_record(state,
837  "WAL file is from different database system: WAL file database system identifier is %llu, pg_control database system identifier is %llu",
838  (unsigned long long) longhdr->xlp_sysid,
839  (unsigned long long) state->system_identifier);
840  return false;
841  }
842  else if (longhdr->xlp_seg_size != state->segcxt.ws_segsize)
843  {
844  report_invalid_record(state,
845  "WAL file is from different database system: incorrect segment size in page header");
846  return false;
847  }
848  else if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
849  {
850  report_invalid_record(state,
851  "WAL file is from different database system: incorrect XLOG_BLCKSZ in page header");
852  return false;
853  }
854  }
855  else if (offset == 0)
856  {
857  char fname[MAXFNAMELEN];
858 
859  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
860 
861  /* hmm, first page of file doesn't have a long header? */
862  report_invalid_record(state,
863  "invalid info bits %04X in log segment %s, offset %u",
864  hdr->xlp_info,
865  fname,
866  offset);
867  return false;
868  }
869 
870  /*
871  * Check that the address on the page agrees with what we expected. This
872  * check typically fails when an old WAL segment is recycled, and hasn't
873  * yet been overwritten with new data yet.
874  */
875  if (hdr->xlp_pageaddr != recaddr)
876  {
877  char fname[MAXFNAMELEN];
878 
879  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
880 
881  report_invalid_record(state,
882  "unexpected pageaddr %X/%X in log segment %s, offset %u",
883  (uint32) (hdr->xlp_pageaddr >> 32), (uint32) hdr->xlp_pageaddr,
884  fname,
885  offset);
886  return false;
887  }
888 
889  /*
890  * Since child timelines are always assigned a TLI greater than their
891  * immediate parent's TLI, we should never see TLI go backwards across
892  * successive pages of a consistent WAL sequence.
893  *
894  * Sometimes we re-read a segment that's already been (partially) read. So
895  * we only verify TLIs for pages that are later than the last remembered
896  * LSN.
897  */
898  if (recptr > state->latestPagePtr)
899  {
900  if (hdr->xlp_tli < state->latestPageTLI)
901  {
902  char fname[MAXFNAMELEN];
903 
904  XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
905 
906  report_invalid_record(state,
907  "out-of-sequence timeline ID %u (after %u) in log segment %s, offset %u",
908  hdr->xlp_tli,
909  state->latestPageTLI,
910  fname,
911  offset);
912  return false;
913  }
914  }
915  state->latestPagePtr = recptr;
916  state->latestPageTLI = hdr->xlp_tli;
917 
918  return true;
919 }
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:58
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
signed int int32
Definition: c.h:362
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:74
WALOpenSegment seg
Definition: xlogreader.h:215
XLogRecPtr latestPagePtr
Definition: xlogreader.h:222
#define XLOG_PAGE_MAGIC
Definition: xlog_internal.h:34
uint64 XLogSegNo
Definition: xlogdefs.h:41
unsigned int uint32
Definition: c.h:374
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:745
#define XLP_LONG_HEADER
Definition: xlog_internal.h:79
#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, 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  (uint32) (RecPtr >> 32), (uint32) 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  (uint32) (RecPtr >> 32), (uint32) 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  (uint32) (RecPtr >> 32), (uint32) 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,
424  (uint32) (RecPtr >> 32), (uint32) RecPtr);
425  goto err;
426  }
427 
428  /* Copy the first fragment of the record from the first page. */
429  memcpy(state->readRecordBuf,
430  state->readBuf + RecPtr % XLOG_BLCKSZ, len);
431  buffer = state->readRecordBuf + len;
432  gotlen = len;
433 
434  do
435  {
436  /* Calculate pointer to beginning of next page */
437  targetPagePtr += XLOG_BLCKSZ;
438 
439  /* Wait for the next page to become available */
440  readOff = ReadPageInternal(state, targetPagePtr,
441  Min(total_len - gotlen + SizeOfXLogShortPHD,
442  XLOG_BLCKSZ));
443 
444  if (readOff < 0)
445  goto err;
446 
447  Assert(SizeOfXLogShortPHD <= readOff);
448 
449  /* Check that the continuation on next page looks valid */
450  pageHeader = (XLogPageHeader) state->readBuf;
451  if (!(pageHeader->xlp_info & XLP_FIRST_IS_CONTRECORD))
452  {
453  report_invalid_record(state,
454  "there is no contrecord flag at %X/%X",
455  (uint32) (RecPtr >> 32), (uint32) RecPtr);
456  goto err;
457  }
458 
459  /*
460  * Cross-check that xlp_rem_len agrees with how much of the record
461  * we expect there to be left.
462  */
463  if (pageHeader->xlp_rem_len == 0 ||
464  total_len != (pageHeader->xlp_rem_len + gotlen))
465  {
466  report_invalid_record(state,
467  "invalid contrecord length %u at %X/%X",
468  pageHeader->xlp_rem_len,
469  (uint32) (RecPtr >> 32), (uint32) 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:85
#define Min(x, y)
Definition: c.h:927
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:57
bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record, char **errormsg)
Definition: xlogreader.c:1176
static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
Definition: xlogreader.c:575
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
static uint32 readOff
Definition: xlog.c:813
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:374
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:745
#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:55
uint8 xl_info
Definition: xlogrecord.h:46
#define XLP_FIRST_IS_CONTRECORD
Definition: xlog_internal.h:77
#define MAXALIGN(LEN)
Definition: c.h:698
#define XLOG_SWITCH
Definition: pg_control.h:71
static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
Definition: xlogreader.c:757
#define XRecOffIsValid(xlrp)
WALSegmentContext segcxt
Definition: xlogreader.h:214
char * errormsg_buf
Definition: xlogreader.h:254
static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:1151

◆ XLogRecGetBlockData()

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

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

1522 {
1523  DecodedBkpBlock *bkpb;
1524 
1525  if (!record->blocks[block_id].in_use)
1526  return NULL;
1527 
1528  bkpb = &record->blocks[block_id];
1529 
1530  if (!bkpb->has_data)
1531  {
1532  if (len)
1533  *len = 0;
1534  return NULL;
1535  }
1536  else
1537  {
1538  if (len)
1539  *len = bkpb->data_len;
1540  return bkpb->data;
1541  }
1542 }
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 1497 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().

1499 {
1500  DecodedBkpBlock *bkpb;
1501 
1502  if (!record->blocks[block_id].in_use)
1503  return false;
1504 
1505  bkpb = &record->blocks[block_id];
1506  if (rnode)
1507  *rnode = bkpb->rnode;
1508  if (forknum)
1509  *forknum = bkpb->forknum;
1510  if (blknum)
1511  *blknum = bkpb->blkno;
1512  return true;
1513 }
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 1603 of file xlogreader.c.

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

1604 {
1605  TransactionId xid,
1606  next_xid;
1607  uint32 epoch;
1608 
1609  /*
1610  * This function is only safe during replay, because it depends on the
1611  * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
1612  */
1614 
1615  xid = XLogRecGetXid(record);
1618 
1619  /*
1620  * If xid is numerically greater than next_xid, it has to be from the last
1621  * epoch.
1622  */
1623  if (unlikely(xid > next_xid))
1624  --epoch;
1625 
1626  return FullTransactionIdFromEpochAndXid(epoch, xid);
1627 }
#define AmStartupProcess()
Definition: miscadmin.h:431
uint32 TransactionId
Definition: c.h:520
FullTransactionId nextXid
Definition: transam.h:213
#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:374
#define XLogRecGetXid(decoder)
Definition: xlogreader.h:307
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define Assert(condition)
Definition: c.h:745
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:206