gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

209 linhas
6.7KB

  1. /*-------------------------------------------------------------------------
  2. * slot.h
  3. * Replication slot management.
  4. *
  5. * Copyright (c) 2012-2019, PostgreSQL Global Development Group
  6. *
  7. *-------------------------------------------------------------------------
  8. */
  9. #ifndef SLOT_H
  10. #define SLOT_H
  11. #include "fmgr.h"
  12. #include "access/xlog.h"
  13. #include "access/xlogreader.h"
  14. #include "storage/condition_variable.h"
  15. #include "storage/lwlock.h"
  16. #include "storage/shmem.h"
  17. #include "storage/spin.h"
  18. /*
  19. * Behaviour of replication slots, upon release or crash.
  20. *
  21. * Slots marked as PERSISTENT are crash-safe and will not be dropped when
  22. * released. Slots marked as EPHEMERAL will be dropped when released or after
  23. * restarts. Slots marked TEMPORARY will be dropped at the end of a session
  24. * or on error.
  25. *
  26. * EPHEMERAL is used as a not-quite-ready state when creating persistent
  27. * slots. EPHEMERAL slots can be made PERSISTENT by calling
  28. * ReplicationSlotPersist(). For a slot that goes away at the end of a
  29. * session, TEMPORARY is the appropriate choice.
  30. */
  31. typedef enum ReplicationSlotPersistency
  32. {
  33. RS_PERSISTENT,
  34. RS_EPHEMERAL,
  35. RS_TEMPORARY
  36. } ReplicationSlotPersistency;
  37. /*
  38. * On-Disk data of a replication slot, preserved across restarts.
  39. */
  40. typedef struct ReplicationSlotPersistentData
  41. {
  42. /* The slot's identifier */
  43. NameData name;
  44. /* database the slot is active on */
  45. Oid database;
  46. /*
  47. * The slot's behaviour when being dropped (or restored after a crash).
  48. */
  49. ReplicationSlotPersistency persistency;
  50. /*
  51. * xmin horizon for data
  52. *
  53. * NB: This may represent a value that hasn't been written to disk yet;
  54. * see notes for effective_xmin, below.
  55. */
  56. TransactionId xmin;
  57. /*
  58. * xmin horizon for catalog tuples
  59. *
  60. * NB: This may represent a value that hasn't been written to disk yet;
  61. * see notes for effective_xmin, below.
  62. */
  63. TransactionId catalog_xmin;
  64. /* oldest LSN that might be required by this replication slot */
  65. XLogRecPtr restart_lsn;
  66. /*
  67. * Oldest LSN that the client has acked receipt for. This is used as the
  68. * start_lsn point in case the client doesn't specify one, and also as a
  69. * safety measure to jump forwards in case the client specifies a
  70. * start_lsn that's further in the past than this value.
  71. */
  72. XLogRecPtr confirmed_flush;
  73. /* plugin name */
  74. NameData plugin;
  75. } ReplicationSlotPersistentData;
  76. /*
  77. * Shared memory state of a single replication slot.
  78. *
  79. * The in-memory data of replication slots follows a locking model based
  80. * on two linked concepts:
  81. * - A replication slot's in_use flag is switched when added or discarded using
  82. * the LWLock ReplicationSlotControlLock, which needs to be hold in exclusive
  83. * mode when updating the flag by the backend owning the slot and doing the
  84. * operation, while readers (concurrent backends not owning the slot) need
  85. * to hold it in shared mode when looking at replication slot data.
  86. * - Individual fields are protected by mutex where only the backend owning
  87. * the slot is authorized to update the fields from its own slot. The
  88. * backend owning the slot does not need to take this lock when reading its
  89. * own fields, while concurrent backends not owning this slot should take the
  90. * lock when reading this slot's data.
  91. */
  92. typedef struct ReplicationSlot
  93. {
  94. /* lock, on same cacheline as effective_xmin */
  95. slock_t mutex;
  96. /* is this slot defined */
  97. bool in_use;
  98. /* Who is streaming out changes for this slot? 0 in unused slots. */
  99. pid_t active_pid;
  100. /* any outstanding modifications? */
  101. bool just_dirtied;
  102. bool dirty;
  103. /*
  104. * For logical decoding, it's extremely important that we never remove any
  105. * data that's still needed for decoding purposes, even after a crash;
  106. * otherwise, decoding will produce wrong answers. Ordinary streaming
  107. * replication also needs to prevent old row versions from being removed
  108. * too soon, but the worst consequence we might encounter there is
  109. * unwanted query cancellations on the standby. Thus, for logical
  110. * decoding, this value represents the latest xmin that has actually been
  111. * written to disk, whereas for streaming replication, it's just the same
  112. * as the persistent value (data.xmin).
  113. */
  114. TransactionId effective_xmin;
  115. TransactionId effective_catalog_xmin;
  116. /* data surviving shutdowns and crashes */
  117. ReplicationSlotPersistentData data;
  118. /* is somebody performing io on this slot? */
  119. LWLock io_in_progress_lock;
  120. /* Condition variable signalled when active_pid changes */
  121. ConditionVariable active_cv;
  122. /* all the remaining data is only used for logical slots */
  123. /*
  124. * When the client has confirmed flushes >= candidate_xmin_lsn we can
  125. * advance the catalog xmin. When restart_valid has been passed,
  126. * restart_lsn can be increased.
  127. */
  128. TransactionId candidate_catalog_xmin;
  129. XLogRecPtr candidate_xmin_lsn;
  130. XLogRecPtr candidate_restart_valid;
  131. XLogRecPtr candidate_restart_lsn;
  132. } ReplicationSlot;
  133. #define SlotIsPhysical(slot) ((slot)->data.database == InvalidOid)
  134. #define SlotIsLogical(slot) ((slot)->data.database != InvalidOid)
  135. /*
  136. * Shared memory control area for all of replication slots.
  137. */
  138. typedef struct ReplicationSlotCtlData
  139. {
  140. /*
  141. * This array should be declared [FLEXIBLE_ARRAY_MEMBER], but for some
  142. * reason you can't do that in an otherwise-empty struct.
  143. */
  144. ReplicationSlot replication_slots[1];
  145. } ReplicationSlotCtlData;
  146. /*
  147. * Pointers to shared memory
  148. */
  149. extern PGDLLIMPORT ReplicationSlotCtlData *ReplicationSlotCtl;
  150. extern PGDLLIMPORT ReplicationSlot *MyReplicationSlot;
  151. /* GUCs */
  152. extern PGDLLIMPORT int max_replication_slots;
  153. /* shmem initialization functions */
  154. extern Size ReplicationSlotsShmemSize(void);
  155. extern void ReplicationSlotsShmemInit(void);
  156. /* management of individual slots */
  157. extern void ReplicationSlotCreate(const char *name, bool db_specific,
  158. ReplicationSlotPersistency p);
  159. extern void ReplicationSlotPersist(void);
  160. extern void ReplicationSlotDrop(const char *name, bool nowait);
  161. extern void ReplicationSlotAcquire(const char *name, bool nowait);
  162. extern void ReplicationSlotRelease(void);
  163. extern void ReplicationSlotCleanup(void);
  164. extern void ReplicationSlotSave(void);
  165. extern void ReplicationSlotMarkDirty(void);
  166. /* misc stuff */
  167. extern bool ReplicationSlotValidateName(const char *name, int elevel);
  168. extern void ReplicationSlotReserveWal(void);
  169. extern void ReplicationSlotsComputeRequiredXmin(bool already_locked);
  170. extern void ReplicationSlotsComputeRequiredLSN(void);
  171. extern XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN(void);
  172. extern bool ReplicationSlotsCountDBSlots(Oid dboid, int *nslots, int *nactive);
  173. extern void ReplicationSlotsDropDBSlots(Oid dboid);
  174. extern void StartupReplicationSlots(void);
  175. extern void CheckPointReplicationSlots(void);
  176. extern void CheckSlotRequirements(void);
  177. #endif /* SLOT_H */
上海开阖软件有限公司 沪ICP备12045867号-1