2
0

snapmgr.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*-------------------------------------------------------------------------
  2. *
  3. * snapmgr.h
  4. * POSTGRES snapshot manager
  5. *
  6. * Portions Copyright (c) 1996-2022, 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. #include "access/transam.h"
  16. #include "utils/relcache.h"
  17. #include "utils/resowner.h"
  18. #include "utils/snapshot.h"
  19. /*
  20. * The structure used to map times to TransactionId values for the "snapshot
  21. * too old" feature must have a few entries at the tail to hold old values;
  22. * otherwise the lookup will often fail and the expected early pruning or
  23. * vacuum will not usually occur. It is best if this padding is for a number
  24. * of minutes greater than a thread would normally be stalled, but it's OK if
  25. * early vacuum opportunities are occasionally missed, so there's no need to
  26. * use an extreme value or get too fancy. 10 minutes seems plenty.
  27. */
  28. #define OLD_SNAPSHOT_PADDING_ENTRIES 10
  29. #define OLD_SNAPSHOT_TIME_MAP_ENTRIES (old_snapshot_threshold + OLD_SNAPSHOT_PADDING_ENTRIES)
  30. /*
  31. * Common definition of relation properties that allow early pruning/vacuuming
  32. * when old_snapshot_threshold >= 0.
  33. */
  34. #define RelationAllowsEarlyPruning(rel) \
  35. ( \
  36. RelationIsPermanent(rel) && !IsCatalogRelation(rel) \
  37. && !RelationIsAccessibleInLogicalDecoding(rel) \
  38. )
  39. #define EarlyPruningEnabled(rel) (old_snapshot_threshold >= 0 && RelationAllowsEarlyPruning(rel))
  40. /* GUC variables */
  41. extern PGDLLIMPORT int old_snapshot_threshold;
  42. extern Size SnapMgrShmemSize(void);
  43. extern void SnapMgrInit(void);
  44. extern TimestampTz GetSnapshotCurrentTimestamp(void);
  45. extern TimestampTz GetOldSnapshotThresholdTimestamp(void);
  46. extern void SnapshotTooOldMagicForTest(void);
  47. extern PGDLLIMPORT bool FirstSnapshotSet;
  48. extern PGDLLIMPORT TransactionId TransactionXmin;
  49. extern PGDLLIMPORT TransactionId RecentXmin;
  50. /* Variables representing various special snapshot semantics */
  51. extern PGDLLIMPORT SnapshotData SnapshotSelfData;
  52. extern PGDLLIMPORT SnapshotData SnapshotAnyData;
  53. extern PGDLLIMPORT SnapshotData CatalogSnapshotData;
  54. #define SnapshotSelf (&SnapshotSelfData)
  55. #define SnapshotAny (&SnapshotAnyData)
  56. /*
  57. * We don't provide a static SnapshotDirty variable because it would be
  58. * non-reentrant. Instead, users of that snapshot type should declare a
  59. * local variable of type SnapshotData, and initialize it with this macro.
  60. */
  61. #define InitDirtySnapshot(snapshotdata) \
  62. ((snapshotdata).snapshot_type = SNAPSHOT_DIRTY)
  63. /*
  64. * Similarly, some initialization is required for a NonVacuumable snapshot.
  65. * The caller must supply the visibility cutoff state to use (c.f.
  66. * GlobalVisTestFor()).
  67. */
  68. #define InitNonVacuumableSnapshot(snapshotdata, vistestp) \
  69. ((snapshotdata).snapshot_type = SNAPSHOT_NON_VACUUMABLE, \
  70. (snapshotdata).vistest = (vistestp))
  71. /*
  72. * Similarly, some initialization is required for SnapshotToast. We need
  73. * to set lsn and whenTaken correctly to support snapshot_too_old.
  74. */
  75. #define InitToastSnapshot(snapshotdata, l, w) \
  76. ((snapshotdata).snapshot_type = SNAPSHOT_TOAST, \
  77. (snapshotdata).lsn = (l), \
  78. (snapshotdata).whenTaken = (w))
  79. /* This macro encodes the knowledge of which snapshots are MVCC-safe */
  80. #define IsMVCCSnapshot(snapshot) \
  81. ((snapshot)->snapshot_type == SNAPSHOT_MVCC || \
  82. (snapshot)->snapshot_type == SNAPSHOT_HISTORIC_MVCC)
  83. #ifndef FRONTEND
  84. static inline bool
  85. OldSnapshotThresholdActive(void)
  86. {
  87. return old_snapshot_threshold >= 0;
  88. }
  89. #endif
  90. extern Snapshot GetTransactionSnapshot(void);
  91. extern Snapshot GetLatestSnapshot(void);
  92. extern void SnapshotSetCommandId(CommandId curcid);
  93. extern Snapshot GetOldestSnapshot(void);
  94. extern Snapshot GetCatalogSnapshot(Oid relid);
  95. extern Snapshot GetNonHistoricCatalogSnapshot(Oid relid);
  96. extern void InvalidateCatalogSnapshot(void);
  97. extern void InvalidateCatalogSnapshotConditionally(void);
  98. extern void PushActiveSnapshot(Snapshot snapshot);
  99. extern void PushActiveSnapshotWithLevel(Snapshot snapshot, int snap_level);
  100. extern void PushCopiedSnapshot(Snapshot snapshot);
  101. extern void UpdateActiveSnapshotCommandId(void);
  102. extern void PopActiveSnapshot(void);
  103. extern Snapshot GetActiveSnapshot(void);
  104. extern bool ActiveSnapshotSet(void);
  105. extern Snapshot RegisterSnapshot(Snapshot snapshot);
  106. extern void UnregisterSnapshot(Snapshot snapshot);
  107. extern Snapshot RegisterSnapshotOnOwner(Snapshot snapshot, ResourceOwner owner);
  108. extern void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner);
  109. extern void AtSubCommit_Snapshot(int level);
  110. extern void AtSubAbort_Snapshot(int level);
  111. extern void AtEOXact_Snapshot(bool isCommit, bool resetXmin);
  112. extern void ImportSnapshot(const char *idstr);
  113. extern bool XactHasExportedSnapshots(void);
  114. extern void DeleteAllExportedSnapshotFiles(void);
  115. extern void WaitForOlderSnapshots(TransactionId limitXmin, bool progress);
  116. extern bool ThereAreNoPriorRegisteredSnapshots(void);
  117. extern bool HaveRegisteredOrActiveSnapshot(void);
  118. extern bool TransactionIdLimitedForOldSnapshots(TransactionId recentXmin,
  119. Relation relation,
  120. TransactionId *limit_xid,
  121. TimestampTz *limit_ts);
  122. extern void SetOldSnapshotThresholdTimestamp(TimestampTz ts, TransactionId xlimit);
  123. extern void MaintainOldSnapshotTimeMapping(TimestampTz whenTaken,
  124. TransactionId xmin);
  125. extern char *ExportSnapshot(Snapshot snapshot);
  126. /*
  127. * These live in procarray.c because they're intimately linked to the
  128. * procarray contents, but thematically they better fit into snapmgr.h.
  129. */
  130. typedef struct GlobalVisState GlobalVisState;
  131. extern GlobalVisState *GlobalVisTestFor(Relation rel);
  132. extern bool GlobalVisTestIsRemovableXid(GlobalVisState *state, TransactionId xid);
  133. extern bool GlobalVisTestIsRemovableFullXid(GlobalVisState *state, FullTransactionId fxid);
  134. extern FullTransactionId GlobalVisTestNonRemovableFullHorizon(GlobalVisState *state);
  135. extern TransactionId GlobalVisTestNonRemovableHorizon(GlobalVisState *state);
  136. extern bool GlobalVisCheckRemovableXid(Relation rel, TransactionId xid);
  137. extern bool GlobalVisCheckRemovableFullXid(Relation rel, FullTransactionId fxid);
  138. /*
  139. * Utility functions for implementing visibility routines in table AMs.
  140. */
  141. extern bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
  142. /* Support for catalog timetravel for logical decoding */
  143. struct HTAB;
  144. extern struct HTAB *HistoricSnapshotGetTupleCids(void);
  145. extern void SetupHistoricSnapshot(Snapshot snapshot_now, struct HTAB *tuplecids);
  146. extern void TeardownHistoricSnapshot(bool is_error);
  147. extern bool HistoricSnapshotActive(void);
  148. extern Size EstimateSnapshotSpace(Snapshot snapshot);
  149. extern void SerializeSnapshot(Snapshot snapshot, char *start_address);
  150. extern Snapshot RestoreSnapshot(char *start_address);
  151. extern void RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc);
  152. #endif /* SNAPMGR_H */