00001 /*------------------------------------------------------------------------- 00002 * 00003 * xlogreader.h 00004 * Definitions for the generic XLog reading facility 00005 * 00006 * Portions Copyright (c) 2013, PostgreSQL Global Development Group 00007 * 00008 * IDENTIFICATION 00009 * src/include/access/xlogreader.h 00010 * 00011 * NOTES 00012 * See the definition of the XLogReaderState struct for instructions on 00013 * how to use the XLogReader infrastructure. 00014 * 00015 * The basic idea is to allocate an XLogReaderState via 00016 * XLogReaderAllocate(), and call XLogReadRecord() until it returns NULL. 00017 *------------------------------------------------------------------------- 00018 */ 00019 #ifndef XLOGREADER_H 00020 #define XLOGREADER_H 00021 00022 #include "access/xlog_internal.h" 00023 00024 typedef struct XLogReaderState XLogReaderState; 00025 00026 /* Function type definition for the read_page callback */ 00027 typedef int (*XLogPageReadCB) (XLogReaderState *xlogreader, 00028 XLogRecPtr targetPagePtr, 00029 int reqLen, 00030 XLogRecPtr targetRecPtr, 00031 char *readBuf, 00032 TimeLineID *pageTLI); 00033 00034 struct XLogReaderState 00035 { 00036 /* ---------------------------------------- 00037 * Public parameters 00038 * ---------------------------------------- 00039 */ 00040 00041 /* 00042 * Data input callback (mandatory). 00043 * 00044 * This callback shall read at least reqLen valid bytes of the xlog page 00045 * starting at targetPagePtr, and store them in readBuf. The callback 00046 * shall return the number of bytes read (never more than XLOG_BLCKSZ), or 00047 * -1 on failure. The callback shall sleep, if necessary, to wait for the 00048 * requested bytes to become available. The callback will not be invoked 00049 * again for the same page unless more than the returned number of bytes 00050 * are needed. 00051 * 00052 * targetRecPtr is the position of the WAL record we're reading. Usually 00053 * it is equal to targetPagePtr + reqLen, but sometimes xlogreader needs 00054 * to read and verify the page or segment header, before it reads the 00055 * actual WAL record it's interested in. In that case, targetRecPtr can 00056 * be used to determine which timeline to read the page from. 00057 * 00058 * The callback shall set *pageTLI to the TLI of the file the page was 00059 * read from. It is currently used only for error reporting purposes, to 00060 * reconstruct the name of the WAL file where an error occurred. 00061 */ 00062 XLogPageReadCB read_page; 00063 00064 /* 00065 * System identifier of the xlog files we're about to read. Set to zero 00066 * (the default value) if unknown or unimportant. 00067 */ 00068 uint64 system_identifier; 00069 00070 /* 00071 * Opaque data for callbacks to use. Not used by XLogReader. 00072 */ 00073 void *private_data; 00074 00075 /* 00076 * Start and end point of last record read. EndRecPtr is also used as the 00077 * position to read next, if XLogReadRecord receives an invalid recptr. 00078 */ 00079 XLogRecPtr ReadRecPtr; /* start of last record read */ 00080 XLogRecPtr EndRecPtr; /* end+1 of last record read */ 00081 00082 /* ---------------------------------------- 00083 * private/internal state 00084 * ---------------------------------------- 00085 */ 00086 00087 /* Buffer for currently read page (XLOG_BLCKSZ bytes) */ 00088 char *readBuf; 00089 00090 /* last read segment, segment offset, read length, TLI */ 00091 XLogSegNo readSegNo; 00092 uint32 readOff; 00093 uint32 readLen; 00094 TimeLineID readPageTLI; 00095 00096 /* beginning of last page read, and its TLI */ 00097 XLogRecPtr latestPagePtr; 00098 TimeLineID latestPageTLI; 00099 00100 /* beginning of the WAL record being read. */ 00101 XLogRecPtr currRecPtr; 00102 00103 /* Buffer for current ReadRecord result (expandable) */ 00104 char *readRecordBuf; 00105 uint32 readRecordBufSize; 00106 00107 /* Buffer to hold error message */ 00108 char *errormsg_buf; 00109 }; 00110 00111 /* Get a new XLogReader */ 00112 extern XLogReaderState *XLogReaderAllocate(XLogPageReadCB pagereadfunc, 00113 void *private_data); 00114 00115 /* Free an XLogReader */ 00116 extern void XLogReaderFree(XLogReaderState *state); 00117 00118 /* Read the next XLog record. Returns NULL on end-of-WAL or failure */ 00119 extern struct XLogRecord *XLogReadRecord(XLogReaderState *state, 00120 XLogRecPtr recptr, char **errormsg); 00121 00122 #ifdef FRONTEND 00123 extern XLogRecPtr XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr); 00124 #endif /* FRONTEND */ 00125 00126 #endif /* XLOGREADER_H */