2 47.6. Logical Decoding Output Plugins #
4 47.6.1. Initialization Function
7 47.6.4. Output Plugin Callbacks
8 47.6.5. Functions for Producing Output
10 An example output plugin can be found in the contrib/test_decoding
11 subdirectory of the PostgreSQL source tree.
13 47.6.1. Initialization Function #
15 An output plugin is loaded by dynamically loading a shared library with
16 the output plugin's name as the library base name. The normal library
17 search path is used to locate the library. To provide the required
18 output plugin callbacks and to indicate that the library is actually an
19 output plugin it needs to provide a function named
20 _PG_output_plugin_init. This function is passed a struct that needs to
21 be filled with the callback function pointers for individual actions.
22 typedef struct OutputPluginCallbacks
24 LogicalDecodeStartupCB startup_cb;
25 LogicalDecodeBeginCB begin_cb;
26 LogicalDecodeChangeCB change_cb;
27 LogicalDecodeTruncateCB truncate_cb;
28 LogicalDecodeCommitCB commit_cb;
29 LogicalDecodeMessageCB message_cb;
30 LogicalDecodeFilterByOriginCB filter_by_origin_cb;
31 LogicalDecodeShutdownCB shutdown_cb;
32 LogicalDecodeFilterPrepareCB filter_prepare_cb;
33 LogicalDecodeBeginPrepareCB begin_prepare_cb;
34 LogicalDecodePrepareCB prepare_cb;
35 LogicalDecodeCommitPreparedCB commit_prepared_cb;
36 LogicalDecodeRollbackPreparedCB rollback_prepared_cb;
37 LogicalDecodeStreamStartCB stream_start_cb;
38 LogicalDecodeStreamStopCB stream_stop_cb;
39 LogicalDecodeStreamAbortCB stream_abort_cb;
40 LogicalDecodeStreamPrepareCB stream_prepare_cb;
41 LogicalDecodeStreamCommitCB stream_commit_cb;
42 LogicalDecodeStreamChangeCB stream_change_cb;
43 LogicalDecodeStreamMessageCB stream_message_cb;
44 LogicalDecodeStreamTruncateCB stream_truncate_cb;
45 } OutputPluginCallbacks;
47 typedef void (*LogicalOutputPluginInit) (struct OutputPluginCallbacks *cb);
49 The begin_cb, change_cb and commit_cb callbacks are required, while
50 startup_cb, truncate_cb, message_cb, filter_by_origin_cb, and
51 shutdown_cb are optional. If truncate_cb is not set but a TRUNCATE is
52 to be decoded, the action will be ignored.
54 An output plugin may also define functions to support streaming of
55 large, in-progress transactions. The stream_start_cb, stream_stop_cb,
56 stream_abort_cb, stream_commit_cb, and stream_change_cb are required,
57 while stream_message_cb and stream_truncate_cb are optional. The
58 stream_prepare_cb is also required if the output plugin also support
61 An output plugin may also define functions to support two-phase
62 commits, which allows actions to be decoded on the PREPARE TRANSACTION.
63 The begin_prepare_cb, prepare_cb, commit_prepared_cb and
64 rollback_prepared_cb callbacks are required, while filter_prepare_cb is
65 optional. The stream_prepare_cb is also required if the output plugin
66 also supports the streaming of large in-progress transactions.
68 47.6.2. Capabilities #
70 To decode, format and output changes, output plugins can use most of
71 the backend's normal infrastructure, including calling output
72 functions. Read only access to relations is permitted as long as only
73 relations are accessed that either have been created by initdb in the
74 pg_catalog schema, or have been marked as user provided catalog tables
76 ALTER TABLE user_catalog_table SET (user_catalog_table = true);
77 CREATE TABLE another_catalog_table(data text) WITH (user_catalog_table = true);
79 Note that access to user catalog tables or regular system catalog
80 tables in the output plugins has to be done via the systable_* scan
81 APIs only. Access via the heap_* scan APIs will error out.
82 Additionally, any actions leading to transaction ID assignment are
83 prohibited. That, among others, includes writing to tables, performing
84 DDL changes, and calling pg_current_xact_id().
86 47.6.3. Output Modes #
88 Output plugin callbacks can pass data to the consumer in nearly
89 arbitrary formats. For some use cases, like viewing the changes via
90 SQL, returning data in a data type that can contain arbitrary data
91 (e.g., bytea) is cumbersome. If the output plugin only outputs textual
92 data in the server's encoding, it can declare that by setting
93 OutputPluginOptions.output_type to OUTPUT_PLUGIN_TEXTUAL_OUTPUT instead
94 of OUTPUT_PLUGIN_BINARY_OUTPUT in the startup callback. In that case,
95 all the data has to be in the server's encoding so that a text datum
96 can contain it. This is checked in assertion-enabled builds.
98 47.6.4. Output Plugin Callbacks #
100 An output plugin gets notified about changes that are happening via
101 various callbacks it needs to provide.
103 Concurrent transactions are decoded in commit order, and only changes
104 belonging to a specific transaction are decoded between the begin and
105 commit callbacks. Transactions that were rolled back explicitly or
106 implicitly never get decoded. Successful savepoints are folded into the
107 transaction containing them in the order they were executed within that
108 transaction. A transaction that is prepared for a two-phase commit
109 using PREPARE TRANSACTION will also be decoded if the output plugin
110 callbacks needed for decoding them are provided. It is possible that
111 the current prepared transaction which is being decoded is aborted
112 concurrently via a ROLLBACK PREPARED command. In that case, the logical
113 decoding of this transaction will be aborted too. All the changes of
114 such a transaction are skipped once the abort is detected and the
115 prepare_cb callback is invoked. Thus even in case of a concurrent
116 abort, enough information is provided to the output plugin for it to
117 properly deal with ROLLBACK PREPARED once that is decoded.
121 Only transactions that have already safely been flushed to disk will be
122 decoded. That can lead to a COMMIT not immediately being decoded in a
123 directly following pg_logical_slot_get_changes() when
124 synchronous_commit is set to off.
126 47.6.4.1. Startup Callback #
128 The optional startup_cb callback is called whenever a replication slot
129 is created or asked to stream changes, independent of the number of
130 changes that are ready to be put out.
131 typedef void (*LogicalDecodeStartupCB) (struct LogicalDecodingContext *ctx,
132 OutputPluginOptions *options,
135 The is_init parameter will be true when the replication slot is being
136 created and false otherwise. options points to a struct of options that
137 output plugins can set:
138 typedef struct OutputPluginOptions
140 OutputPluginOutputType output_type;
141 bool receive_rewrites;
142 } OutputPluginOptions;
144 output_type has to either be set to OUTPUT_PLUGIN_TEXTUAL_OUTPUT or
145 OUTPUT_PLUGIN_BINARY_OUTPUT. See also Section 47.6.3. If
146 receive_rewrites is true, the output plugin will also be called for
147 changes made by heap rewrites during certain DDL operations. These are
148 of interest to plugins that handle DDL replication, but they require
151 The startup callback should validate the options present in
152 ctx->output_plugin_options. If the output plugin needs to have a state,
153 it can use ctx->output_plugin_private to store it.
155 47.6.4.2. Shutdown Callback #
157 The optional shutdown_cb callback is called whenever a formerly active
158 replication slot is not used anymore and can be used to deallocate
159 resources private to the output plugin. The slot isn't necessarily
160 being dropped, streaming is just being stopped.
161 typedef void (*LogicalDecodeShutdownCB) (struct LogicalDecodingContext *ctx);
163 47.6.4.3. Transaction Begin Callback #
165 The required begin_cb callback is called whenever a start of a
166 committed transaction has been decoded. Aborted transactions and their
167 contents never get decoded.
168 typedef void (*LogicalDecodeBeginCB) (struct LogicalDecodingContext *ctx,
169 ReorderBufferTXN *txn);
171 The txn parameter contains meta information about the transaction, like
172 the time stamp at which it has been committed and its XID.
174 47.6.4.4. Transaction End Callback #
176 The required commit_cb callback is called whenever a transaction commit
177 has been decoded. The change_cb callbacks for all modified rows will
178 have been called before this, if there have been any modified rows.
179 typedef void (*LogicalDecodeCommitCB) (struct LogicalDecodingContext *ctx,
180 ReorderBufferTXN *txn,
181 XLogRecPtr commit_lsn);
183 47.6.4.5. Change Callback #
185 The required change_cb callback is called for every individual row
186 modification inside a transaction, may it be an INSERT, UPDATE, or
187 DELETE. Even if the original command modified several rows at once the
188 callback will be called individually for each row. The change_cb
189 callback may access system or user catalog tables to aid in the process
190 of outputting the row modification details. In case of decoding a
191 prepared (but yet uncommitted) transaction or decoding of an
192 uncommitted transaction, this change callback might also error out due
193 to simultaneous rollback of this very same transaction. In that case,
194 the logical decoding of this aborted transaction is stopped gracefully.
195 typedef void (*LogicalDecodeChangeCB) (struct LogicalDecodingContext *ctx,
196 ReorderBufferTXN *txn,
198 ReorderBufferChange *change);
200 The ctx and txn parameters have the same contents as for the begin_cb
201 and commit_cb callbacks, but additionally the relation descriptor
202 relation points to the relation the row belongs to and a struct change
203 describing the row modification are passed in.
207 Only changes in user defined tables that are not unlogged (see
208 UNLOGGED) and not temporary (see TEMPORARY or TEMP) can be extracted
209 using logical decoding.
211 47.6.4.6. Truncate Callback #
213 The optional truncate_cb callback is called for a TRUNCATE command.
214 typedef void (*LogicalDecodeTruncateCB) (struct LogicalDecodingContext *ctx,
215 ReorderBufferTXN *txn,
217 Relation relations[],
218 ReorderBufferChange *change);
220 The parameters are analogous to the change_cb callback. However,
221 because TRUNCATE actions on tables connected by foreign keys need to be
222 executed together, this callback receives an array of relations instead
223 of just a single one. See the description of the TRUNCATE statement for
226 47.6.4.7. Origin Filter Callback #
228 The optional filter_by_origin_cb callback is called to determine
229 whether data that has been replayed from origin_id is of interest to
231 typedef bool (*LogicalDecodeFilterByOriginCB) (struct LogicalDecodingContext *ct
233 RepOriginId origin_id);
235 The ctx parameter has the same contents as for the other callbacks. No
236 information but the origin is available. To signal that changes
237 originating on the passed in node are irrelevant, return true, causing
238 them to be filtered away; false otherwise. The other callbacks will not
239 be called for transactions and changes that have been filtered away.
241 This is useful when implementing cascading or multidirectional
242 replication solutions. Filtering by the origin allows to prevent
243 replicating the same changes back and forth in such setups. While
244 transactions and changes also carry information about the origin,
245 filtering via this callback is noticeably more efficient.
247 47.6.4.8. Generic Message Callback #
249 The optional message_cb callback is called whenever a logical decoding
250 message has been decoded.
251 typedef void (*LogicalDecodeMessageCB) (struct LogicalDecodingContext *ctx,
252 ReorderBufferTXN *txn,
253 XLogRecPtr message_lsn,
257 const char *message);
259 The txn parameter contains meta information about the transaction, like
260 the time stamp at which it has been committed and its XID. Note however
261 that it can be NULL when the message is non-transactional and the XID
262 was not assigned yet in the transaction which logged the message. The
263 lsn has WAL location of the message. The transactional says if the
264 message was sent as transactional or not. Similar to the change
265 callback, in case of decoding a prepared (but yet uncommitted)
266 transaction or decoding of an uncommitted transaction, this message
267 callback might also error out due to simultaneous rollback of this very
268 same transaction. In that case, the logical decoding of this aborted
269 transaction is stopped gracefully. The prefix is arbitrary
270 null-terminated prefix which can be used for identifying interesting
271 messages for the current plugin. And finally the message parameter
272 holds the actual message of message_size size.
274 Extra care should be taken to ensure that the prefix the output plugin
275 considers interesting is unique. Using name of the extension or the
276 output plugin itself is often a good choice.
278 47.6.4.9. Prepare Filter Callback #
280 The optional filter_prepare_cb callback is called to determine whether
281 data that is part of the current two-phase commit transaction should be
282 considered for decoding at this prepare stage or later as a regular
283 one-phase transaction at COMMIT PREPARED time. To signal that decoding
284 should be skipped, return true; false otherwise. When the callback is
285 not defined, false is assumed (i.e. no filtering, all transactions
286 using two-phase commit are decoded in two phases as well).
287 typedef bool (*LogicalDecodeFilterPrepareCB) (struct LogicalDecodingContext *ctx
292 The ctx parameter has the same contents as for the other callbacks. The
293 parameters xid and gid provide two different ways to identify the
294 transaction. The later COMMIT PREPARED or ROLLBACK PREPARED carries
295 both identifiers, providing an output plugin the choice of what to use.
297 The callback may be invoked multiple times per transaction to decode
298 and must provide the same static answer for a given pair of xid and gid
299 every time it is called.
301 47.6.4.10. Transaction Begin Prepare Callback #
303 The required begin_prepare_cb callback is called whenever the start of
304 a prepared transaction has been decoded. The gid field, which is part
305 of the txn parameter, can be used in this callback to check if the
306 plugin has already received this PREPARE in which case it can either
307 error out or skip the remaining changes of the transaction.
308 typedef void (*LogicalDecodeBeginPrepareCB) (struct LogicalDecodingContext *ctx,
309 ReorderBufferTXN *txn);
311 47.6.4.11. Transaction Prepare Callback #
313 The required prepare_cb callback is called whenever a transaction which
314 is prepared for two-phase commit has been decoded. The change_cb
315 callback for all modified rows will have been called before this, if
316 there have been any modified rows. The gid field, which is part of the
317 txn parameter, can be used in this callback.
318 typedef void (*LogicalDecodePrepareCB) (struct LogicalDecodingContext *ctx,
319 ReorderBufferTXN *txn,
320 XLogRecPtr prepare_lsn);
322 47.6.4.12. Transaction Commit Prepared Callback #
324 The required commit_prepared_cb callback is called whenever a
325 transaction COMMIT PREPARED has been decoded. The gid field, which is
326 part of the txn parameter, can be used in this callback.
327 typedef void (*LogicalDecodeCommitPreparedCB) (struct LogicalDecodingContext *ct
329 ReorderBufferTXN *txn,
330 XLogRecPtr commit_lsn);
332 47.6.4.13. Transaction Rollback Prepared Callback #
334 The required rollback_prepared_cb callback is called whenever a
335 transaction ROLLBACK PREPARED has been decoded. The gid field, which is
336 part of the txn parameter, can be used in this callback. The parameters
337 prepare_end_lsn and prepare_time can be used to check if the plugin has
338 received this PREPARE TRANSACTION in which case it can apply the
339 rollback, otherwise, it can skip the rollback operation. The gid alone
340 is not sufficient because the downstream node can have a prepared
341 transaction with same identifier.
342 typedef void (*LogicalDecodeRollbackPreparedCB) (struct LogicalDecodingContext *
344 ReorderBufferTXN *txn,
345 XLogRecPtr prepare_end_lsn,
346 TimestampTz prepare_time);
348 47.6.4.14. Stream Start Callback #
350 The required stream_start_cb callback is called when opening a block of
351 streamed changes from an in-progress transaction.
352 typedef void (*LogicalDecodeStreamStartCB) (struct LogicalDecodingContext *ctx,
353 ReorderBufferTXN *txn);
355 47.6.4.15. Stream Stop Callback #
357 The required stream_stop_cb callback is called when closing a block of
358 streamed changes from an in-progress transaction.
359 typedef void (*LogicalDecodeStreamStopCB) (struct LogicalDecodingContext *ctx,
360 ReorderBufferTXN *txn);
362 47.6.4.16. Stream Abort Callback #
364 The required stream_abort_cb callback is called to abort a previously
365 streamed transaction.
366 typedef void (*LogicalDecodeStreamAbortCB) (struct LogicalDecodingContext *ctx,
367 ReorderBufferTXN *txn,
368 XLogRecPtr abort_lsn);
370 47.6.4.17. Stream Prepare Callback #
372 The stream_prepare_cb callback is called to prepare a previously
373 streamed transaction as part of a two-phase commit. This callback is
374 required when the output plugin supports both the streaming of large
375 in-progress transactions and two-phase commits.
376 typedef void (*LogicalDecodeStreamPrepareCB) (struct LogicalDecodingContext *ctx
378 ReorderBufferTXN *txn,
379 XLogRecPtr prepare_lsn);
381 47.6.4.18. Stream Commit Callback #
383 The required stream_commit_cb callback is called to commit a previously
384 streamed transaction.
385 typedef void (*LogicalDecodeStreamCommitCB) (struct LogicalDecodingContext *ctx,
386 ReorderBufferTXN *txn,
387 XLogRecPtr commit_lsn);
389 47.6.4.19. Stream Change Callback #
391 The required stream_change_cb callback is called when sending a change
392 in a block of streamed changes (demarcated by stream_start_cb and
393 stream_stop_cb calls). The actual changes are not displayed as the
394 transaction can abort at a later point in time and we don't decode
395 changes for aborted transactions.
396 typedef void (*LogicalDecodeStreamChangeCB) (struct LogicalDecodingContext *ctx,
397 ReorderBufferTXN *txn,
399 ReorderBufferChange *change);
401 47.6.4.20. Stream Message Callback #
403 The optional stream_message_cb callback is called when sending a
404 generic message in a block of streamed changes (demarcated by
405 stream_start_cb and stream_stop_cb calls). The message contents for
406 transactional messages are not displayed as the transaction can abort
407 at a later point in time and we don't decode changes for aborted
409 typedef void (*LogicalDecodeStreamMessageCB) (struct LogicalDecodingContext *ctx
411 ReorderBufferTXN *txn,
412 XLogRecPtr message_lsn,
416 const char *message);
418 47.6.4.21. Stream Truncate Callback #
420 The optional stream_truncate_cb callback is called for a TRUNCATE
421 command in a block of streamed changes (demarcated by stream_start_cb
422 and stream_stop_cb calls).
423 typedef void (*LogicalDecodeStreamTruncateCB) (struct LogicalDecodingContext *ct
425 ReorderBufferTXN *txn,
427 Relation relations[],
428 ReorderBufferChange *change);
430 The parameters are analogous to the stream_change_cb callback. However,
431 because TRUNCATE actions on tables connected by foreign keys need to be
432 executed together, this callback receives an array of relations instead
433 of just a single one. See the description of the TRUNCATE statement for
436 47.6.5. Functions for Producing Output #
438 To actually produce output, output plugins can write data to the
439 StringInfo output buffer in ctx->out when inside the begin_cb,
440 commit_cb, or change_cb callbacks. Before writing to the output buffer,
441 OutputPluginPrepareWrite(ctx, last_write) has to be called, and after
442 finishing writing to the buffer, OutputPluginWrite(ctx, last_write) has
443 to be called to perform the write. The last_write indicates whether a
444 particular write was the callback's last write.
446 The following example shows how to output data to the consumer of an
448 OutputPluginPrepareWrite(ctx, true);
449 appendStringInfo(ctx->out, "BEGIN %u", txn->xid);
450 OutputPluginWrite(ctx, true);