/
Ao
/
MaxReport
Обзор
Документация
Войти
/
Ao
/
MaxReport
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
pgsql/doc/src/sgml/html/logicaldecoding-explanation.html
199 строк
19 KB
AoAnima
first_commit
13 июл 2026, 17:47
13 июл 2026, 17:47
f1a670a
Код
Авторство
О чём код?
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!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.2. Logical Decoding Concepts</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-example.html" title="47.1. Logical Decoding Examples" /><link rel="next" href="logicaldecoding-walsender.html" title="47.3. Streaming Replication Protocol Interface" /></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.2. Logical Decoding Concepts</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="logicaldecoding-example.html" title="47.1. Logical Decoding Examples">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.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="logicaldecoding-walsender.html" title="47.3. Streaming Replication Protocol Interface">Next</a></td></tr></table><hr /></div><div class="sect1" id="LOGICALDECODING-EXPLANATION"><div class="titlepage"><div><div><h2 class="title" style="clear: both">47.2. Logical Decoding Concepts <a href="#LOGICALDECODING-EXPLANATION" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="logicaldecoding-explanation.html#LOGICALDECODING-EXPLANATION-LOG-DEC">47.2.1. Logical Decoding</a></span></dt><dt><span class="sect2"><a href="logicaldecoding-explanation.html#LOGICALDECODING-REPLICATION-SLOTS">47.2.2. Replication Slots</a></span></dt><dt><span class="sect2"><a href="logicaldecoding-explanation.html#LOGICALDECODING-REPLICATION-SLOTS-SYNCHRONIZATION">47.2.3. Replication Slot Synchronization</a></span></dt><dt><span class="sect2"><a href="logicaldecoding-explanation.html#LOGICALDECODING-EXPLANATION-OUTPUT-PLUGINS">47.2.4. Output Plugins</a></span></dt><dt><span class="sect2"><a href="logicaldecoding-explanation.html#LOGICALDECODING-EXPLANATION-EXPORTED-SNAPSHOTS">47.2.5. Exported Snapshots</a></span></dt></dl></div><div class="sect2" id="LOGICALDECODING-EXPLANATION-LOG-DEC"><div class="titlepage"><div><div><h3 class="title">47.2.1. Logical Decoding <a href="#LOGICALDECODING-EXPLANATION-LOG-DEC" class="id_link">#</a></h3></div></div></div><a id="id-1.8.14.8.2.2" class="indexterm"></a><p> Logical decoding is the process of extracting all persistent changes to a database's tables into a coherent, easy to understand format which can be interpreted without detailed knowledge of the database's internal state. </p><p> In <span class="productname">PostgreSQL</span>, logical decoding is implemented by decoding the contents of the <a class="link" href="wal.html" title="Chapter 28. Reliability and the Write-Ahead Log">write-ahead log</a>, which describe changes on a storage level, into an application-specific form such as a stream of tuples or SQL statements. </p></div><div class="sect2" id="LOGICALDECODING-REPLICATION-SLOTS"><div class="titlepage"><div><div><h3 class="title">47.2.2. Replication Slots <a href="#LOGICALDECODING-REPLICATION-SLOTS" class="id_link">#</a></h3></div></div></div><a id="id-1.8.14.8.3.2" class="indexterm"></a><p> In the context of logical replication, a slot represents a stream of changes that can be replayed to a client in the order they were made on the origin server. Each slot streams a sequence of changes from a single database. </p><div class="note"><h3 class="title">Note</h3><p><span class="productname">PostgreSQL</span> also has streaming replication slots (see <a class="xref" href="warm-standby.html#STREAMING-REPLICATION" title="26.2.5. Streaming Replication">Section 26.2.5</a>), but they are used somewhat differently there. </p></div><p> A replication slot has an identifier that is unique across all databases in a <span class="productname">PostgreSQL</span> cluster. Slots persist independently of the connection using them and are crash-safe. </p><p> A logical slot will emit each change just once in normal operation. The current position of each slot is persisted only at checkpoint, so in the case of a crash the slot might return to an earlier LSN, which will then cause recent changes to be sent again when the server restarts. Logical decoding clients are responsible for avoiding ill effects from handling the same message more than once. Clients may wish to record the last LSN they saw when decoding and skip over any repeated data or (when using the replication protocol) request that decoding start from that LSN rather than letting the server determine the start point. The Replication Progress Tracking feature is designed for this purpose, refer to <a class="link" href="replication-origins.html" title="Chapter 48. Replication Progress Tracking">replication origins</a>. </p><p> Multiple independent slots may exist for a single database. Each slot has its own state, allowing different consumers to receive changes from different points in the database change stream. For most applications, a separate slot will be required for each consumer. </p><p> A logical replication slot knows nothing about the state of the receiver(s). It's even possible to have multiple different receivers using the same slot at different times; they'll just get the changes following on from when the last receiver stopped consuming them. Only one receiver may consume changes from a slot at any given time. </p><p> A logical replication slot can also be created on a hot standby. To prevent <code class="command">VACUUM</code> from removing required rows from the system catalogs, <code class="varname">hot_standby_feedback</code> should be set on the standby. In spite of that, if any required rows get removed, the slot gets invalidated. It's highly recommended to use a physical slot between the primary and the standby. Otherwise, <code class="varname">hot_standby_feedback</code> will work but only while the connection is alive (for example a node restart would break it). Then, the primary may delete system catalog rows that could be needed by the logical decoding on the standby (as it does not know about the <code class="literal">catalog_xmin</code> on the standby). Existing logical slots on standby also get invalidated if <code class="varname">wal_level</code> on the primary is reduced to less than <code class="literal">logical</code>. This is done as soon as the standby detects such a change in the WAL stream. It means that, for walsenders that are lagging (if any), some WAL records up to the <code class="varname">wal_level</code> parameter change on the primary won't be decoded. </p><p> Creation of a logical slot requires information about all the currently running transactions. On the primary, this information is available directly, but on a standby, this information has to be obtained from primary. Thus, slot creation may need to wait for some activity to happen on the primary. If the primary is idle, creating a logical slot on standby may take noticeable time. This can be sped up by calling the <code class="function">pg_log_standby_snapshot</code> function on the primary. </p><div class="caution"><h3 class="title">Caution</h3><p> Replication slots persist across crashes and know nothing about the state of their consumer(s). They will prevent removal of required resources even when there is no connection using them. This consumes storage because neither required WAL nor required rows from the system catalogs can be removed by <code class="command">VACUUM</code> as long as they are required by a replication slot. In extreme cases this could cause the database to shut down to prevent transaction ID wraparound (see <a class="xref" href="routine-vacuuming.html#VACUUM-FOR-WRAPAROUND" title="24.1.5. Preventing Transaction ID Wraparound Failures">Section 24.1.5</a>). So if a slot is no longer required it should be dropped. </p></div></div><div class="sect2" id="LOGICALDECODING-REPLICATION-SLOTS-SYNCHRONIZATION"><div class="titlepage"><div><div><h3 class="title">47.2.3. Replication Slot Synchronization <a href="#LOGICALDECODING-REPLICATION-SLOTS-SYNCHRONIZATION" class="id_link">#</a></h3></div></div></div><p> The logical replication slots on the primary can be synchronized to the hot standby by using the <code class="literal">failover</code> parameter of <a class="link" href="functions-admin.html#PG-CREATE-LOGICAL-REPLICATION-SLOT"> <code class="function">pg_create_logical_replication_slot</code></a>, or by using the <a class="link" href="sql-createsubscription.html#SQL-CREATESUBSCRIPTION-PARAMS-WITH-FAILOVER"> <code class="literal">failover</code></a> option of <code class="command">CREATE SUBSCRIPTION</code> during slot creation. Additionally, enabling <a class="link" href="runtime-config-replication.html#GUC-SYNC-REPLICATION-SLOTS"> <code class="varname">sync_replication_slots</code></a> on the standby is required. By enabling <a class="link" href="runtime-config-replication.html#GUC-SYNC-REPLICATION-SLOTS"> <code class="varname">sync_replication_slots</code></a> on the standby, the failover slots can be synchronized periodically in the slotsync worker. For the synchronization to work, it is mandatory to have a physical replication slot between the primary and the standby (i.e., <a class="link" href="runtime-config-replication.html#GUC-PRIMARY-SLOT-NAME"><code class="varname">primary_slot_name</code></a> should be configured on the standby), and <a class="link" href="runtime-config-replication.html#GUC-HOT-STANDBY-FEEDBACK"><code class="varname">hot_standby_feedback</code></a> must be enabled on the standby. It is also necessary to specify a valid <code class="literal">dbname</code> in the <a class="link" href="runtime-config-replication.html#GUC-PRIMARY-CONNINFO"><code class="varname">primary_conninfo</code></a>. It's highly recommended that the said physical replication slot is named in <a class="link" href="runtime-config-replication.html#GUC-SYNCHRONIZED-STANDBY-SLOTS"><code class="varname">synchronized_standby_slots</code></a> list on the primary, to prevent the subscriber from consuming changes faster than the hot standby. Even when correctly configured, some latency is expected when sending changes to logical subscribers due to the waiting on slots named in <a class="link" href="runtime-config-replication.html#GUC-SYNCHRONIZED-STANDBY-SLOTS"><code class="varname">synchronized_standby_slots</code></a>. When <code class="varname">synchronized_standby_slots</code> is utilized, the primary server will not completely shut down until the corresponding standbys, associated with the physical replication slots specified in <code class="varname">synchronized_standby_slots</code>, have confirmed receiving the WAL up to the latest flushed position on the primary server. </p><div class="note"><h3 class="title">Note</h3><p> While enabling <a class="link" href="runtime-config-replication.html#GUC-SYNC-REPLICATION-SLOTS"> <code class="varname">sync_replication_slots</code></a> allows for automatic periodic synchronization of failover slots, they can also be manually synchronized using the <a class="link" href="functions-admin.html#PG-SYNC-REPLICATION-SLOTS"> <code class="function">pg_sync_replication_slots</code></a> function on the standby. However, this function is primarily intended for testing and debugging and should be used with caution. Unlike automatic synchronization, it does not include cyclic retries, making it more prone to synchronization failures, particularly during initial sync scenarios where the required WAL files or catalog rows for the slot might have already been removed or are at risk of being removed on the standby. In contrast, automatic synchronization via <code class="varname">sync_replication_slots</code> provides continuous slot updates, enabling seamless failover and supporting high availability. Therefore, it is the recommended method for synchronizing slots. </p></div><p> When slot synchronization is configured as recommended, and the initial synchronization is performed either automatically or manually via <code class="function">pg_sync_replication_slots</code>, the standby can persist the synchronized slot only if the following condition is met: The logical replication slot on the primary must retain WALs and system catalog rows that are still available on the standby. This ensures data integrity and allows logical replication to continue smoothly after promotion. If the required WALs or catalog rows have already been purged from the standby, the slot will not be persisted to avoid data loss. In such cases, the following log message may appear: </p><pre class="programlisting"> LOG: could not synchronize replication slot "failover_slot" DETAIL: Synchronization could lead to data loss, because the remote slot needs WAL at LSN 0/3003F28 and catalog xmin 754, but the standby has LSN 0/3003F28 and catalog xmin 756. </pre><p> If the logical replication slot is actively used by a consumer, no manual intervention is needed; the slot will advance automatically, and synchronization will resume in the next cycle. However, if no consumer is configured, it is advisable to manually advance the slot on the primary using <a class="link" href="functions-admin.html#PG-LOGICAL-SLOT-GET-CHANGES"> <code class="function">pg_logical_slot_get_changes</code></a> or <a class="link" href="functions-admin.html#PG-LOGICAL-SLOT-GET-BINARY-CHANGES"> <code class="function">pg_logical_slot_get_binary_changes</code></a>, allowing synchronization to proceed. </p><p> The ability to resume logical replication after failover depends upon the <a class="link" href="view-pg-replication-slots.html" title="53.20. pg_replication_slots">pg_replication_slots</a>.<code class="structfield">synced</code> value for the synchronized slots on the standby at the time of failover. Only persistent slots that have attained synced state as true on the standby before failover can be used for logical replication after failover. Temporary synced slots cannot be used for logical decoding, therefore logical replication for those slots cannot be resumed. For example, if the synchronized slot could not become persistent on the standby due to a disabled subscription, then the subscription cannot be resumed after failover even when it is enabled. </p><p> To resume logical replication after failover from the synced logical slots, the subscription's 'conninfo' must be altered to point to the new primary server. This is done using <a class="link" href="sql-altersubscription.html#SQL-ALTERSUBSCRIPTION-PARAMS-CONNECTION"><code class="command">ALTER SUBSCRIPTION ... CONNECTION</code></a>. It is recommended that subscriptions are first disabled before promoting the standby and are re-enabled after altering the connection string. </p><div class="caution"><h3 class="title">Caution</h3><p> There is a chance that the old primary is up again during the promotion and if subscriptions are not disabled, the logical subscribers may continue to receive data from the old primary server even after promotion until the connection string is altered. This might result in data inconsistency issues, preventing the logical subscribers from being able to continue replication from the new primary server. </p></div></div><div class="sect2" id="LOGICALDECODING-EXPLANATION-OUTPUT-PLUGINS"><div class="titlepage"><div><div><h3 class="title">47.2.4. Output Plugins <a href="#LOGICALDECODING-EXPLANATION-OUTPUT-PLUGINS" class="id_link">#</a></h3></div></div></div><p> Output plugins transform the data from the write-ahead log's internal representation into the format the consumer of a replication slot desires. </p></div><div class="sect2" id="LOGICALDECODING-EXPLANATION-EXPORTED-SNAPSHOTS"><div class="titlepage"><div><div><h3 class="title">47.2.5. Exported Snapshots <a href="#LOGICALDECODING-EXPLANATION-EXPORTED-SNAPSHOTS" class="id_link">#</a></h3></div></div></div><p> When a new replication slot is created using the streaming replication interface (see <a class="xref" href="protocol-replication.html#PROTOCOL-REPLICATION-CREATE-REPLICATION-SLOT">CREATE_REPLICATION_SLOT</a>), a snapshot is exported (see <a class="xref" href="functions-admin.html#FUNCTIONS-SNAPSHOT-SYNCHRONIZATION" title="9.28.5. Snapshot Synchronization Functions">Section 9.28.5</a>), which will show exactly the state of the database after which all changes will be included in the change stream. This can be used to create a new replica by using <a class="link" href="sql-set-transaction.html" title="SET TRANSACTION"><code class="literal">SET TRANSACTION SNAPSHOT</code></a> to read the state of the database at the moment the slot was created. This transaction can then be used to dump the database's state at that point in time, which afterwards can be updated using the slot's contents without losing any changes. </p><p> Applications that do not require snapshot export may suppress it with the <code class="literal">SNAPSHOT 'nothing'</code> option. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="logicaldecoding-example.html" title="47.1. Logical Decoding Examples">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="logicaldecoding-walsender.html" title="47.3. Streaming Replication Protocol Interface">Next</a></td></tr><tr><td width="40%" align="left" valign="top">47.1. Logical Decoding Examples </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.4 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 47.3. Streaming Replication Protocol Interface</td></tr></table></div></body></html>