Debian Codegraph @

src/test/subscription/t/024_add_drop_pub.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # This test checks behaviour of ALTER SUBSCRIPTION ... ADD/DROP PUBLICATION and
5 # ensures that creating a publication associated with a subscription at a later
6 # point of time does not break logical replication.
7 use strict;
8 use warnings FATAL => 'all';
9 use PostgreSQL::Test::Cluster;
10 use PostgreSQL::Test::Utils;
11 use Test::More;
12
13 # Initialize publisher node
14 my $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
15 $node_publisher->init(allows_streaming => 'logical');
16 $node_publisher->start;
17
18 # Create subscriber node
19 my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
20 $node_subscriber->init;
21 $node_subscriber->start;
22
23 # Create table on publisher
24 $node_publisher->safe_psql('postgres', "CREATE TABLE tab_1 (a int)");
25 $node_publisher->safe_psql('postgres',
26 "INSERT INTO tab_1 SELECT generate_series(1,10)");
27
28 # Create table on subscriber
29 $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_1 (a int)");
30
31 # Setup logical replication
32 my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
33 $node_publisher->safe_psql('postgres',
34 "CREATE PUBLICATION tap_pub_1 FOR TABLE tab_1");
35 $node_publisher->safe_psql('postgres', "CREATE PUBLICATION tap_pub_2");
36
37 $node_subscriber->safe_psql('postgres',
38 "CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub_1, tap_pub_2"
39 );
40
41 # Wait for initial table sync to finish
42 $node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
43
44 # Check the initial data of tab_1 is copied to subscriber
45 my $result = $node_subscriber->safe_psql('postgres',
46 "SELECT count(*), min(a), max(a) FROM tab_1");
47 is($result, qq(10|1|10), 'check initial data is copied to subscriber');
48
49 # Create a new table on publisher
50 $node_publisher->safe_psql('postgres', "CREATE TABLE tab_2 (a int)");
51 $node_publisher->safe_psql('postgres',
52 "INSERT INTO tab_2 SELECT generate_series(1,10)");
53
54 # Create a new table on subscriber
55 $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_2 (a int)");
56
57 # Add the table to publication
58 $node_publisher->safe_psql('postgres',
59 "ALTER PUBLICATION tap_pub_2 ADD TABLE tab_2");
60
61 # Dropping tap_pub_1 will refresh the entire publication list
62 $node_subscriber->safe_psql('postgres',
63 "ALTER SUBSCRIPTION tap_sub DROP PUBLICATION tap_pub_1");
64
65 # Wait for initial table sync to finish
66 $node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
67
68 # Check the initial data of tab_2 was copied to subscriber
69 $result = $node_subscriber->safe_psql('postgres',
70 "SELECT count(*), min(a), max(a) FROM tab_2");
71 is($result, qq(10|1|10), 'check initial data is copied to subscriber');
72
73 # Re-adding tap_pub_1 will refresh the entire publication list
74 $node_subscriber->safe_psql('postgres',
75 "ALTER SUBSCRIPTION tap_sub ADD PUBLICATION tap_pub_1");
76
77 # Wait for initial table sync to finish
78 $node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
79
80 # Check the initial data of tab_1 was copied to subscriber again
81 $result = $node_subscriber->safe_psql('postgres',
82 "SELECT count(*), min(a), max(a) FROM tab_1");
83 is($result, qq(20|1|10), 'check initial data is copied to subscriber');
84
85 # Ensure that setting a missing publication to the subscription does not
86 # disrupt existing logical replication. Instead, it should log a warning
87 # while allowing replication to continue. Additionally, verify that replication
88 # resumes after the missing publication is created for the publication table.
89
90 # Create table on publisher and subscriber
91 $node_publisher->safe_psql('postgres', "CREATE TABLE tab_3 (a int)");
92 $node_subscriber->safe_psql('postgres', "CREATE TABLE tab_3 (a int)");
93
94 my $oldpid = $node_publisher->safe_psql('postgres',
95 "SELECT pid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
96 );
97
98 # Set the subscription with a missing publication
99 $node_subscriber->safe_psql('postgres',
100 "ALTER SUBSCRIPTION tap_sub SET PUBLICATION tap_pub_3");
101
102 # Wait for the walsender to restart after altering the subscription
103 $node_publisher->poll_query_until('postgres',
104 "SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = 'tap_sub' AND state = 'streaming';"
105 )
106 or die
107 "Timed out while waiting for apply worker to restart after altering the subscription";
108
109 my $offset = -s $node_publisher->logfile;
110
111 $node_publisher->safe_psql('postgres', "INSERT INTO tab_3 values(1)");
112
113 # Verify that a warning is logged.
114 $node_publisher->wait_for_log(
115 qr/WARNING: ( [A-Z0-9]+:)? skipped loading publication "tap_pub_3"/,
116 $offset);
117
118 $node_publisher->safe_psql('postgres',
119 "CREATE PUBLICATION tap_pub_3 FOR TABLE tab_3");
120
121 $node_subscriber->safe_psql('postgres',
122 "ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
123
124 $node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
125
126 $node_publisher->safe_psql('postgres', "INSERT INTO tab_3 values(2)");
127
128 $node_publisher->wait_for_catchup('tap_sub');
129
130 # Verify that the insert operation gets replicated to subscriber after
131 # publication is created.
132 $result = $node_subscriber->safe_psql('postgres', "SELECT * FROM tab_3");
133 is( $result, qq(1
134 2),
135 'check that the incremental data is replicated after the publication is created'
136 );
137
138 # shutdown
139 $node_subscriber->stop('fast');
140 $node_publisher->stop('fast');
141
142 done_testing();