Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e3034a2

Browse files
vigneshwaran-cCommitfest Bot
authored and
Commitfest Bot
committed
Documentation for sequence synchronization feature.
Documentation for sequence synchronization feature.
1 parent b4b9efc commit e3034a2

File tree

8 files changed

+450
-60
lines changed

8 files changed

+450
-60
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8155,16 +8155,19 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
81558155
</indexterm>
81568156

81578157
<para>
8158-
The catalog <structname>pg_subscription_rel</structname> contains the
8159-
state for each replicated relation in each subscription. This is a
8160-
many-to-many mapping.
8158+
The catalog <structname>pg_subscription_rel</structname> stores the
8159+
state of each replicated table and sequence for each subscription. This
8160+
is a many-to-many mapping.
81618161
</para>
81628162

81638163
<para>
8164-
This catalog only contains tables known to the subscription after running
8165-
either <link linkend="sql-createsubscription"><command>CREATE SUBSCRIPTION</command></link> or
8166-
<link linkend="sql-altersubscription"><command>ALTER SUBSCRIPTION ... REFRESH
8167-
PUBLICATION</command></link>.
8164+
This catalog only contains tables and sequences known to the subscription
8165+
after running
8166+
<link linkend="sql-createsubscription"><command>CREATE SUBSCRIPTION</command></link> or
8167+
<link linkend="sql-altersubscription-params-refresh-publication">
8168+
<command>ALTER SUBSCRIPTION ... REFRESH PUBLICATION</command></link> or
8169+
<link linkend="sql-altersubscription-params-refresh-publication-sequences">
8170+
<command>ALTER SUBSCRIPTION ... REFRESH PUBLICATION SEQUENCES</command></link>.
81688171
</para>
81698172

81708173
<table>
@@ -8198,7 +8201,7 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
81988201
(references <link linkend="catalog-pg-class"><structname>pg_class</structname></link>.<structfield>oid</structfield>)
81998202
</para>
82008203
<para>
8201-
Reference to relation
8204+
Reference to table or sequence
82028205
</para></entry>
82038206
</row>
82048207

@@ -8207,12 +8210,20 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
82078210
<structfield>srsubstate</structfield> <type>char</type>
82088211
</para>
82098212
<para>
8210-
State code:
8213+
State code for the table or sequence.
8214+
</para>
8215+
<para>
8216+
State codes for tables:
82118217
<literal>i</literal> = initialize,
82128218
<literal>d</literal> = data is being copied,
82138219
<literal>f</literal> = finished table copy,
82148220
<literal>s</literal> = synchronized,
82158221
<literal>r</literal> = ready (normal replication)
8222+
</para>
8223+
<para>
8224+
State codes for sequences:
8225+
<literal>i</literal> = initialize,
8226+
<literal>r</literal> = ready
82168227
</para></entry>
82178228
</row>
82188229

