Header And Logo

PostgreSQL
| The world's most advanced open source database.

xlog_internal.h

Go to the documentation of this file.
00001 /*
00002  * xlog_internal.h
00003  *
00004  * PostgreSQL transaction log internal declarations
00005  *
00006  * NOTE: this file is intended to contain declarations useful for
00007  * manipulating the XLOG files directly, but it is not supposed to be
00008  * needed by rmgr routines (redo support for individual record types).
00009  * So the XLogRecord typedef and associated stuff appear in xlog.h.
00010  *
00011  * Note: This file must be includable in both frontend and backend contexts,
00012  * to allow stand-alone tools like pg_receivexlog to deal with WAL files.
00013  *
00014  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00015  * Portions Copyright (c) 1994, Regents of the University of California
00016  *
00017  * src/include/access/xlog_internal.h
00018  */
00019 #ifndef XLOG_INTERNAL_H
00020 #define XLOG_INTERNAL_H
00021 
00022 #include "access/xlogdefs.h"
00023 #include "datatype/timestamp.h"
00024 #include "lib/stringinfo.h"
00025 #include "pgtime.h"
00026 #include "storage/block.h"
00027 #include "storage/relfilenode.h"
00028 
00029 
00030 /*
00031  * Header info for a backup block appended to an XLOG record.
00032  *
00033  * As a trivial form of data compression, the XLOG code is aware that
00034  * PG data pages usually contain an unused "hole" in the middle, which
00035  * contains only zero bytes.  If hole_length > 0 then we have removed
00036  * such a "hole" from the stored data (and it's not counted in the
00037  * XLOG record's CRC, either).  Hence, the amount of block data actually
00038  * present following the BkpBlock struct is BLCKSZ - hole_length bytes.
00039  *
00040  * Note that we don't attempt to align either the BkpBlock struct or the
00041  * block's data.  So, the struct must be copied to aligned local storage
00042  * before use.
00043  */
00044 typedef struct BkpBlock
00045 {
00046     RelFileNode node;           /* relation containing block */
00047     ForkNumber  fork;           /* fork within the relation */
00048     BlockNumber block;          /* block number */
00049     uint16      hole_offset;    /* number of bytes before "hole" */
00050     uint16      hole_length;    /* number of bytes in "hole" */
00051 
00052     /* ACTUAL BLOCK DATA FOLLOWS AT END OF STRUCT */
00053 } BkpBlock;
00054 
00055 /*
00056  * Each page of XLOG file has a header like this:
00057  */
00058 #define XLOG_PAGE_MAGIC 0xD075  /* can be used as WAL version indicator */
00059 
00060 typedef struct XLogPageHeaderData
00061 {
00062     uint16      xlp_magic;      /* magic value for correctness checks */
00063     uint16      xlp_info;       /* flag bits, see below */
00064     TimeLineID  xlp_tli;        /* TimeLineID of first record on page */
00065     XLogRecPtr  xlp_pageaddr;   /* XLOG address of this page */
00066 
00067     /*
00068      * When there is not enough space on current page for whole record, we
00069      * continue on the next page.  xlp_rem_len is the number of bytes
00070      * remaining from a previous page.
00071      *
00072      * Note that xl_rem_len includes backup-block data; that is, it tracks
00073      * xl_tot_len not xl_len in the initial header.  Also note that the
00074      * continuation data isn't necessarily aligned.
00075      */
00076     uint32      xlp_rem_len;    /* total len of remaining data for record */
00077 } XLogPageHeaderData;
00078 
00079 #define SizeOfXLogShortPHD  MAXALIGN(sizeof(XLogPageHeaderData))
00080 
00081 typedef XLogPageHeaderData *XLogPageHeader;
00082 
00083 /*
00084  * When the XLP_LONG_HEADER flag is set, we store additional fields in the
00085  * page header.  (This is ordinarily done just in the first page of an
00086  * XLOG file.)  The additional fields serve to identify the file accurately.
00087  */
00088 typedef struct XLogLongPageHeaderData
00089 {
00090     XLogPageHeaderData std;     /* standard header fields */
00091     uint64      xlp_sysid;      /* system identifier from pg_control */
00092     uint32      xlp_seg_size;   /* just as a cross-check */
00093     uint32      xlp_xlog_blcksz;    /* just as a cross-check */
00094 } XLogLongPageHeaderData;
00095 
00096 #define SizeOfXLogLongPHD   MAXALIGN(sizeof(XLogLongPageHeaderData))
00097 
00098 typedef XLogLongPageHeaderData *XLogLongPageHeader;
00099 
00100 /* When record crosses page boundary, set this flag in new page's header */
00101 #define XLP_FIRST_IS_CONTRECORD     0x0001
00102 /* This flag indicates a "long" page header */
00103 #define XLP_LONG_HEADER             0x0002
00104 /* This flag indicates backup blocks starting in this page are optional */
00105 #define XLP_BKP_REMOVABLE           0x0004
00106 /* All defined flag bits in xlp_info (used for validity checking of header) */
00107 #define XLP_ALL_FLAGS               0x0007
00108 
00109 #define XLogPageHeaderSize(hdr)     \
00110     (((hdr)->xlp_info & XLP_LONG_HEADER) ? SizeOfXLogLongPHD : SizeOfXLogShortPHD)
00111 
00112 /*
00113  * The XLOG is split into WAL segments (physical files) of the size indicated
00114  * by XLOG_SEG_SIZE.
00115  */
00116 #define XLogSegSize     ((uint32) XLOG_SEG_SIZE)
00117 #define XLogSegmentsPerXLogId   (UINT64CONST(0x100000000) / XLOG_SEG_SIZE)
00118 
00119 #define XLogSegNoOffsetToRecPtr(segno, offset, dest) \
00120         (dest) = (segno) * XLOG_SEG_SIZE + (offset)
00121 
00122 /*
00123  * Compute ID and segment from an XLogRecPtr.
00124  *
00125  * For XLByteToSeg, do the computation at face value.  For XLByteToPrevSeg,
00126  * a boundary byte is taken to be in the previous segment.  This is suitable
00127  * for deciding which segment to write given a pointer to a record end,
00128  * for example.
00129  */
00130 #define XLByteToSeg(xlrp, logSegNo) \
00131     logSegNo = (xlrp) / XLogSegSize
00132 
00133 #define XLByteToPrevSeg(xlrp, logSegNo) \
00134     logSegNo = ((xlrp) - 1) / XLogSegSize
00135 
00136 /*
00137  * Is an XLogRecPtr within a particular XLOG segment?
00138  *
00139  * For XLByteInSeg, do the computation at face value.  For XLByteInPrevSeg,
00140  * a boundary byte is taken to be in the previous segment.
00141  */
00142 #define XLByteInSeg(xlrp, logSegNo) \
00143     (((xlrp) / XLogSegSize) == (logSegNo))
00144 
00145 #define XLByteInPrevSeg(xlrp, logSegNo) \
00146     ((((xlrp) - 1) / XLogSegSize) == (logSegNo))
00147 
00148 /* Check if an XLogRecPtr value is in a plausible range */
00149 #define XRecOffIsValid(xlrp) \
00150         ((xlrp) % XLOG_BLCKSZ >= SizeOfXLogShortPHD)
00151 
00152 /*
00153  * The XLog directory and control file (relative to $PGDATA)
00154  */
00155 #define XLOGDIR             "pg_xlog"
00156 #define XLOG_CONTROL_FILE   "global/pg_control"
00157 
00158 /*
00159  * These macros encapsulate knowledge about the exact layout of XLog file
00160  * names, timeline history file names, and archive-status file names.
00161  */
00162 #define MAXFNAMELEN     64
00163 
00164 #define XLogFileName(fname, tli, logSegNo)  \
00165     snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli,       \
00166              (uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
00167              (uint32) ((logSegNo) % XLogSegmentsPerXLogId))
00168 
00169 #define XLogFromFileName(fname, tli, logSegNo)  \
00170     do {                                                \
00171         uint32 log;                                     \
00172         uint32 seg;                                     \
00173         sscanf(fname, "%08X%08X%08X", tli, &log, &seg); \
00174         *logSegNo = (uint64) log * XLogSegmentsPerXLogId + seg; \
00175     } while (0)
00176 
00177 #define XLogFilePath(path, tli, logSegNo)   \
00178     snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X", tli,             \
00179              (uint32) ((logSegNo) / XLogSegmentsPerXLogId),             \
00180              (uint32) ((logSegNo) % XLogSegmentsPerXLogId))
00181 
00182 #define TLHistoryFileName(fname, tli)   \
00183     snprintf(fname, MAXFNAMELEN, "%08X.history", tli)
00184 
00185 #define TLHistoryFilePath(path, tli)    \
00186     snprintf(path, MAXPGPATH, XLOGDIR "/%08X.history", tli)
00187 
00188 #define StatusFilePath(path, xlog, suffix)  \
00189     snprintf(path, MAXPGPATH, XLOGDIR "/archive_status/%s%s", xlog, suffix)
00190 
00191 #define BackupHistoryFileName(fname, tli, logSegNo, offset) \
00192     snprintf(fname, MAXFNAMELEN, "%08X%08X%08X.%08X.backup", tli, \
00193              (uint32) ((logSegNo) / XLogSegmentsPerXLogId),       \
00194              (uint32) ((logSegNo) % XLogSegmentsPerXLogId), offset)
00195 
00196 #define BackupHistoryFilePath(path, tli, logSegNo, offset)  \
00197     snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X.%08X.backup", tli, \
00198              (uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
00199              (uint32) ((logSegNo) % XLogSegmentsPerXLogId), offset)
00200 
00201 /*
00202  * Information logged when we detect a change in one of the parameters
00203  * important for Hot Standby.
00204  */
00205 typedef struct xl_parameter_change
00206 {
00207     int         MaxConnections;
00208     int         max_prepared_xacts;
00209     int         max_locks_per_xact;
00210     int         wal_level;
00211 } xl_parameter_change;
00212 
00213 /* logs restore point */
00214 typedef struct xl_restore_point
00215 {
00216     TimestampTz rp_time;
00217     char        rp_name[MAXFNAMELEN];
00218 } xl_restore_point;
00219 
00220 /* End of recovery mark, when we don't do an END_OF_RECOVERY checkpoint */
00221 typedef struct xl_end_of_recovery
00222 {
00223     TimestampTz end_time;
00224     TimeLineID  ThisTimeLineID; /* new TLI */
00225     TimeLineID  PrevTimeLineID; /* previous TLI we forked off from */
00226 } xl_end_of_recovery;
00227 
00228 /*
00229  * XLogRecord is defined in xlog.h, but we avoid #including that to keep
00230  * this file includable in stand-alone programs.
00231  */
00232 struct XLogRecord;
00233 
00234 /*
00235  * Method table for resource managers.
00236  *
00237  * This struct must be kept in sync with the PG_RMGR definition in
00238  * rmgr.c.
00239  *
00240  * RmgrTable[] is indexed by RmgrId values (see rmgrlist.h).
00241  */
00242 typedef struct RmgrData
00243 {
00244     const char *rm_name;
00245     void        (*rm_redo) (XLogRecPtr lsn, struct XLogRecord *rptr);
00246     void        (*rm_desc) (StringInfo buf, uint8 xl_info, char *rec);
00247     void        (*rm_startup) (void);
00248     void        (*rm_cleanup) (void);
00249     bool        (*rm_safe_restartpoint) (void);
00250 } RmgrData;
00251 
00252 extern const RmgrData RmgrTable[];
00253 
00254 /*
00255  * Exported to support xlog switching from checkpointer
00256  */
00257 extern pg_time_t GetLastSegSwitchTime(void);
00258 extern XLogRecPtr RequestXLogSwitch(void);
00259 
00260 extern void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli);
00261 
00262 /*
00263  * Exported for the functions in timeline.c and xlogarchive.c.  Only valid
00264  * in the startup process.
00265  */
00266 extern bool ArchiveRecoveryRequested;
00267 extern bool InArchiveRecovery;
00268 extern bool StandbyMode;
00269 extern char *recoveryRestoreCommand;
00270 
00271 /*
00272  * Prototypes for functions in xlogarchive.c
00273  */
00274 extern bool RestoreArchivedFile(char *path, const char *xlogfname,
00275                     const char *recovername, off_t expectedSize,
00276                     bool cleanupEnabled);
00277 extern void ExecuteRecoveryCommand(char *command, char *commandName,
00278                        bool failOnerror);
00279 extern void KeepFileRestoredFromArchive(char  *path, char *xlogfname);
00280 extern void XLogArchiveNotify(const char *xlog);
00281 extern void XLogArchiveNotifySeg(XLogSegNo segno);
00282 extern void XLogArchiveForceDone(const char *xlog);
00283 extern bool XLogArchiveCheckDone(const char *xlog);
00284 extern bool XLogArchiveIsBusy(const char *xlog);
00285 extern void XLogArchiveCleanup(const char *xlog);
00286 
00287 #endif   /* XLOG_INTERNAL_H */