From 568c676775fed1ac41de66466edbeb9f886906c3 Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Mon, 3 Feb 2025 09:46:10 +1100 Subject: [PATCH] DOCS - Generated Column Replication Examples --- doc/src/sgml/logical-replication.sgml | 146 ++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) diff --git a/doc/src/sgml/logical-replication.sgml b/doc/src/sgml/logical-replication.sgml index 3d18e507bbcd..c395cba135dc 100644 --- a/doc/src/sgml/logical-replication.sgml +++ b/doc/src/sgml/logical-replication.sgml @@ -1784,6 +1784,152 @@ test_sub=# SELECT * from tab_gen_to_gen; the publisher. + + + Examples + + + Setup the publisher and subscriber tables. Note that the subscriber + table columns have same names, but are not defined the same as the + publisher columns. + +test_pub=# CREATE TABLE t1 ( + a int PRIMARY KEY, + b int, + c int GENERATED ALWAYS AS (a + 1) STORED, + d int GENERATED ALWAYS AS (b + 1) STORED); + +test_pub=# CREATE TABLE t2 ( + a int PRIMARY KEY, + b int, + c int GENERATED ALWAYS AS (a + 1) STORED, + d int GENERATED ALWAYS AS (b + 1) STORED); + + + +test_sub=# CREATE TABLE t1 ( + a int PRIMARY KEY, + b int, + c int, + d int GENERATED ALWAYS AS (b * 100) STORED); + +test_sub=# CREATE TABLE t2 ( + a int PRIMARY KEY, + b int, + c int, + d int); + + + + + Create the PUBLICATION and the SUBSCRIPTION. + Note that the publication specifies a column list for table t2. + The publication also sets parameter publish_generated_columns=none, + but that is just for demonstration because none is the + default anyway. + +test_pub=# CREATE PUBLICATION pub1 FOR TABLE t1, t2(a,c) + WITH (publish_generated_columns=none); + + + +test_sub=# CREATE SUBSCRIPTION sub1 + CONNECTION 'dbname=test_pub' + PUBLICATION pub1; + + + + + Insert some data to the publisher tables: + +test_pub=# INSERT INTO t1 VALUES (1,2); +INSERT 0 1 +test_pub=# INSERT INTO t2 VALUES (1,2); +INSERT 0 1 + +test_pub=# SELECT * FROM t1; + a | b | c | d +---+---+---+--- + 1 | 2 | 2 | 3 +(1 row) + +test_pub=# SELECT * FROM t2; + a | b | c | d +---+---+---+--- + 1 | 2 | 2 | 3 +(1 row) + + + + + Observe how columns for table t1 were replicated: + +test_sub=# SELECT * FROM t1; + a | b | c | d +---+---+---+----- + 1 | 2 | | 200 +(1 row) + + + + t1.a is a regular column. It gets replicated normally. + + + + t1.b is a regular column. It gets replicated normally. + + + + t1.c is a generated column. It is not replicated because + publish_generated_columns=none. The subscriber + t2.c default column value is used. + + + + t1.d is a generated column. It is not replicated because + publish_generated_columns=none. The subscriber + t2.d generated column value is used. + + + + + + Observe how columns for table t2 were replicated. + +test_sub=# SELECT * FROM t2; + a | b | c | d +---+---+---+--- + 1 | | 2 | +(1 row) + + + + t2.a is a regular column. It was specified in the column + list, so is replicated normally. + + + + t2.b is a regular column. It was not specified in column + list so is not replicated. The subscriber t2.b default + value is used. + + + + t2.c is a generated column. It was specified in the + column list, so is replicated to the subscriber t2.c + regular column. + + + + t2.d is a generated column. It was not specified in the + column list, so is not replicated. The subscriber t2.d + default value is used. + + + + + +