PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xlogreader.h File Reference
Include dependency graph for xlogreader.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  DecodedBkpBlock
 
struct  XLogReaderState
 

Macros

#define XLogRecGetTotalLen(decoder)   ((decoder)->decoded_record->xl_tot_len)
 
#define XLogRecGetPrev(decoder)   ((decoder)->decoded_record->xl_prev)
 
#define XLogRecGetInfo(decoder)   ((decoder)->decoded_record->xl_info)
 
#define XLogRecGetRmid(decoder)   ((decoder)->decoded_record->xl_rmid)
 
#define XLogRecGetXid(decoder)   ((decoder)->decoded_record->xl_xid)
 
#define XLogRecGetOrigin(decoder)   ((decoder)->record_origin)
 
#define XLogRecGetData(decoder)   ((decoder)->main_data)
 
#define XLogRecGetDataLen(decoder)   ((decoder)->main_data_len)
 
#define XLogRecHasAnyBlockRefs(decoder)   ((decoder)->max_block_id >= 0)
 
#define XLogRecHasBlockRef(decoder, block_id)   ((decoder)->blocks[block_id].in_use)
 
#define XLogRecHasBlockImage(decoder, block_id)   ((decoder)->blocks[block_id].has_image)
 
#define XLogRecBlockImageApply(decoder, block_id)   ((decoder)->blocks[block_id].apply_image)
 

Typedefs

typedef struct XLogReaderState XLogReaderState
 
typedef int(* XLogPageReadCB )(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf, TimeLineID *pageTLI)
 

Functions

XLogReaderStateXLogReaderAllocate (int wal_segment_size, XLogPageReadCB pagereadfunc, void *private_data)
 
void XLogReaderFree (XLogReaderState *state)
 
struct XLogRecordXLogReadRecord (XLogReaderState *state, XLogRecPtr recptr, char **errormsg)
 
void XLogReaderInvalReadState (XLogReaderState *state)
 
bool DecodeXLogRecord (XLogReaderState *state, XLogRecord *record, char **errmsg)
 
bool RestoreBlockImage (XLogReaderState *recoder, uint8 block_id, char *dst)
 
char * XLogRecGetBlockData (XLogReaderState *record, uint8 block_id, Size *len)
 
bool XLogRecGetBlockTag (XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
 

Macro Definition Documentation

#define XLogRecBlockImageApply (   decoder,
  block_id 
)    ((decoder)->blocks[block_id].apply_image)
#define XLogRecGetData (   decoder)    ((decoder)->main_data)

Definition at line 226 of file xlogreader.h.

