102
102
A <firstterm>publication</firstterm> can be defined on any physical
103
103
replication primary. The node where a publication is defined is referred to
104
104
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.
107
108
</para>
108
109
109
110
<para>
110
111
Publications are different from schemas and do not affect how the table is
111
112
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"/>.
115
119
</para>
116
120
117
121
<para>
@@ -1711,6 +1715,204 @@ Publications:
1711
1715
</note>
1712
1716
</sect1>
1713
1717
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
+
1714
1916
<sect1 id="logical-replication-conflicts">
1715
1917
<title>Conflicts</title>
1716
1918
@@ -2040,16 +2242,19 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER
2040
2242
2041
2243
<listitem>
2042
2244
<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.
2053
2258
</para>
2054
2259
</listitem>
2055
2260
@@ -2367,8 +2572,8 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER
2367
2572
<para>
2368
2573
<link linkend="guc-max-logical-replication-workers"><varname>max_logical_replication_workers</varname></link>
2369
2574
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 .
2372
2577
</para>
2373
2578
2374
2579
<para>
@@ -2381,8 +2586,9 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER
2381
2586
2382
2587
<para>
2383
2588
<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.
2386
2592
</para>
2387
2593
2388
2594
<para>
0 commit comments