PostgreSQL Source Code  git master
snapmgr.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * snapmgr.h
4  * POSTGRES snapshot manager
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/utils/snapmgr.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef SNAPMGR_H
14 #define SNAPMGR_H
15 
16 #include "access/transam.h"
17 #include "utils/relcache.h"
18 #include "utils/resowner.h"
19 #include "utils/snapshot.h"
20 
21 
22 /*
23  * The structure used to map times to TransactionId values for the "snapshot
24  * too old" feature must have a few entries at the tail to hold old values;
25  * otherwise the lookup will often fail and the expected early pruning or
26  * vacuum will not usually occur. It is best if this padding is for a number
27  * of minutes greater than a thread would normally be stalled, but it's OK if
28  * early vacuum opportunities are occasionally missed, so there's no need to
29  * use an extreme value or get too fancy. 10 minutes seems plenty.
30  */
31 #define OLD_SNAPSHOT_PADDING_ENTRIES 10
32 #define OLD_SNAPSHOT_TIME_MAP_ENTRIES (old_snapshot_threshold + OLD_SNAPSHOT_PADDING_ENTRIES)
33 
34 /*
35  * Common definition of relation properties that allow early pruning/vacuuming
36  * when old_snapshot_threshold >= 0.
37  */
38 #define RelationAllowsEarlyPruning(rel) \
39 ( \
40  RelationNeedsWAL(rel) \
41  && !IsCatalogRelation(rel) \
42  && !RelationIsAccessibleInLogicalDecoding(rel) \
43 )
44 
45 #define EarlyPruningEnabled(rel) (old_snapshot_threshold >= 0 && RelationAllowsEarlyPruning(rel))
46 
47 /* GUC variables */
49 
50 
51 extern Size SnapMgrShmemSize(void);
52 extern void SnapMgrInit(void);
55 extern void SnapshotTooOldMagicForTest(void);
56 
57 extern bool FirstSnapshotSet;
58 
61 
62 /* Variables representing various special snapshot semantics */
66 
67 #define SnapshotSelf (&SnapshotSelfData)
68 #define SnapshotAny (&SnapshotAnyData)
69 
70 /*
71  * We don't provide a static SnapshotDirty variable because it would be
72  * non-reentrant. Instead, users of that snapshot type should declare a
73  * local variable of type SnapshotData, and initialize it with this macro.
74  */
75 #define InitDirtySnapshot(snapshotdata) \
76  ((snapshotdata).snapshot_type = SNAPSHOT_DIRTY)
77 
78 /*
79  * Similarly, some initialization is required for a NonVacuumable snapshot.
80  * The caller must supply the visibility cutoff state to use (c.f.
81  * GlobalVisTestFor()).
82  */
83 #define InitNonVacuumableSnapshot(snapshotdata, vistestp) \
84  ((snapshotdata).snapshot_type = SNAPSHOT_NON_VACUUMABLE, \
85  (snapshotdata).vistest = (vistestp))
86 
87 /*
88  * Similarly, some initialization is required for SnapshotToast. We need
89  * to set lsn and whenTaken correctly to support snapshot_too_old.
90  */
91 #define InitToastSnapshot(snapshotdata, l, w) \
92  ((snapshotdata).snapshot_type = SNAPSHOT_TOAST, \
93  (snapshotdata).lsn = (l), \
94  (snapshotdata).whenTaken = (w))
95 
96 /* This macro encodes the knowledge of which snapshots are MVCC-safe */
97 #define IsMVCCSnapshot(snapshot) \
98  ((snapshot)->snapshot_type == SNAPSHOT_MVCC || \
99  (snapshot)->snapshot_type == SNAPSHOT_HISTORIC_MVCC)
100 
101 static inline bool
103 {
104  return old_snapshot_threshold >= 0;
105 }
106 
107 extern Snapshot GetTransactionSnapshot(void);
108 extern Snapshot GetLatestSnapshot(void);
109 extern void SnapshotSetCommandId(CommandId curcid);
110 extern Snapshot GetOldestSnapshot(void);
111 
112 extern Snapshot GetCatalogSnapshot(Oid relid);
114 extern void InvalidateCatalogSnapshot(void);
116 
117 extern void PushActiveSnapshot(Snapshot snapshot);
118 extern void PushCopiedSnapshot(Snapshot snapshot);
119 extern void UpdateActiveSnapshotCommandId(void);
120 extern void PopActiveSnapshot(void);
121 extern Snapshot GetActiveSnapshot(void);
122 extern bool ActiveSnapshotSet(void);
123 
124 extern Snapshot RegisterSnapshot(Snapshot snapshot);
125 extern void UnregisterSnapshot(Snapshot snapshot);
127 extern void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner);
128 
129 extern void AtSubCommit_Snapshot(int level);
130 extern void AtSubAbort_Snapshot(int level);
131 extern void AtEOXact_Snapshot(bool isCommit, bool resetXmin);
132 
133 extern void ImportSnapshot(const char *idstr);
134 extern bool XactHasExportedSnapshots(void);
135 extern void DeleteAllExportedSnapshotFiles(void);
136 extern bool ThereAreNoPriorRegisteredSnapshots(void);
138  Relation relation,
139  TransactionId *limit_xid,
140  TimestampTz *limit_ts);
142 extern void MaintainOldSnapshotTimeMapping(TimestampTz whenTaken,
143  TransactionId xmin);
144 
145 extern char *ExportSnapshot(Snapshot snapshot);
146 
147 /*
148  * These live in procarray.c because they're intimately linked to the
149  * procarray contents, but thematically they better fit into snapmgr.h.
150  */
159 
160 /*
161  * Utility functions for implementing visibility routines in table AMs.
162  */
163 extern bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
164 
165 /* Support for catalog timetravel for logical decoding */
166 struct HTAB;
167 extern struct HTAB *HistoricSnapshotGetTupleCids(void);
168 extern void SetupHistoricSnapshot(Snapshot snapshot_now, struct HTAB *tuplecids);
169 extern void TeardownHistoricSnapshot(bool is_error);
170 extern bool HistoricSnapshotActive(void);
171 
172 extern Size EstimateSnapshotSpace(Snapshot snapshot);
173 extern void SerializeSnapshot(Snapshot snapshot, char *start_address);
174 extern Snapshot RestoreSnapshot(char *start_address);
175 extern void RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc);
176 
177 #endif /* SNAPMGR_H */
bool GlobalVisCheckRemovableXid(Relation rel, TransactionId xid)
Definition: procarray.c:4094
uint32 CommandId
Definition: c.h:534
void AtSubCommit_Snapshot(int level)
Definition: snapmgr.c:949
bool TransactionIdLimitedForOldSnapshots(TransactionId recentXmin, Relation relation, TransactionId *limit_xid, TimestampTz *limit_ts)
Definition: snapmgr.c:1751
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:325
void AtSubAbort_Snapshot(int level)
Definition: snapmgr.c:970
Snapshot RestoreSnapshot(char *start_address)
Definition: snapmgr.c:2157
uint32 TransactionId
Definition: c.h:520
struct HTAB * HistoricSnapshotGetTupleCids(void)
Definition: snapmgr.c:2060
bool GlobalVisIsRemovableFullXid(Relation rel, FullTransactionId fxid)
Definition: procarray.c:4080
int64 TimestampTz
Definition: timestamp.h:39
bool GlobalVisTestIsRemovableFullXid(GlobalVisState *state, FullTransactionId fxid)
Definition: procarray.c:3986
void TeardownHistoricSnapshot(bool is_error)
Definition: snapmgr.c:2047
static bool OldSnapshotThresholdActive(void)
Definition: snapmgr.h:102
bool XactHasExportedSnapshots(void)
Definition: snapmgr.c:1552
void SetOldSnapshotThresholdTimestamp(TimestampTz ts, TransactionId xlimit)
Definition: snapmgr.c:1672
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:786
Size EstimateSnapshotSpace(Snapshot snapshot)
Definition: snapmgr.c:2074
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
unsigned int Oid
Definition: postgres_ext.h:31
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:810
#define PGDLLIMPORT
Definition: c.h:1257
char * ExportSnapshot(Snapshot snapshot)
Definition: snapmgr.c:1106
PGDLLIMPORT TransactionId RecentXmin
Definition: snapmgr.c:113
void SerializeSnapshot(Snapshot snapshot, char *start_address)
Definition: snapmgr.c:2098
Definition: dynahash.c:218
Snapshot GetOldestSnapshot(void)
Definition: snapmgr.c:357
void DeleteAllExportedSnapshotFiles(void)
Definition: snapmgr.c:1565
PGDLLIMPORT int old_snapshot_threshold
Definition: snapmgr.c:78
Size SnapMgrShmemSize(void)
Definition: snapmgr.c:197
TransactionId GlobalVisTestNonRemovableHorizon(GlobalVisState *state)
Definition: procarray.c:4066
PGDLLIMPORT SnapshotData SnapshotSelfData
Definition: snapmgr.c:98
void SnapshotTooOldMagicForTest(void)
Definition: snapmgr.c:1689
void SetupHistoricSnapshot(Snapshot snapshot_now, struct HTAB *tuplecids)
Definition: snapmgr.c:2031
void PushActiveSnapshot(Snapshot snapshot)
Definition: snapmgr.c:680
void ImportSnapshot(const char *idstr)
Definition: snapmgr.c:1377
void MaintainOldSnapshotTimeMapping(TimestampTz whenTaken, TransactionId xmin)
Definition: snapmgr.c:1854
TimestampTz GetSnapshotCurrentTimestamp(void)
Definition: snapmgr.c:1635
void SnapshotSetCommandId(CommandId curcid)
Definition: snapmgr.c:490
bool ThereAreNoPriorRegisteredSnapshots(void)
Definition: snapmgr.c:1604
TimestampTz GetOldSnapshotThresholdTimestamp(void)
Definition: snapmgr.c:1660
bool HistoricSnapshotActive(void)
Definition: snapmgr.c:2054
bool GlobalVisTestIsRemovableXid(GlobalVisState *state, TransactionId xid)
Definition: procarray.c:4028
bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
Definition: snapmgr.c:2238
bool FirstSnapshotSet
Definition: snapmgr.c:149
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1006
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:798
GlobalVisState * GlobalVisTestFor(Relation rel)
Definition: procarray.c:3866
Definition: regguts.h:298
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
Snapshot GetCatalogSnapshot(Oid relid)
Definition: snapmgr.c:386
size_t Size
Definition: c.h:473
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:456
void RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc)
Definition: snapmgr.c:2222
PGDLLIMPORT SnapshotData SnapshotAnyData
Definition: snapmgr.c:99
Snapshot GetNonHistoricCatalogSnapshot(Oid relid)
Definition: snapmgr.c:408
Snapshot RegisterSnapshotOnOwner(Snapshot snapshot, ResourceOwner owner)
Definition: snapmgr.c:823
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:728
PGDLLIMPORT SnapshotData CatalogSnapshotData
Definition: snapmgr.c:97
void InvalidateCatalogSnapshotConditionally(void)
Definition: snapmgr.c:477
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:852
void SnapMgrInit(void)
Definition: snapmgr.c:213
void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner)
Definition: snapmgr.c:865
PGDLLIMPORT TransactionId TransactionXmin
Definition: snapmgr.c:112
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:716
FullTransactionId GlobalVisTestNonRemovableFullHorizon(GlobalVisState *state)
Definition: procarray.c:4055