codekingpro/portable-devtools
115k
1/*-------------------------------------------------------------------------2 *3 * snapmgr.h4 * POSTGRES snapshot manager5 *6 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group7 * Portions Copyright (c) 1994, Regents of the University of California8 *9 * src/include/utils/snapmgr.h10 *11 *-------------------------------------------------------------------------12 */13#ifndef SNAPMGR_H14#define SNAPMGR_H15 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 "snapshot24 * 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 or26 * vacuum will not usually occur. It is best if this padding is for a number27 * of minutes greater than a thread would normally be stalled, but it's OK if28 * early vacuum opportunities are occasionally missed, so there's no need to29 * use an extreme value or get too fancy. 10 minutes seems plenty.30 */31#define OLD_SNAPSHOT_PADDING_ENTRIES 1032#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/vacuuming36 * 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 */47extern PGDLLIMPORT int old_snapshot_threshold;48 49 50extern Size SnapMgrShmemSize(void);51extern void SnapMgrInit(void);52extern TimestampTz GetSnapshotCurrentTimestamp(void);53extern TimestampTz GetOldSnapshotThresholdTimestamp(void);54extern void SnapshotTooOldMagicForTest(void);55 56extern PGDLLIMPORT bool FirstSnapshotSet;57 58extern PGDLLIMPORT TransactionId TransactionXmin;59extern PGDLLIMPORT TransactionId RecentXmin;60 61/* Variables representing various special snapshot semantics */62extern PGDLLIMPORT SnapshotData SnapshotSelfData;63extern PGDLLIMPORT SnapshotData SnapshotAnyData;64extern PGDLLIMPORT SnapshotData CatalogSnapshotData;65 66#define SnapshotSelf (&SnapshotSelfData)67#define SnapshotAny (&SnapshotAnyData)68 69/*70 * We don't provide a static SnapshotDirty variable because it would be71 * non-reentrant. Instead, users of that snapshot type should declare a72 * 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 need88 * 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 FRONTEND101static inline bool102OldSnapshotThresholdActive(void)103{104 return old_snapshot_threshold >= 0;105}106#endif107 108extern Snapshot GetTransactionSnapshot(void);109extern Snapshot GetLatestSnapshot(void);110extern void SnapshotSetCommandId(CommandId curcid);111extern Snapshot GetOldestSnapshot(void);112 113extern Snapshot GetCatalogSnapshot(Oid relid);114extern Snapshot GetNonHistoricCatalogSnapshot(Oid relid);115extern void InvalidateCatalogSnapshot(void);116extern void InvalidateCatalogSnapshotConditionally(void);117 118extern void PushActiveSnapshot(Snapshot snapshot);119extern void PushActiveSnapshotWithLevel(Snapshot snapshot, int snap_level);120extern void PushCopiedSnapshot(Snapshot snapshot);121extern void UpdateActiveSnapshotCommandId(void);122extern void PopActiveSnapshot(void);123extern Snapshot GetActiveSnapshot(void);124extern bool ActiveSnapshotSet(void);125 126extern Snapshot RegisterSnapshot(Snapshot snapshot);127extern void UnregisterSnapshot(Snapshot snapshot);128extern Snapshot RegisterSnapshotOnOwner(Snapshot snapshot, ResourceOwner owner);129extern void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner);130 131extern void AtSubCommit_Snapshot(int level);132extern void AtSubAbort_Snapshot(int level);133extern void AtEOXact_Snapshot(bool isCommit, bool resetXmin);134 135extern void ImportSnapshot(const char *idstr);136extern bool XactHasExportedSnapshots(void);137extern void DeleteAllExportedSnapshotFiles(void);138extern void WaitForOlderSnapshots(TransactionId limitXmin, bool progress);139extern bool ThereAreNoPriorRegisteredSnapshots(void);140extern bool HaveRegisteredOrActiveSnapshot(void);141extern bool TransactionIdLimitedForOldSnapshots(TransactionId recentXmin,142 Relation relation,143 TransactionId *limit_xid,144 TimestampTz *limit_ts);145extern void SetOldSnapshotThresholdTimestamp(TimestampTz ts, TransactionId xlimit);146extern void MaintainOldSnapshotTimeMapping(TimestampTz whenTaken,147 TransactionId xmin);148 149extern char *ExportSnapshot(Snapshot snapshot);150 151/*152 * These live in procarray.c because they're intimately linked to the153 * procarray contents, but thematically they better fit into snapmgr.h.154 */155typedef struct GlobalVisState GlobalVisState;156extern GlobalVisState *GlobalVisTestFor(Relation rel);157extern bool GlobalVisTestIsRemovableXid(GlobalVisState *state, TransactionId xid);158extern bool GlobalVisTestIsRemovableFullXid(GlobalVisState *state, FullTransactionId fxid);159extern FullTransactionId GlobalVisTestNonRemovableFullHorizon(GlobalVisState *state);160extern TransactionId GlobalVisTestNonRemovableHorizon(GlobalVisState *state);161extern bool GlobalVisCheckRemovableXid(Relation rel, TransactionId xid);162extern bool GlobalVisCheckRemovableFullXid(Relation rel, FullTransactionId fxid);163 164/*165 * Utility functions for implementing visibility routines in table AMs.166 */167extern bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);168 169/* Support for catalog timetravel for logical decoding */170struct HTAB;171extern struct HTAB *HistoricSnapshotGetTupleCids(void);172extern void SetupHistoricSnapshot(Snapshot historic_snapshot, struct HTAB *tuplecids);173extern void TeardownHistoricSnapshot(bool is_error);174extern bool HistoricSnapshotActive(void);175 176extern Size EstimateSnapshotSpace(Snapshot snapshot);177extern void SerializeSnapshot(Snapshot snapshot, char *start_address);178extern Snapshot RestoreSnapshot(char *start_address);179extern void RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc);180 181#endif /* SNAPMGR_H */182 