]> begriffs open source - ai-pg/blob - full-docs/txt/logical-replication-gencols.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / logical-replication-gencols.txt
1
2 29.6. Generated Column Replication #
3
4    Typically, a table at the subscriber will be defined the same as the
5    publisher table, so if the publisher table has a GENERATED column then
6    the subscriber table will have a matching generated column. In this
7    case, it is always the subscriber table generated column value that is
8    used.
9
10    For example, note below that subscriber table generated column value
11    comes from the subscriber column's calculation.
12 /* pub # */ CREATE TABLE tab_gen_to_gen (a int, b int GENERATED ALWAYS AS (a + 1
13 ) STORED);
14 /* pub # */ INSERT INTO tab_gen_to_gen VALUES (1),(2),(3);
15 /* pub # */ CREATE PUBLICATION pub1 FOR TABLE tab_gen_to_gen;
16 /* pub # */ SELECT * FROM tab_gen_to_gen;
17  a | b
18 ---+---
19  1 | 2
20  2 | 3
21  3 | 4
22 (3 rows)
23
24 /* sub # */ CREATE TABLE tab_gen_to_gen (a int, b int GENERATED ALWAYS AS (a * 1
25 00) STORED);
26 /* sub # */ CREATE SUBSCRIPTION sub1 CONNECTION 'dbname=test_pub' PUBLICATION pu
27 b1;
28 /* sub # */ SELECT * from tab_gen_to_gen;
29  a | b
30 ---+----
31  1 | 100
32  2 | 200
33  3 | 300
34 (3 rows)
35
36    In fact, prior to version 18.0, logical replication does not publish
37    GENERATED columns at all.
38
39    But, replicating a generated column to a regular column can sometimes
40    be desirable.
41
42 Tip
43
44    This feature may be useful when replicating data to a non-PostgreSQL
45    database via output plugin, especially if the target database does not
46    support generated columns.
47
48    Generated columns are not published by default, but users can opt to
49    publish stored generated columns just like regular ones.
50
51    There are two ways to do this:
52      * Set the PUBLICATION parameter publish_generated_columns to stored.
53        This instructs PostgreSQL logical replication to publish current
54        and future stored generated columns of the publication's tables.
55      * Specify a table column list to explicitly nominate which stored
56        generated columns will be published.
57
58 Note
59        When determining which table columns will be published, a column
60        list takes precedence, overriding the effect of the
61        publish_generated_columns parameter.
62
63    The following table summarizes behavior when there are generated
64    columns involved in the logical replication. Results are shown for when
65    publishing generated columns is not enabled, and for when it is
66    enabled.
67
68    Table 29.2. Replication Result Summary
69    Publish generated columns? Publisher table column Subscriber table
70    column Result
71    No GENERATED GENERATED Publisher table column is not replicated. Use
72    the subscriber table generated column value.
73    No GENERATED regular Publisher table column is not replicated. Use the
74    subscriber table regular column default value.
75    No GENERATED --missing-- Publisher table column is not replicated.
76    Nothing happens.
77    Yes GENERATED GENERATED ERROR. Not supported.
78    Yes GENERATED regular Publisher table column value is replicated to the
79    subscriber table column.
80    Yes GENERATED --missing-- ERROR. The column is reported as missing from
81    the subscriber table.
82
83 Warning
84
85    There's currently no support for subscriptions comprising several
86    publications where the same table has been published with different
87    column lists. See Section 29.5.
88
89    This same situation can occur if one publication is publishing
90    generated columns, while another publication in the same subscription
91    is not publishing generated columns for the same table.
92
93 Note
94
95    If the subscriber is from a release prior to 18, then initial table
96    synchronization won't copy generated columns even if they are defined
97    in the publisher.