snapshot.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*-------------------------------------------------------------------------
  2. *
  3. * snapshot.h
  4. * POSTGRES snapshot definition
  5. *
  6. * Portions Copyright (c) 1996-2016, 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 "lib/pairingheap.h"
  18. #include "storage/buf.h"
  19. typedef struct SnapshotData *Snapshot;
  20. #define InvalidSnapshot ((Snapshot) NULL)
  21. /*
  22. * We use SnapshotData structures to represent both "regular" (MVCC)
  23. * snapshots and "special" snapshots that have non-MVCC semantics.
  24. * The specific semantics of a snapshot are encoded by the "satisfies"
  25. * function.
  26. */
  27. typedef bool (*SnapshotSatisfiesFunc) (HeapTuple htup,
  28. Snapshot snapshot, Buffer buffer);
  29. /*
  30. * Struct representing all kind of possible snapshots.
  31. *
  32. * There are several different kinds of snapshots:
  33. * * Normal MVCC snapshots
  34. * * MVCC snapshots taken during recovery (in Hot-Standby mode)
  35. * * Historic MVCC snapshots used during logical decoding
  36. * * snapshots passed to HeapTupleSatisfiesDirty()
  37. * * snapshots used for SatisfiesAny, Toast, Self where no members are
  38. * accessed.
  39. *
  40. * TODO: It's probably a good idea to split this struct using a NodeTag
  41. * similar to how parser and executor nodes are handled, with one type for
  42. * each different kind of snapshot to avoid overloading the meaning of
  43. * individual fields.
  44. */
  45. typedef struct SnapshotData
  46. {
  47. SnapshotSatisfiesFunc satisfies; /* tuple test function */
  48. /*
  49. * The remaining fields are used only for MVCC snapshots, and are normally
  50. * just zeroes in special snapshots. (But xmin and xmax are used
  51. * specially by HeapTupleSatisfiesDirty.)
  52. *
  53. * An MVCC snapshot can never see the effects of XIDs >= xmax. It can see
  54. * the effects of all older XIDs except those listed in the snapshot. xmin
  55. * is stored as an optimization to avoid needing to search the XID arrays
  56. * for most tuples.
  57. */
  58. TransactionId xmin; /* all XID < xmin are visible to me */
  59. TransactionId xmax; /* all XID >= xmax are invisible to me */
  60. /*
  61. * For normal MVCC snapshot this contains the all xact IDs that are in
  62. * progress, unless the snapshot was taken during recovery in which case
  63. * it's empty. For historic MVCC snapshots, the meaning is inverted, i.e.
  64. * it contains *committed* transactions between xmin and xmax.
  65. *
  66. * note: all ids in xip[] satisfy xmin <= xip[i] < xmax
  67. */
  68. TransactionId *xip;
  69. uint32 xcnt; /* # of xact ids in xip[] */
  70. /*
  71. * For non-historic MVCC snapshots, this contains subxact IDs that are in
  72. * progress (and other transactions that are in progress if taken during
  73. * recovery). For historic snapshot it contains *all* xids assigned to the
  74. * replayed transaction, including the toplevel xid.
  75. *
  76. * note: all ids in subxip[] are >= xmin, but we don't bother filtering
  77. * out any that are >= xmax
  78. */
  79. TransactionId *subxip;
  80. int32 subxcnt; /* # of xact ids in subxip[] */
  81. bool suboverflowed; /* has the subxip array overflowed? */
  82. bool takenDuringRecovery; /* recovery-shaped snapshot? */
  83. bool copied; /* false if it's a static snapshot */
  84. CommandId curcid; /* in my xact, CID < curcid are visible */
  85. /*
  86. * An extra return value for HeapTupleSatisfiesDirty, not used in MVCC
  87. * snapshots.
  88. */
  89. uint32 speculativeToken;
  90. /*
  91. * Book-keeping information, used by the snapshot manager
  92. */
  93. uint32 active_count; /* refcount on ActiveSnapshot stack */
  94. uint32 regd_count; /* refcount on RegisteredSnapshots */
  95. pairingheap_node ph_node; /* link in the RegisteredSnapshots heap */
  96. int64 whenTaken; /* timestamp when snapshot was taken */
  97. XLogRecPtr lsn; /* position in the WAL stream when taken */
  98. } SnapshotData;
  99. /*
  100. * Result codes for HeapTupleSatisfiesUpdate. This should really be in
  101. * tqual.h, but we want to avoid including that file elsewhere.
  102. */
  103. typedef enum
  104. {
  105. HeapTupleMayBeUpdated,
  106. HeapTupleInvisible,
  107. HeapTupleSelfUpdated,
  108. HeapTupleUpdated,
  109. HeapTupleBeingUpdated,
  110. HeapTupleWouldBlock /* can be returned by heap_tuple_lock */
  111. } HTSU_Result;
  112. #endif /* SNAPSHOT_H */