xlogreader.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*-------------------------------------------------------------------------
  2. *
  3. * xlogreader.h
  4. * Definitions for the generic XLog reading facility
  5. *
  6. * Portions Copyright (c) 2013-2016, 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;
  48. char *bkp_image;
  49. uint16 hole_offset;
  50. uint16 hole_length;
  51. uint16 bimg_len;
  52. uint8 bimg_info;
  53. /* Buffer holding the rmgr-specific data associated with this block */
  54. bool has_data;
  55. char *data;
  56. uint16 data_len;
  57. uint16 data_bufsz;
  58. } DecodedBkpBlock;
  59. struct XLogReaderState
  60. {
  61. /* ----------------------------------------
  62. * Public parameters
  63. * ----------------------------------------
  64. */
  65. /*
  66. * Data input callback (mandatory).
  67. *
  68. * This callback shall read at least reqLen valid bytes of the xlog page
  69. * starting at targetPagePtr, and store them in readBuf. The callback
  70. * shall return the number of bytes read (never more than XLOG_BLCKSZ), or
  71. * -1 on failure. The callback shall sleep, if necessary, to wait for the
  72. * requested bytes to become available. The callback will not be invoked
  73. * again for the same page unless more than the returned number of bytes
  74. * are needed.
  75. *
  76. * targetRecPtr is the position of the WAL record we're reading. Usually
  77. * it is equal to targetPagePtr + reqLen, but sometimes xlogreader needs
  78. * to read and verify the page or segment header, before it reads the
  79. * actual WAL record it's interested in. In that case, targetRecPtr can
  80. * be used to determine which timeline to read the page from.
  81. *
  82. * The callback shall set *pageTLI to the TLI of the file the page was
  83. * read from. It is currently used only for error reporting purposes, to
  84. * reconstruct the name of the WAL file where an error occurred.
  85. */
  86. XLogPageReadCB read_page;
  87. /*
  88. * System identifier of the xlog files we're about to read. Set to zero
  89. * (the default value) if unknown or unimportant.
  90. */
  91. uint64 system_identifier;
  92. /*
  93. * Opaque data for callbacks to use. Not used by XLogReader.
  94. */
  95. void *private_data;
  96. /*
  97. * Start and end point of last record read. EndRecPtr is also used as the
  98. * position to read next, if XLogReadRecord receives an invalid recptr.
  99. */
  100. XLogRecPtr ReadRecPtr; /* start of last record read */
  101. XLogRecPtr EndRecPtr; /* end+1 of last record read */
  102. /* ----------------------------------------
  103. * Decoded representation of current record
  104. *
  105. * Use XLogRecGet* functions to investigate the record; these fields
  106. * should not be accessed directly.
  107. * ----------------------------------------
  108. */
  109. XLogRecord *decoded_record; /* currently decoded record */
  110. char *main_data; /* record's main data portion */
  111. uint32 main_data_len; /* main data portion's length */
  112. uint32 main_data_bufsz; /* allocated size of the buffer */
  113. RepOriginId record_origin;
  114. /* information about blocks referenced by the record. */
  115. DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID + 1];
  116. int max_block_id; /* highest block_id in use (-1 if none) */
  117. /* ----------------------------------------
  118. * private/internal state
  119. * ----------------------------------------
  120. */
  121. /*
  122. * Buffer for currently read page (XLOG_BLCKSZ bytes, valid up to at least
  123. * readLen bytes)
  124. */
  125. char *readBuf;
  126. uint32 readLen;
  127. /* last read segment, segment offset, TLI for data currently in readBuf */
  128. XLogSegNo readSegNo;
  129. uint32 readOff;
  130. TimeLineID readPageTLI;
  131. /*
  132. * beginning of prior page read, and its TLI. Doesn't necessarily
  133. * correspond to what's in readBuf; used for timeline sanity checks.
  134. */
  135. XLogRecPtr latestPagePtr;
  136. TimeLineID latestPageTLI;
  137. /* beginning of the WAL record being read. */
  138. XLogRecPtr currRecPtr;
  139. /* Buffer for current ReadRecord result (expandable) */
  140. char *readRecordBuf;
  141. uint32 readRecordBufSize;
  142. /* Buffer to hold error message */
  143. char *errormsg_buf;
  144. };
  145. /* Get a new XLogReader */
  146. extern XLogReaderState *XLogReaderAllocate(XLogPageReadCB pagereadfunc,
  147. void *private_data);
  148. /* Free an XLogReader */
  149. extern void XLogReaderFree(XLogReaderState *state);
  150. /* Read the next XLog record. Returns NULL on end-of-WAL or failure */
  151. extern struct XLogRecord *XLogReadRecord(XLogReaderState *state,
  152. XLogRecPtr recptr, char **errormsg);
  153. /* Invalidate read state */
  154. extern void XLogReaderInvalReadState(XLogReaderState *state);
  155. #ifdef FRONTEND
  156. extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr);
  157. #endif /* FRONTEND */
  158. /* Functions for decoding an XLogRecord */
  159. extern bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record,
  160. char **errmsg);
  161. #define XLogRecGetTotalLen(decoder) ((decoder)->decoded_record->xl_tot_len)
  162. #define XLogRecGetPrev(decoder) ((decoder)->decoded_record->xl_prev)
  163. #define XLogRecGetInfo(decoder) ((decoder)->decoded_record->xl_info)
  164. #define XLogRecGetRmid(decoder) ((decoder)->decoded_record->xl_rmid)
  165. #define XLogRecGetXid(decoder) ((decoder)->decoded_record->xl_xid)
  166. #define XLogRecGetOrigin(decoder) ((decoder)->record_origin)
  167. #define XLogRecGetData(decoder) ((decoder)->main_data)
  168. #define XLogRecGetDataLen(decoder) ((decoder)->main_data_len)
  169. #define XLogRecHasAnyBlockRefs(decoder) ((decoder)->max_block_id >= 0)
  170. #define XLogRecHasBlockRef(decoder, block_id) \
  171. ((decoder)->blocks[block_id].in_use)
  172. #define XLogRecHasBlockImage(decoder, block_id) \
  173. ((decoder)->blocks[block_id].has_image)
  174. extern bool RestoreBlockImage(XLogReaderState *recoder, uint8 block_id, char *dst);
  175. extern char *XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len);
  176. extern bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id,
  177. RelFileNode *rnode, ForkNumber *forknum,
  178. BlockNumber *blknum);
  179. #endif /* XLOGREADER_H */