snapshot.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*-------------------------------------------------------------------------
  2. *
  3. * snapshot.h
  4. * POSTGRES snapshot definition
  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/snapshot.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef SNAPSHOT_H
  14. #define SNAPSHOT_H
  15. #include "access/htup.h"
  16. #include "access/xlogdefs.h"
  17. #include "datatype/timestamp.h"
  18. #include "lib/pairingheap.h"
  19. #include "storage/buf.h"
  20. /*
  21. * The different snapshot types. We use SnapshotData structures to represent
  22. * both "regular" (MVCC) snapshots and "special" snapshots that have non-MVCC
  23. * semantics. The specific semantics of a snapshot are encoded by its type.
  24. *
  25. * The behaviour of each type of snapshot should be documented alongside its
  26. * enum value, best in terms that are not specific to an individual table AM.
  27. *
  28. * The reason the snapshot type rather than a callback as it used to be is
  29. * that that allows to use the same snapshot for different table AMs without
  30. * having one callback per AM.
  31. */
  32. typedef enum SnapshotType
  33. {
  34. /*-------------------------------------------------------------------------
  35. * A tuple is visible iff the tuple is valid for the given MVCC snapshot.
  36. *
  37. * Here, we consider the effects of:
  38. * - all transactions committed as of the time of the given snapshot
  39. * - previous commands of this transaction
  40. *
  41. * Does _not_ include:
  42. * - transactions shown as in-progress by the snapshot
  43. * - transactions started after the snapshot was taken
  44. * - changes made by the current command
  45. * -------------------------------------------------------------------------
  46. */
  47. SNAPSHOT_MVCC = 0,
  48. /*-------------------------------------------------------------------------
  49. * A tuple is visible iff the tuple is valid "for itself".
  50. *
  51. * Here, we consider the effects of:
  52. * - all committed transactions (as of the current instant)
  53. * - previous commands of this transaction
  54. * - changes made by the current command
  55. *
  56. * Does _not_ include:
  57. * - in-progress transactions (as of the current instant)
  58. * -------------------------------------------------------------------------
  59. */
  60. SNAPSHOT_SELF,
  61. /*
  62. * Any tuple is visible.
  63. */
  64. SNAPSHOT_ANY,
  65. /*
  66. * A tuple is visible iff the tuple is valid as a TOAST row.
  67. */
  68. SNAPSHOT_TOAST,
  69. /*-------------------------------------------------------------------------
  70. * A tuple is visible iff the tuple is valid including effects of open
  71. * transactions.
  72. *
  73. * Here, we consider the effects of:
  74. * - all committed and in-progress transactions (as of the current instant)
  75. * - previous commands of this transaction
  76. * - changes made by the current command
  77. *
  78. * This is essentially like SNAPSHOT_SELF as far as effects of the current
  79. * transaction and committed/aborted xacts are concerned. However, it
  80. * also includes the effects of other xacts still in progress.
  81. *
  82. * A special hack is that when a snapshot of this type is used to
  83. * determine tuple visibility, the passed-in snapshot struct is used as an
  84. * output argument to return the xids of concurrent xacts that affected
  85. * the tuple. snapshot->xmin is set to the tuple's xmin if that is
  86. * another transaction that's still in progress; or to
  87. * InvalidTransactionId if the tuple's xmin is committed good, committed
  88. * dead, or my own xact. Similarly for snapshot->xmax and the tuple's
  89. * xmax. If the tuple was inserted speculatively, meaning that the
  90. * inserter might still back down on the insertion without aborting the
  91. * whole transaction, the associated token is also returned in
  92. * snapshot->speculativeToken. See also InitDirtySnapshot().
  93. * -------------------------------------------------------------------------
  94. */
  95. SNAPSHOT_DIRTY,
  96. /*
  97. * A tuple is visible iff it follows the rules of SNAPSHOT_MVCC, but
  98. * supports being called in timetravel context (for decoding catalog
  99. * contents in the context of logical decoding).
  100. */
  101. SNAPSHOT_HISTORIC_MVCC,
  102. /*
  103. * A tuple is visible iff the tuple might be visible to some transaction;
  104. * false if it's surely dead to everyone, i.e., vacuumable.
  105. *
  106. * For visibility checks snapshot->min must have been set up with the xmin
  107. * horizon to use.
  108. */
  109. SNAPSHOT_NON_VACUUMABLE
  110. } SnapshotType;
  111. typedef struct SnapshotData *Snapshot;
  112. #define InvalidSnapshot ((Snapshot) NULL)
  113. /*
  114. * Struct representing all kind of possible snapshots.
  115. *
  116. * There are several different kinds of snapshots:
  117. * * Normal MVCC snapshots
  118. * * MVCC snapshots taken during recovery (in Hot-Standby mode)
  119. * * Historic MVCC snapshots used during logical decoding
  120. * * snapshots passed to HeapTupleSatisfiesDirty()
  121. * * snapshots passed to HeapTupleSatisfiesNonVacuumable()
  122. * * snapshots used for SatisfiesAny, Toast, Self where no members are
  123. * accessed.
  124. *
  125. * TODO: It's probably a good idea to split this struct using a NodeTag
  126. * similar to how parser and executor nodes are handled, with one type for
  127. * each different kind of snapshot to avoid overloading the meaning of
  128. * individual fields.
  129. */
  130. typedef struct SnapshotData
  131. {
  132. SnapshotType snapshot_type; /* type of snapshot */
  133. /*
  134. * The remaining fields are used only for MVCC snapshots, and are normally
  135. * just zeroes in special snapshots. (But xmin and xmax are used
  136. * specially by HeapTupleSatisfiesDirty, and xmin is used specially by
  137. * HeapTupleSatisfiesNonVacuumable.)
  138. *
  139. * An MVCC snapshot can never see the effects of XIDs >= xmax. It can see
  140. * the effects of all older XIDs except those listed in the snapshot. xmin
  141. * is stored as an optimization to avoid needing to search the XID arrays
  142. * for most tuples.
  143. */
  144. TransactionId xmin; /* all XID < xmin are visible to me */
  145. TransactionId xmax; /* all XID >= xmax are invisible to me */
  146. /*
  147. * For normal MVCC snapshot this contains the all xact IDs that are in
  148. * progress, unless the snapshot was taken during recovery in which case
  149. * it's empty. For historic MVCC snapshots, the meaning is inverted, i.e.
  150. * it contains *committed* transactions between xmin and xmax.
  151. *
  152. * note: all ids in xip[] satisfy xmin <= xip[i] < xmax
  153. */
  154. TransactionId *xip;
  155. uint32 xcnt; /* # of xact ids in xip[] */
  156. /*
  157. * For non-historic MVCC snapshots, this contains subxact IDs that are in
  158. * progress (and other transactions that are in progress if taken during
  159. * recovery). For historic snapshot it contains *all* xids assigned to the
  160. * replayed transaction, including the toplevel xid.
  161. *
  162. * note: all ids in subxip[] are >= xmin, but we don't bother filtering
  163. * out any that are >= xmax
  164. */
  165. TransactionId *subxip;
  166. int32 subxcnt; /* # of xact ids in subxip[] */
  167. bool suboverflowed; /* has the subxip array overflowed? */
  168. bool takenDuringRecovery; /* recovery-shaped snapshot? */
  169. bool copied; /* false if it's a static snapshot */
  170. CommandId curcid; /* in my xact, CID < curcid are visible */
  171. /*
  172. * An extra return value for HeapTupleSatisfiesDirty, not used in MVCC
  173. * snapshots.
  174. */
  175. uint32 speculativeToken;
  176. /*
  177. * For SNAPSHOT_NON_VACUUMABLE (and hopefully more in the future) this is
  178. * used to determine whether row could be vacuumed.
  179. */
  180. struct GlobalVisState *vistest;
  181. /*
  182. * Book-keeping information, used by the snapshot manager
  183. */
  184. uint32 active_count; /* refcount on ActiveSnapshot stack */
  185. uint32 regd_count; /* refcount on RegisteredSnapshots */
  186. pairingheap_node ph_node; /* link in the RegisteredSnapshots heap */
  187. TimestampTz whenTaken; /* timestamp when snapshot was taken */
  188. XLogRecPtr lsn; /* position in the WAL stream when taken */
  189. /*
  190. * The transaction completion count at the time GetSnapshotData() built
  191. * this snapshot. Allows to avoid re-computing static snapshots when no
  192. * transactions completed since the last GetSnapshotData().
  193. */
  194. uint64 snapXactCompletionCount;
  195. } SnapshotData;
  196. #endif /* SNAPSHOT_H */