|
| 1 | + |
| 2 | +# Copyright (c) 2021, PostgreSQL Global Development Group |
| 3 | + |
| 4 | +# This test checks behaviour of ALTER SUBSCRIPTION ... ADD/DROP PUBLICATION |
| 5 | +use strict; |
| 6 | +use warnings; |
| 7 | +use PostgresNode; |
| 8 | +use TestLib; |
| 9 | +use Test::More tests => 3; |
| 10 | + |
| 11 | +# Initialize publisher node |
| 12 | +my $node_publisher = PostgresNode->new('publisher'); |
| 13 | +$node_publisher->init(allows_streaming => 'logical'); |
| 14 | +$node_publisher->start; |
| 15 | + |
| 16 | +# Create subscriber node |
| 17 | +my $node_subscriber = PostgresNode->new('subscriber'); |
| 18 | +$node_subscriber->init(allows_streaming => 'logical'); |
| 19 | +$node_subscriber->start; |
| 20 | + |
| 21 | +# Create table on publisher |
| 22 | +$node_publisher->safe_psql('postgres', "CREATE TABLE tab_1 (a int)"); |
| 23 | +$node_publisher->safe_psql('postgres', |
| 24 | + "INSERT INTO tab_1 SELECT generate_series(1,10)"); |
| 25 | + |
| 26 | +# Create table on subscriber |
| 27 | +$node_subscriber->safe_psql('postgres', "CREATE TABLE tab_1 (a int)"); |
| 28 | + |
| 29 | +# Setup logical replication |
| 30 | +my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 31 | +$node_publisher->safe_psql('postgres', |
| 32 | + "CREATE PUBLICATION tap_pub_1 FOR TABLE tab_1"); |
| 33 | +$node_publisher->safe_psql('postgres', |
| 34 | + "CREATE PUBLICATION tap_pub_2"); |
| 35 | + |
| 36 | +$node_subscriber->safe_psql('postgres', |
| 37 | + "CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub_1, tap_pub_2" |
| 38 | +); |
| 39 | + |
| 40 | +# Wait for initial table sync to finish |
| 41 | +my $synced_query = |
| 42 | + "SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');"; |
| 43 | + |
| 44 | +$node_subscriber->poll_query_until('postgres', $synced_query) |
| 45 | + or die "Timed out while waiting for subscriber to synchronize data"; |
| 46 | + |
| 47 | +$node_publisher->wait_for_catchup('tap_sub'); |
| 48 | + |
| 49 | +# Check the initial data of tab_1 is copied to subscriber |
| 50 | +my $result = $node_subscriber->safe_psql('postgres', |
| 51 | + "SELECT count(*), min(a), max(a) FROM tab_1"); |
| 52 | +is($result, qq(10|1|10), 'check initial data is copied to subscriber'); |
| 53 | + |
| 54 | +# Create a new table on publisher |
| 55 | +$node_publisher->safe_psql('postgres', "CREATE TABLE tab_2 (a int)"); |
| 56 | +$node_publisher->safe_psql('postgres', |
| 57 | + "INSERT INTO tab_2 SELECT generate_series(1,10)"); |
| 58 | + |
| 59 | +# Create a new table on subscriber |
| 60 | +$node_subscriber->safe_psql('postgres', "CREATE TABLE tab_2 (a int)"); |
| 61 | + |
| 62 | +# Add the table to publication |
| 63 | +$node_publisher->safe_psql('postgres', |
| 64 | + "ALTER PUBLICATION tap_pub_2 ADD TABLE tab_2"); |
| 65 | + |
| 66 | +# Dropping tap_pub_1 will refresh the entire publication list |
| 67 | +$node_subscriber->safe_psql('postgres', |
| 68 | + "ALTER SUBSCRIPTION tap_sub DROP PUBLICATION tap_pub_1"); |
| 69 | + |
| 70 | +# Wait for initial table sync to finish |
| 71 | +$node_subscriber->poll_query_until('postgres', $synced_query) |
| 72 | + or die "Timed out while waiting for subscriber to synchronize data"; |
| 73 | + |
| 74 | +$node_publisher->wait_for_catchup('tap_sub'); |
| 75 | + |
| 76 | +# Check the initial data of tab_drop_refresh was copied to subscriber |
| 77 | +$result = $node_subscriber->safe_psql('postgres', |
| 78 | + "SELECT count(*), min(a), max(a) FROM tab_2"); |
| 79 | +is($result, qq(10|1|10), 'check initial data is copied to subscriber'); |
| 80 | + |
| 81 | +# Re-adding tap_pub_1 will refresh the entire publication list |
| 82 | +$node_subscriber->safe_psql('postgres', |
| 83 | + "ALTER SUBSCRIPTION tap_sub ADD PUBLICATION tap_pub_1"); |
| 84 | + |
| 85 | +# Wait for initial table sync to finish |
| 86 | +$node_subscriber->poll_query_until('postgres', $synced_query) |
| 87 | + or die "Timed out while waiting for subscriber to synchronize data"; |
| 88 | + |
| 89 | +$node_publisher->wait_for_catchup('tap_sub'); |
| 90 | + |
| 91 | +# Check the initial data of tab_1 was copied to subscriber again |
| 92 | +$result = $node_subscriber->safe_psql('postgres', |
| 93 | + "SELECT count(*), min(a), max(a) FROM tab_1"); |
| 94 | +is($result, qq(20|1|10), 'check initial data is copied to subscriber'); |
| 95 | + |
| 96 | +# shutdown |
| 97 | +$node_subscriber->stop('fast'); |
| 98 | +$node_publisher->stop('fast'); |
0 commit comments