Debian Codegraph @

src/test/subscription/t/025_rep_changes_for_schema.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Logical replication tests for schema publications
5 use strict;
6 use warnings FATAL => 'all';
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9 use Test::More;
10
11 # Initialize 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 # Test replication with publications created using FOR TABLES IN SCHEMA
22 # option.
23 # Create schemas and tables on publisher
24 $node_publisher->safe_psql('postgres', "CREATE SCHEMA sch1");
25 $node_publisher->safe_psql('postgres',
26 "CREATE TABLE sch1.tab1 AS SELECT generate_series(1,10) AS a");
27 $node_publisher->safe_psql('postgres',
28 "CREATE TABLE sch1.tab2 AS SELECT generate_series(1,10) AS a");
29 $node_publisher->safe_psql('postgres',
30 "CREATE TABLE sch1.tab1_parent (a int PRIMARY KEY, b text) PARTITION BY LIST (a)"
31 );
32 $node_publisher->safe_psql('postgres',
33 "CREATE TABLE public.tab1_child1 PARTITION OF sch1.tab1_parent FOR VALUES IN (1, 2, 3)"
34 );
35 $node_publisher->safe_psql('postgres',
36 "CREATE TABLE public.tab1_child2 PARTITION OF sch1.tab1_parent FOR VALUES IN (4, 5, 6)"
37 );
38
39 $node_publisher->safe_psql('postgres',
40 "INSERT INTO sch1.tab1_parent values (1),(4)");
41
42 # Create schemas and tables on subscriber
43 $node_subscriber->safe_psql('postgres', "CREATE SCHEMA sch1");
44 $node_subscriber->safe_psql('postgres', "CREATE TABLE sch1.tab1 (a int)");
45 $node_subscriber->safe_psql('postgres', "CREATE TABLE sch1.tab2 (a int)");
46 $node_subscriber->safe_psql('postgres',
47 "CREATE TABLE sch1.tab1_parent (a int PRIMARY KEY, b text) PARTITION BY LIST (a)"
48 );
49 $node_subscriber->safe_psql('postgres',
50 "CREATE TABLE public.tab1_child1 PARTITION OF sch1.tab1_parent FOR VALUES IN (1, 2, 3)"
51 );
52 $node_subscriber->safe_psql('postgres',
53 "CREATE TABLE public.tab1_child2 PARTITION OF sch1.tab1_parent FOR VALUES IN (4, 5, 6)"
54 );
55
56 # Setup logical replication
57 my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
58 $node_publisher->safe_psql('postgres',
59 "CREATE PUBLICATION tap_pub_schema FOR TABLES IN SCHEMA sch1");
60
61 $node_subscriber->safe_psql('postgres',
62 "CREATE SUBSCRIPTION tap_sub_schema CONNECTION '$publisher_connstr' PUBLICATION tap_pub_schema"
63 );
64
65 # Wait for initial table sync to finish
66 $node_subscriber->wait_for_subscription_sync($node_publisher,
67 'tap_sub_schema');
68
69 # Check the schema table data is synced up
70 my $result = $node_subscriber->safe_psql('postgres',
71 "SELECT count(*), min(a), max(a) FROM sch1.tab1");
72 is($result, qq(10|1|10), 'check rows on subscriber catchup');
73
74 $result = $node_subscriber->safe_psql('postgres',
75 "SELECT count(*), min(a), max(a) FROM sch1.tab2");
76 is($result, qq(10|1|10), 'check rows on subscriber catchup');
77
78 $result = $node_subscriber->safe_psql('postgres',
79 "SELECT * FROM sch1.tab1_parent order by 1");
80 is( $result, qq(1|
81 4|), 'check rows on subscriber catchup');
82
83 # Insert some data into few tables and verify that inserted data is replicated
84 $node_publisher->safe_psql('postgres',
85 "INSERT INTO sch1.tab1 VALUES(generate_series(11,20))");
86
87 $node_publisher->safe_psql('postgres',
88 "INSERT INTO sch1.tab1_parent values (2),(5)");
89
90 $node_publisher->wait_for_catchup('tap_sub_schema');
91
92 $result = $node_subscriber->safe_psql('postgres',
93 "SELECT count(*), min(a), max(a) FROM sch1.tab1");
94 is($result, qq(20|1|20), 'check replicated inserts on subscriber');
95
96 $result = $node_subscriber->safe_psql('postgres',
97 "SELECT * FROM sch1.tab1_parent order by 1");
98 is( $result, qq(1|
99 2|
100 4|
101 5|), 'check replicated inserts on subscriber');
102
103 # Create new table in the publication schema, verify that subscriber does not get
104 # the new table data before refresh.
105 $node_publisher->safe_psql('postgres',
106 "CREATE TABLE sch1.tab3 AS SELECT generate_series(1,10) AS a");
107
108 $node_subscriber->safe_psql('postgres', "CREATE TABLE sch1.tab3(a int)");
109
110 $node_publisher->wait_for_catchup('tap_sub_schema');
111
112 $result =
113 $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM sch1.tab3");
114 is($result, qq(0), 'check replicated inserts on subscriber');
115
116 # Table data should be reflected after refreshing the publication in
117 # subscriber.
118 $node_subscriber->safe_psql('postgres',
119 "ALTER SUBSCRIPTION tap_sub_schema REFRESH PUBLICATION");
120
121 # Wait for sync to finish
122 $node_subscriber->wait_for_subscription_sync;
123
124 $node_publisher->safe_psql('postgres', "INSERT INTO sch1.tab3 VALUES(11)");
125
126 $node_publisher->wait_for_catchup('tap_sub_schema');
127
128 $result = $node_subscriber->safe_psql('postgres',
129 "SELECT count(*), min(a), max(a) FROM sch1.tab3");
130 is($result, qq(11|1|11), 'check rows on subscriber catchup');
131
132 # Set the schema of a publication schema table to a non publication schema and
133 # verify that inserted data is not reflected by the subscriber.
134 $node_publisher->safe_psql('postgres',
135 "ALTER TABLE sch1.tab3 SET SCHEMA public");
136 $node_publisher->safe_psql('postgres', "INSERT INTO public.tab3 VALUES(12)");
137
138 $node_publisher->wait_for_catchup('tap_sub_schema');
139
140 $result = $node_subscriber->safe_psql('postgres',
141 "SELECT count(*), min(a), max(a) FROM sch1.tab3");
142 is($result, qq(11|1|11), 'check replicated inserts on subscriber');
143
144 # Verify that the subscription relation list is updated after refresh
145 $result = $node_subscriber->safe_psql('postgres',
146 "SELECT count(*) FROM pg_subscription_rel WHERE srsubid IN (SELECT oid FROM pg_subscription WHERE subname = 'tap_sub_schema')"
147 );
148 is($result, qq(5),
149 'check subscription relation status is not yet dropped on subscriber');
150
151 # Ask for data sync
152 $node_subscriber->safe_psql('postgres',
153 "ALTER SUBSCRIPTION tap_sub_schema REFRESH PUBLICATION");
154
155 # Wait for sync to finish
156 $node_subscriber->wait_for_subscription_sync;
157
158 $result = $node_subscriber->safe_psql('postgres',
159 "SELECT count(*) FROM pg_subscription_rel WHERE srsubid IN (SELECT oid FROM pg_subscription WHERE subname = 'tap_sub_schema')"
160 );
161 is($result, qq(4),
162 'check subscription relation status was dropped on subscriber');
163
164 # Drop table from the publication schema, verify that subscriber removes the
165 # table entry after refresh.
166 $node_publisher->safe_psql('postgres', "DROP TABLE sch1.tab2");
167 $node_publisher->wait_for_catchup('tap_sub_schema');
168 $result = $node_subscriber->safe_psql('postgres',
169 "SELECT count(*) FROM pg_subscription_rel WHERE srsubid IN (SELECT oid FROM pg_subscription WHERE subname = 'tap_sub_schema')"
170 );
171 is($result, qq(4),
172 'check subscription relation status is not yet dropped on subscriber');
173
174 # Table should be removed from pg_subscription_rel after refreshing the
175 # publication in subscriber.
176 $node_subscriber->safe_psql('postgres',
177 "ALTER SUBSCRIPTION tap_sub_schema REFRESH PUBLICATION");
178
179 # Wait for sync to finish
180 $node_subscriber->wait_for_subscription_sync;
181
182 $result = $node_subscriber->safe_psql('postgres',
183 "SELECT count(*) FROM pg_subscription_rel WHERE srsubid IN (SELECT oid FROM pg_subscription WHERE subname = 'tap_sub_schema')"
184 );
185 is($result, qq(3),
186 'check subscription relation status was dropped on subscriber');
187
188 # Drop schema from publication, verify that the inserts are not published after
189 # dropping the schema from publication. Here 2nd insert should not be
190 # published.
191 $node_publisher->safe_psql(
192 'postgres', "
193 INSERT INTO sch1.tab1 VALUES(21);
194 ALTER PUBLICATION tap_pub_schema DROP TABLES IN SCHEMA sch1;
195 INSERT INTO sch1.tab1 values(22);"
196 );
197
198 $node_publisher->wait_for_catchup('tap_sub_schema');
199
200 $result = $node_subscriber->safe_psql('postgres',
201 "SELECT count(*), min(a), max(a) FROM sch1.tab1");
202 is($result, qq(21|1|21), 'check replicated inserts on subscriber');
203
204 $node_subscriber->stop('fast');
205 $node_publisher->stop('fast');
206
207 done_testing();