Skip to content

Commit 96b3784

Browse files
author
Amit Kapila
committed
Add "ALL SEQUENCES" support to publications.
This patch adds support for the ALL SEQUENCES clause in publications, enabling synchronization/replication of all sequences that is useful for upgrades. Publications can now include all sequences via FOR ALL SEQUENCES. psql enhancements: \d shows publications for a given sequence. \dRp indicates if a publication includes all sequences. ALL SEQUENCES can be combined with ALL TABLES, but not with other options like TABLE or TABLES IN SCHEMA. We can extend support for more granular clauses in future. The view pg_publication_sequences provides information about the mapping between publications and sequences. This patch enables publishing of sequences; subscriber-side support will be added in upcoming patches. Author: vignesh C <[email protected]> Author: Tomas Vondra <[email protected]> Reviewed-by: shveta malik <[email protected]> Reviewed-by: Dilip Kumar <[email protected]> Reviewed-by: Peter Smith <[email protected]> Reviewed-by: Hayato Kuroda <[email protected]> Reviewed-by: Masahiko Sawada <[email protected]> Reviewed-by: Nisha Moond <[email protected]> Reviewed-by: Shlok Kyal <[email protected]> Reviewed-by: Amit Kapila <[email protected]> Discussion: https://postgr.es/m/CAA4eK1LC+KJiAkSrpE_NwvNdidw9F2os7GERUeSxSKv71gXysQ@mail.gmail.com
1 parent ef5e60a commit 96b3784

File tree

23 files changed

+929
-354
lines changed

23 files changed

+929
-354
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6374,6 +6374,16 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
63746374
</para></entry>
63756375
</row>
63766376

6377+
<row>
6378+
<entry role="catalog_table_entry"><para role="column_definition">
6379+
<structfield>puballsequences</structfield> <type>bool</type>
6380+
</para>
6381+
<para>
6382+
If true, this publication automatically includes all sequences
6383+
in the database, including any that will be created in the future.
6384+
</para></entry>
6385+
</row>
6386+
63776387
<row>
63786388
<entry role="catalog_table_entry"><para role="column_definition">
63796389
<structfield>pubinsert</structfield> <type>bool</type>

doc/src/sgml/logical-replication.sgml

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,18 @@
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, 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>, <literal>FOR ALL TABLES</literal>,
116+
or <literal>FOR ALL SEQUENCES</literal>.
115117
</para>
116118

