gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

256 行
20KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>48.6. Logical Decoding Output Plugins</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="logicaldecoding-catalogs.html" title="48.5. System Catalogs Related to Logical Decoding" /><link rel="next" href="logicaldecoding-writer.html" title="48.7. Logical Decoding Output Writers" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">48.6. Logical Decoding Output Plugins</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="logicaldecoding-catalogs.html" title="48.5. System Catalogs Related to Logical Decoding">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="logicaldecoding.html" title="Chapter 48. Logical Decoding">Up</a></td><th width="60%" align="center">Chapter 48. Logical Decoding</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="logicaldecoding-writer.html" title="48.7. Logical Decoding Output Writers">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LOGICALDECODING-OUTPUT-PLUGIN"><div class="titlepage"><div><div><h2 class="title" style="clear: both">48.6. Logical Decoding Output Plugins</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="logicaldecoding-output-plugin.html#LOGICALDECODING-OUTPUT-INIT">48.6.1. Initialization Function</a></span></dt><dt><span class="sect2"><a href="logicaldecoding-output-plugin.html#LOGICALDECODING-CAPABILITIES">48.6.2. Capabilities</a></span></dt><dt><span class="sect2"><a href="logicaldecoding-output-plugin.html#LOGICALDECODING-OUTPUT-MODE">48.6.3. Output Modes</a></span></dt><dt><span class="sect2"><a href="logicaldecoding-output-plugin.html#LOGICALDECODING-OUTPUT-PLUGIN-CALLBACKS">48.6.4. Output Plugin Callbacks</a></span></dt><dt><span class="sect2"><a href="logicaldecoding-output-plugin.html#LOGICALDECODING-OUTPUT-PLUGIN-OUTPUT">48.6.5. Functions for Producing Output</a></span></dt></dl></div><p>
  3. An example output plugin can be found in the
  4. <a class="link" href="test-decoding.html" title="F.40. test_decoding">
  5. <code class="filename">contrib/test_decoding</code>
  6. </a>
  7. subdirectory of the PostgreSQL source tree.
  8. </p><div class="sect2" id="LOGICALDECODING-OUTPUT-INIT"><div class="titlepage"><div><div><h3 class="title">48.6.1. Initialization Function</h3></div></div></div><a id="id-1.8.14.12.3.2" class="indexterm"></a><p>
  9. An output plugin is loaded by dynamically loading a shared library with
  10. the output plugin's name as the library base name. The normal library
  11. search path is used to locate the library. To provide the required output
  12. plugin callbacks and to indicate that the library is actually an output
  13. plugin it needs to provide a function named
  14. <code class="function">_PG_output_plugin_init</code>. This function is passed a
  15. struct that needs to be filled with the callback function pointers for
  16. individual actions.
  17. </p><pre class="programlisting">
  18. typedef struct OutputPluginCallbacks
  19. {
  20. LogicalDecodeStartupCB startup_cb;
  21. LogicalDecodeBeginCB begin_cb;
  22. LogicalDecodeChangeCB change_cb;
  23. LogicalDecodeTruncateCB truncate_cb;
  24. LogicalDecodeCommitCB commit_cb;
  25. LogicalDecodeMessageCB message_cb;
  26. LogicalDecodeFilterByOriginCB filter_by_origin_cb;
  27. LogicalDecodeShutdownCB shutdown_cb;
  28. } OutputPluginCallbacks;
  29. typedef void (*LogicalOutputPluginInit) (struct OutputPluginCallbacks *cb);
  30. </pre><p>
  31. The <code class="function">begin_cb</code>, <code class="function">change_cb</code>
  32. and <code class="function">commit_cb</code> callbacks are required,
  33. while <code class="function">startup_cb</code>,
  34. <code class="function">filter_by_origin_cb</code>, <code class="function">truncate_cb</code>,
  35. and <code class="function">shutdown_cb</code> are optional.
  36. If <code class="function">truncate_cb</code> is not set but a
  37. <code class="command">TRUNCATE</code> is to be decoded, the action will be ignored.
  38. </p></div><div class="sect2" id="LOGICALDECODING-CAPABILITIES"><div class="titlepage"><div><div><h3 class="title">48.6.2. Capabilities</h3></div></div></div><p>
  39. To decode, format and output changes, output plugins can use most of the
  40. backend's normal infrastructure, including calling output functions. Read
  41. only access to relations is permitted as long as only relations are
  42. accessed that either have been created by <code class="command">initdb</code> in
  43. the <code class="literal">pg_catalog</code> schema, or have been marked as user
  44. provided catalog tables using
  45. </p><pre class="programlisting">
  46. ALTER TABLE user_catalog_table SET (user_catalog_table = true);
  47. CREATE TABLE another_catalog_table(data text) WITH (user_catalog_table = true);
  48. </pre><p>
  49. Any actions leading to transaction ID assignment are prohibited. That, among others,
  50. includes writing to tables, performing DDL changes, and
  51. calling <code class="literal">txid_current()</code>.
  52. </p></div><div class="sect2" id="LOGICALDECODING-OUTPUT-MODE"><div class="titlepage"><div><div><h3 class="title">48.6.3. Output Modes</h3></div></div></div><p>
  53. Output plugin callbacks can pass data to the consumer in nearly arbitrary
  54. formats. For some use cases, like viewing the changes via SQL, returning
  55. data in a data type that can contain arbitrary data (e.g., <code class="type">bytea</code>) is
  56. cumbersome. If the output plugin only outputs textual data in the
  57. server's encoding, it can declare that by
  58. setting <code class="literal">OutputPluginOptions.output_type</code>
  59. to <code class="literal">OUTPUT_PLUGIN_TEXTUAL_OUTPUT</code> instead
  60. of <code class="literal">OUTPUT_PLUGIN_BINARY_OUTPUT</code> in
  61. the <a class="link" href="logicaldecoding-output-plugin.html#LOGICALDECODING-OUTPUT-PLUGIN-STARTUP" title="48.6.4.1. Startup Callback">startup
  62. callback</a>. In that case, all the data has to be in the server's encoding
  63. so that a <code class="type">text</code> datum can contain it. This is checked in assertion-enabled
  64. builds.
  65. </p></div><div class="sect2" id="LOGICALDECODING-OUTPUT-PLUGIN-CALLBACKS"><div class="titlepage"><div><div><h3 class="title">48.6.4. Output Plugin Callbacks</h3></div></div></div><p>
  66. An output plugin gets notified about changes that are happening via
  67. various callbacks it needs to provide.
  68. </p><p>
  69. Concurrent transactions are decoded in commit order, and only changes
  70. belonging to a specific transaction are decoded between
  71. the <code class="literal">begin</code> and <code class="literal">commit</code>
  72. callbacks. Transactions that were rolled back explicitly or implicitly
  73. never get
  74. decoded. Successful savepoints are
  75. folded into the transaction containing them in the order they were
  76. executed within that transaction.
  77. </p><div class="note"><h3 class="title">Note</h3><p>
  78. Only transactions that have already safely been flushed to disk will be
  79. decoded. That can lead to a <code class="command">COMMIT</code> not immediately being decoded in a
  80. directly following <code class="literal">pg_logical_slot_get_changes()</code>
  81. when <code class="varname">synchronous_commit</code> is set
  82. to <code class="literal">off</code>.
  83. </p></div><div class="sect3" id="LOGICALDECODING-OUTPUT-PLUGIN-STARTUP"><div class="titlepage"><div><div><h4 class="title">48.6.4.1. Startup Callback</h4></div></div></div><p>
  84. The optional <code class="function">startup_cb</code> callback is called whenever
  85. a replication slot is created or asked to stream changes, independent
  86. of the number of changes that are ready to be put out.
  87. </p><pre class="programlisting">
  88. typedef void (*LogicalDecodeStartupCB) (struct LogicalDecodingContext *ctx,
  89. OutputPluginOptions *options,
  90. bool is_init);
  91. </pre><p>
  92. The <code class="literal">is_init</code> parameter will be true when the
  93. replication slot is being created and false
  94. otherwise. <em class="parameter"><code>options</code></em> points to a struct of options
  95. that output plugins can set:
  96. </p><pre class="programlisting">
  97. typedef struct OutputPluginOptions
  98. {
  99. OutputPluginOutputType output_type;
  100. bool receive_rewrites;
  101. } OutputPluginOptions;
  102. </pre><p>
  103. <code class="literal">output_type</code> has to either be set to
  104. <code class="literal">OUTPUT_PLUGIN_TEXTUAL_OUTPUT</code>
  105. or <code class="literal">OUTPUT_PLUGIN_BINARY_OUTPUT</code>. See also
  106. <a class="xref" href="logicaldecoding-output-plugin.html#LOGICALDECODING-OUTPUT-MODE" title="48.6.3. Output Modes">Section 48.6.3</a>.
  107. If <code class="literal">receive_rewrites</code> is true, the output plugin will
  108. also be called for changes made by heap rewrites during certain DDL
  109. operations. These are of interest to plugins that handle DDL
  110. replication, but they require special handling.
  111. </p><p>
  112. The startup callback should validate the options present in
  113. <code class="literal">ctx-&gt;output_plugin_options</code>. If the output plugin
  114. needs to have a state, it can
  115. use <code class="literal">ctx-&gt;output_plugin_private</code> to store it.
  116. </p></div><div class="sect3" id="LOGICALDECODING-OUTPUT-PLUGIN-SHUTDOWN"><div class="titlepage"><div><div><h4 class="title">48.6.4.2. Shutdown Callback</h4></div></div></div><p>
  117. The optional <code class="function">shutdown_cb</code> callback is called
  118. whenever a formerly active replication slot is not used anymore and can
  119. be used to deallocate resources private to the output plugin. The slot
  120. isn't necessarily being dropped, streaming is just being stopped.
  121. </p><pre class="programlisting">
  122. typedef void (*LogicalDecodeShutdownCB) (struct LogicalDecodingContext *ctx);
  123. </pre><p>
  124. </p></div><div class="sect3" id="LOGICALDECODING-OUTPUT-PLUGIN-BEGIN"><div class="titlepage"><div><div><h4 class="title">48.6.4.3. Transaction Begin Callback</h4></div></div></div><p>
  125. The required <code class="function">begin_cb</code> callback is called whenever a
  126. start of a committed transaction has been decoded. Aborted transactions
  127. and their contents never get decoded.
  128. </p><pre class="programlisting">
  129. typedef void (*LogicalDecodeBeginCB) (struct LogicalDecodingContext *ctx,
  130. ReorderBufferTXN *txn);
  131. </pre><p>
  132. The <em class="parameter"><code>txn</code></em> parameter contains meta information about
  133. the transaction, like the time stamp at which it has been committed and
  134. its XID.
  135. </p></div><div class="sect3" id="LOGICALDECODING-OUTPUT-PLUGIN-COMMIT"><div class="titlepage"><div><div><h4 class="title">48.6.4.4. Transaction End Callback</h4></div></div></div><p>
  136. The required <code class="function">commit_cb</code> callback is called whenever
  137. a transaction commit has been
  138. decoded. The <code class="function">change_cb</code> callbacks for all modified
  139. rows will have been called before this, if there have been any modified
  140. rows.
  141. </p><pre class="programlisting">
  142. typedef void (*LogicalDecodeCommitCB) (struct LogicalDecodingContext *ctx,
  143. ReorderBufferTXN *txn,
  144. XLogRecPtr commit_lsn);
  145. </pre><p>
  146. </p></div><div class="sect3" id="LOGICALDECODING-OUTPUT-PLUGIN-CHANGE"><div class="titlepage"><div><div><h4 class="title">48.6.4.5. Change Callback</h4></div></div></div><p>
  147. The required <code class="function">change_cb</code> callback is called for every
  148. individual row modification inside a transaction, may it be
  149. an <code class="command">INSERT</code>, <code class="command">UPDATE</code>,
  150. or <code class="command">DELETE</code>. Even if the original command modified
  151. several rows at once the callback will be called individually for each
  152. row.
  153. </p><pre class="programlisting">
  154. typedef void (*LogicalDecodeChangeCB) (struct LogicalDecodingContext *ctx,
  155. ReorderBufferTXN *txn,
  156. Relation relation,
  157. ReorderBufferChange *change);
  158. </pre><p>
  159. The <em class="parameter"><code>ctx</code></em> and <em class="parameter"><code>txn</code></em> parameters
  160. have the same contents as for the <code class="function">begin_cb</code>
  161. and <code class="function">commit_cb</code> callbacks, but additionally the
  162. relation descriptor <em class="parameter"><code>relation</code></em> points to the
  163. relation the row belongs to and a struct
  164. <em class="parameter"><code>change</code></em> describing the row modification are passed
  165. in.
  166. </p><div class="note"><h3 class="title">Note</h3><p>
  167. Only changes in user defined tables that are not unlogged
  168. (see <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-UNLOGGED"><code class="literal">UNLOGGED</code></a>) and not temporary
  169. (see <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-TEMPORARY"><code class="literal">TEMPORARY</code> or <code class="literal">TEMP</code></a>) can be extracted using
  170. logical decoding.
  171. </p></div></div><div class="sect3" id="LOGICALDECODING-OUTPUT-PLUGIN-TRUNCATE"><div class="titlepage"><div><div><h4 class="title">48.6.4.6. Truncate Callback</h4></div></div></div><p>
  172. The <code class="function">truncate_cb</code> callback is called for a
  173. <code class="command">TRUNCATE</code> command.
  174. </p><pre class="programlisting">
  175. typedef void (*LogicalDecodeTruncateCB) (struct LogicalDecodingContext *ctx,
  176. ReorderBufferTXN *txn,
  177. int nrelations,
  178. Relation relations[],
  179. ReorderBufferChange *change);
  180. </pre><p>
  181. The parameters are analogous to the <code class="function">change_cb</code>
  182. callback. However, because <code class="command">TRUNCATE</code> actions on
  183. tables connected by foreign keys need to be executed together, this
  184. callback receives an array of relations instead of just a single one.
  185. See the description of the <a class="xref" href="sql-truncate.html" title="TRUNCATE"><span class="refentrytitle">TRUNCATE</span></a> statement for
  186. details.
  187. </p></div><div class="sect3" id="LOGICALDECODING-OUTPUT-PLUGIN-FILTER-ORIGIN"><div class="titlepage"><div><div><h4 class="title">48.6.4.7. Origin Filter Callback</h4></div></div></div><p>
  188. The optional <code class="function">filter_by_origin_cb</code> callback
  189. is called to determine whether data that has been replayed
  190. from <em class="parameter"><code>origin_id</code></em> is of interest to the
  191. output plugin.
  192. </p><pre class="programlisting">
  193. typedef bool (*LogicalDecodeFilterByOriginCB) (struct LogicalDecodingContext *ctx,
  194. RepOriginId origin_id);
  195. </pre><p>
  196. The <em class="parameter"><code>ctx</code></em> parameter has the same contents
  197. as for the other callbacks. No information but the origin is
  198. available. To signal that changes originating on the passed in
  199. node are irrelevant, return true, causing them to be filtered
  200. away; false otherwise. The other callbacks will not be called
  201. for transactions and changes that have been filtered away.
  202. </p><p>
  203. This is useful when implementing cascading or multidirectional
  204. replication solutions. Filtering by the origin allows to
  205. prevent replicating the same changes back and forth in such
  206. setups. While transactions and changes also carry information
  207. about the origin, filtering via this callback is noticeably
  208. more efficient.
  209. </p></div><div class="sect3" id="LOGICALDECODING-OUTPUT-PLUGIN-MESSAGE"><div class="titlepage"><div><div><h4 class="title">48.6.4.8. Generic Message Callback</h4></div></div></div><p>
  210. The optional <code class="function">message_cb</code> callback is called whenever
  211. a logical decoding message has been decoded.
  212. </p><pre class="programlisting">
  213. typedef void (*LogicalDecodeMessageCB) (struct LogicalDecodingContext *ctx,
  214. ReorderBufferTXN *txn,
  215. XLogRecPtr message_lsn,
  216. bool transactional,
  217. const char *prefix,
  218. Size message_size,
  219. const char *message);
  220. </pre><p>
  221. The <em class="parameter"><code>txn</code></em> parameter contains meta information about
  222. the transaction, like the time stamp at which it has been committed and
  223. its XID. Note however that it can be NULL when the message is
  224. non-transactional and the XID was not assigned yet in the transaction
  225. which logged the message. The <em class="parameter"><code>lsn</code></em> has WAL
  226. location of the message. The <em class="parameter"><code>transactional</code></em> says
  227. if the message was sent as transactional or not.
  228. The <em class="parameter"><code>prefix</code></em> is arbitrary null-terminated prefix
  229. which can be used for identifying interesting messages for the current
  230. plugin. And finally the <em class="parameter"><code>message</code></em> parameter holds
  231. the actual message of <em class="parameter"><code>message_size</code></em> size.
  232. </p><p>
  233. Extra care should be taken to ensure that the prefix the output plugin
  234. considers interesting is unique. Using name of the extension or the
  235. output plugin itself is often a good choice.
  236. </p></div></div><div class="sect2" id="LOGICALDECODING-OUTPUT-PLUGIN-OUTPUT"><div class="titlepage"><div><div><h3 class="title">48.6.5. Functions for Producing Output</h3></div></div></div><p>
  237. To actually produce output, output plugins can write data to
  238. the <code class="literal">StringInfo</code> output buffer
  239. in <code class="literal">ctx-&gt;out</code> when inside
  240. the <code class="function">begin_cb</code>, <code class="function">commit_cb</code>,
  241. or <code class="function">change_cb</code> callbacks. Before writing to the output
  242. buffer, <code class="function">OutputPluginPrepareWrite(ctx, last_write)</code> has
  243. to be called, and after finishing writing to the
  244. buffer, <code class="function">OutputPluginWrite(ctx, last_write)</code> has to be
  245. called to perform the write. The <em class="parameter"><code>last_write</code></em>
  246. indicates whether a particular write was the callback's last write.
  247. </p><p>
  248. The following example shows how to output data to the consumer of an
  249. output plugin:
  250. </p><pre class="programlisting">
  251. OutputPluginPrepareWrite(ctx, true);
  252. appendStringInfo(ctx-&gt;out, "BEGIN %u", txn-&gt;xid);
  253. OutputPluginWrite(ctx, true);
  254. </pre><p>
  255. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="logicaldecoding-catalogs.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="logicaldecoding.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="logicaldecoding-writer.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">48.5. System Catalogs Related to Logical Decoding </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 48.7. Logical Decoding Output Writers</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1