1 /*-------------------------------------------------------------------------
2  *
3  * snapmgr.h
4  *	  POSTGRES snapshot manager
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/snapmgr.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef SNAPMGR_H
14 #define SNAPMGR_H
15 
16 #include "fmgr.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 */
48 extern PGDLLIMPORT int old_snapshot_threshold;
49 
50 
51 extern Size SnapMgrShmemSize(void);
52 extern void SnapMgrInit(void);
53 extern TimestampTz GetSnapshotCurrentTimestamp(void);
54 extern TimestampTz GetOldSnapshotThresholdTimestamp(void);
55 
56 extern bool FirstSnapshotSet;
57 
58 extern PGDLLIMPORT TransactionId TransactionXmin;
59 extern PGDLLIMPORT TransactionId RecentXmin;
60 extern PGDLLIMPORT TransactionId RecentGlobalXmin;
61 extern PGDLLIMPORT TransactionId RecentGlobalDataXmin;
62 
63 extern Snapshot GetTransactionSnapshot(void);
64 extern Snapshot GetLatestSnapshot(void);
65 extern void SnapshotSetCommandId(CommandId curcid);
66 extern Snapshot GetOldestSnapshot(void);
67 
68 extern Snapshot GetCatalogSnapshot(Oid relid);
69 extern Snapshot GetNonHistoricCatalogSnapshot(Oid relid);
70 extern void InvalidateCatalogSnapshot(void);
71 extern void InvalidateCatalogSnapshotConditionally(void);
72 
73 extern void PushActiveSnapshot(Snapshot snapshot);
74 extern void PushCopiedSnapshot(Snapshot snapshot);
75 extern void UpdateActiveSnapshotCommandId(void);
76 extern void PopActiveSnapshot(void);
77 extern Snapshot GetActiveSnapshot(void);
78 extern bool ActiveSnapshotSet(void);
79 
80 extern Snapshot RegisterSnapshot(Snapshot snapshot);
81 extern void UnregisterSnapshot(Snapshot snapshot);
82 extern Snapshot RegisterSnapshotOnOwner(Snapshot snapshot, ResourceOwner owner);
83 extern void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner);
84 
85 extern void AtSubCommit_Snapshot(int level);
86 extern void AtSubAbort_Snapshot(int level);
87 extern void AtEOXact_Snapshot(bool isCommit, bool resetXmin);
88 
89 extern void ImportSnapshot(const char *idstr);
90 extern bool XactHasExportedSnapshots(void);
91 extern void DeleteAllExportedSnapshotFiles(void);
92 extern bool ThereAreNoPriorRegisteredSnapshots(void);
93 extern TransactionId TransactionIdLimitedForOldSnapshots(TransactionId recentXmin,
94 									Relation relation);
95 extern void MaintainOldSnapshotTimeMapping(TimestampTz whenTaken,
96 							   TransactionId xmin);
97 
98 extern char *ExportSnapshot(Snapshot snapshot);
99 
100 /* Support for catalog timetravel for logical decoding */
101 struct HTAB;
102 extern struct HTAB *HistoricSnapshotGetTupleCids(void);
103 extern void SetupHistoricSnapshot(Snapshot snapshot_now, struct HTAB *tuplecids);
104 extern void TeardownHistoricSnapshot(bool is_error);
105 extern bool HistoricSnapshotActive(void);
106 
107 extern Size EstimateSnapshotSpace(Snapshot snapshot);
108 extern void SerializeSnapshot(Snapshot snapshot, char *start_address);
109 extern Snapshot RestoreSnapshot(char *start_address);
110 extern void RestoreTransactionSnapshot(Snapshot snapshot, void *master_pgproc);
111 
112 #endif							/* SNAPMGR_H */
113