117119
<para>
@@ -1049,24 +1051,24 @@ HINT: To initiate replication, you must manually create the replication slot, e
10491051
<programlisting><![CDATA[
10501052
/* pub # */ \dRp+
10511053
Publication p1
1052-
Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root
1053-
----------+------------+---------+---------+---------+-----------+-------------------+----------
1054-
postgres | f | t | t | t | t | none | f
1054+
Owner | All tables | All sequences | Inserts | Updates | Deletes | Truncates | Generated columns | Via root
1055+
----------+------------+---------------+---------+---------+---------+-----------+-------------------+----------
1056+
postgres | f | f | t | t | t | t | none | f
10551057
Tables:
10561058
"public.t1" WHERE ((a > 5) AND (c = 'NSW'::text))
10571059

1058-
Publication p2
1059-
Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root
1060-
----------+------------+---------+---------+---------+-----------+-------------------+----------
1061-
postgres | f | t | t | t | t | none | f
1060+
Publication p2
1061+
Owner | All tables | All sequences | Inserts | Updates | Deletes | Truncates | Generated columns | Via root
1062+
----------+------------+---------------+---------+---------+---------+-----------+-------------------+----------
1063+
postgres | f | f | t | t | t | t | none | f
10621064
Tables:
10631065
"public.t1"
10641066
"public.t2" WHERE (e = 99)
10651067

1066-
Publication p3
1067-
Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root
1068-
----------+------------+---------+---------+---------+-----------+-------------------+----------
1069-
postgres | f | t | t | t | t | none | f
1068+
Publication p3
1069+
Owner | All tables | All sequences | Inserts | Updates | Deletes | Truncates | Generated columns | Via root
1070+
----------+------------+---------------+---------+---------+---------+-----------+-------------------+----------
1071+
postgres | f | f | t | t | t | t | none | f
10701072
Tables:
10711073
"public.t2" WHERE (d = 10)
10721074
"public.t3" WHERE (g = 10)
@@ -1491,10 +1493,10 @@ Publications:
14911493
for each publication.
14921494
<programlisting>
14931495
/* pub # */ \dRp+
1494-
Publication p1
1495-
Owner | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root
1496-
----------+------------+---------+---------+---------+-----------+-------------------+----------
1497-
postgres | f | t | t | t | t | none | f
1496+
Publication p1
1497+
Owner | All tables | All sequences | Inserts | Updates | Deletes | Truncates | Generated columns | Via root
1498+
----------+------------+---------------+---------+---------+---------+-----------+-------------------+----------
1499+
postgres | f | f | t | t | t | t | none | f
14981500
Tables:
14991501
"public.t1" (id, a, b, d)
15001502
</programlisting></para>

doc/src/sgml/ref/alter_publication.sgml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ ALTER PUBLICATION <replaceable class="parameter">name</replaceable> RENAME TO <r
8282
new owning role, and that role must have <literal>CREATE</literal>
8383
privilege on the database.
8484
Also, the new owner of a
85-
<link linkend="sql-createpublication-params-for-all-tables"><literal>FOR ALL TABLES</literal></link>
86-
or <link linkend="sql-createpublication-params-for-tables-in-schema"><literal>FOR TABLES IN SCHEMA</literal></link>
85+
<link linkend="sql-createpublication-params-for-tables-in-schema"><literal>FOR TABLES IN SCHEMA</literal></link>
86+
or <link linkend="sql-createpublication-params-for-all-tables"><literal>FOR ALL TABLES</literal></link>
87+
or <link linkend="sql-createpublication-params-for-all-sequences"><literal>FOR ALL SEQUENCES</literal></link>
8788
publication must be a superuser. However, a superuser can
8889
change the ownership of a publication regardless of these restrictions.
8990
</para>
@@ -153,6 +154,7 @@ ALTER PUBLICATION <replaceable class="parameter">name</replaceable> RENAME TO <r
153154
<para>
154155
This clause alters publication parameters originally set by
155156
<xref linkend="sql-createpublication"/>. See there for more information.
157+
This clause is not applicable to sequences.
156158
</para>
157159
<caution>
158160
<para>

doc/src/sgml/ref/create_publication.sgml

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@ PostgreSQL documentation
2222
<refsynopsisdiv>
2323
<synopsis>
2424
CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
25-
[ FOR ALL TABLES
26-
| FOR <replaceable class="parameter">publication_object</replaceable> [, ... ] ]
25+
[ FOR { <replaceable class="parameter">publication_object</replaceable> [, ... ] | <replaceable class="parameter">all_publication_object</replaceable> [, ... ] } ]
2726
[ WITH ( <replaceable class="parameter">publication_parameter</replaceable> [= <replaceable class="parameter">value</replaceable>] [, ... ] ) ]
2827

2928
<phrase>where <replaceable class="parameter">publication_object</replaceable> is one of:</phrase>
3029

3130
TABLE [ ONLY ] <replaceable class="parameter">table_name</replaceable> [ * ] [ ( <replaceable class="parameter">column_name</replaceable> [, ... ] ) ] [ WHERE ( <replaceable class="parameter">expression</replaceable> ) ] [, ... ]
3231
TABLES IN SCHEMA { <replaceable class="parameter">schema_name</replaceable> | CURRENT_SCHEMA } [, ... ]
32+
33+
<phrase>where <replaceable class="parameter">all_publication_object</replaceable> is one of:</phrase>
34+
35+
ALL TABLES
36+
ALL SEQUENCES
3337
</synopsis>
3438
</refsynopsisdiv>
3539

@@ -120,16 +124,6 @@ CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
120124
</listitem>
121125
</varlistentry>
122126

123-
<varlistentry id="sql-createpublication-params-for-all-tables">
124-
<term><literal>FOR ALL TABLES</literal></term>
125-
<listitem>
126-
<para>
127-
Marks the publication as one that replicates changes for all tables in
128-
the database, including tables created in the future.
129-
</para>
130-
</listitem>
131-
</varlistentry>
132-
133127
<varlistentry id="sql-createpublication-params-for-tables-in-schema">
134128
<term><literal>FOR TABLES IN SCHEMA</literal></term>
135129
<listitem>
@@ -161,11 +155,37 @@ CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
161155
</listitem>
162156
</varlistentry>
163157

158+
<varlistentry id="sql-createpublication-params-for-all-tables">
159+
<term><literal>FOR ALL TABLES</literal></term>
160+
<listitem>
161+
<para>
162+
Marks the publication as one that replicates changes for all tables in
163+
the database, including tables created in the future.
164+
</para>
165+
</listitem>
166+
</varlistentry>
167+
168+
<varlistentry id="sql-createpublication-params-for-all-sequences">
169+
<term><literal>FOR ALL SEQUENCES</literal></term>
170+
<listitem>
171+
<para>
172+
Marks the publication as one that synchronizes changes for all sequences
173+
in the database, including sequences created in the future.
174+
</para>
175+
176+
<para>
177+
Only persistent sequences are included in the publication. Temporary
178+
sequences and unlogged sequences are excluded from the publication.
179+
</para>
180+
</listitem>
181+
</varlistentry>
182+
164183
<varlistentry id="sql-createpublication-params-with">
165184
<term><literal>WITH ( <replaceable class="parameter">publication_parameter</replaceable> [= <replaceable class="parameter">value</replaceable>] [, ... ] )</literal></term>
166185
<listitem>
167186
<para>
168-
This clause specifies optional parameters for a publication. The
187+
This clause specifies optional parameters for a publication when
188+
publishing tables. This clause is not applicable to sequences. The
169189
following parameters are supported:
170190

171191
<variablelist>
@@ -279,10 +299,10 @@ CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
279299
<title>Notes</title>
280300

281301
<para>
282-
If <literal>FOR TABLE</literal>, <literal>FOR ALL TABLES</literal> or
283-
<literal>FOR TABLES IN SCHEMA</literal> are not specified, then the
284-
publication starts out with an empty set of tables. That is useful if
285-
tables or schemas are to be added later.
302+
If <literal>FOR TABLE</literal>, <literal>FOR TABLES IN SCHEMA</literal>,
303+
<literal>FOR ALL TABLES</literal> or <literal>FOR ALL SEQUENCES</literal>
304+
are not specified, then the publication starts out with an empty set of
305+
tables. That is useful if tables or schemas are to be added later.
286306
</para>
287307

288308
<para>
@@ -298,8 +318,9 @@ CREATE PUBLICATION <replaceable class="parameter">name</replaceable>
298318

299319
<para>
300320
To add a table to a publication, the invoking user must have ownership
301-
rights on the table. The <command>FOR ALL TABLES</command> and
302-
<command>FOR TABLES IN SCHEMA</command> clauses require the invoking
321+
rights on the table. The <literal>FOR TABLES IN SCHEMA</literal>,
322+
<literal>FOR ALL TABLES</literal> and
323+
<literal>FOR ALL SEQUENCES</literal> clauses require the invoking
303324
user to be a superuser.
304325
</para>
305326

@@ -449,6 +470,21 @@ CREATE PUBLICATION sales_publication FOR TABLES IN SCHEMA marketing, sales;
449470
<programlisting>
450471
CREATE PUBLICATION users_filtered FOR TABLE users (user_id, firstname);
451472
</programlisting></para>
473+
474+
<para>
475+
Create a publication that publishes all sequences for synchronization:
476+
<programlisting>
477+
CREATE PUBLICATION all_sequences FOR ALL SEQUENCES;
478+
</programlisting>
479+
</para>
480+
481+
<para>
482+
Create a publication that publishes all changes in all tables, and
483+
all sequences for synchronization:
484+
<programlisting>
485+
CREATE PUBLICATION all_tables_sequences FOR ALL TABLES, ALL SEQUENCES;
486+
</programlisting>
487+
</para>
452488
</refsect1>
453489

454490
<refsect1>

doc/src/sgml/system-views.sgml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@
136136
<entry>prepared transactions</entry>
137137
</row>
138138

139+
<row>
140+
<entry><link linkend="view-pg-publication-sequences"><structname>pg_publication_sequences</structname></link></entry>
141+
<entry>publications and information of their associated sequences</entry>
142+
</row>
143+
139144
<row>
140145
<entry><link linkend="view-pg-publication-tables"><structname>pg_publication_tables</structname></link></entry>
141146
<entry>publications and information of their associated tables</entry>
@@ -2549,6 +2554,67 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
25492554

25502555
</sect1>
25512556

2557+
<sect1 id="view-pg-publication-sequences">
2558+
<title><structname>pg_publication_sequences</structname></title>
2559+
2560+
<indexterm zone="view-pg-publication-sequences">
2561+
<primary>pg_publication_sequences</primary>
2562+
</indexterm>
2563+
2564+
<para>
2565+
The view <structname>pg_publication_sequences</structname> provides
2566+
information about the mapping between publications and sequences.
2567+
</para>
2568+
2569+
<table>
2570+
<title><structname>pg_publication_sequences</structname> Columns</title>
2571+
<tgroup cols="1">
2572+
<thead>
2573+
<row>
2574+
<entry role="catalog_table_entry"><para role="column_definition">
2575+
Column Type
2576+
</para>
2577+
<para>
2578+
Description
2579+
</para></entry>
2580+
</row>
2581+
</thead>
2582+
2583+
<tbody>
2584+
<row>
2585+
<entry role="catalog_table_entry"><para role="column_definition">
2586+
<structfield>pubname</structfield> <type>name</type>
2587+
(references <link linkend="catalog-pg-publication"><structname>pg_publication</structname></link>.<structfield>pubname</structfield>)
2588+
</para>
2589+
<para>
2590+
Name of publication
2591+
</para></entry>
2592+
</row>
2593+
2594+
<row>
2595+
<entry role="catalog_table_entry"><para role="column_definition">
2596+
<structfield>schemaname</structfield> <type>name</type>
2597+
(references <link linkend="catalog-pg-namespace"><structname>pg_namespace</structname></link>.<structfield>nspname</structfield>)
2598+
</para>
2599+
<para>
2600+
Name of schema containing sequence
2601+
</para></entry>
2602+
</row>
2603+
2604+
<row>
2605+
<entry role="catalog_table_entry"><para role="column_definition">
2606+
<structfield>sequencename</structfield> <type>name</type>
2607+
(references <link linkend="catalog-pg-class"><structname>pg_class</structname></link>.<structfield>relname</structfield>)
2608+
</para>
2609+
<para>
2610+
Name of sequence
2611+
</para></entry>
2612+
</row>
2613+
</tbody>
2614+
</tgroup>
2615+
</table>
2616+
</sect1>
2617+
25522618
<sect1 id="view-pg-publication-tables">
25532619
<title><structname>pg_publication_tables</structname></title>
25542620

0 commit comments

Comments
 (0)