output_plugin.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*-------------------------------------------------------------------------
  2. * output_plugin.h
  3. * PostgreSQL Logical Decode Plugin Interface
  4. *
  5. * Copyright (c) 2012-2022, PostgreSQL Global Development Group
  6. *
  7. *-------------------------------------------------------------------------
  8. */
  9. #ifndef OUTPUT_PLUGIN_H
  10. #define OUTPUT_PLUGIN_H
  11. #include "replication/reorderbuffer.h"
  12. struct LogicalDecodingContext;
  13. struct OutputPluginCallbacks;
  14. typedef enum OutputPluginOutputType
  15. {
  16. OUTPUT_PLUGIN_BINARY_OUTPUT,
  17. OUTPUT_PLUGIN_TEXTUAL_OUTPUT
  18. } OutputPluginOutputType;
  19. /*
  20. * Options set by the output plugin, in the startup callback.
  21. */
  22. typedef struct OutputPluginOptions
  23. {
  24. OutputPluginOutputType output_type;
  25. bool receive_rewrites;
  26. } OutputPluginOptions;
  27. /*
  28. * Type of the shared library symbol _PG_output_plugin_init that is looked up
  29. * when loading an output plugin shared library.
  30. */
  31. typedef void (*LogicalOutputPluginInit) (struct OutputPluginCallbacks *cb);
  32. /*
  33. * Callback that gets called in a user-defined plugin. ctx->private_data can
  34. * be set to some private data.
  35. *
  36. * "is_init" will be set to "true" if the decoding slot just got defined. When
  37. * the same slot is used from there one, it will be "false".
  38. */
  39. typedef void (*LogicalDecodeStartupCB) (struct LogicalDecodingContext *ctx,
  40. OutputPluginOptions *options,
  41. bool is_init);
  42. /*
  43. * Callback called for every (explicit or implicit) BEGIN of a successful
  44. * transaction.
  45. */
  46. typedef void (*LogicalDecodeBeginCB) (struct LogicalDecodingContext *ctx,
  47. ReorderBufferTXN *txn);
  48. /*
  49. * Callback for every individual change in a successful transaction.
  50. */
  51. typedef void (*LogicalDecodeChangeCB) (struct LogicalDecodingContext *ctx,
  52. ReorderBufferTXN *txn,
  53. Relation relation,
  54. ReorderBufferChange *change);
  55. /*
  56. * Callback for every TRUNCATE in a successful transaction.
  57. */
  58. typedef void (*LogicalDecodeTruncateCB) (struct LogicalDecodingContext *ctx,
  59. ReorderBufferTXN *txn,
  60. int nrelations,
  61. Relation relations[],
  62. ReorderBufferChange *change);
  63. /*
  64. * Called for every (explicit or implicit) COMMIT of a successful transaction.
  65. */
  66. typedef void (*LogicalDecodeCommitCB) (struct LogicalDecodingContext *ctx,
  67. ReorderBufferTXN *txn,
  68. XLogRecPtr commit_lsn);
  69. /*
  70. * Called for the generic logical decoding messages.
  71. */
  72. typedef void (*LogicalDecodeMessageCB) (struct LogicalDecodingContext *ctx,
  73. ReorderBufferTXN *txn,
  74. XLogRecPtr message_lsn,
  75. bool transactional,
  76. const char *prefix,
  77. Size message_size,
  78. const char *message);
  79. /*
  80. * Filter changes by origin.
  81. */
  82. typedef bool (*LogicalDecodeFilterByOriginCB) (struct LogicalDecodingContext *ctx,
  83. RepOriginId origin_id);
  84. /*
  85. * Called to shutdown an output plugin.
  86. */
  87. typedef void (*LogicalDecodeShutdownCB) (struct LogicalDecodingContext *ctx);
  88. /*
  89. * Called before decoding of PREPARE record to decide whether this
  90. * transaction should be decoded with separate calls to prepare and
  91. * commit_prepared/rollback_prepared callbacks or wait till COMMIT PREPARED
  92. * and sent as usual transaction.
  93. */
  94. typedef bool (*LogicalDecodeFilterPrepareCB) (struct LogicalDecodingContext *ctx,
  95. TransactionId xid,
  96. const char *gid);
  97. /*
  98. * Callback called for every BEGIN of a prepared trnsaction.
  99. */
  100. typedef void (*LogicalDecodeBeginPrepareCB) (struct LogicalDecodingContext *ctx,
  101. ReorderBufferTXN *txn);
  102. /*
  103. * Called for PREPARE record unless it was filtered by filter_prepare()
  104. * callback.
  105. */
  106. typedef void (*LogicalDecodePrepareCB) (struct LogicalDecodingContext *ctx,
  107. ReorderBufferTXN *txn,
  108. XLogRecPtr prepare_lsn);
  109. /*
  110. * Called for COMMIT PREPARED.
  111. */
  112. typedef void (*LogicalDecodeCommitPreparedCB) (struct LogicalDecodingContext *ctx,
  113. ReorderBufferTXN *txn,
  114. XLogRecPtr commit_lsn);
  115. /*
  116. * Called for ROLLBACK PREPARED.
  117. */
  118. typedef void (*LogicalDecodeRollbackPreparedCB) (struct LogicalDecodingContext *ctx,
  119. ReorderBufferTXN *txn,
  120. XLogRecPtr prepare_end_lsn,
  121. TimestampTz prepare_time);
  122. /*
  123. * Called when starting to stream a block of changes from in-progress
  124. * transaction (may be called repeatedly, if it's streamed in multiple
  125. * chunks).
  126. */
  127. typedef void (*LogicalDecodeStreamStartCB) (struct LogicalDecodingContext *ctx,
  128. ReorderBufferTXN *txn);
  129. /*
  130. * Called when stopping to stream a block of changes from in-progress
  131. * transaction to a remote node (may be called repeatedly, if it's streamed
  132. * in multiple chunks).
  133. */
  134. typedef void (*LogicalDecodeStreamStopCB) (struct LogicalDecodingContext *ctx,
  135. ReorderBufferTXN *txn);
  136. /*
  137. * Called to discard changes streamed to remote node from in-progress
  138. * transaction.
  139. */
  140. typedef void (*LogicalDecodeStreamAbortCB) (struct LogicalDecodingContext *ctx,
  141. ReorderBufferTXN *txn,
  142. XLogRecPtr abort_lsn);
  143. /*
  144. * Called to prepare changes streamed to remote node from in-progress
  145. * transaction. This is called as part of a two-phase commit.
  146. */
  147. typedef void (*LogicalDecodeStreamPrepareCB) (struct LogicalDecodingContext *ctx,
  148. ReorderBufferTXN *txn,
  149. XLogRecPtr prepare_lsn);
  150. /*
  151. * Called to apply changes streamed to remote node from in-progress
  152. * transaction.
  153. */
  154. typedef void (*LogicalDecodeStreamCommitCB) (struct LogicalDecodingContext *ctx,
  155. ReorderBufferTXN *txn,
  156. XLogRecPtr commit_lsn);
  157. /*
  158. * Callback for streaming individual changes from in-progress transactions.
  159. */
  160. typedef void (*LogicalDecodeStreamChangeCB) (struct LogicalDecodingContext *ctx,
  161. ReorderBufferTXN *txn,
  162. Relation relation,
  163. ReorderBufferChange *change);
  164. /*
  165. * Callback for streaming generic logical decoding messages from in-progress
  166. * transactions.
  167. */
  168. typedef void (*LogicalDecodeStreamMessageCB) (struct LogicalDecodingContext *ctx,
  169. ReorderBufferTXN *txn,
  170. XLogRecPtr message_lsn,
  171. bool transactional,
  172. const char *prefix,
  173. Size message_size,
  174. const char *message);
  175. /*
  176. * Callback for streaming truncates from in-progress transactions.
  177. */
  178. typedef void (*LogicalDecodeStreamTruncateCB) (struct LogicalDecodingContext *ctx,
  179. ReorderBufferTXN *txn,
  180. int nrelations,
  181. Relation relations[],
  182. ReorderBufferChange *change);
  183. /*
  184. * Output plugin callbacks
  185. */
  186. typedef struct OutputPluginCallbacks
  187. {
  188. LogicalDecodeStartupCB startup_cb;
  189. LogicalDecodeBeginCB begin_cb;
  190. LogicalDecodeChangeCB change_cb;
  191. LogicalDecodeTruncateCB truncate_cb;
  192. LogicalDecodeCommitCB commit_cb;
  193. LogicalDecodeMessageCB message_cb;
  194. LogicalDecodeFilterByOriginCB filter_by_origin_cb;
  195. LogicalDecodeShutdownCB shutdown_cb;
  196. /* streaming of changes at prepare time */
  197. LogicalDecodeFilterPrepareCB filter_prepare_cb;
  198. LogicalDecodeBeginPrepareCB begin_prepare_cb;
  199. LogicalDecodePrepareCB prepare_cb;
  200. LogicalDecodeCommitPreparedCB commit_prepared_cb;
  201. LogicalDecodeRollbackPreparedCB rollback_prepared_cb;
  202. /* streaming of changes */
  203. LogicalDecodeStreamStartCB stream_start_cb;
  204. LogicalDecodeStreamStopCB stream_stop_cb;
  205. LogicalDecodeStreamAbortCB stream_abort_cb;
  206. LogicalDecodeStreamPrepareCB stream_prepare_cb;
  207. LogicalDecodeStreamCommitCB stream_commit_cb;
  208. LogicalDecodeStreamChangeCB stream_change_cb;
  209. LogicalDecodeStreamMessageCB stream_message_cb;
  210. LogicalDecodeStreamTruncateCB stream_truncate_cb;
  211. } OutputPluginCallbacks;
  212. /* Functions in replication/logical/logical.c */
  213. extern void OutputPluginPrepareWrite(struct LogicalDecodingContext *ctx, bool last_write);
  214. extern void OutputPluginWrite(struct LogicalDecodingContext *ctx, bool last_write);
  215. extern void OutputPluginUpdateProgress(struct LogicalDecodingContext *ctx, bool skipped_xact);
  216. #endif /* OUTPUT_PLUGIN_H */