]> begriffs open source - ai-pg/blob - full-docs/txt/logical-replication-col-lists.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / logical-replication-col-lists.txt
1
2 29.5. Column Lists #
3
4    29.5.1. Examples
5
6    Each publication can optionally specify which columns of each table are
7    replicated to subscribers. The table on the subscriber side must have
8    at least all the columns that are published. If no column list is
9    specified, then all columns on the publisher are replicated. See CREATE
10    PUBLICATION for details on the syntax.
11
12    The choice of columns can be based on behavioral or performance
13    reasons. However, do not rely on this feature for security: a malicious
14    subscriber is able to obtain data from columns that are not
15    specifically published. If security is a consideration, protections can
16    be applied at the publisher side.
17
18    If no column list is specified, any columns added to the table later
19    are automatically replicated. This means that having a column list
20    which names all columns is not the same as having no column list at
21    all.
22
23    A column list can contain only simple column references. The order of
24    columns in the list is not preserved.
25
26    Generated columns can also be specified in a column list. This allows
27    generated columns to be published, regardless of the publication
28    parameter publish_generated_columns. See Section 29.6 for details.
29
30    Specifying a column list when the publication also publishes FOR TABLES
31    IN SCHEMA is not supported.
32
33    For partitioned tables, the publication parameter
34    publish_via_partition_root determines which column list is used. If
35    publish_via_partition_root is true, the root partitioned table's column
36    list is used. Otherwise, if publish_via_partition_root is false (the
37    default), each partition's column list is used.
38
39    If a publication publishes UPDATE or DELETE operations, any column list
40    must include the table's replica identity columns (see REPLICA
41    IDENTITY). If a publication publishes only INSERT operations, then the
42    column list may omit replica identity columns.
43
44    Column lists have no effect for the TRUNCATE command.
45
46    During initial data synchronization, only the published columns are
47    copied. However, if the subscriber is from a release prior to 15, then
48    all the columns in the table are copied during initial data
49    synchronization, ignoring any column lists. If the subscriber is from a
50    release prior to 18, then initial table synchronization won't copy
51    generated columns even if they are defined in the publisher.
52
53 Warning: Combining Column Lists from Multiple Publications
54
55    There's currently no support for subscriptions comprising several
56    publications where the same table has been published with different
57    column lists. CREATE SUBSCRIPTION disallows creating such
58    subscriptions, but it is still possible to get into that situation by
59    adding or altering column lists on the publication side after a
60    subscription has been created.
61
62    This means changing the column lists of tables on publications that are
63    already subscribed could lead to errors being thrown on the subscriber
64    side.
65
66    If a subscription is affected by this problem, the only way to resume
67    replication is to adjust one of the column lists on the publication
68    side so that they all match; and then either recreate the subscription,
69    or use ALTER SUBSCRIPTION ... DROP PUBLICATION to remove one of the
70    offending publications and add it again.
71
72 29.5.1. Examples #
73
74    Create a table t1 to be used in the following example.
75 /* pub # */ CREATE TABLE t1(id int, a text, b text, c text, d text, e text, PRIM
76 ARY KEY(id));
77
78    Create a publication p1. A column list is defined for table t1 to
79    reduce the number of columns that will be replicated. Notice that the
80    order of column names in the column list does not matter.
81 /* pub # */ CREATE PUBLICATION p1 FOR TABLE t1 (id, b, a, d);
82
83    psql can be used to show the column lists (if defined) for each
84    publication.
85 /* pub # */ \dRp+
86                                          Publication p1
87   Owner   | All tables | Inserts | Updates | Deletes | Truncates | Generated col
88 umns | Via root
89 ----------+------------+---------+---------+---------+-----------+--------------
90 -----+----------
91  postgres | f          | t       | t       | t       | t         | none
92      | f
93 Tables:
94     "public.t1" (id, a, b, d)
95
96    psql can be used to show the column lists (if defined) for each table.
97 /* pub # */ \d t1
98                  Table "public.t1"
99  Column |  Type   | Collation | Nullable | Default
100 --------+---------+-----------+----------+---------
101  id     | integer |           | not null |
102  a      | text    |           |          |
103  b      | text    |           |          |
104  c      | text    |           |          |
105  d      | text    |           |          |
106  e      | text    |           |          |
107 Indexes:
108     "t1_pkey" PRIMARY KEY, btree (id)
109 Publications:
110     "p1" (id, a, b, d)
111
112    On the subscriber node, create a table t1 which now only needs a subset
113    of the columns that were on the publisher table t1, and also create the
114    subscription s1 that subscribes to the publication p1.
115 /* sub # */ CREATE TABLE t1(id int, b text, a text, d text, PRIMARY KEY(id));
116 /* sub # */ CREATE SUBSCRIPTION s1
117 /* sub - */ CONNECTION 'host=localhost dbname=test_pub application_name=s1'
118 /* sub - */ PUBLICATION p1;
119
120    On the publisher node, insert some rows to table t1.
121 /* pub # */ INSERT INTO t1 VALUES(1, 'a-1', 'b-1', 'c-1', 'd-1', 'e-1');
122 /* pub # */ INSERT INTO t1 VALUES(2, 'a-2', 'b-2', 'c-2', 'd-2', 'e-2');
123 /* pub # */ INSERT INTO t1 VALUES(3, 'a-3', 'b-3', 'c-3', 'd-3', 'e-3');
124 /* pub # */ SELECT * FROM t1 ORDER BY id;
125  id |  a  |  b  |  c  |  d  |  e
126 ----+-----+-----+-----+-----+-----
127   1 | a-1 | b-1 | c-1 | d-1 | e-1
128   2 | a-2 | b-2 | c-2 | d-2 | e-2
129   3 | a-3 | b-3 | c-3 | d-3 | e-3
130 (3 rows)
131
132    Only data from the column list of publication p1 is replicated.
133 /* sub # */ SELECT * FROM t1 ORDER BY id;
134  id |  b  |  a  |  d
135 ----+-----+-----+-----
136   1 | b-1 | a-1 | d-1
137   2 | b-2 | a-2 | d-2
138   3 | b-3 | a-3 | d-3
139 (3 rows)