1 /*-------------------------------------------------------------------------
2  *
3  * snapshot.h
4  *	  POSTGRES snapshot definition
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/utils/snapshot.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef SNAPSHOT_H
14 #define SNAPSHOT_H
15 
16 #include "access/htup.h"
17 #include "access/xlogdefs.h"
18 #include "datatype/timestamp.h"
19 #include "lib/pairingheap.h"
20 #include "storage/buf.h"
21 
22 
23 typedef struct SnapshotData *Snapshot;
24 
25 #define InvalidSnapshot		((Snapshot) NULL)
26 
27 /*
28  * We use SnapshotData structures to represent both "regular" (MVCC)
29  * snapshots and "special" snapshots that have non-MVCC semantics.
30  * The specific semantics of a snapshot are encoded by the "satisfies"
31  * function.
32  */
33 typedef bool (*SnapshotSatisfiesFunc) (HeapTuple htup,
34 									   Snapshot snapshot, Buffer buffer);
35 
36 /*
37  * Struct representing all kind of possible snapshots.
38  *
39  * There are several different kinds of snapshots:
40  * * Normal MVCC snapshots
41  * * MVCC snapshots taken during recovery (in Hot-Standby mode)
42  * * Historic MVCC snapshots used during logical decoding
43  * * snapshots passed to HeapTupleSatisfiesDirty()
44  * * snapshots used for SatisfiesAny, Toast, Self where no members are
45  *	 accessed.
46  *
47  * TODO: It's probably a good idea to split this struct using a NodeTag
48  * similar to how parser and executor nodes are handled, with one type for
49  * each different kind of snapshot to avoid overloading the meaning of
50  * individual fields.
51  */
52 typedef struct SnapshotData
53 {
54 	SnapshotSatisfiesFunc satisfies;	/* tuple test function */
55 
56 	/*
57 	 * The remaining fields are used only for MVCC snapshots, and are normally
58 	 * just zeroes in special snapshots.  (But xmin and xmax are used
59 	 * specially by HeapTupleSatisfiesDirty.)
60 	 *
61 	 * An MVCC snapshot can never see the effects of XIDs >= xmax. It can see
62 	 * the effects of all older XIDs except those listed in the snapshot. xmin
63 	 * is stored as an optimization to avoid needing to search the XID arrays
64 	 * for most tuples.
65 	 */
66 	TransactionId xmin;			/* all XID < xmin are visible to me */
67 	TransactionId xmax;			/* all XID >= xmax are invisible to me */
68 
69 	/*
70 	 * For normal MVCC snapshot this contains the all xact IDs that are in
71 	 * progress, unless the snapshot was taken during recovery in which case
72 	 * it's empty. For historic MVCC snapshots, the meaning is inverted, i.e.
73 	 * it contains *committed* transactions between xmin and xmax.
74 	 *
75 	 * note: all ids in xip[] satisfy xmin <= xip[i] < xmax
76 	 */
77 	TransactionId *xip;
78 	uint32		xcnt;			/* # of xact ids in xip[] */
79 
80 	/*
81 	 * For non-historic MVCC snapshots, this contains subxact IDs that are in
82 	 * progress (and other transactions that are in progress if taken during
83 	 * recovery). For historic snapshot it contains *all* xids assigned to the
84 	 * replayed transaction, including the toplevel xid.
85 	 *
86 	 * note: all ids in subxip[] are >= xmin, but we don't bother filtering
87 	 * out any that are >= xmax
88 	 */
89 	TransactionId *subxip;
90 	int32		subxcnt;		/* # of xact ids in subxip[] */
91 	bool		suboverflowed;	/* has the subxip array overflowed? */
92 
93 	bool		takenDuringRecovery;	/* recovery-shaped snapshot? */
94 	bool		copied;			/* false if it's a static snapshot */
95 
96 	CommandId	curcid;			/* in my xact, CID < curcid are visible */
97 
98 	/*
99 	 * An extra return value for HeapTupleSatisfiesDirty, not used in MVCC
100 	 * snapshots.
101 	 */
102 	uint32		speculativeToken;
103 
104 	/*
105 	 * Book-keeping information, used by the snapshot manager
106 	 */
107 	uint32		active_count;	/* refcount on ActiveSnapshot stack */
108 	uint32		regd_count;		/* refcount on RegisteredSnapshots */
109 	pairingheap_node ph_node;	/* link in the RegisteredSnapshots heap */
110 
111 	TimestampTz whenTaken;		/* timestamp when snapshot was taken */
112 	XLogRecPtr	lsn;			/* position in the WAL stream when taken */
113 } SnapshotData;
114 
115 /*
116  * Result codes for HeapTupleSatisfiesUpdate.  This should really be in
117  * tqual.h, but we want to avoid including that file elsewhere.
118  */
119 typedef enum
120 {
121 	HeapTupleMayBeUpdated,
122 	HeapTupleInvisible,
123 	HeapTupleSelfUpdated,
124 	HeapTupleUpdated,
125 	HeapTupleBeingUpdated,
126 	HeapTupleWouldBlock			/* can be returned by heap_tuple_lock */
127 } HTSU_Result;
128 
129 #endif							/* SNAPSHOT_H */
130