]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/logical-replication-col-lists.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / logical-replication-col-lists.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!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>29.5. Column Lists</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="logical-replication-row-filter.html" title="29.4. Row Filters" /><link rel="next" href="logical-replication-gencols.html" title="29.6. Generated Column Replication" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">29.5. Column Lists</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="logical-replication-row-filter.html" title="29.4. Row Filters">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="logical-replication.html" title="Chapter 29. Logical Replication">Up</a></td><th width="60%" align="center">Chapter 29. Logical Replication</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="logical-replication-gencols.html" title="29.6. Generated Column Replication">Next</a></td></tr></table><hr /></div><div class="sect1" id="LOGICAL-REPLICATION-COL-LISTS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">29.5. Column Lists <a href="#LOGICAL-REPLICATION-COL-LISTS" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="logical-replication-col-lists.html#LOGICAL-REPLICATION-COL-LIST-EXAMPLES">29.5.1. Examples</a></span></dt></dl></div><p>
3    Each publication can optionally specify which columns of each table are
4    replicated to subscribers. The table on the subscriber side must have at
5    least all the columns that are published. If no column list is specified,
6    then all columns on the publisher are replicated.
7    See <a class="xref" href="sql-createpublication.html" title="CREATE PUBLICATION"><span class="refentrytitle">CREATE PUBLICATION</span></a> for details on the syntax.
8   </p><p>
9    The choice of columns can be based on behavioral or performance reasons.
10    However, do not rely on this feature for security: a malicious subscriber
11    is able to obtain data from columns that are not specifically
12    published.  If security is a consideration, protections can be applied
13    at the publisher side.
14   </p><p>
15    If no column list is specified, any columns added to the table later are
16    automatically replicated. This means that having a column list which names
17    all columns is not the same as having no column list at all.
18   </p><p>
19    A column list can contain only simple column references.  The order
20    of columns in the list is not preserved.
21   </p><p>
22    Generated columns can also be specified in a column list. This allows
23    generated columns to be published, regardless of the publication parameter
24    <a class="link" href="sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-WITH-PUBLISH-GENERATED-COLUMNS">
25    <code class="literal">publish_generated_columns</code></a>. See
26    <a class="xref" href="logical-replication-gencols.html" title="29.6. Generated Column Replication">Section 29.6</a> for details.
27   </p><p>
28    Specifying a column list when the publication also publishes
29    <a class="link" href="sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-FOR-TABLES-IN-SCHEMA"><code class="literal">FOR TABLES IN SCHEMA</code></a>
30    is not supported.
31   </p><p>
32    For partitioned tables, the publication parameter
33    <a class="link" href="sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-WITH-PUBLISH-VIA-PARTITION-ROOT"><code class="literal">publish_via_partition_root</code></a>
34    determines which column list is used. If <code class="literal">publish_via_partition_root</code>
35    is <code class="literal">true</code>, the root partitioned table's column list is
36    used. Otherwise, if <code class="literal">publish_via_partition_root</code> is
37    <code class="literal">false</code> (the default), each partition's column list is used.
38   </p><p>
39    If a publication publishes <code class="command">UPDATE</code> or
40    <code class="command">DELETE</code> operations, any column list must include the
41    table's replica identity columns (see
42    <a class="xref" href="sql-altertable.html#SQL-ALTERTABLE-REPLICA-IDENTITY"><code class="literal">REPLICA IDENTITY</code></a>).
43    If a publication publishes only <code class="command">INSERT</code> operations, then
44    the column list may omit replica identity columns.
45   </p><p>
46    Column lists have no effect for the <code class="literal">TRUNCATE</code> command.
47   </p><p>
48    During initial data synchronization, only the published columns are
49    copied.  However, if the subscriber is from a release prior to 15, then
50    all the columns in the table are copied during initial data synchronization,
51    ignoring any column lists. If the subscriber is from a release prior to 18,
52    then initial table synchronization won't copy generated columns even if they
53    are defined in the publisher.
54   </p><div class="warning" id="LOGICAL-REPLICATION-COL-LIST-COMBINING"><h3 class="title">Warning: Combining Column Lists from Multiple Publications</h3><p>
55      There's currently no support for subscriptions comprising several
56      publications where the same table has been published with different
57      column lists.  <a class="xref" href="sql-createsubscription.html" title="CREATE SUBSCRIPTION"><span class="refentrytitle">CREATE SUBSCRIPTION</span></a> disallows
58      creating such subscriptions, but it is still possible to get into
59      that situation by adding or altering column lists on the publication
60      side after a subscription has been created.
61     </p><p>
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     </p><p>
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 <a class="link" href="sql-altersubscription.html#SQL-ALTERSUBSCRIPTION-PARAMS-SETADDDROP-PUBLICATION">
70      <code class="literal">ALTER SUBSCRIPTION ... DROP PUBLICATION</code></a> to
71      remove one of the offending publications and add it again.
72     </p></div><div class="sect2" id="LOGICAL-REPLICATION-COL-LIST-EXAMPLES"><div class="titlepage"><div><div><h3 class="title">29.5.1. Examples <a href="#LOGICAL-REPLICATION-COL-LIST-EXAMPLES" class="id_link">#</a></h3></div></div></div><p>
73     Create a table <code class="literal">t1</code> to be used in the following example.
74 </p><pre class="programlisting">
75 /* pub # */ CREATE TABLE t1(id int, a text, b text, c text, d text, e text, PRIMARY KEY(id));
76 </pre><p>
77     Create a publication <code class="literal">p1</code>. A column list is defined for
78     table <code class="literal">t1</code> to reduce the number of columns that will be
79     replicated. Notice that the order of column names in the column list does
80     not matter.
81 </p><pre class="programlisting">
82 /* pub # */ CREATE PUBLICATION p1 FOR TABLE t1 (id, b, a, d);
83 </pre><p>
84      <code class="literal">psql</code> can be used to show the column lists (if defined)
85      for each publication.
86 </p><pre class="programlisting">
87 /* pub # */ \dRp+
88                                          Publication p1
89   Owner   | All tables | Inserts | Updates | Deletes | Truncates | Generated columns | Via root
90 ----------+------------+---------+---------+---------+-----------+-------------------+----------
91  postgres | f          | t       | t       | t       | t         | none              | f
92 Tables:
93     "public.t1" (id, a, b, d)
94 </pre><p>
95      <code class="literal">psql</code> can be used to show the column lists (if defined)
96      for each table.
97 </p><pre class="programlisting">
98 /* pub # */ \d t1
99                  Table "public.t1"
100  Column |  Type   | Collation | Nullable | Default
101 --------+---------+-----------+----------+---------
102  id     | integer |           | not null |
103  a      | text    |           |          |
104  b      | text    |           |          |
105  c      | text    |           |          |
106  d      | text    |           |          |
107  e      | text    |           |          |
108 Indexes:
109     "t1_pkey" PRIMARY KEY, btree (id)
110 Publications:
111     "p1" (id, a, b, d)
112 </pre><p>
113      On the subscriber node, create a table <code class="literal">t1</code> which now
114      only needs a subset of the columns that were on the publisher table
115      <code class="literal">t1</code>, and also create the subscription
116      <code class="literal">s1</code> that subscribes to the publication
117      <code class="literal">p1</code>.
118 </p><pre class="programlisting">
119 /* sub # */ CREATE TABLE t1(id int, b text, a text, d text, PRIMARY KEY(id));
120 /* sub # */ CREATE SUBSCRIPTION s1
121 /* sub - */ CONNECTION 'host=localhost dbname=test_pub application_name=s1'
122 /* sub - */ PUBLICATION p1;
123 </pre><p>
124      On the publisher node, insert some rows to table <code class="literal">t1</code>.
125 </p><pre class="programlisting">
126 /* pub # */ INSERT INTO t1 VALUES(1, 'a-1', 'b-1', 'c-1', 'd-1', 'e-1');
127 /* pub # */ INSERT INTO t1 VALUES(2, 'a-2', 'b-2', 'c-2', 'd-2', 'e-2');
128 /* pub # */ INSERT INTO t1 VALUES(3, 'a-3', 'b-3', 'c-3', 'd-3', 'e-3');
129 /* pub # */ SELECT * FROM t1 ORDER BY id;
130  id |  a  |  b  |  c  |  d  |  e
131 ----+-----+-----+-----+-----+-----
132   1 | a-1 | b-1 | c-1 | d-1 | e-1
133   2 | a-2 | b-2 | c-2 | d-2 | e-2
134   3 | a-3 | b-3 | c-3 | d-3 | e-3
135 (3 rows)
136 </pre><p>
137      Only data from the column list of publication <code class="literal">p1</code> is
138      replicated.
139 </p><pre class="programlisting">
140 /* sub # */ SELECT * FROM t1 ORDER BY id;
141  id |  b  |  a  |  d
142 ----+-----+-----+-----
143   1 | b-1 | a-1 | d-1
144   2 | b-2 | a-2 | d-2
145   3 | b-3 | a-3 | d-3
146 (3 rows)
147 </pre></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="logical-replication-row-filter.html" title="29.4. Row Filters">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="logical-replication.html" title="Chapter 29. Logical Replication">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="logical-replication-gencols.html" title="29.6. Generated Column Replication">Next</a></td></tr><tr><td width="40%" align="left" valign="top">29.4. Row Filters </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 29.6. Generated Column Replication</td></tr></table></div></body></html>