Referenced by brin_desc(), brin_xlog_createidx(), brin_xlog_desummarize_page(), brin_xlog_insert(), brin_xlog_revmap_extend(), brin_xlog_samepage_update(), brin_xlog_update(), btree_desc(), btree_xlog_delete(), btree_xlog_delete_get_latestRemovedXid(), btree_xlog_insert(), btree_xlog_mark_page_halfdead(), btree_xlog_newroot(), btree_xlog_reuse_page(), btree_xlog_split(), btree_xlog_unlink_page(), btree_xlog_vacuum(), clog_desc(), clog_redo(), commit_ts_desc(), commit_ts_redo(), dbase_desc(), dbase_redo(), DecodeDelete(), DecodeHeap2Op(), DecodeInsert(), DecodeLogicalMsgOp(), DecodeMultiInsert(), DecodeStandbyOp(), DecodeUpdate(), DecodeXactOp(), findLastCheckpoint(), generic_desc(), getRecordTimestamp(), gin_desc(), ginRedoCreatePTree(), ginRedoDeleteListPages(), ginRedoDeletePage(), ginRedoInsert(), ginRedoInsertListPage(), ginRedoSplit(), ginRedoUpdateMetapage(), gist_desc(), gistRedoPageSplitRecord(), gistRedoPageUpdateRecord(), hash_desc(), hash_xlog_add_ovfl_page(), hash_xlog_delete(), hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), hash_xlog_insert(), hash_xlog_move_page_contents(), hash_xlog_split_allocate_page(), hash_xlog_split_complete(), hash_xlog_squeeze_page(), hash_xlog_update_meta_page(), hash_xlog_vacuum_get_latestRemovedXid(), hash_xlog_vacuum_one_page(), heap2_desc(), heap_desc(), heap_xlog_clean(), heap_xlog_cleanup_info(), heap_xlog_confirm(), heap_xlog_delete(), heap_xlog_freeze_page(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_logical_rewrite(), heap_xlog_multi_insert(), heap_xlog_update(), heap_xlog_visible(), logicalmsg_desc(), multixact_desc(), multixact_redo(), recoveryStopsAfter(), recoveryStopsBefore(), relmap_desc(), relmap_redo(), replorigin_desc(), replorigin_redo(), seq_desc(), seq_redo(), smgr_desc(), smgr_redo(), spg_desc(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), spgRedoSplitTuple(), spgRedoVacuumLeaf(), spgRedoVacuumRedirect(), spgRedoVacuumRoot(), standby_desc(), standby_redo(), StartupXLOG(), tblspc_desc(), tblspc_redo(), xact_desc(), xact_redo(), xlog_desc(), xlog_redo(), and XlogReadTwoPhaseData().

#define XLogRecGetDataLen (   decoder)    ((decoder)->main_data_len)
#define XLogRecGetOrigin (   decoder)    ((decoder)->record_origin)
#define XLogRecGetPrev (   decoder)    ((decoder)->decoded_record->xl_prev)

Definition at line 221 of file xlogreader.h.

Referenced by XLogDumpDisplayRecord().

#define XLogRecGetTotalLen (   decoder)    ((decoder)->decoded_record->xl_tot_len)

Definition at line 220 of file xlogreader.h.

Referenced by XLogDumpDisplayRecord(), and XLogDumpRecordLen().

#define XLogRecHasAnyBlockRefs (   decoder)    ((decoder)->max_block_id >= 0)
#define XLogRecHasBlockImage (   decoder,
  block_id 
)    ((decoder)->blocks[block_id].has_image)
#define XLogRecHasBlockRef (   decoder,
  block_id 
)    ((decoder)->blocks[block_id].in_use)

Typedef Documentation

typedef int(* XLogPageReadCB)(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf, TimeLineID *pageTLI)

Definition at line 33 of file xlogreader.h.

Definition at line 30 of file xlogreader.h.

Function Documentation

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

Definition at line 1014 of file xlogreader.c.

References DecodedBkpBlock::apply_image, Assert, DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, DecodedBkpBlock::bkp_image, BKPBLOCK_FORK_MASK, BKPBLOCK_HAS_DATA, BKPBLOCK_HAS_IMAGE, BKPBLOCK_SAME_REL, BKPIMAGE_APPLY, BKPIMAGE_HAS_HOLE, BKPIMAGE_IS_COMPRESSED, DecodedBkpBlock::blkno, XLogReaderState::blocks, COPY_HEADER_FIELD, DecodedBkpBlock::data, DecodedBkpBlock::data_bufsz, DecodedBkpBlock::data_len, XLogReaderState::decoded_record, XLogReaderState::errormsg_buf, DecodedBkpBlock::flags, DecodedBkpBlock::forknum, DecodedBkpBlock::has_data, DecodedBkpBlock::has_image, DecodedBkpBlock::hole_length, DecodedBkpBlock::hole_offset, DecodedBkpBlock::in_use, InvalidRepOriginId, XLogReaderState::main_data, XLogReaderState::main_data_bufsz, XLogReaderState::main_data_len, XLogReaderState::max_block_id, palloc(), pfree(), XLogReaderState::ReadRecPtr, XLogReaderState::record_origin, remaining, report_invalid_record(), ResetDecoder(), DecodedBkpBlock::rnode, SizeOfXLogRecord, XLogRecord::xl_tot_len, XLR_BLOCK_ID_DATA_LONG, XLR_BLOCK_ID_DATA_SHORT, XLR_BLOCK_ID_ORIGIN, and XLR_MAX_BLOCK_ID.

Referenced by XLogInsertRecord(), and XLogReadRecord().

