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-2023, 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  RelationIsPermanent(rel) && !IsCatalogRelation(rel) \
41  && !RelationIsAccessibleInLogicalDecoding(rel) \
42 )
43 
44 #define EarlyPruningEnabled(rel) (old_snapshot_threshold >= 0 && RelationAllowsEarlyPruning(rel))
45 
46 /* GUC variables */
48 
49 
50 extern Size SnapMgrShmemSize(void);
51 extern void SnapMgrInit(void);
54 extern void SnapshotTooOldMagicForTest(void);
55 
56 extern PGDLLIMPORT bool FirstSnapshotSet;
57 
60 
61 /* Variables representing various special snapshot semantics */
65 
66 #define SnapshotSelf (&SnapshotSelfData)
67 #define SnapshotAny (&SnapshotAnyData)
68 
69 /*
70  * We don't provide a static SnapshotDirty variable because it would be
71  * non-reentrant. Instead, users of that snapshot type should declare a
72  * local variable of type SnapshotData, and initialize it with this macro.
73  */
74 #define InitDirtySnapshot(snapshotdata) \
75  ((snapshotdata).snapshot_type = SNAPSHOT_DIRTY)
76 
77 /*
78  * Similarly, some initialization is required for a NonVacuumable snapshot.
79  * The caller must supply the visibility cutoff state to use (c.f.
80  * GlobalVisTestFor()).
81  */
82 #define InitNonVacuumableSnapshot(snapshotdata, vistestp) \
83  ((snapshotdata).snapshot_type = SNAPSHOT_NON_VACUUMABLE, \
84  (snapshotdata).vistest = (vistestp))
85 
86 /*
87  * Similarly, some initialization is required for SnapshotToast. We need
88  * to set lsn and whenTaken correctly to support snapshot_too_old.
89  */
90 #define InitToastSnapshot(snapshotdata, l, w) \
91  ((snapshotdata).snapshot_type = SNAPSHOT_TOAST, \
92  (snapshotdata).lsn = (l), \
93  (snapshotdata).whenTaken = (w))
94 
95 /* This macro encodes the knowledge of which snapshots are MVCC-safe */
96 #define IsMVCCSnapshot(snapshot) \
97  ((snapshot)->snapshot_type == SNAPSHOT_MVCC || \
98  (snapshot)->snapshot_type == SNAPSHOT_HISTORIC_MVCC)
99 
100 #ifndef FRONTEND
101 static inline bool
103 {
104  return old_snapshot_threshold >= 0;
105 }
106 #endif
107 
108 extern Snapshot GetTransactionSnapshot(void);
109 extern Snapshot GetLatestSnapshot(void);
110 extern void SnapshotSetCommandId(CommandId curcid);
111 extern Snapshot GetOldestSnapshot(void);
112 
113 extern Snapshot GetCatalogSnapshot(Oid relid);
115 extern void InvalidateCatalogSnapshot(void);
117 
118 extern void PushActiveSnapshot(Snapshot snapshot);
119 extern void PushActiveSnapshotWithLevel(Snapshot snapshot, int snap_level);
120 extern void PushCopiedSnapshot(Snapshot snapshot);
121 extern void UpdateActiveSnapshotCommandId(void);
122 extern void PopActiveSnapshot(void);
123 extern Snapshot GetActiveSnapshot(void);
124 extern bool ActiveSnapshotSet(void);
125 
126 extern Snapshot RegisterSnapshot(Snapshot snapshot);
127 extern void UnregisterSnapshot(Snapshot snapshot);
129 extern void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner);
130 
131 extern void AtSubCommit_Snapshot(int level);
132 extern void AtSubAbort_Snapshot(int level);
133 extern void AtEOXact_Snapshot(bool isCommit, bool resetXmin);
134 
135 extern void ImportSnapshot(const char *idstr);
136 extern bool XactHasExportedSnapshots(void);
137 extern void DeleteAllExportedSnapshotFiles(void);
138 extern void WaitForOlderSnapshots(TransactionId limitXmin, bool progress);
139 extern bool ThereAreNoPriorRegisteredSnapshots(void);
140 extern bool HaveRegisteredOrActiveSnapshot(void);
142  Relation relation,
143  TransactionId *limit_xid,
144  TimestampTz *limit_ts);
146 extern void MaintainOldSnapshotTimeMapping(TimestampTz whenTaken,
147  TransactionId xmin);
148 
149 extern char *ExportSnapshot(Snapshot snapshot);
150 
151 /*
152  * These live in procarray.c because they're intimately linked to the
153  * procarray contents, but thematically they better fit into snapmgr.h.
154  */
155 typedef struct GlobalVisState GlobalVisState;
163 
164 /*
165  * Utility functions for implementing visibility routines in table AMs.
166  */
167 extern bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
168 
169 /* Support for catalog timetravel for logical decoding */
170 struct HTAB;
171 extern struct HTAB *HistoricSnapshotGetTupleCids(void);
172 extern void SetupHistoricSnapshot(Snapshot historic_snapshot, struct HTAB *tuplecids);
173 extern void TeardownHistoricSnapshot(bool is_error);
174 extern bool HistoricSnapshotActive(void);
175 
176 extern Size EstimateSnapshotSpace(Snapshot snapshot);
177 extern void SerializeSnapshot(Snapshot snapshot, char *start_address);
178 extern Snapshot RestoreSnapshot(char *start_address);
179 extern void RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc);
180 
181 #endif /* SNAPMGR_H */
#define PGDLLIMPORT
Definition: c.h:1303
uint32 CommandId
Definition: c.h:650
uint32 TransactionId
Definition: c.h:636
size_t Size
Definition: c.h:589
int64 TimestampTz
Definition: timestamp.h:39
int progress
Definition: pgbench.c:271
unsigned int Oid
Definition: postgres_ext.h:31
void SnapshotTooOldMagicForTest(void)
Definition: snapmgr.c:1734
void MaintainOldSnapshotTimeMapping(TimestampTz whenTaken, TransactionId xmin)
Definition: snapmgr.c:1903
bool GlobalVisTestIsRemovableFullXid(GlobalVisState *state, FullTransactionId fxid)
Definition: procarray.c:4206
void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner)
Definition: snapmgr.c:884
bool GlobalVisTestIsRemovableXid(GlobalVisState *state, TransactionId xid)
Definition: procarray.c:4248
bool GlobalVisCheckRemovableFullXid(Relation rel, FullTransactionId fxid)
Definition: procarray.c:4300
TimestampTz GetSnapshotCurrentTimestamp(void)
Definition: snapmgr.c:1680
struct HTAB * HistoricSnapshotGetTupleCids(void)
Definition: snapmgr.c:2109
void AtSubAbort_Snapshot(int level)
Definition: snapmgr.c:989
void SerializeSnapshot(Snapshot snapshot, char *start_address)
Definition: snapmgr.c:2147
void SnapMgrInit(void)
Definition: snapmgr.c:214
void AtEOXact_Snapshot(bool isCommit, bool resetXmin)
Definition: snapmgr.c:1025
bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
Definition: snapmgr.c:2287
static bool OldSnapshotThresholdActive(void)
Definition: snapmgr.h:102
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:251
Snapshot GetLatestSnapshot(void)
Definition: snapmgr.c:326
void TeardownHistoricSnapshot(bool is_error)
Definition: snapmgr.c:2096
Snapshot GetCatalogSnapshot(Oid relid)
Definition: snapmgr.c:387
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:871
void PushActiveSnapshot(Snapshot snapshot)
Definition: snapmgr.c:683
Snapshot RestoreSnapshot(char *start_address)
Definition: snapmgr.c:2206
void AtSubCommit_Snapshot(int level)
Definition: snapmgr.c:968
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:747
PGDLLIMPORT int old_snapshot_threshold
Definition: snapmgr.c:79
PGDLLIMPORT TransactionId TransactionXmin
Definition: snapmgr.c:113
TransactionId GlobalVisTestNonRemovableHorizon(GlobalVisState *state)
Definition: procarray.c:4286
char * ExportSnapshot(Snapshot snapshot)
Definition: snapmgr.c:1125
bool HistoricSnapshotActive(void)
Definition: snapmgr.c:2103
void ImportSnapshot(const char *idstr)
Definition: snapmgr.c:1396
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:817
PGDLLIMPORT SnapshotData SnapshotAnyData
Definition: snapmgr.c:100
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:829
bool XactHasExportedSnapshots(void)
Definition: snapmgr.c:1571
GlobalVisState * GlobalVisTestFor(Relation rel)
Definition: procarray.c:4091
void DeleteAllExportedSnapshotFiles(void)
Definition: snapmgr.c:1584
PGDLLIMPORT SnapshotData CatalogSnapshotData
Definition: snapmgr.c:98
bool GlobalVisCheckRemovableXid(Relation rel, TransactionId xid)
Definition: procarray.c:4314
bool HaveRegisteredOrActiveSnapshot(void)
Definition: snapmgr.c:1641
void InvalidateCatalogSnapshotConditionally(void)
Definition: snapmgr.c:478
bool ThereAreNoPriorRegisteredSnapshots(void)
Definition: snapmgr.c:1623
void RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc)
Definition: snapmgr.c:2271
bool TransactionIdLimitedForOldSnapshots(TransactionId recentXmin, Relation relation, TransactionId *limit_xid, TimestampTz *limit_ts)
Definition: snapmgr.c:1796
void SnapshotSetCommandId(CommandId curcid)
Definition: snapmgr.c:491
void PopActiveSnapshot(void)
Definition: snapmgr.c:778
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:735
PGDLLIMPORT SnapshotData SnapshotSelfData
Definition: snapmgr.c:99
Size EstimateSnapshotSpace(Snapshot snapshot)
Definition: snapmgr.c:2123
void WaitForOlderSnapshots(TransactionId limitXmin, bool progress)
Definition: indexcmds.c:423
TimestampTz GetOldSnapshotThresholdTimestamp(void)
Definition: snapmgr.c:1705
void SetupHistoricSnapshot(Snapshot historic_snapshot, struct HTAB *tuplecids)
Definition: snapmgr.c:2080
PGDLLIMPORT bool FirstSnapshotSet
Definition: snapmgr.c:150
Snapshot RegisterSnapshotOnOwner(Snapshot snapshot, ResourceOwner owner)
Definition: snapmgr.c:842
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:457
void PushActiveSnapshotWithLevel(Snapshot snapshot, int snap_level)
Definition: snapmgr.c:697
Snapshot GetNonHistoricCatalogSnapshot(Oid relid)
Definition: snapmgr.c:409
FullTransactionId GlobalVisTestNonRemovableFullHorizon(GlobalVisState *state)
Definition: procarray.c:4275
PGDLLIMPORT TransactionId RecentXmin
Definition: snapmgr.c:114
Size SnapMgrShmemSize(void)
Definition: snapmgr.c:198
Snapshot GetOldestSnapshot(void)
Definition: snapmgr.c:358
void SetOldSnapshotThresholdTimestamp(TimestampTz ts, TransactionId xlimit)
Definition: snapmgr.c:1717
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:805
Definition: dynahash.c:220
Definition: regguts.h:318