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 (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 218 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 213 of file xlogreader.h.

Referenced by XLogDumpDisplayRecord().

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

Definition at line 212 of file xlogreader.h.

Referenced by XLogDumpDisplayRecord().

#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 1012 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, NULL, 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().

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

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

1361 {
1362  DecodedBkpBlock *bkpb;
1363  char *ptr;
1364  char tmp[BLCKSZ];
1365 
1366  if (!record->blocks[block_id].in_use)
1367  return false;
1368  if (!record->blocks[block_id].has_image)
1369  return false;
1370 
1371  bkpb = &record->blocks[block_id];
1372  ptr = bkpb->bkp_image;
1373 
1374  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1375  {
1376  /* If a backup block image is compressed, decompress it */
1377  if (pglz_decompress(ptr, bkpb->bimg_len, tmp,
1378  BLCKSZ - bkpb->hole_length) < 0)
1379  {
1380  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1381  (uint32) (record->ReadRecPtr >> 32),
1382  (uint32) record->ReadRecPtr,
1383  block_id);
1384  return false;
1385  }
1386  ptr = tmp;
1387  }
1388 
1389  /* generate page, taking into account hole if necessary */
1390  if (bkpb->hole_length == 0)
1391  {
1392  memcpy(page, ptr, BLCKSZ);
1393  }
1394  else
1395  {
1396  memcpy(page, ptr, bkpb->hole_offset);
1397  /* must zero-fill the hole */
1398  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1399  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1400  ptr + bkpb->hole_offset,
1401  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1402  }
1403 
1404  return true;
1405 }
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:857
uint16 bimg_len
Definition: xlogreader.h:59
uint16 hole_length
Definition: xlogreader.h:58
unsigned int uint32
Definition: c.h:268
#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 ( 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, NULL, palloc_extended(), pfree(), XLogReaderState::private_data, XLogReaderState::read_page, and XLogReaderState::readBuf.

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

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

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

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

Definition at line 604 of file xlogreader.c.

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

Referenced by ReadPageInternal(), and XLogReadRecord().

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

Definition at line 193 of file xlogreader.c.

References allocate_recordbuf(), Assert, buffer, XLogReaderState::currRecPtr, DecodeXLogRecord(), XLogReaderState::EndRecPtr, XLogReaderState::errormsg_buf, InvalidXLogRecPtr, MAXALIGN, Min, NULL, XLogReaderState::readBuf, readOff, ReadPageInternal(), XLogReaderState::readRecordBuf, XLogReaderState::readRecordBufSize, XLogReaderState::ReadRecPtr, report_invalid_record(), ResetDecoder(), SizeOfXLogRecord, SizeOfXLogShortPHD, ValidXLogRecord(), ValidXLogRecordHeader(), XLogRecord::xl_info, XLogRecord::xl_rmid, XLogRecord::xl_tot_len, XLOG_SWITCH, XLogPageHeaderSize, XLogReaderInvalReadState(), XLogSegSize, 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().

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

Definition at line 1331 of file xlogreader.c.

References XLogReaderState::blocks, DecodedBkpBlock::data, DecodedBkpBlock::data_len, DecodedBkpBlock::has_data, DecodedBkpBlock::in_use, and NULL.

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().

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

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

1309 {
1310  DecodedBkpBlock *bkpb;
1311 
1312  if (!record->blocks[block_id].in_use)
1313  return false;
1314 
1315  bkpb = &record->blocks[block_id];
1316  if (rnode)
1317  *rnode = bkpb->rnode;
1318  if (forknum)
1319  *forknum = bkpb->forknum;
1320  if (blknum)
1321  *blknum = bkpb->blkno;
1322  return true;
1323 }
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:134