Debian Codegraph @

src/test/subscription/t/008_diff_schema.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test behavior with different schema on subscriber
5 use strict;
6 use warnings FATAL => 'all';
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 # Create publisher node
12 my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
13 $node_publisher->init(allows_streaming => 'logical');
14 $node_publisher->start;
15
16 # Create subscriber node
17 my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
18 $node_subscriber->init;
19 $node_subscriber->start;
20
21 # Create some preexisting content on publisher
22 $node_publisher->safe_psql('postgres',
23 "CREATE TABLE test_tab (a int primary key, b varchar)");
24 $node_publisher->safe_psql('postgres',
25 "INSERT INTO test_tab VALUES (1, 'foo'), (2, 'bar')");
26
27 # Setup structure on subscriber
28 $node_subscriber->safe_psql('postgres',
29 "CREATE TABLE test_tab (a int primary key, b text, c timestamptz DEFAULT now(), d bigint DEFAULT 999, e int GENERATED BY DEFAULT AS IDENTITY)"
30 );
31
32 # Setup logical replication
33 my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
34 $node_publisher->safe_psql('postgres',
35 "CREATE PUBLICATION tap_pub FOR ALL TABLES");
36
37 $node_subscriber->safe_psql('postgres',
38 "CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub"
39 );
40
41 # Wait for initial table sync to finish
42 $node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
43
44 my $result =
45 $node_subscriber->safe_psql('postgres',
46 "SELECT count(*), count(c), count(d = 999) FROM test_tab");
47 is($result, qq(2|2|2), 'check initial data was copied to subscriber');
48
49 # Update the rows on the publisher and check the additional columns on
50 # subscriber didn't change
51 $node_publisher->safe_psql('postgres',
52 "UPDATE test_tab SET b = encode(sha256(b::bytea), 'hex')");
53
54 $node_publisher->wait_for_catchup('tap_sub');
55
56 $result =
57 $node_subscriber->safe_psql('postgres',
58 "SELECT count(*), count(c), count(d = 999), count(e) FROM test_tab");
59 is($result, qq(2|2|2|2),
60 'check extra columns contain local defaults after copy');
61
62 # Change the local values of the extra columns on the subscriber,
63 # update publisher, and check that subscriber retains the expected
64 # values
65 $node_subscriber->safe_psql('postgres',
66 "UPDATE test_tab SET c = 'epoch'::timestamptz + 987654321 * interval '1s'"
67 );
68 $node_publisher->safe_psql('postgres',
69 "UPDATE test_tab SET b = encode(sha256(a::text::bytea), 'hex')");
70
71 $node_publisher->wait_for_catchup('tap_sub');
72
73 $result = $node_subscriber->safe_psql('postgres',
74 "SELECT count(*), count(extract(epoch from c) = 987654321), count(d = 999) FROM test_tab"
75 );
76 is($result, qq(2|2|2), 'check extra columns contain locally changed data');
77
78 # Another insert
79 $node_publisher->safe_psql('postgres',
80 "INSERT INTO test_tab VALUES (3, 'baz')");
81
82 $node_publisher->wait_for_catchup('tap_sub');
83
84 $result =
85 $node_subscriber->safe_psql('postgres',
86 "SELECT count(*), count(c), count(d = 999), count(e) FROM test_tab");
87 is($result, qq(3|3|3|3),
88 'check extra columns contain local defaults after apply');
89
90
91 # Check a bug about adding a replica identity column on the subscriber
92 # that was not yet mapped to a column on the publisher. This would
93 # result in errors on the subscriber and replication thus not
94 # progressing.
95 # (https://www.postgresql.org/message-id/flat/a9139c29-7ddd-973b-aa7f-71fed9c38d75%40minerva.info)
96
97 $node_publisher->safe_psql('postgres', "CREATE TABLE test_tab2 (a int)");
98
99 $node_subscriber->safe_psql('postgres', "CREATE TABLE test_tab2 (a int)");
100
101 $node_subscriber->safe_psql('postgres',
102 "ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
103
104 $node_subscriber->wait_for_subscription_sync;
105
106 # Add replica identity column. (The serial is not necessary, but it's
107 # a convenient way to get a default on the new column so that rows
108 # from the publisher that don't have the column yet can be inserted.)
109 $node_subscriber->safe_psql('postgres',
110 "ALTER TABLE test_tab2 ADD COLUMN b serial PRIMARY KEY");
111
112 $node_publisher->safe_psql('postgres', "INSERT INTO test_tab2 VALUES (1)");
113
114 $node_publisher->wait_for_catchup('tap_sub');
115
116 is( $node_subscriber->safe_psql(
117 'postgres', "SELECT count(*), min(a), max(a) FROM test_tab2"),
118 qq(1|1|1),
119 'check replicated inserts on subscriber');
120
121 # Test if the expected error is reported when the subscriber table is missing
122 # columns which were specified on the publisher table.
123 $node_publisher->safe_psql('postgres',
124 "CREATE TABLE test_tab3 (a int, b int, c int)");
125 $node_subscriber->safe_psql('postgres', "CREATE TABLE test_tab3 (a int)");
126
127 my $offset = -s $node_subscriber->logfile;
128
129 $node_subscriber->safe_psql('postgres',
130 "ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
131
132 $node_subscriber->wait_for_log(
133 qr/ERROR: ( [A-Z0-9]+:)? logical replication target relation "public.test_tab3" is missing replicated columns: "b", "c"/,
134 $offset);
135
136
137 $node_subscriber->stop;
138 $node_publisher->stop;
139
140 done_testing();