| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Tests for logical replication table syncing |
| 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->append_conf('postgresql.conf', |
| 20 |
"wal_retrieve_retry_interval = 1ms"); |
| 21 |
$node_subscriber->start; |
| 22 |
|
| 23 |
# Create some preexisting content on publisher |
| 24 |
$node_publisher->safe_psql('postgres', |
| 25 |
"CREATE TABLE tab_rep (a int primary key)"); |
| 26 |
$node_publisher->safe_psql('postgres', |
| 27 |
"INSERT INTO tab_rep SELECT generate_series(1,10)"); |
| 28 |
|
| 29 |
# Setup structure on subscriber |
| 30 |
$node_subscriber->safe_psql('postgres', |
| 31 |
"CREATE TABLE tab_rep (a int primary key)"); |
| 32 |
|
| 33 |
# Setup logical replication |
| 34 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 35 |
$node_publisher->safe_psql('postgres', |
| 36 |
"CREATE PUBLICATION tap_pub FOR ALL TABLES"); |
| 37 |
|
| 38 |
$node_subscriber->safe_psql('postgres', |
| 39 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub" |
| 40 |
); |
| 41 |
|
| 42 |
# Wait for initial table sync to finish |
| 43 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub'); |
| 44 |
|
| 45 |
my $result = |
| 46 |
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_rep"); |
| 47 |
is($result, qq(10), 'initial data synced for first sub'); |
| 48 |
|
| 49 |
# drop subscription so that there is unreplicated data |
| 50 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub"); |
| 51 |
|
| 52 |
$node_publisher->safe_psql('postgres', |
| 53 |
"INSERT INTO tab_rep SELECT generate_series(11,20)"); |
| 54 |
|
| 55 |
# recreate the subscription, it will try to do initial copy |
| 56 |
$node_subscriber->safe_psql('postgres', |
| 57 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub" |
| 58 |
); |
| 59 |
|
| 60 |
# but it will be stuck on data copy as it will fail on constraint |
| 61 |
my $started_query = "SELECT srsubstate = 'd' FROM pg_subscription_rel;"; |
| 62 |
$node_subscriber->poll_query_until('postgres', $started_query) |
| 63 |
or die "Timed out while waiting for subscriber to start sync"; |
| 64 |
|
| 65 |
# remove the conflicting data |
| 66 |
$node_subscriber->safe_psql('postgres', "DELETE FROM tab_rep;"); |
| 67 |
|
| 68 |
# wait for sync to finish this time |
| 69 |
$node_subscriber->wait_for_subscription_sync; |
| 70 |
|
| 71 |
# check that all data is synced |
| 72 |
$result = |
| 73 |
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_rep"); |
| 74 |
is($result, qq(20), 'initial data synced for second sub'); |
| 75 |
|
| 76 |
# now check another subscription for the same node pair |
| 77 |
$node_subscriber->safe_psql('postgres', |
| 78 |
"CREATE SUBSCRIPTION tap_sub2 CONNECTION '$publisher_connstr' PUBLICATION tap_pub WITH (copy_data = false)" |
| 79 |
); |
| 80 |
|
| 81 |
# wait for it to start |
| 82 |
$node_subscriber->poll_query_until('postgres', |
| 83 |
"SELECT pid IS NOT NULL FROM pg_stat_subscription WHERE subname = 'tap_sub2' AND worker_type = 'apply'" |
| 84 |
) or die "Timed out while waiting for subscriber to start"; |
| 85 |
|
| 86 |
# and drop both subscriptions |
| 87 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub"); |
| 88 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub2"); |
| 89 |
|
| 90 |
# check subscriptions are removed |
| 91 |
$result = $node_subscriber->safe_psql('postgres', |
| 92 |
"SELECT count(*) FROM pg_subscription"); |
| 93 |
is($result, qq(0), 'second and third sub are dropped'); |
| 94 |
|
| 95 |
# remove the conflicting data |
| 96 |
$node_subscriber->safe_psql('postgres', "DELETE FROM tab_rep;"); |
| 97 |
|
| 98 |
# recreate the subscription again |
| 99 |
$node_subscriber->safe_psql('postgres', |
| 100 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub" |
| 101 |
); |
| 102 |
|
| 103 |
# and wait for data sync to finish again |
| 104 |
$node_subscriber->wait_for_subscription_sync; |
| 105 |
|
| 106 |
# check that all data is synced |
| 107 |
$result = |
| 108 |
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_rep"); |
| 109 |
is($result, qq(20), 'initial data synced for fourth sub'); |
| 110 |
|
| 111 |
# add new table on subscriber |
| 112 |
$node_subscriber->safe_psql('postgres', "CREATE TABLE tab_rep_next (a int)"); |
| 113 |
|
| 114 |
# setup structure with existing data on publisher |
| 115 |
$node_publisher->safe_psql('postgres', |
| 116 |
"CREATE TABLE tab_rep_next (a) AS SELECT generate_series(1,10)"); |
| 117 |
|
| 118 |
$node_publisher->wait_for_catchup('tap_sub'); |
| 119 |
|
| 120 |
$result = $node_subscriber->safe_psql('postgres', |
| 121 |
"SELECT count(*) FROM tab_rep_next"); |
| 122 |
is($result, qq(0), 'no data for table added after subscription initialized'); |
| 123 |
|
| 124 |
# ask for data sync |
| 125 |
$node_subscriber->safe_psql('postgres', |
| 126 |
"ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION"); |
| 127 |
|
| 128 |
# wait for sync to finish |
| 129 |
$node_subscriber->wait_for_subscription_sync; |
| 130 |
|
| 131 |
$result = $node_subscriber->safe_psql('postgres', |
| 132 |
"SELECT count(*) FROM tab_rep_next"); |
| 133 |
is($result, qq(10), |
| 134 |
'data for table added after subscription initialized are now synced'); |
| 135 |
|
| 136 |
# Add some data |
| 137 |
$node_publisher->safe_psql('postgres', |
| 138 |
"INSERT INTO tab_rep_next SELECT generate_series(1,10)"); |
| 139 |
|
| 140 |
$node_publisher->wait_for_catchup('tap_sub'); |
| 141 |
|
| 142 |
$result = $node_subscriber->safe_psql('postgres', |
| 143 |
"SELECT count(*) FROM tab_rep_next"); |
| 144 |
is($result, qq(20), |
| 145 |
'changes for table added after subscription initialized replicated'); |
| 146 |
|
| 147 |
# clean up |
| 148 |
$node_publisher->safe_psql('postgres', "DROP TABLE tab_rep_next"); |
| 149 |
$node_subscriber->safe_psql('postgres', "DROP TABLE tab_rep_next"); |
| 150 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub"); |
| 151 |
|
| 152 |
# Table tab_rep already has the same records on both publisher and subscriber |
| 153 |
# at this time. Recreate the subscription which will do the initial copy of |
| 154 |
# the table again and fails due to unique constraint violation. |
| 155 |
$node_subscriber->safe_psql('postgres', |
| 156 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub" |
| 157 |
); |
| 158 |
|
| 159 |
$node_subscriber->poll_query_until('postgres', $started_query) |
| 160 |
or die "Timed out while waiting for subscriber to start sync"; |
| 161 |
|
| 162 |
# DROP SUBSCRIPTION must clean up slots on the publisher side when the |
| 163 |
# subscriber is stuck on data copy for constraint violation. |
| 164 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub"); |
| 165 |
|
| 166 |
# When DROP SUBSCRIPTION tries to drop the tablesync slot, the slot may not |
| 167 |
# have been created, which causes the slot to be created after the DROP |
| 168 |
# SUBSCRIPTION finishes. Such slots eventually get dropped at walsender exit |
| 169 |
# time. So, to prevent being affected by such ephemeral tablesync slots, we |
| 170 |
# wait until all the slots have been cleaned. |
| 171 |
ok( $node_publisher->poll_query_until( |
| 172 |
'postgres', 'SELECT count(*) = 0 FROM pg_replication_slots'), |
| 173 |
'DROP SUBSCRIPTION during error can clean up the slots on the publisher'); |
| 174 |
|
| 175 |
# After dropping the subscription, all replication origins, whether created by |
| 176 |
# an apply worker or table sync worker, should have been cleaned up. |
| 177 |
$result = $node_subscriber->safe_psql('postgres', |
| 178 |
"SELECT count(*) FROM pg_replication_origin_status"); |
| 179 |
is($result, qq(0), 'all replication origins have been cleaned up'); |
| 180 |
|
| 181 |
$node_subscriber->stop('fast'); |
| 182 |
$node_publisher->stop('fast'); |
| 183 |
|
| 184 |
done_testing(); |