1015 {
1016  /*
1017  * read next _size bytes from record buffer, but check for overrun first.
1018  */
1019 #define COPY_HEADER_FIELD(_dst, _size) \
1020  do { \
1021  if (remaining < _size) \
1022  goto shortdata_err; \
1023  memcpy(_dst, ptr, _size); \
1024  ptr += _size; \
1025  remaining -= _size; \
1026  } while(0)
1027 
1028  char *ptr;
1029  uint32 remaining;
1030  uint32 datatotal;
1031  RelFileNode *rnode = NULL;
1032  uint8 block_id;
1033 
1034  ResetDecoder(state);
1035 
1036  state->decoded_record = record;
1038 
1039  ptr = (char *) record;
1040  ptr += SizeOfXLogRecord;
1041  remaining = record->xl_tot_len - SizeOfXLogRecord;
1042 
1043  /* Decode the headers */
1044  datatotal = 0;
1045  while (remaining > datatotal)
1046  {
1047  COPY_HEADER_FIELD(&block_id, sizeof(uint8));
1048 
1049  if (block_id == XLR_BLOCK_ID_DATA_SHORT)
1050  {
1051  /* XLogRecordDataHeaderShort */
1052  uint8 main_data_len;
1053 
1054  COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
1055 
1056  state->main_data_len = main_data_len;
1057  datatotal += main_data_len;
1058  break; /* by convention, the main data fragment is
1059  * always last */
1060  }
1061  else if (block_id == XLR_BLOCK_ID_DATA_LONG)
1062  {
1063  /* XLogRecordDataHeaderLong */
1064  uint32 main_data_len;
1065 
1066  COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
1067  state->main_data_len = main_data_len;
1068  datatotal += main_data_len;
1069  break; /* by convention, the main data fragment is
1070  * always last */
1071  }
1072  else if (block_id == XLR_BLOCK_ID_ORIGIN)
1073  {
1074  COPY_HEADER_FIELD(&state->record_origin, sizeof(RepOriginId));
1075  }
1076  else if (block_id <= XLR_MAX_BLOCK_ID)
1077  {
1078  /* XLogRecordBlockHeader */
1079  DecodedBkpBlock *blk;
1080  uint8 fork_flags;
1081 
1082  if (block_id <= state->max_block_id)
1083  {
1084  report_invalid_record(state,
1085  "out-of-order block_id %u at %X/%X",
1086  block_id,
1087  (uint32) (state->ReadRecPtr >> 32),
1088  (uint32) state->ReadRecPtr);
1089  goto err;
1090  }
1091  state->max_block_id = block_id;
1092 
1093  blk = &state->blocks[block_id];
1094  blk->in_use = true;
1095  blk->apply_image = false;
1096 
1097  COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
1098  blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
1099  blk->flags = fork_flags;
1100  blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
1101  blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
1102 
1103  COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
1104  /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
1105  if (blk->has_data && blk->data_len == 0)
1106  {
1107  report_invalid_record(state,
1108  "BKPBLOCK_HAS_DATA set, but no data included at %X/%X",
1109  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1110  goto err;
1111  }
1112  if (!blk->has_data && blk->data_len != 0)
1113  {
1114  report_invalid_record(state,
1115  "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%X",
1116  (unsigned int) blk->data_len,
1117  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1118  goto err;
1119  }
1120  datatotal += blk->data_len;
1121 
1122  if (blk->has_image)
1123  {
1124  COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
1125  COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
1126  COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
1127 
1128  blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
1129 
1130  if (blk->bimg_info & BKPIMAGE_IS_COMPRESSED)
1131  {
1132  if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
1133  COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
1134  else
1135  blk->hole_length = 0;
1136  }
1137  else
1138  blk->hole_length = BLCKSZ - blk->bimg_len;
1139  datatotal += blk->bimg_len;
1140 
1141  /*
1142  * cross-check that hole_offset > 0, hole_length > 0 and
1143  * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
1144  */
1145  if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1146  (blk->hole_offset == 0 ||
1147  blk->hole_length == 0 ||
1148  blk->bimg_len == BLCKSZ))
1149  {
1150  report_invalid_record(state,
1151  "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%X",
1152  (unsigned int) blk->hole_offset,
1153  (unsigned int) blk->hole_length,
1154  (unsigned int) blk->bimg_len,
1155  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1156  goto err;
1157  }
1158 
1159  /*
1160  * cross-check that hole_offset == 0 and hole_length == 0 if
1161  * the HAS_HOLE flag is not set.
1162  */
1163  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1164  (blk->hole_offset != 0 || blk->hole_length != 0))
1165  {
1166  report_invalid_record(state,
1167  "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%X",
1168  (unsigned int) blk->hole_offset,
1169  (unsigned int) blk->hole_length,
1170  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1171  goto err;
1172  }
1173 
1174  /*
1175  * cross-check that bimg_len < BLCKSZ if the IS_COMPRESSED
1176  * flag is set.
1177  */
1178  if ((blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1179  blk->bimg_len == BLCKSZ)
1180  {
1181  report_invalid_record(state,
1182  "BKPIMAGE_IS_COMPRESSED set, but block image length %u at %X/%X",
1183  (unsigned int) blk->bimg_len,
1184  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1185  goto err;
1186  }
1187 
1188  /*
1189  * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE nor
1190  * IS_COMPRESSED flag is set.
1191  */
1192  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1193  !(blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1194  blk->bimg_len != BLCKSZ)
1195  {
1196  report_invalid_record(state,
1197  "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_IS_COMPRESSED set, but block image length is %u at %X/%X",
1198  (unsigned int) blk->data_len,
1199  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1200  goto err;
1201  }
1202  }
1203  if (!(fork_flags & BKPBLOCK_SAME_REL))
1204  {
1205  COPY_HEADER_FIELD(&blk->rnode, sizeof(RelFileNode));
1206  rnode = &blk->rnode;
1207  }
1208  else
1209  {
1210  if (rnode == NULL)
1211  {
1212  report_invalid_record(state,
1213  "BKPBLOCK_SAME_REL set but no previous rel at %X/%X",
1214  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1215  goto err;
1216  }
1217 
1218  blk->rnode = *rnode;
1219  }
1220  COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
1221  }
1222  else
1223  {
1224  report_invalid_record(state,
1225  "invalid block_id %u at %X/%X",
1226  block_id,
1227  (uint32) (state->ReadRecPtr >> 32),
1228  (uint32) state->ReadRecPtr);
1229  goto err;
1230  }
1231  }
1232 
1233  if (remaining != datatotal)
1234  goto shortdata_err;
1235 
1236  /*
1237  * Ok, we've parsed the fragment headers, and verified that the total
1238  * length of the payload in the fragments is equal to the amount of data
1239  * left. Copy the data of each fragment to a separate buffer.
1240  *
1241  * We could just set up pointers into readRecordBuf, but we want to align
1242  * the data for the convenience of the callers. Backup images are not
1243  * copied, however; they don't need alignment.
1244  */
1245 
1246  /* block data first */
1247  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1248  {
1249  DecodedBkpBlock *blk = &state->blocks[block_id];
1250 
1251  if (!blk->in_use)
1252  continue;
1253 
1254  Assert(blk->has_image || !blk->apply_image);
1255 
1256  if (blk->has_image)
1257  {
1258  blk->bkp_image = ptr;
1259  ptr += blk->bimg_len;
1260  }
1261  if (blk->has_data)
1262  {
1263  if (!blk->data || blk->data_len > blk->data_bufsz)
1264  {
1265  if (blk->data)
1266  pfree(blk->data);
1267  blk->data_bufsz = blk->data_len;
1268  blk->data = palloc(blk->data_bufsz);
1269  }
1270  memcpy(blk->data, ptr, blk->data_len);
1271  ptr += blk->data_len;
1272  }
1273  }
1274 
1275  /* and finally, the main data */
1276  if (state->main_data_len > 0)
1277  {
1278  if (!state->main_data || state->main_data_len > state->main_data_bufsz)
1279  {
1280  if (state->main_data)
1281  pfree(state->main_data);
1282  state->main_data_bufsz = state->main_data_len;
1283  state->main_data = palloc(state->main_data_bufsz);
1284  }
1285  memcpy(state->main_data, ptr, state->main_data_len);
1286  ptr += state->main_data_len;
1287  }
1288 
1289  return true;
1290 
1291 shortdata_err:
1292  report_invalid_record(state,
1293  "record with invalid length at %X/%X",
1294  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1295 err:
1296  *errormsg = state->errormsg_buf;
1297 
1298  return false;
1299 }
int remaining
Definition: informix.c:692
BlockNumber blkno
Definition: xlogreader.h:48
#define BKPIMAGE_HAS_HOLE
Definition: xlogrecord.h:147
#define XLR_BLOCK_ID_DATA_LONG
Definition: xlogrecord.h:225
uint16 hole_offset
Definition: xlogreader.h:57
unsigned char uint8
Definition: c.h:256
uint16 RepOriginId
Definition: xlogdefs.h:51
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:50
uint32 BlockNumber
Definition: block.h:31
uint16 bimg_len
Definition: xlogreader.h:59
unsigned short uint16
Definition: c.h:257
void pfree(void *pointer)
Definition: mcxt.c:949
uint16 hole_length
Definition: xlogreader.h:58
uint32 xl_tot_len
Definition: xlogrecord.h:43
uint32 main_data_len
Definition: xlogreader.h:133
#define BKPIMAGE_APPLY
Definition: xlogrecord.h:149
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:119
XLogRecord * decoded_record
Definition: xlogreader.h:130
#define COPY_HEADER_FIELD(_dst, _size)
unsigned int uint32
Definition: c.h:258
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:222
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define BKPBLOCK_SAME_REL
Definition: xlogrecord.h:184
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:148
#define BKPBLOCK_HAS_IMAGE
Definition: xlogrecord.h:181
ForkNumber forknum
Definition: xlogreader.h:47
uint16 data_len
Definition: xlogreader.h:65
#define Assert(condition)
Definition: c.h:681
uint16 data_bufsz
Definition: xlogreader.h:66
#define XLR_BLOCK_ID_ORIGIN
Definition: xlogrecord.h:226
#define InvalidRepOriginId
Definition: origin.h:34
char * bkp_image
Definition: xlogreader.h:56
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:224
uint32 main_data_bufsz
Definition: xlogreader.h:134
#define BKPBLOCK_FORK_MASK
Definition: xlogrecord.h:179
void * palloc(Size size)
Definition: mcxt.c:848
char * errormsg_buf
Definition: xlogreader.h:193
char * main_data
Definition: xlogreader.h:132
RelFileNode rnode
Definition: xlogreader.h:46
RepOriginId record_origin
Definition: xlogreader.h:136
static void static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:989
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139
#define BKPBLOCK_HAS_DATA
Definition: xlogrecord.h:182
bool RestoreBlockImage ( XLogReaderState recoder,
uint8  block_id,
char *  dst 
)

Definition at line 1362 of file xlogreader.c.

References DecodedBkpBlock::bimg_info, DecodedBkpBlock::bimg_len, DecodedBkpBlock::bkp_image, BKPIMAGE_IS_COMPRESSED, XLogReaderState::blocks, 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().

1363 {
1364  DecodedBkpBlock *bkpb;
1365  char *ptr;
1366  char tmp[BLCKSZ];
1367 
1368  if (!record->blocks[block_id].in_use)
1369  return false;
1370  if (!record->blocks[block_id].has_image)
1371  return false;
1372 
1373  bkpb = &record->blocks[block_id];
1374  ptr = bkpb->bkp_image;
1375 
1376  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1377  {
1378  /* If a backup block image is compressed, decompress it */
1379  if (pglz_decompress(ptr, bkpb->bimg_len, tmp,
1380  BLCKSZ - bkpb->hole_length) < 0)
1381  {
1382  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1383  (uint32) (record->ReadRecPtr >> 32),
1384  (uint32) record->ReadRecPtr,
1385  block_id);
1386  return false;
1387  }
1388  ptr = tmp;
1389  }
1390 
1391  /* generate page, taking into account hole if necessary */
1392  if (bkpb->hole_length == 0)
1393  {
1394  memcpy(page, ptr, BLCKSZ);
1395  }
1396  else
1397  {
1398  memcpy(page, ptr, bkpb->hole_offset);
1399  /* must zero-fill the hole */
1400  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1401  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1402  ptr + bkpb->hole_offset,
1403  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1404  }
1405 
1406  return true;
1407 }
uint16 hole_offset
Definition: xlogreader.h:57
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:50
#define MemSet(start, val, len)
Definition: c.h:863
uint16 bimg_len
Definition: xlogreader.h:59
uint16 hole_length
Definition: xlogreader.h:58
unsigned int uint32
Definition: c.h:258
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:148
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize)
char * bkp_image
Definition: xlogreader.h:56
XLogReaderState* XLogReaderAllocate ( int  wal_segment_size,
XLogPageReadCB  pagereadfunc,
void *  private_data 
)

Definition at line 67 of file xlogreader.c.

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

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

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

Definition at line 127 of file xlogreader.c.

References XLogReaderState::blocks, DecodedBkpBlock::data, XLogReaderState::errormsg_buf, XLogReaderState::main_data, pfree(), XLogReaderState::readBuf, XLogReaderState::readRecordBuf, and XLR_MAX_BLOCK_ID.

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

128 {
129  int block_id;
130 
131  for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
132  {
133  if (state->blocks[block_id].data)
134  pfree(state->blocks[block_id].data);
135  }
136  if (state->main_data)
137  pfree(state->main_data);
138 
139  pfree(state->errormsg_buf);
140  if (state->readRecordBuf)
141  pfree(state->readRecordBuf);
142  pfree(state->readBuf);
143  pfree(state);
144 }
char * readRecordBuf
Definition: xlogreader.h:189
void pfree(void *pointer)
Definition: mcxt.c:949
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:222
char * errormsg_buf
Definition: xlogreader.h:193
char * main_data
Definition: xlogreader.h:132
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139
void XLogReaderInvalReadState ( XLogReaderState state)

Definition at line 606 of file xlogreader.c.

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

Referenced by ReadPageInternal(), and XLogReadRecord().

607 {
608  state->readSegNo = 0;
609  state->readOff = 0;
610  state->readLen = 0;
611 }
XLogSegNo readSegNo
Definition: xlogreader.h:156
struct XLogRecord* XLogReadRecord ( XLogReaderState state,
XLogRecPtr  recptr,
char **  errormsg 
)

Definition at line 195 of file xlogreader.c.

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

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

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

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

1334 {
1335  DecodedBkpBlock *bkpb;
1336 
1337  if (!record->blocks[block_id].in_use)
1338  return NULL;
1339 
1340  bkpb = &record->blocks[block_id];
1341 
1342  if (!bkpb->has_data)
1343  {
1344  if (len)
1345  *len = 0;
1346  return NULL;
1347  }
1348  else
1349  {
1350  if (len)
1351  *len = bkpb->data_len;
1352  return bkpb->data;
1353  }
1354 }
uint16 data_len
Definition: xlogreader.h:65
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139
bool XLogRecGetBlockTag ( XLogReaderState record,
uint8  block_id,
RelFileNode rnode,
ForkNumber forknum,
BlockNumber blknum 
)

Definition at line 1309 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_delete_get_latestRemovedXid(), btree_xlog_split(), btree_xlog_vacuum(), checkXLogConsistency(), DecodeDelete(), DecodeInsert(), DecodeMultiInsert(), DecodeSpecConfirm(), DecodeUpdate(), extractPageInfo(), gistRedoPageSplitRecord(), hash_xlog_add_ovfl_page(), hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), hash_xlog_vacuum_get_latestRemovedXid(), 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(), XLogDumpDisplayRecord(), and XLogReadBufferForRedoExtended().

1311 {
1312  DecodedBkpBlock *bkpb;
1313 
1314  if (!record->blocks[block_id].in_use)
1315  return false;
1316 
1317  bkpb = &record->blocks[block_id];
1318  if (rnode)
1319  *rnode = bkpb->rnode;
1320  if (forknum)
1321  *forknum = bkpb->forknum;
1322  if (blknum)
1323  *blknum = bkpb->blkno;
1324  return true;
1325 }
BlockNumber blkno
Definition: xlogreader.h:48
ForkNumber forknum
Definition: xlogreader.h:47
RelFileNode rnode
Definition: xlogreader.h:46
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139