Header And Logo

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

xact.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * xact.h
00004  *    postgres transaction system definitions
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  * src/include/access/xact.h
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #ifndef XACT_H
00015 #define XACT_H
00016 
00017 #include "access/xlog.h"
00018 #include "nodes/pg_list.h"
00019 #include "storage/relfilenode.h"
00020 
00021 
00022 /*
00023  * Xact isolation levels
00024  */
00025 #define XACT_READ_UNCOMMITTED   0
00026 #define XACT_READ_COMMITTED     1
00027 #define XACT_REPEATABLE_READ    2
00028 #define XACT_SERIALIZABLE       3
00029 
00030 extern int  DefaultXactIsoLevel;
00031 extern PGDLLIMPORT int XactIsoLevel;
00032 
00033 /*
00034  * We implement three isolation levels internally.
00035  * The two stronger ones use one snapshot per database transaction;
00036  * the others use one snapshot per statement.
00037  * Serializable uses predicate locks in addition to snapshots.
00038  * These macros should be used to check which isolation level is selected.
00039  */
00040 #define IsolationUsesXactSnapshot() (XactIsoLevel >= XACT_REPEATABLE_READ)
00041 #define IsolationIsSerializable() (XactIsoLevel == XACT_SERIALIZABLE)
00042 
00043 /* Xact read-only state */
00044 extern bool DefaultXactReadOnly;
00045 extern bool XactReadOnly;
00046 
00047 /*
00048  * Xact is deferrable -- only meaningful (currently) for read only
00049  * SERIALIZABLE transactions
00050  */
00051 extern bool DefaultXactDeferrable;
00052 extern bool XactDeferrable;
00053 
00054 typedef enum
00055 {
00056     SYNCHRONOUS_COMMIT_OFF,     /* asynchronous commit */
00057     SYNCHRONOUS_COMMIT_LOCAL_FLUSH,     /* wait for local flush only */
00058     SYNCHRONOUS_COMMIT_REMOTE_WRITE,    /* wait for local flush and remote
00059                                          * write */
00060     SYNCHRONOUS_COMMIT_REMOTE_FLUSH     /* wait for local and remote flush */
00061 }   SyncCommitLevel;
00062 
00063 /* Define the default setting for synchonous_commit */
00064 #define SYNCHRONOUS_COMMIT_ON   SYNCHRONOUS_COMMIT_REMOTE_FLUSH
00065 
00066 /* Synchronous commit level */
00067 extern int  synchronous_commit;
00068 
00069 /* Kluge for 2PC support */
00070 extern bool MyXactAccessedTempRel;
00071 
00072 /*
00073  *  start- and end-of-transaction callbacks for dynamically loaded modules
00074  */
00075 typedef enum
00076 {
00077     XACT_EVENT_COMMIT,
00078     XACT_EVENT_ABORT,
00079     XACT_EVENT_PREPARE,
00080     XACT_EVENT_PRE_COMMIT,
00081     XACT_EVENT_PRE_PREPARE
00082 } XactEvent;
00083 
00084 typedef void (*XactCallback) (XactEvent event, void *arg);
00085 
00086 typedef enum
00087 {
00088     SUBXACT_EVENT_START_SUB,
00089     SUBXACT_EVENT_COMMIT_SUB,
00090     SUBXACT_EVENT_ABORT_SUB,
00091     SUBXACT_EVENT_PRE_COMMIT_SUB
00092 } SubXactEvent;
00093 
00094 typedef void (*SubXactCallback) (SubXactEvent event, SubTransactionId mySubid,
00095                                     SubTransactionId parentSubid, void *arg);
00096 
00097 
00098 /* ----------------
00099  *      transaction-related XLOG entries
00100  * ----------------
00101  */
00102 
00103 /*
00104  * XLOG allows to store some information in high 4 bits of log
00105  * record xl_info field
00106  */
00107 #define XLOG_XACT_COMMIT            0x00
00108 #define XLOG_XACT_PREPARE           0x10
00109 #define XLOG_XACT_ABORT             0x20
00110 #define XLOG_XACT_COMMIT_PREPARED   0x30
00111 #define XLOG_XACT_ABORT_PREPARED    0x40
00112 #define XLOG_XACT_ASSIGNMENT        0x50
00113 #define XLOG_XACT_COMMIT_COMPACT    0x60
00114 
00115 typedef struct xl_xact_assignment
00116 {
00117     TransactionId xtop;         /* assigned XID's top-level XID */
00118     int         nsubxacts;      /* number of subtransaction XIDs */
00119     TransactionId xsub[1];      /* assigned subxids */
00120 } xl_xact_assignment;
00121 
00122 #define MinSizeOfXactAssignment offsetof(xl_xact_assignment, xsub)
00123 
00124 typedef struct xl_xact_commit_compact
00125 {
00126     TimestampTz xact_time;      /* time of commit */
00127     int         nsubxacts;      /* number of subtransaction XIDs */
00128     /* ARRAY OF COMMITTED SUBTRANSACTION XIDs FOLLOWS */
00129     TransactionId subxacts[1];  /* VARIABLE LENGTH ARRAY */
00130 } xl_xact_commit_compact;
00131 
00132 #define MinSizeOfXactCommitCompact offsetof(xl_xact_commit_compact, subxacts)
00133 
00134 typedef struct xl_xact_commit
00135 {
00136     TimestampTz xact_time;      /* time of commit */
00137     uint32      xinfo;          /* info flags */
00138     int         nrels;          /* number of RelFileNodes */
00139     int         nsubxacts;      /* number of subtransaction XIDs */
00140     int         nmsgs;          /* number of shared inval msgs */
00141     Oid         dbId;           /* MyDatabaseId */
00142     Oid         tsId;           /* MyDatabaseTableSpace */
00143     /* Array of RelFileNode(s) to drop at commit */
00144     RelFileNode xnodes[1];      /* VARIABLE LENGTH ARRAY */
00145     /* ARRAY OF COMMITTED SUBTRANSACTION XIDs FOLLOWS */
00146     /* ARRAY OF SHARED INVALIDATION MESSAGES FOLLOWS */
00147 } xl_xact_commit;
00148 
00149 #define MinSizeOfXactCommit offsetof(xl_xact_commit, xnodes)
00150 
00151 /*
00152  * These flags are set in the xinfo fields of WAL commit records,
00153  * indicating a variety of additional actions that need to occur
00154  * when emulating transaction effects during recovery.
00155  * They are named XactCompletion... to differentiate them from
00156  * EOXact... routines which run at the end of the original
00157  * transaction completion.
00158  */
00159 #define XACT_COMPLETION_UPDATE_RELCACHE_FILE    0x01
00160 #define XACT_COMPLETION_FORCE_SYNC_COMMIT       0x02
00161 
00162 /* Access macros for above flags */
00163 #define XactCompletionRelcacheInitFileInval(xinfo)  (xinfo & XACT_COMPLETION_UPDATE_RELCACHE_FILE)
00164 #define XactCompletionForceSyncCommit(xinfo)        (xinfo & XACT_COMPLETION_FORCE_SYNC_COMMIT)
00165 
00166 typedef struct xl_xact_abort
00167 {
00168     TimestampTz xact_time;      /* time of abort */
00169     int         nrels;          /* number of RelFileNodes */
00170     int         nsubxacts;      /* number of subtransaction XIDs */
00171     /* Array of RelFileNode(s) to drop at abort */
00172     RelFileNode xnodes[1];      /* VARIABLE LENGTH ARRAY */
00173     /* ARRAY OF ABORTED SUBTRANSACTION XIDs FOLLOWS */
00174 } xl_xact_abort;
00175 
00176 /* Note the intentional lack of an invalidation message array c.f. commit */
00177 
00178 #define MinSizeOfXactAbort offsetof(xl_xact_abort, xnodes)
00179 
00180 /*
00181  * COMMIT_PREPARED and ABORT_PREPARED are identical to COMMIT/ABORT records
00182  * except that we have to store the XID of the prepared transaction explicitly
00183  * --- the XID in the record header will be for the transaction doing the
00184  * COMMIT PREPARED or ABORT PREPARED command.
00185  */
00186 
00187 typedef struct xl_xact_commit_prepared
00188 {
00189     TransactionId xid;          /* XID of prepared xact */
00190     xl_xact_commit crec;        /* COMMIT record */
00191     /* MORE DATA FOLLOWS AT END OF STRUCT */
00192 } xl_xact_commit_prepared;
00193 
00194 #define MinSizeOfXactCommitPrepared offsetof(xl_xact_commit_prepared, crec.xnodes)
00195 
00196 typedef struct xl_xact_abort_prepared
00197 {
00198     TransactionId xid;          /* XID of prepared xact */
00199     xl_xact_abort arec;         /* ABORT record */
00200     /* MORE DATA FOLLOWS AT END OF STRUCT */
00201 } xl_xact_abort_prepared;
00202 
00203 #define MinSizeOfXactAbortPrepared offsetof(xl_xact_abort_prepared, arec.xnodes)
00204 
00205 
00206 /* ----------------
00207  *      extern definitions
00208  * ----------------
00209  */
00210 extern bool IsTransactionState(void);
00211 extern bool IsAbortedTransactionBlockState(void);
00212 extern TransactionId GetTopTransactionId(void);
00213 extern TransactionId GetTopTransactionIdIfAny(void);
00214 extern TransactionId GetCurrentTransactionId(void);
00215 extern TransactionId GetCurrentTransactionIdIfAny(void);
00216 extern TransactionId GetStableLatestTransactionId(void);
00217 extern SubTransactionId GetCurrentSubTransactionId(void);
00218 extern bool SubTransactionIsActive(SubTransactionId subxid);
00219 extern CommandId GetCurrentCommandId(bool used);
00220 extern TimestampTz GetCurrentTransactionStartTimestamp(void);
00221 extern TimestampTz GetCurrentStatementStartTimestamp(void);
00222 extern TimestampTz GetCurrentTransactionStopTimestamp(void);
00223 extern void SetCurrentStatementStartTimestamp(void);
00224 extern int  GetCurrentTransactionNestLevel(void);
00225 extern bool TransactionIdIsCurrentTransactionId(TransactionId xid);
00226 extern void CommandCounterIncrement(void);
00227 extern void ForceSyncCommit(void);
00228 extern void StartTransactionCommand(void);
00229 extern void CommitTransactionCommand(void);
00230 extern void AbortCurrentTransaction(void);
00231 extern void BeginTransactionBlock(void);
00232 extern bool EndTransactionBlock(void);
00233 extern bool PrepareTransactionBlock(char *gid);
00234 extern void UserAbortTransactionBlock(void);
00235 extern void ReleaseSavepoint(List *options);
00236 extern void DefineSavepoint(char *name);
00237 extern void RollbackToSavepoint(List *options);
00238 extern void BeginInternalSubTransaction(char *name);
00239 extern void ReleaseCurrentSubTransaction(void);
00240 extern void RollbackAndReleaseCurrentSubTransaction(void);
00241 extern bool IsSubTransaction(void);
00242 extern bool IsTransactionBlock(void);
00243 extern bool IsTransactionOrTransactionBlock(void);
00244 extern char TransactionBlockStatusCode(void);
00245 extern void AbortOutOfAnyTransaction(void);
00246 extern void PreventTransactionChain(bool isTopLevel, const char *stmtType);
00247 extern void RequireTransactionChain(bool isTopLevel, const char *stmtType);
00248 extern bool IsInTransactionChain(bool isTopLevel);
00249 extern void RegisterXactCallback(XactCallback callback, void *arg);
00250 extern void UnregisterXactCallback(XactCallback callback, void *arg);
00251 extern void RegisterSubXactCallback(SubXactCallback callback, void *arg);
00252 extern void UnregisterSubXactCallback(SubXactCallback callback, void *arg);
00253 
00254 extern int  xactGetCommittedChildren(TransactionId **ptr);
00255 
00256 extern void xact_redo(XLogRecPtr lsn, XLogRecord *record);
00257 extern void xact_desc(StringInfo buf, uint8 xl_info, char *rec);
00258 
00259 #endif   /* XACT_H */