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>47.10. Two-phase Commit Support for Logical Decoding</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 Vsnapshot" /><link rel="prev" href="logicaldecoding-streaming.html" title="47.9. Streaming of Large Transactions for Logical Decoding" /><link rel="next" href="replication-origins.html" title="Chapter 48. Replication Progress Tracking" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">47.10. Two-phase Commit Support for Logical Decoding</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="logicaldecoding-streaming.html" title="47.9. Streaming of Large Transactions for Logical Decoding">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="logicaldecoding.html" title="Chapter 47. Logical Decoding">Up</a></td><th width="60%" align="center">Chapter 47. Logical Decoding</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="replication-origins.html" title="Chapter 48. Replication Progress Tracking">Next</a></td></tr></table><hr /></div><div class="sect1" id="LOGICALDECODING-TWO-PHASE-COMMITS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">47.10. Two-phase Commit Support for Logical Decoding <a href="#LOGICALDECODING-TWO-PHASE-COMMITS" class="id_link">#</a></h2></div></div></div><p>
3 With the basic output plugin callbacks (eg., <code class="function">begin_cb</code>,
4 <code class="function">change_cb</code>, <code class="function">commit_cb</code> and
5 <code class="function">message_cb</code>) two-phase commit commands like
6 <code class="command">PREPARE TRANSACTION</code>, <code class="command">COMMIT PREPARED</code>
7 and <code class="command">ROLLBACK PREPARED</code> are not decoded. While the
8 <code class="command">PREPARE TRANSACTION</code> is ignored,
9 <code class="command">COMMIT PREPARED</code> is decoded as a <code class="command">COMMIT</code>
10 and <code class="command">ROLLBACK PREPARED</code> is decoded as a
11 <code class="command">ROLLBACK</code>.
13 To support the streaming of two-phase commands, an output plugin needs to
14 provide additional callbacks. There are multiple two-phase commit callbacks
15 that are required, (<code class="function">begin_prepare_cb</code>,
16 <code class="function">prepare_cb</code>, <code class="function">commit_prepared_cb</code>,
17 <code class="function">rollback_prepared_cb</code> and
18 <code class="function">stream_prepare_cb</code>) and an optional callback
19 (<code class="function">filter_prepare_cb</code>).
21 If the output plugin callbacks for decoding two-phase commit commands are
22 provided, then on <code class="command">PREPARE TRANSACTION</code>, the changes of
23 that transaction are decoded, passed to the output plugin, and the
24 <code class="function">prepare_cb</code> callback is invoked. This differs from the
25 basic decoding setup where changes are only passed to the output plugin
26 when a transaction is committed. The start of a prepared transaction is
27 indicated by the <code class="function">begin_prepare_cb</code> callback.
29 When a prepared transaction is rolled back using the
30 <code class="command">ROLLBACK PREPARED</code>, then the
31 <code class="function">rollback_prepared_cb</code> callback is invoked and when the
32 prepared transaction is committed using <code class="command">COMMIT PREPARED</code>,
33 then the <code class="function">commit_prepared_cb</code> callback is invoked.
35 Optionally the output plugin can define filtering rules via
36 <code class="function">filter_prepare_cb</code> to decode only specific transaction
37 in two phases. This can be achieved by pattern matching on the
38 <em class="parameter"><code>gid</code></em> or via lookups using the
39 <em class="parameter"><code>xid</code></em>.
41 The users that want to decode prepared transactions need to be careful about
42 below mentioned points:
44 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
45 If the prepared transaction has locked [user] catalog tables exclusively
46 then decoding prepare can block till the main transaction is committed.
47 </p></li><li class="listitem"><p>
48 The logical replication solution that builds distributed two phase commit
49 using this feature can deadlock if the prepared transaction has locked
50 [user] catalog tables exclusively. To avoid this users must refrain from
51 having locks on catalog tables (e.g. explicit <code class="command">LOCK</code> command)
53 See <a class="xref" href="logicaldecoding-synchronous.html#LOGICALDECODING-SYNCHRONOUS-CAVEATS" title="47.8.2. Caveats">Section 47.8.2</a> for the details.
54 </p></li></ul></div><p>
55 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="logicaldecoding-streaming.html" title="47.9. Streaming of Large Transactions for Logical Decoding">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="logicaldecoding.html" title="Chapter 47. Logical Decoding">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="replication-origins.html" title="Chapter 48. Replication Progress Tracking">Next</a></td></tr><tr><td width="40%" align="left" valign="top">47.9. Streaming of Large Transactions for Logical Decoding </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> Chapter 48. Replication Progress Tracking</td></tr></table></div></body></html>