gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

442 lignes
13KB

  1. /*
  2. * reorderbuffer.h
  3. * PostgreSQL logical replay/reorder buffer management.
  4. *
  5. * Copyright (c) 2012-2019, PostgreSQL Global Development Group
  6. *
  7. * src/include/replication/reorderbuffer.h
  8. */
  9. #ifndef REORDERBUFFER_H
  10. #define REORDERBUFFER_H
  11. #include "access/htup_details.h"
  12. #include "lib/ilist.h"
  13. #include "storage/sinval.h"
  14. #include "utils/hsearch.h"
  15. #include "utils/relcache.h"
  16. #include "utils/snapshot.h"
  17. #include "utils/timestamp.h"
  18. /* an individual tuple, stored in one chunk of memory */
  19. typedef struct ReorderBufferTupleBuf
  20. {
  21. /* position in preallocated list */
  22. slist_node node;
  23. /* tuple header, the interesting bit for users of logical decoding */
  24. HeapTupleData tuple;
  25. /* pre-allocated size of tuple buffer, different from tuple size */
  26. Size alloc_tuple_size;
  27. /* actual tuple data follows */
  28. } ReorderBufferTupleBuf;
  29. /* pointer to the data stored in a TupleBuf */
  30. #define ReorderBufferTupleBufData(p) \
  31. ((HeapTupleHeader) MAXALIGN(((char *) p) + sizeof(ReorderBufferTupleBuf)))
  32. /*
  33. * Types of the change passed to a 'change' callback.
  34. *
  35. * For efficiency and simplicity reasons we want to keep Snapshots, CommandIds
  36. * and ComboCids in the same list with the user visible INSERT/UPDATE/DELETE
  37. * changes. Users of the decoding facilities will never see changes with
  38. * *_INTERNAL_* actions.
  39. *
  40. * The INTERNAL_SPEC_INSERT and INTERNAL_SPEC_CONFIRM changes concern
  41. * "speculative insertions", and their confirmation respectively. They're
  42. * used by INSERT .. ON CONFLICT .. UPDATE. Users of logical decoding don't
  43. * have to care about these.
  44. */
  45. enum ReorderBufferChangeType
  46. {
  47. REORDER_BUFFER_CHANGE_INSERT,
  48. REORDER_BUFFER_CHANGE_UPDATE,
  49. REORDER_BUFFER_CHANGE_DELETE,
  50. REORDER_BUFFER_CHANGE_MESSAGE,
  51. REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT,
  52. REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID,
  53. REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID,
  54. REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT,
  55. REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM,
  56. REORDER_BUFFER_CHANGE_TRUNCATE
  57. };
  58. /*
  59. * a single 'change', can be an insert (with one tuple), an update (old, new),
  60. * or a delete (old).
  61. *
  62. * The same struct is also used internally for other purposes but that should
  63. * never be visible outside reorderbuffer.c.
  64. */
  65. typedef struct ReorderBufferChange
  66. {
  67. XLogRecPtr lsn;
  68. /* The type of change. */
  69. enum ReorderBufferChangeType action;
  70. RepOriginId origin_id;
  71. /*
  72. * Context data for the change. Which part of the union is valid depends
  73. * on action.
  74. */
  75. union
  76. {
  77. /* Old, new tuples when action == *_INSERT|UPDATE|DELETE */
  78. struct
  79. {
  80. /* relation that has been changed */
  81. RelFileNode relnode;
  82. /* no previously reassembled toast chunks are necessary anymore */
  83. bool clear_toast_afterwards;
  84. /* valid for DELETE || UPDATE */
  85. ReorderBufferTupleBuf *oldtuple;
  86. /* valid for INSERT || UPDATE */
  87. ReorderBufferTupleBuf *newtuple;
  88. } tp;
  89. /*
  90. * Truncate data for REORDER_BUFFER_CHANGE_TRUNCATE representing one
  91. * set of relations to be truncated.
  92. */
  93. struct
  94. {
  95. Size nrelids;
  96. bool cascade;
  97. bool restart_seqs;
  98. Oid *relids;
  99. } truncate;
  100. /* Message with arbitrary data. */
  101. struct
  102. {
  103. char *prefix;
  104. Size message_size;
  105. char *message;
  106. } msg;
  107. /* New snapshot, set when action == *_INTERNAL_SNAPSHOT */
  108. Snapshot snapshot;
  109. /*
  110. * New command id for existing snapshot in a catalog changing tx. Set
  111. * when action == *_INTERNAL_COMMAND_ID.
  112. */
  113. CommandId command_id;
  114. /*
  115. * New cid mapping for catalog changing transaction, set when action
  116. * == *_INTERNAL_TUPLECID.
  117. */
  118. struct
  119. {
  120. RelFileNode node;
  121. ItemPointerData tid;
  122. CommandId cmin;
  123. CommandId cmax;
  124. CommandId combocid;
  125. } tuplecid;
  126. } data;
  127. /*
  128. * While in use this is how a change is linked into a transactions,
  129. * otherwise it's the preallocated list.
  130. */
  131. dlist_node node;
  132. } ReorderBufferChange;
  133. typedef struct ReorderBufferTXN
  134. {
  135. /*
  136. * The transactions transaction id, can be a toplevel or sub xid.
  137. */
  138. TransactionId xid;
  139. /* did the TX have catalog changes */
  140. bool has_catalog_changes;
  141. /* Do we know this is a subxact? Xid of top-level txn if so */
  142. bool is_known_as_subxact;
  143. TransactionId toplevel_xid;
  144. /*
  145. * LSN of the first data carrying, WAL record with knowledge about this
  146. * xid. This is allowed to *not* be first record adorned with this xid, if
  147. * the previous records aren't relevant for logical decoding.
  148. */
  149. XLogRecPtr first_lsn;
  150. /* ----
  151. * LSN of the record that lead to this xact to be committed or
  152. * aborted. This can be a
  153. * * plain commit record
  154. * * plain commit record, of a parent transaction
  155. * * prepared transaction commit
  156. * * plain abort record
  157. * * prepared transaction abort
  158. *
  159. * This can also become set to earlier values than transaction end when
  160. * a transaction is spilled to disk; specifically it's set to the LSN of
  161. * the latest change written to disk so far.
  162. * ----
  163. */
  164. XLogRecPtr final_lsn;
  165. /*
  166. * LSN pointing to the end of the commit record + 1.
  167. */
  168. XLogRecPtr end_lsn;
  169. /*
  170. * LSN of the last lsn at which snapshot information reside, so we can
  171. * restart decoding from there and fully recover this transaction from
  172. * WAL.
  173. */
  174. XLogRecPtr restart_decoding_lsn;
  175. /* origin of the change that caused this transaction */
  176. RepOriginId origin_id;
  177. XLogRecPtr origin_lsn;
  178. /*
  179. * Commit time, only known when we read the actual commit record.
  180. */
  181. TimestampTz commit_time;
  182. /*
  183. * The base snapshot is used to decode all changes until either this
  184. * transaction modifies the catalog, or another catalog-modifying
  185. * transaction commits.
  186. */
  187. Snapshot base_snapshot;
  188. XLogRecPtr base_snapshot_lsn;
  189. dlist_node base_snapshot_node; /* link in txns_by_base_snapshot_lsn */
  190. /*
  191. * How many ReorderBufferChange's do we have in this txn.
  192. *
  193. * Changes in subtransactions are *not* included but tracked separately.
  194. */
  195. uint64 nentries;
  196. /*
  197. * How many of the above entries are stored in memory in contrast to being
  198. * spilled to disk.
  199. */
  200. uint64 nentries_mem;
  201. /*
  202. * Has this transaction been spilled to disk? It's not always possible to
  203. * deduce that fact by comparing nentries with nentries_mem, because e.g.
  204. * subtransactions of a large transaction might get serialized together
  205. * with the parent - if they're restored to memory they'd have
  206. * nentries_mem == nentries.
  207. */
  208. bool serialized;
  209. /*
  210. * List of ReorderBufferChange structs, including new Snapshots and new
  211. * CommandIds
  212. */
  213. dlist_head changes;
  214. /*
  215. * List of (relation, ctid) => (cmin, cmax) mappings for catalog tuples.
  216. * Those are always assigned to the toplevel transaction. (Keep track of
  217. * #entries to create a hash of the right size)
  218. */
  219. dlist_head tuplecids;
  220. uint64 ntuplecids;
  221. /*
  222. * On-demand built hash for looking up the above values.
  223. */
  224. HTAB *tuplecid_hash;
  225. /*
  226. * Hash containing (potentially partial) toast entries. NULL if no toast
  227. * tuples have been found for the current change.
  228. */
  229. HTAB *toast_hash;
  230. /*
  231. * non-hierarchical list of subtransactions that are *not* aborted. Only
  232. * used in toplevel transactions.
  233. */
  234. dlist_head subtxns;
  235. uint32 nsubtxns;
  236. /*
  237. * Stored cache invalidations. This is not a linked list because we get
  238. * all the invalidations at once.
  239. */
  240. uint32 ninvalidations;
  241. SharedInvalidationMessage *invalidations;
  242. /* ---
  243. * Position in one of three lists:
  244. * * list of subtransactions if we are *known* to be subxact
  245. * * list of toplevel xacts (can be an as-yet unknown subxact)
  246. * * list of preallocated ReorderBufferTXNs (if unused)
  247. * ---
  248. */
  249. dlist_node node;
  250. } ReorderBufferTXN;
  251. /* so we can define the callbacks used inside struct ReorderBuffer itself */
  252. typedef struct ReorderBuffer ReorderBuffer;
  253. /* change callback signature */
  254. typedef void (*ReorderBufferApplyChangeCB) (ReorderBuffer *rb,
  255. ReorderBufferTXN *txn,
  256. Relation relation,
  257. ReorderBufferChange *change);
  258. /* truncate callback signature */
  259. typedef void (*ReorderBufferApplyTruncateCB) (ReorderBuffer *rb,
  260. ReorderBufferTXN *txn,
  261. int nrelations,
  262. Relation relations[],
  263. ReorderBufferChange *change);
  264. /* begin callback signature */
  265. typedef void (*ReorderBufferBeginCB) (ReorderBuffer *rb,
  266. ReorderBufferTXN *txn);
  267. /* commit callback signature */
  268. typedef void (*ReorderBufferCommitCB) (ReorderBuffer *rb,
  269. ReorderBufferTXN *txn,
  270. XLogRecPtr commit_lsn);
  271. /* message callback signature */
  272. typedef void (*ReorderBufferMessageCB) (ReorderBuffer *rb,
  273. ReorderBufferTXN *txn,
  274. XLogRecPtr message_lsn,
  275. bool transactional,
  276. const char *prefix, Size sz,
  277. const char *message);
  278. struct ReorderBuffer
  279. {
  280. /*
  281. * xid => ReorderBufferTXN lookup table
  282. */
  283. HTAB *by_txn;
  284. /*
  285. * Transactions that could be a toplevel xact, ordered by LSN of the first
  286. * record bearing that xid.
  287. */
  288. dlist_head toplevel_by_lsn;
  289. /*
  290. * Transactions and subtransactions that have a base snapshot, ordered by
  291. * LSN of the record which caused us to first obtain the base snapshot.
  292. * This is not the same as toplevel_by_lsn, because we only set the base
  293. * snapshot on the first logical-decoding-relevant record (eg. heap
  294. * writes), whereas the initial LSN could be set by other operations.
  295. */
  296. dlist_head txns_by_base_snapshot_lsn;
  297. /*
  298. * one-entry sized cache for by_txn. Very frequently the same txn gets
  299. * looked up over and over again.
  300. */
  301. TransactionId by_txn_last_xid;
  302. ReorderBufferTXN *by_txn_last_txn;
  303. /*
  304. * Callbacks to be called when a transactions commits.
  305. */
  306. ReorderBufferBeginCB begin;
  307. ReorderBufferApplyChangeCB apply_change;
  308. ReorderBufferApplyTruncateCB apply_truncate;
  309. ReorderBufferCommitCB commit;
  310. ReorderBufferMessageCB message;
  311. /*
  312. * Pointer that will be passed untouched to the callbacks.
  313. */
  314. void *private_data;
  315. /*
  316. * Saved output plugin option
  317. */
  318. bool output_rewrites;
  319. /*
  320. * Private memory context.
  321. */
  322. MemoryContext context;
  323. /*
  324. * Memory contexts for specific types objects
  325. */
  326. MemoryContext change_context;
  327. MemoryContext txn_context;
  328. MemoryContext tup_context;
  329. XLogRecPtr current_restart_decoding_lsn;
  330. /* buffer for disk<->memory conversions */
  331. char *outbuf;
  332. Size outbufsize;
  333. };
  334. ReorderBuffer *ReorderBufferAllocate(void);
  335. void ReorderBufferFree(ReorderBuffer *);
  336. ReorderBufferTupleBuf *ReorderBufferGetTupleBuf(ReorderBuffer *, Size tuple_len);
  337. void ReorderBufferReturnTupleBuf(ReorderBuffer *, ReorderBufferTupleBuf *tuple);
  338. ReorderBufferChange *ReorderBufferGetChange(ReorderBuffer *);
  339. void ReorderBufferReturnChange(ReorderBuffer *, ReorderBufferChange *);
  340. Oid *ReorderBufferGetRelids(ReorderBuffer *, int nrelids);
  341. void ReorderBufferReturnRelids(ReorderBuffer *, Oid *relids);
  342. void ReorderBufferQueueChange(ReorderBuffer *, TransactionId, XLogRecPtr lsn, ReorderBufferChange *);
  343. void ReorderBufferQueueMessage(ReorderBuffer *, TransactionId, Snapshot snapshot, XLogRecPtr lsn,
  344. bool transactional, const char *prefix,
  345. Size message_size, const char *message);
  346. void ReorderBufferCommit(ReorderBuffer *, TransactionId,
  347. XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
  348. TimestampTz commit_time, RepOriginId origin_id, XLogRecPtr origin_lsn);
  349. void ReorderBufferAssignChild(ReorderBuffer *, TransactionId, TransactionId, XLogRecPtr commit_lsn);
  350. void ReorderBufferCommitChild(ReorderBuffer *, TransactionId, TransactionId,
  351. XLogRecPtr commit_lsn, XLogRecPtr end_lsn);
  352. void ReorderBufferAbort(ReorderBuffer *, TransactionId, XLogRecPtr lsn);
  353. void ReorderBufferAbortOld(ReorderBuffer *, TransactionId xid);
  354. void ReorderBufferForget(ReorderBuffer *, TransactionId, XLogRecPtr lsn);
  355. void ReorderBufferSetBaseSnapshot(ReorderBuffer *, TransactionId, XLogRecPtr lsn, struct SnapshotData *snap);
  356. void ReorderBufferAddSnapshot(ReorderBuffer *, TransactionId, XLogRecPtr lsn, struct SnapshotData *snap);
  357. void ReorderBufferAddNewCommandId(ReorderBuffer *, TransactionId, XLogRecPtr lsn,
  358. CommandId cid);
  359. void ReorderBufferAddNewTupleCids(ReorderBuffer *, TransactionId, XLogRecPtr lsn,
  360. RelFileNode node, ItemPointerData pt,
  361. CommandId cmin, CommandId cmax, CommandId combocid);
  362. void ReorderBufferAddInvalidations(ReorderBuffer *, TransactionId, XLogRecPtr lsn,
  363. Size nmsgs, SharedInvalidationMessage *msgs);
  364. void ReorderBufferImmediateInvalidation(ReorderBuffer *, uint32 ninvalidations,
  365. SharedInvalidationMessage *invalidations);
  366. void ReorderBufferProcessXid(ReorderBuffer *, TransactionId xid, XLogRecPtr lsn);
  367. void ReorderBufferXidSetCatalogChanges(ReorderBuffer *, TransactionId xid, XLogRecPtr lsn);
  368. bool ReorderBufferXidHasCatalogChanges(ReorderBuffer *, TransactionId xid);
  369. bool ReorderBufferXidHasBaseSnapshot(ReorderBuffer *, TransactionId xid);
  370. ReorderBufferTXN *ReorderBufferGetOldestTXN(ReorderBuffer *);
  371. TransactionId ReorderBufferGetOldestXmin(ReorderBuffer *rb);
  372. void ReorderBufferSetRestartPoint(ReorderBuffer *, XLogRecPtr ptr);
  373. void StartupReorderBuffer(void);
  374. #endif
上海开阖软件有限公司 沪ICP备12045867号-1