codekingpro/portable-devtools
115k
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>49.9. Streaming of Large Transactions 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-synchronous.html" title="49.8. Synchronous Replication Support for Logical Decoding" /><link rel="next" href="logicaldecoding-two-phase-commits.html" title="49.10. Two-phase Commit Support for Logical Decoding" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">49.9. Streaming of Large Transactions for Logical Decoding</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="logicaldecoding-synchronous.html" title="49.8. Synchronous Replication Support for Logical Decoding">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="logicaldecoding.html" title="Chapter 49. Logical Decoding">Up</a></td><th width="60%" align="center">Chapter 49. Logical Decoding</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="logicaldecoding-two-phase-commits.html" title="49.10. Two-phase Commit Support for Logical Decoding">Next</a></td></tr></table><hr /></div><div class="sect1" id="LOGICALDECODING-STREAMING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">49.9. Streaming of Large Transactions for Logical Decoding <a href="#LOGICALDECODING-STREAMING" class="id_link">#</a></h2></div></div></div><p>3 The basic output plugin callbacks (e.g., <code class="function">begin_cb</code>,4 <code class="function">change_cb</code>, <code class="function">commit_cb</code> and5 <code class="function">message_cb</code>) are only invoked when the transaction6 actually commits. The changes are still decoded from the transaction7 log, but are only passed to the output plugin at commit (and discarded8 if the transaction aborts).9 </p><p>10 This means that while the decoding happens incrementally, and may spill11 to disk to keep memory usage under control, all the decoded changes have12 to be transmitted when the transaction finally commits (or more precisely,13 when the commit is decoded from the transaction log). Depending on the14 size of the transaction and network bandwidth, the transfer time may15 significantly increase the apply lag.16 </p><p>17 To reduce the apply lag caused by large transactions, an output plugin18 may provide additional callback to support incremental streaming of19 in-progress transactions. There are multiple required streaming callbacks20 (<code class="function">stream_start_cb</code>, <code class="function">stream_stop_cb</code>,21 <code class="function">stream_abort_cb</code>, <code class="function">stream_commit_cb</code>22 and <code class="function">stream_change_cb</code>) and two optional callbacks23 (<code class="function">stream_message_cb</code> and <code class="function">stream_truncate_cb</code>).24 Also, if streaming of two-phase commands is to be supported, then additional25 callbacks must be provided. (See <a class="xref" href="logicaldecoding-two-phase-commits.html" title="49.10. Two-phase Commit Support for Logical Decoding">Section 49.10</a>26 for details).27 </p><p>28 When streaming an in-progress transaction, the changes (and messages) are29 streamed in blocks demarcated by <code class="function">stream_start_cb</code>30 and <code class="function">stream_stop_cb</code> callbacks. Once all the decoded31 changes are transmitted, the transaction can be committed using the32 <code class="function">stream_commit_cb</code> callback33 (or possibly aborted using the <code class="function">stream_abort_cb</code> callback).34 If two-phase commits are supported, the transaction can be prepared using the35 <code class="function">stream_prepare_cb</code> callback,36 <code class="command">COMMIT PREPARED</code> using the37 <code class="function">commit_prepared_cb</code> callback or aborted using the38 <code class="function">rollback_prepared_cb</code>.39 </p><p>40 One example sequence of streaming callback calls for one transaction may41 look like this:42</p><pre class="programlisting">43stream_start_cb(...); <-- start of first block of changes44 stream_change_cb(...);45 stream_change_cb(...);46 stream_message_cb(...);47 stream_change_cb(...);48 ...49 stream_change_cb(...);50stream_stop_cb(...); <-- end of first block of changes51 52stream_start_cb(...); <-- start of second block of changes53 stream_change_cb(...);54 stream_change_cb(...);55 stream_change_cb(...);56 ...57 stream_message_cb(...);58 stream_change_cb(...);59stream_stop_cb(...); <-- end of second block of changes60 61 62[a. when using normal commit]63stream_commit_cb(...); <-- commit of the streamed transaction64 65[b. when using two-phase commit]66stream_prepare_cb(...); <-- prepare the streamed transaction67commit_prepared_cb(...); <-- commit of the prepared transaction68</pre><p>69 </p><p>70 The actual sequence of callback calls may be more complicated, of course.71 There may be blocks for multiple streamed transactions, some of the72 transactions may get aborted, etc.73 </p><p>74 Similar to spill-to-disk behavior, streaming is triggered when the total75 amount of changes decoded from the WAL (for all in-progress transactions)76 exceeds the limit defined by <code class="varname">logical_decoding_work_mem</code> setting.77 At that point, the largest top-level transaction (measured by the amount of memory78 currently used for decoded changes) is selected and streamed. However, in79 some cases we still have to spill to disk even if streaming is enabled80 because we exceed the memory threshold but still have not decoded the81 complete tuple e.g., only decoded toast table insert but not the main table82 insert.83 </p><p>84 Even when streaming large transactions, the changes are still applied in85 commit order, preserving the same guarantees as the non-streaming mode.86 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="logicaldecoding-synchronous.html" title="49.8. Synchronous Replication Support for Logical Decoding">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="logicaldecoding.html" title="Chapter 49. Logical Decoding">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="logicaldecoding-two-phase-commits.html" title="49.10. Two-phase Commit Support for Logical Decoding">Next</a></td></tr><tr><td width="40%" align="left" valign="top">49.8. Synchronous Replication Support for Logical Decoding </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 49.10. Two-phase Commit Support for Logical Decoding</td></tr></table></div></body></html>