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-2019, 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 
56 extern bool FirstSnapshotSet;
57 
62 
63 /* Variables representing various special snapshot semantics */
67 
68 #define SnapshotSelf (&SnapshotSelfData)
69 #define SnapshotAny (&SnapshotAnyData)
70 
71 /*
72  * We don't provide a static SnapshotDirty variable because it would be
73  * non-reentrant. Instead, users of that snapshot type should declare a
74  * local variable of type SnapshotData, and initialize it with this macro.
75  */
76 #define InitDirtySnapshot(snapshotdata) \
77  ((snapshotdata).snapshot_type = SNAPSHOT_DIRTY)
78 
79 /*
80  * Similarly, some initialization is required for a NonVacuumable snapshot.
81  * The caller must supply the xmin horizon to use (e.g., RecentGlobalXmin).
82  */
83 #define InitNonVacuumableSnapshot(snapshotdata, xmin_horizon) \
84  ((snapshotdata).snapshot_type = SNAPSHOT_NON_VACUUMABLE, \
85  (snapshotdata).xmin = (xmin_horizon))
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 
102 extern Snapshot GetTransactionSnapshot(void);
103 extern Snapshot GetLatestSnapshot(void);
104 extern void SnapshotSetCommandId(CommandId curcid);
105 extern Snapshot GetOldestSnapshot(void);
106 
107 extern Snapshot GetCatalogSnapshot(Oid relid);
109 extern void InvalidateCatalogSnapshot(void);
111 
112 extern void PushActiveSnapshot(Snapshot snapshot);
113 extern void PushCopiedSnapshot(Snapshot snapshot);
114 extern void UpdateActiveSnapshotCommandId(void);
115 extern void PopActiveSnapshot(void);
116 extern Snapshot GetActiveSnapshot(void);
117 extern bool ActiveSnapshotSet(void);
118 
119 extern Snapshot RegisterSnapshot(Snapshot snapshot);
120 extern void UnregisterSnapshot(Snapshot snapshot);
122 extern void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner);
123 
125 
126 extern void AtSubCommit_Snapshot(int level);
127 extern void AtSubAbort_Snapshot(int level);
128 extern void AtEOXact_Snapshot(bool isCommit, bool resetXmin);
129 
130 extern void ImportSnapshot(const char *idstr);
131 extern bool XactHasExportedSnapshots(void);
132 extern void DeleteAllExportedSnapshotFiles(void);
133 extern bool ThereAreNoPriorRegisteredSnapshots(void);
135  Relation relation);
136 extern void MaintainOldSnapshotTimeMapping(TimestampTz whenTaken,
137  TransactionId xmin);
138 
139 extern char *ExportSnapshot(Snapshot snapshot);
140 
141 /*
142  * Utility functions for implementing visibility routines in table AMs.
143  */
144 extern bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
145 
146 /* Support for catalog timetravel for logical decoding */
147 struct HTAB;
148 extern struct HTAB *HistoricSnapshotGetTupleCids(void);
149 extern void SetupHistoricSnapshot(Snapshot snapshot_now, struct HTAB *tuplecids);
150 extern void TeardownHistoricSnapshot(bool is_error);
151 extern bool HistoricSnapshotActive(void);
152 
153 extern Size EstimateSnapshotSpace(Snapshot snapshot);
154 extern void SerializeSnapshot(Snapshot snapshot, char *start_address);
155 extern Snapshot RestoreSnapshot(char *start_address);
156 extern void RestoreTransactionSnapshot(Snapshot snapshot, void *master_pgproc);
157 
158 #endif /* SNAPMGR_H */
uint32 CommandId
Definition: c.h:521
void AtSubCommit_Snapshot(int level)
Definition: snapmgr.c:1034
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:381
TransactionId TransactionIdLimitedForOldSnapshots(TransactionId recentXmin, Relation relation)
Definition: snapmgr.c:1775
void AtSubAbort_Snapshot(int level)
Definition: snapmgr.c:1055
Snapshot RestoreSnapshot(char *start_address)
Definition: snapmgr.c:2161
uint32 TransactionId
Definition: c.h:507
struct HTAB * HistoricSnapshotGetTupleCids(void)
Definition: snapmgr.c:2064
int64 TimestampTz
Definition: timestamp.h:39
void TeardownHistoricSnapshot(bool is_error)
Definition: snapmgr.c:2051
bool XactHasExportedSnapshots(void)
Definition: snapmgr.c:1637
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:841
Size EstimateSnapshotSpace(Snapshot snapshot)
Definition: snapmgr.c:2078
void PopActiveSnapshot(void)
Definition: snapmgr.c:814
unsigned int Oid
Definition: postgres_ext.h:31
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:865
#define PGDLLIMPORT
Definition: c.h:1243
char * ExportSnapshot(Snapshot snapshot)
Definition: snapmgr.c:1191
PGDLLIMPORT TransactionId RecentXmin
Definition: snapmgr.c:167
void RestoreTransactionSnapshot(Snapshot snapshot, void *master_pgproc)
Definition: snapmgr.c:2225
void SerializeSnapshot(Snapshot snapshot, char *start_address)
Definition: snapmgr.c:2102
Definition: dynahash.c:208
Snapshot GetOldestSnapshot(void)
Definition: snapmgr.c:413
void DeleteAllExportedSnapshotFiles(void)
Definition: snapmgr.c:1650
PGDLLIMPORT int old_snapshot_threshold
Definition: snapmgr.c:75
Size SnapMgrShmemSize(void)
Definition: snapmgr.c:253
PGDLLIMPORT SnapshotData SnapshotSelfData
Definition: snapmgr.c:147
void SetupHistoricSnapshot(Snapshot snapshot_now, struct HTAB *tuplecids)
Definition: snapmgr.c:2035
void PushActiveSnapshot(Snapshot snapshot)
Definition: snapmgr.c:735
void ImportSnapshot(const char *idstr)
Definition: snapmgr.c:1462
void MaintainOldSnapshotTimeMapping(TimestampTz whenTaken, TransactionId xmin)
Definition: snapmgr.c:1882
TimestampTz GetSnapshotCurrentTimestamp(void)
Definition: snapmgr.c:1720
void SnapshotSetCommandId(CommandId curcid)
Definition: snapmgr.c:546
bool ThereAreNoPriorRegisteredSnapshots(void)
Definition: snapmgr.c:1689
TimestampTz GetOldSnapshotThresholdTimestamp(void)
Definition: snapmgr.c:1745
bool HistoricSnapshotActive(void)
Definition: snapmgr.c:2058
bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
Definition: snapmgr.c:2241
bool FirstSnapshotSet
Definition: snapmgr.c:205
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1091
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:853
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:306
Snapshot GetCatalogSnapshot(Oid relid)
Definition: snapmgr.c:442
FullTransactionId GetFullRecentGlobalXmin(void)
Definition: snapmgr.c:963
size_t Size
Definition: c.h:466
PGDLLIMPORT TransactionId RecentGlobalDataXmin
Definition: snapmgr.c:169
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:512
PGDLLIMPORT SnapshotData SnapshotAnyData
Definition: snapmgr.c:148
PGDLLIMPORT TransactionId RecentGlobalXmin
Definition: snapmgr.c:168
Snapshot GetNonHistoricCatalogSnapshot(Oid relid)
Definition: snapmgr.c:464
Snapshot RegisterSnapshotOnOwner(Snapshot snapshot, ResourceOwner owner)
Definition: snapmgr.c:878
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:783
PGDLLIMPORT SnapshotData CatalogSnapshotData
Definition: snapmgr.c:146
void InvalidateCatalogSnapshotConditionally(void)
Definition: snapmgr.c:533
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:907
void SnapMgrInit(void)
Definition: snapmgr.c:269
void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner)
Definition: snapmgr.c:920
PGDLLIMPORT TransactionId TransactionXmin
Definition: snapmgr.c:166
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:771