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