gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 satır
8.1KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * xlogreader.h
  4. * Definitions for the generic XLog reading facility
  5. *
  6. * Portions Copyright (c) 2013-2019, PostgreSQL Global Development Group
  7. *
  8. * IDENTIFICATION
  9. * src/include/access/xlogreader.h
  10. *
  11. * NOTES
  12. * See the definition of the XLogReaderState struct for instructions on
  13. * how to use the XLogReader infrastructure.
  14. *
  15. * The basic idea is to allocate an XLogReaderState via
  16. * XLogReaderAllocate(), and call XLogReadRecord() until it returns NULL.
  17. *
  18. * After reading a record with XLogReadRecord(), it's decomposed into
  19. * the per-block and main data parts, and the parts can be accessed
  20. * with the XLogRec* macros and functions. You can also decode a
  21. * record that's already constructed in memory, without reading from
  22. * disk, by calling the DecodeXLogRecord() function.
  23. *-------------------------------------------------------------------------
  24. */
  25. #ifndef XLOGREADER_H
  26. #define XLOGREADER_H
  27. #include "access/xlogrecord.h"
  28. typedef struct XLogReaderState XLogReaderState;
  29. /* Function type definition for the read_page callback */
  30. typedef int (*XLogPageReadCB) (XLogReaderState *xlogreader,
  31. XLogRecPtr targetPagePtr,
  32. int reqLen,
  33. XLogRecPtr targetRecPtr,
  34. char *readBuf,
  35. TimeLineID *pageTLI);
  36. typedef struct
  37. {
  38. /* Is this block ref in use? */
  39. bool in_use;
  40. /* Identify the block this refers to */
  41. RelFileNode rnode;
  42. ForkNumber forknum;
  43. BlockNumber blkno;
  44. /* copy of the fork_flags field from the XLogRecordBlockHeader */
  45. uint8 flags;
  46. /* Information on full-page image, if any */
  47. bool has_image; /* has image, even for consistency checking */
  48. bool apply_image; /* has image that should be restored */
  49. char *bkp_image;
  50. uint16 hole_offset;
  51. uint16 hole_length;
  52. uint16 bimg_len;
  53. uint8 bimg_info;
  54. /* Buffer holding the rmgr-specific data associated with this block */
  55. bool has_data;
  56. char *data;
  57. uint16 data_len;
  58. uint16 data_bufsz;
  59. } DecodedBkpBlock;
  60. struct XLogReaderState
  61. {
  62. /* ----------------------------------------
  63. * Public parameters
  64. * ----------------------------------------
  65. */
  66. /*
  67. * Segment size of the to-be-parsed data (mandatory).
  68. */
  69. int wal_segment_size;
  70. /*
  71. * Data input callback (mandatory).
  72. *
  73. * This callback shall read at least reqLen valid bytes of the xlog page
  74. * starting at targetPagePtr, and store them in readBuf. The callback
  75. * shall return the number of bytes read (never more than XLOG_BLCKSZ), or
  76. * -1 on failure. The callback shall sleep, if necessary, to wait for the
  77. * requested bytes to become available. The callback will not be invoked
  78. * again for the same page unless more than the returned number of bytes
  79. * are needed.
  80. *
  81. * targetRecPtr is the position of the WAL record we're reading. Usually
  82. * it is equal to targetPagePtr + reqLen, but sometimes xlogreader needs
  83. * to read and verify the page or segment header, before it reads the
  84. * actual WAL record it's interested in. In that case, targetRecPtr can
  85. * be used to determine which timeline to read the page from.
  86. *
  87. * The callback shall set *pageTLI to the TLI of the file the page was
  88. * read from. It is currently used only for error reporting purposes, to
  89. * reconstruct the name of the WAL file where an error occurred.
  90. */
  91. XLogPageReadCB read_page;
  92. /*
  93. * System identifier of the xlog files we're about to read. Set to zero
  94. * (the default value) if unknown or unimportant.
  95. */
  96. uint64 system_identifier;
  97. /*
  98. * Opaque data for callbacks to use. Not used by XLogReader.
  99. */
  100. void *private_data;
  101. /*
  102. * Start and end point of last record read. EndRecPtr is also used as the
  103. * position to read next, if XLogReadRecord receives an invalid recptr.
  104. */
  105. XLogRecPtr ReadRecPtr; /* start of last record read */
  106. XLogRecPtr EndRecPtr; /* end+1 of last record read */
  107. /* ----------------------------------------
  108. * Decoded representation of current record
  109. *
  110. * Use XLogRecGet* functions to investigate the record; these fields
  111. * should not be accessed directly.
  112. * ----------------------------------------
  113. */
  114. XLogRecord *decoded_record; /* currently decoded record */
  115. char *main_data; /* record's main data portion */
  116. uint32 main_data_len; /* main data portion's length */
  117. uint32 main_data_bufsz; /* allocated size of the buffer */
  118. RepOriginId record_origin;
  119. /* information about blocks referenced by the record. */
  120. DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID + 1];
  121. int max_block_id; /* highest block_id in use (-1 if none) */
  122. /* ----------------------------------------
  123. * private/internal state
  124. * ----------------------------------------
  125. */
  126. /*
  127. * Buffer for currently read page (XLOG_BLCKSZ bytes, valid up to at least
  128. * readLen bytes)
  129. */
  130. char *readBuf;
  131. uint32 readLen;
  132. /* last read segment, segment offset, TLI for data currently in readBuf */
  133. XLogSegNo readSegNo;
  134. uint32 readOff;
  135. TimeLineID readPageTLI;
  136. /*
  137. * beginning of prior page read, and its TLI. Doesn't necessarily
  138. * correspond to what's in readBuf; used for timeline sanity checks.
  139. */
  140. XLogRecPtr latestPagePtr;
  141. TimeLineID latestPageTLI;
  142. /* beginning of the WAL record being read. */
  143. XLogRecPtr currRecPtr;
  144. /* timeline to read it from, 0 if a lookup is required */
  145. TimeLineID currTLI;
  146. /*
  147. * Safe point to read to in currTLI if current TLI is historical
  148. * (tliSwitchPoint) or InvalidXLogRecPtr if on current timeline.
  149. *
  150. * Actually set to the start of the segment containing the timeline switch
  151. * that ends currTLI's validity, not the LSN of the switch its self, since
  152. * we can't assume the old segment will be present.
  153. */
  154. XLogRecPtr currTLIValidUntil;
  155. /*
  156. * If currTLI is not the most recent known timeline, the next timeline to
  157. * read from when currTLIValidUntil is reached.
  158. */
  159. TimeLineID nextTLI;
  160. /*
  161. * Buffer for current ReadRecord result (expandable), used when a record
  162. * crosses a page boundary.
  163. */
  164. char *readRecordBuf;
  165. uint32 readRecordBufSize;
  166. /* Buffer to hold error message */
  167. char *errormsg_buf;
  168. };
  169. /* Get a new XLogReader */
  170. extern XLogReaderState *XLogReaderAllocate(int wal_segment_size,
  171. XLogPageReadCB pagereadfunc,
  172. void *private_data);
  173. /* Free an XLogReader */
  174. extern void XLogReaderFree(XLogReaderState *state);
  175. /* Read the next XLog record. Returns NULL on end-of-WAL or failure */
  176. extern struct XLogRecord *XLogReadRecord(XLogReaderState *state,
  177. XLogRecPtr recptr, char **errormsg);
  178. /* Validate a page */
  179. extern bool XLogReaderValidatePageHeader(XLogReaderState *state,
  180. XLogRecPtr recptr, char *phdr);
  181. /* Invalidate read state */
  182. extern void XLogReaderInvalReadState(XLogReaderState *state);
  183. #ifdef FRONTEND
  184. extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr);
  185. #endif /* FRONTEND */
  186. /* Functions for decoding an XLogRecord */
  187. extern bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record,
  188. char **errmsg);
  189. #define XLogRecGetTotalLen(decoder) ((decoder)->decoded_record->xl_tot_len)
  190. #define XLogRecGetPrev(decoder) ((decoder)->decoded_record->xl_prev)
  191. #define XLogRecGetInfo(decoder) ((decoder)->decoded_record->xl_info)
  192. #define XLogRecGetRmid(decoder) ((decoder)->decoded_record->xl_rmid)
  193. #define XLogRecGetXid(decoder) ((decoder)->decoded_record->xl_xid)
  194. #define XLogRecGetOrigin(decoder) ((decoder)->record_origin)
  195. #define XLogRecGetData(decoder) ((decoder)->main_data)
  196. #define XLogRecGetDataLen(decoder) ((decoder)->main_data_len)
  197. #define XLogRecHasAnyBlockRefs(decoder) ((decoder)->max_block_id >= 0)
  198. #define XLogRecHasBlockRef(decoder, block_id) \
  199. ((decoder)->blocks[block_id].in_use)
  200. #define XLogRecHasBlockImage(decoder, block_id) \
  201. ((decoder)->blocks[block_id].has_image)
  202. #define XLogRecBlockImageApply(decoder, block_id) \
  203. ((decoder)->blocks[block_id].apply_image)
  204. extern bool RestoreBlockImage(XLogReaderState *recoder, uint8 block_id, char *dst);
  205. extern char *XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len);
  206. extern bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id,
  207. RelFileNode *rnode, ForkNumber *forknum,
  208. BlockNumber *blknum);
  209. #endif /* XLOGREADER_H */
上海开阖软件有限公司 沪ICP备12045867号-1