doc/src/sgml/config.sgml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5184,9 +5184,9 @@ ANY <replaceable class="parameter">num_sync</replaceable> ( <replaceable class="
51845184
is taken into account.
51855185
</para>
51865186
<para>
5187-
In logical replication, this parameter also limits how often a failing
5188-
replication apply worker or table synchronization worker will be
5189-
respawned.
5187+
In logical replication, this parameter also limits how quickly a
5188+
failing replication apply worker or table synchronization worker or
5189+
sequence synchronization worker will be respawned.
51905190
</para>
51915191
</listitem>
51925192
</varlistentry>
@@ -5327,8 +5327,8 @@ ANY <replaceable class="parameter">num_sync</replaceable> ( <replaceable class="
53275327
<listitem>
53285328
<para>
53295329
Specifies maximum number of logical replication workers. This includes
5330-
leader apply workers, parallel apply workers, and table synchronization
5331-
workers.
5330+
leader apply workers, parallel apply workers, table synchronization
5331+
workers and a sequence synchronization worker.
53325332
</para>
53335333
<para>
53345334
Logical replication workers are taken from the pool defined by
@@ -5351,10 +5351,12 @@ ANY <replaceable class="parameter">num_sync</replaceable> ( <replaceable class="
53515351
<para>
53525352
Maximum number of synchronization workers per subscription. This
53535353
parameter controls the amount of parallelism of the initial data copy
5354-
during the subscription initialization or when new tables are added.
5354+
during the subscription initialization or when new tables or sequences
5355+
are added.
53555356
</para>
53565357
<para>
5357-
Currently, there can be only one synchronization worker per table.
5358+
Currently, there can be only one table synchronization worker per table
5359+
and one sequence synchronization worker to synchronize all sequences.
53585360
</para>
53595361
<para>
53605362
The synchronization workers are taken from the pool defined by

doc/src/sgml/logical-replication.sgml

Lines changed: 225 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,20 @@
102102
A <firstterm>publication</firstterm> can be defined on any physical
103103
replication primary. The node where a publication is defined is referred to
104104
as <firstterm>publisher</firstterm>. A publication is a set of changes
105-
generated from a table or a group of tables, and might also be described as
106-
a change set or replication set. Each publication exists in only one database.
105+
generated from a table or a group of tables or the current state of all
106+
sequences, and might also be described as a change set or replication set.
107+
Each publication exists in only one database.
107108
</para>
108109

109110
<para>
110111
Publications are different from schemas and do not affect how the table is
111112
accessed. Each table can be added to multiple publications if needed.
112-
Publications may currently only contain tables and all tables in schema.
113-
Objects must be added explicitly, except when a publication is created for
114-
<literal>ALL TABLES</literal>.
113+
Publications may currently only contain tables or sequences. Objects must be
114+
added explicitly, except when a publication is created using
115+
<literal>FOR TABLES IN SCHEMA</literal>, or <literal>FOR ALL TABLES</literal>,
116+
or <literal>FOR ALL SEQUENCES</literal>. Unlike tables, the current state of
117+
sequences may be synchronized at any time. For more information, refer to
118+
<xref linkend="logical-replication-sequences"/>.
115119
</para>
116120

117121
<para>
@@ -1711,6 +1715,204 @@ Publications:
17111715
</note>
17121716
</sect1>
17131717

1718+
<sect1 id="logical-replication-sequences">
1719+
<title>Replicating Sequences</title>
1720+
1721+
<para>
1722+
To replicate sequences from a publisher to a subscriber, first publish them
1723+
using <link linkend="sql-createpublication-params-for-all-sequences">
1724+
<command>CREATE PUBLICATION ... FOR ALL SEQUENCES</command></link>.
1725+
</para>
1726+
1727+
<para>
1728+
At the subscriber side:
1729+
<itemizedlist>
1730+
<listitem>
1731+
<para>
1732+
use <link linkend="sql-createsubscription"><command>CREATE SUBSCRIPTION</command></link>
1733+
to initially synchronize the published sequences.
1734+
</para>
1735+
</listitem>
1736+
<listitem>
1737+
<para>
1738+
use <link linkend="sql-altersubscription-params-refresh-publication">
1739+
<command>ALTER SUBSCRIPTION ... REFRESH PUBLICATION</command></link>
1740+
to synchronize only newly added sequences.
1741+
</para>
1742+
</listitem>
1743+
<listitem>
1744+
<para>
1745+
use <link linkend="sql-altersubscription-params-refresh-publication-sequences">
1746+
<command>ALTER SUBSCRIPTION ... REFRESH PUBLICATION SEQUENCES</command></link>
1747+
to re-synchronize all sequences.
1748+
</para>
1749+
</listitem>
1750+
</itemizedlist>
1751+
</para>
1752+
1753+
<para>
1754+
A new <firstterm>sequence synchronization worker</firstterm> will be started
1755+
after executing any of the above subscriber commands, and will exit once the
1756+
sequences are synchronized.
1757+
</para>
1758+
<para>
1759+
The ability to launch a sequence synchronization worker is limited by the
1760+
<link linkend="guc-max-sync-workers-per-subscription">
1761+
<varname>max_sync_workers_per_subscription</varname></link>
1762+
configuration.
1763+
</para>
1764+
1765+
<sect2 id="sequence-definition-mismatches">
1766+
<title>Sequence Definition Mismatches</title>
1767+
<warning>
1768+
<para>
1769+
During sequence synchronization, the sequence definitions of the publisher
1770+
and the subscriber are compared. A WARNING is logged listing all differing
1771+
sequences before the process exits. The apply worker detects the failure
1772+
and repeatedly respawns the sequence synchronization worker to continue
1773+
the synchronization process until all differences are resolved. See also
1774+
<link linkend="guc-wal-retrieve-retry-interval"><varname>wal_retrieve_retry_interval</varname></link>.
1775+
</para>
1776+
</warning>
1777+
<para>
1778+
To resolve this, use
1779+
<link linkend="sql-altersequence"><command>ALTER SEQUENCE</command></link>
1780+
to align the subscriber's sequence parameters with those of the publisher.
1781+
Then, execute <link linkend="sql-altersubscription-params-refresh-publication-sequences">
1782+
<command>ALTER SUBSCRIPTION ... REFRESH PUBLICATION SEQUENCES</command></link>.
1783+
</para>
1784+
</sect2>
1785+
1786+
<sect2 id="sequences-out-of-sync">
1787+
<title>Refreshing Stale Sequences</title>
1788+
<para>
1789+
Subscriber side sequence values may frequently become out of sync due to
1790+
updates on the publisher.
1791+
</para>
1792+
<para>
1793+
To verify, compare the sequence values between the publisher and
1794+
subscriber, and if necessary, execute
1795+
<link linkend="sql-altersubscription-params-refresh-publication-sequences">
1796+
<command>ALTER SUBSCRIPTION ... REFRESH PUBLICATION SEQUENCES</command></link>.
1797+
</para>
1798+
</sect2>
1799+
1800+
<sect2 id="logical-replication-sequences-examples">
1801+
<title>Examples</title>
1802+
1803+
<para>
1804+
Create some sequences on the publisher.
1805+
<programlisting>
1806+
test_pub=# CREATE SEQUENCE s1 START WITH 10 INCREMENT BY 1;
1807+
CREATE SEQUENCE
1808+
test_pub=# CREATE SEQUENCE s2 START WITH 100 INCREMENT BY 10;
1809+
CREATE SEQUENCE
1810+
</programlisting></para>
1811+
1812+
<para>
1813+
Create the same sequences on the subscriber.
1814+
<programlisting>
1815+
test_sub=# CREATE SEQUENCE s1 START WITH 10 INCREMENT BY 1
1816+
CREATE SEQUENCE
1817+
test_sub=# CREATE SEQUENCE s2 START WITH 100 INCREMENT BY 10;
1818+
CREATE SEQUENCE
1819+
</programlisting></para>
1820+
1821+
<para>
1822+
Update the sequences at the publisher side few times.
1823+
<programlisting>
1824+
test_pub=# SELECT nextval('s1');
1825+
nextval
1826+
---------
1827+
10
1828+
(1 row)
1829+
test_pub=# SELECT NEXTVAL('s1');
1830+
nextval
1831+
---------
1832+
11
1833+
(1 row)
1834+
test_pub=# SELECT nextval('s2');
1835+
nextval
1836+
---------
1837+
100
1838+
(1 row)
1839+
test_pub=# SELECT nextval('s2');
1840+
nextval
1841+
---------
1842+
110
1843+
(1 row)
1844+
</programlisting></para>
1845+
1846+
<para>
1847+
Create a publication for the sequences.
1848+
<programlisting>
1849+
test_pub=# CREATE PUBLICATION pub1 FOR ALL SEQUENCES;
1850+
CREATE PUBLICATION
1851+
</programlisting></para>
1852+
1853+
<para>
1854+
Subscribe to the publication.
1855+
<programlisting>
1856+
test_sub=# CREATE SUBSCRIPTION sub1
1857+
test_sub-# CONNECTION 'host=localhost dbname=test_pub application_name=sub1'
1858+
test_sub-# PUBLICATION pub1;
1859+
CREATE SUBSCRIPTION
1860+
</programlisting></para>
1861+
1862+
<para>
1863+
Observe that initial sequence values are synchronized.
1864+
<programlisting>
1865+
test_sub=# SELECT * FROM s1;
1866+
last_value | log_cnt | is_called
1867+
------------+---------+-----------
1868+
11 | 31 | t
1869+
(1 row)
1870+
1871+
test_sub=# SELECT * FROM s2;
1872+
last_value | log_cnt | is_called
1873+
------------+---------+-----------
1874+
110 | 31 | t
1875+
(1 row)
1876+
</programlisting></para>
1877+
1878+
<para>
1879+
Update the sequences at the publisher side.
1880+
<programlisting>
1881+
test_pub=# SELECT nextval('s1');
1882+
nextval
1883+
---------
1884+
12
1885+
(1 row)
1886+
test_pub=# SELECT nextval('s2');
1887+
nextval
1888+
---------
1889+
120
1890+
(1 row)
1891+
</programlisting></para>
1892+
1893+
<para>
1894+
Re-synchronize all the sequences at the subscriber side using
1895+
<link linkend="sql-altersubscription-params-refresh-publication-sequences">
1896+
<command>ALTER SUBSCRIPTION ... REFRESH PUBLICATION SEQUENCES</command></link>.
1897+
<programlisting>
1898+
test_sub=# ALTER SUBSCRIPTION sub1 REFRESH PUBLICATION SEQUENCES;
1899+
ALTER SUBSCRIPTION
1900+
1901+
test_sub=# SELECT * FROM s1;
1902+
last_value | log_cnt | is_called
1903+
------------+---------+-----------
1904+
12 | 30 | t
1905+
(1 row)
1906+
1907+
test_sub=# SELECT * FROM s2
1908+
last_value | log_cnt | is_called
1909+
------------+---------+-----------
1910+
120 | 30 | t
1911+
(1 row)
1912+
</programlisting></para>
1913+
</sect2>
1914+
</sect1>
1915+
17141916
<sect1 id="logical-replication-conflicts">
17151917
<title>Conflicts</title>
17161918

@@ -2040,16 +2242,19 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER
20402242

20412243
<listitem>
20422244
<para>
2043-
Sequence data is not replicated. The data in serial or identity columns
2044-
backed by sequences will of course be replicated as part of the table,
2045-
but the sequence itself would still show the start value on the
2046-
subscriber. If the subscriber is used as a read-only database, then this
2047-
should typically not be a problem. If, however, some kind of switchover
2048-
or failover to the subscriber database is intended, then the sequences
2049-
would need to be updated to the latest values, either by copying the
2050-
current data from the publisher (perhaps
2051-
using <command>pg_dump</command>) or by determining a sufficiently high
2052-
value from the tables themselves.
2245+
Incremental sequence changes are not replicated. Although the data in
2246+
serial or identity columns backed by sequences will be replicated as part
2247+
of the table, the sequences themselves do not replicate ongoing changes.
2248+
On the subscriber, a sequence will retain the last value it synchronized
2249+
from the publisher. If the subscriber is used as a read-only database,
2250+
then this should typically not be a problem. If, however, some kind of
2251+
switchover or failover to the subscriber database is intended, then the
2252+
sequences would need to be updated to the latest values, either by
2253+
executing <link linkend="sql-altersubscription-params-refresh-publication-sequences">
2254+
<command>ALTER SUBSCRIPTION ... REFRESH PUBLICATION SEQUENCES</command></link>
2255+
or by copying the current data from the publisher (perhaps using
2256+
<command>pg_dump</command>) or by determining a sufficiently high value
2257+
from the tables themselves.
20532258
</para>
20542259
</listitem>
20552260

@@ -2367,8 +2572,8 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER
23672572
<para>
23682573
<link linkend="guc-max-logical-replication-workers"><varname>max_logical_replication_workers</varname></link>
23692574
must be set to at least the number of subscriptions (for leader apply
2370-
workers), plus some reserve for the table synchronization workers and
2371-
parallel apply workers.
2575+
workers), plus some reserve for the parallel apply workers, table synchronization workers, and a sequence
2576+
synchronization worker.
23722577
</para>
23732578

23742579
<para>
@@ -2381,8 +2586,9 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER
23812586

23822587
<para>
23832588
<link linkend="guc-max-sync-workers-per-subscription"><varname>max_sync_workers_per_subscription</varname></link>
2384-
controls the amount of parallelism of the initial data copy during the
2385-
subscription initialization or when new tables are added.
2589+
controls how many tables can be synchronized in parallel during
2590+
subscription initialization or when new tables are added. One additional
2591+
worker is also needed for sequence synchronization.
23862592
</para>
23872593

23882594
<para>

0 commit comments

Comments
 (0)