|
3 | 3 | use warnings;
|
4 | 4 | use PostgresNode;
|
5 | 5 | use TestLib;
|
6 |
| -use Test::More tests => 23; |
| 6 | +use Test::More tests => 27; |
7 | 7 |
|
8 | 8 | # Initialize publisher node
|
9 | 9 | my $node_publisher = get_new_node('publisher');
|
|
153 | 153 | $node_publisher->safe_psql('postgres',
|
154 | 154 | "INSERT INTO tab_full SELECT generate_series(1,10)");
|
155 | 155 |
|
| 156 | +# Test behaviour of ALTER PUBLICATION ... DROP TABLE |
| 157 | +# |
| 158 | +# When a publisher drops a table from publication, it should also stop sending |
| 159 | +# its changes to subscribers. We look at the subscriber whether it receives |
| 160 | +# the row that is inserted to the table in the publisher after it is dropped |
| 161 | +# from the publication. |
| 162 | +$result = $node_subscriber->safe_psql('postgres', |
| 163 | + "SELECT count(*), min(a), max(a) FROM tab_ins"); |
| 164 | +is($result, qq(1052|1|1002), 'check rows on subscriber before table drop from publication'); |
| 165 | + |
| 166 | +# Drop the table from publication |
| 167 | +$node_publisher->safe_psql('postgres', |
| 168 | + "ALTER PUBLICATION tap_pub_ins_only DROP TABLE tab_ins"); |
| 169 | + |
| 170 | +# Insert a row in publisher, but publisher will not send this row to subscriber |
| 171 | +$node_publisher->safe_psql('postgres', "INSERT INTO tab_ins VALUES(8888)"); |
| 172 | + |
| 173 | +$node_publisher->wait_for_catchup('tap_sub'); |
| 174 | + |
| 175 | +# Subscriber will not receive the inserted row, after table is dropped from |
| 176 | +# publication, so row count should remain the same. |
| 177 | +$result = $node_subscriber->safe_psql('postgres', |
| 178 | + "SELECT count(*), min(a), max(a) FROM tab_ins"); |
| 179 | +is($result, qq(1052|1|1002), 'check rows on subscriber after table drop from publication'); |
| 180 | + |
| 181 | +# Delete the inserted row in publisher |
| 182 | +$node_publisher->safe_psql('postgres', "DELETE FROM tab_ins WHERE a = 8888"); |
| 183 | + |
| 184 | +# Add the table to publication again |
| 185 | +$node_publisher->safe_psql('postgres', |
| 186 | + "ALTER PUBLICATION tap_pub_ins_only ADD TABLE tab_ins"); |
| 187 | + |
| 188 | +# Refresh publication after table is added to publication |
| 189 | +$node_subscriber->safe_psql('postgres', |
| 190 | + "ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION"); |
| 191 | + |
| 192 | +# Test replication with multiple publications for a subscription such that the |
| 193 | +# operations are performed on the table from the first publication in the list. |
| 194 | + |
| 195 | +# Create tables on publisher |
| 196 | +$node_publisher->safe_psql('postgres', "CREATE TABLE temp1 (a int)"); |
| 197 | +$node_publisher->safe_psql('postgres', "CREATE TABLE temp2 (a int)"); |
| 198 | + |
| 199 | +# Create tables on subscriber |
| 200 | +$node_subscriber->safe_psql('postgres', "CREATE TABLE temp1 (a int)"); |
| 201 | +$node_subscriber->safe_psql('postgres', "CREATE TABLE temp2 (a int)"); |
| 202 | + |
| 203 | +# Setup logical replication that will only be used for this test |
| 204 | +$node_publisher->safe_psql('postgres', |
| 205 | + "CREATE PUBLICATION tap_pub_temp1 FOR TABLE temp1 WITH (publish = insert)"); |
| 206 | +$node_publisher->safe_psql('postgres', |
| 207 | + "CREATE PUBLICATION tap_pub_temp2 FOR TABLE temp2"); |
| 208 | +$node_subscriber->safe_psql('postgres', |
| 209 | + "CREATE SUBSCRIPTION tap_sub_temp1 CONNECTION '$publisher_connstr' PUBLICATION tap_pub_temp1, tap_pub_temp2" |
| 210 | +); |
| 211 | + |
| 212 | +$node_publisher->wait_for_catchup('tap_sub_temp1'); |
| 213 | + |
| 214 | +# Also wait for initial table sync to finish |
| 215 | +$synced_query = |
| 216 | + "SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');"; |
| 217 | +$node_subscriber->poll_query_until('postgres', $synced_query) |
| 218 | + or die "Timed out while waiting for subscriber to synchronize data"; |
| 219 | + |
| 220 | +# Subscriber table will have no rows initially |
| 221 | +$result = $node_subscriber->safe_psql('postgres', |
| 222 | + "SELECT count(*) FROM temp1"); |
| 223 | +is($result, qq(0), 'check initial rows on subscriber with multiple publications'); |
| 224 | + |
| 225 | +# Insert a row into the table that's part of first publication in subscriber |
| 226 | +# list of publications. |
| 227 | +$node_publisher->safe_psql('postgres', "INSERT INTO temp1 VALUES (1)"); |
| 228 | + |
| 229 | +$node_publisher->wait_for_catchup('tap_sub_temp1'); |
| 230 | + |
| 231 | +# Subscriber should receive the inserted row |
| 232 | +$result = $node_subscriber->safe_psql('postgres', |
| 233 | + "SELECT count(*) FROM temp1"); |
| 234 | +is($result, qq(1), 'check rows on subscriber with multiple publications'); |
| 235 | + |
| 236 | +# Drop subscription as we don't need it anymore |
| 237 | +$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_temp1"); |
| 238 | + |
| 239 | +# Drop publications as we don't need them anymore |
| 240 | +$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_temp1"); |
| 241 | +$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_temp2"); |
| 242 | + |
| 243 | +# Clean up the tables on both publisher and subscriber as we don't need them |
| 244 | +$node_publisher->safe_psql('postgres', "DROP TABLE temp1"); |
| 245 | +$node_publisher->safe_psql('postgres', "DROP TABLE temp2"); |
| 246 | +$node_subscriber->safe_psql('postgres', "DROP TABLE temp1"); |
| 247 | +$node_subscriber->safe_psql('postgres', "DROP TABLE temp2"); |
| 248 | + |
156 | 249 | # add REPLICA IDENTITY FULL so we can update
|
157 | 250 | $node_publisher->safe_psql('postgres',
|
158 | 251 | "ALTER TABLE tab_full REPLICA IDENTITY FULL");
|
|
0 commit comments