| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test some logical replication DDL behavior |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
|
| 11 |
my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 12 |
$node_publisher->init(allows_streaming => 'logical'); |
| 13 |
$node_publisher->start; |
| 14 |
|
| 15 |
my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 16 |
$node_subscriber->init; |
| 17 |
$node_subscriber->start; |
| 18 |
|
| 19 |
my $ddl = "CREATE TABLE test1 (a int, b text);"; |
| 20 |
$node_publisher->safe_psql('postgres', $ddl); |
| 21 |
$node_subscriber->safe_psql('postgres', $ddl); |
| 22 |
|
| 23 |
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 24 |
|
| 25 |
$node_publisher->safe_psql('postgres', |
| 26 |
"CREATE PUBLICATION mypub FOR ALL TABLES;"); |
| 27 |
$node_subscriber->safe_psql('postgres', |
| 28 |
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr' PUBLICATION mypub;" |
| 29 |
); |
| 30 |
|
| 31 |
$node_publisher->wait_for_catchup('mysub'); |
| 32 |
|
| 33 |
$node_subscriber->safe_psql( |
| 34 |
'postgres', q{ |
| 35 |
BEGIN; |
| 36 |
ALTER SUBSCRIPTION mysub DISABLE; |
| 37 |
ALTER SUBSCRIPTION mysub SET (slot_name = NONE); |
| 38 |
DROP SUBSCRIPTION mysub; |
| 39 |
COMMIT; |
| 40 |
}); |
| 41 |
|
| 42 |
pass "subscription disable and drop in same transaction did not hang"; |
| 43 |
|
| 44 |
# One of the specified publications exists. |
| 45 |
my ($ret, $stdout, $stderr) = $node_subscriber->psql('postgres', |
| 46 |
"CREATE SUBSCRIPTION mysub1 CONNECTION '$publisher_connstr' PUBLICATION mypub, non_existent_pub" |
| 47 |
); |
| 48 |
ok( $stderr =~ |
| 49 |
m/WARNING: publication "non_existent_pub" does not exist on the publisher/, |
| 50 |
"Create subscription throws warning for non-existent publication"); |
| 51 |
|
| 52 |
# Wait for initial table sync to finish. |
| 53 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'mysub1'); |
| 54 |
|
| 55 |
# Specifying non-existent publication along with add publication. |
| 56 |
($ret, $stdout, $stderr) = $node_subscriber->psql('postgres', |
| 57 |
"ALTER SUBSCRIPTION mysub1 ADD PUBLICATION non_existent_pub1, non_existent_pub2" |
| 58 |
); |
| 59 |
ok( $stderr =~ |
| 60 |
m/WARNING: publications "non_existent_pub1", "non_existent_pub2" do not exist on the publisher/, |
| 61 |
"Alter subscription add publication throws warning for non-existent publications" |
| 62 |
); |
| 63 |
|
| 64 |
# Specifying non-existent publication along with set publication. |
| 65 |
($ret, $stdout, $stderr) = $node_subscriber->psql('postgres', |
| 66 |
"ALTER SUBSCRIPTION mysub1 SET PUBLICATION non_existent_pub"); |
| 67 |
ok( $stderr =~ |
| 68 |
m/WARNING: publication "non_existent_pub" does not exist on the publisher/, |
| 69 |
"Alter subscription set publication throws warning for non-existent publication" |
| 70 |
); |
| 71 |
|
| 72 |
# Cleanup |
| 73 |
$node_publisher->safe_psql( |
| 74 |
'postgres', qq[ |
| 75 |
DROP PUBLICATION mypub; |
| 76 |
SELECT pg_drop_replication_slot('mysub'); |
| 77 |
]); |
| 78 |
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION mysub1"); |
| 79 |
|
| 80 |
# |
| 81 |
# Test ALTER PUBLICATION RENAME command during the replication |
| 82 |
# |
| 83 |
|
| 84 |
# Test function for swapping name of publications |
| 85 |
sub test_swap |
| 86 |
{ |
| 87 |
my ($table_name, $pubname, $appname) = @_; |
| 88 |
|
| 89 |
# Confirms tuples can be replicated |
| 90 |
$node_publisher->safe_psql('postgres', |
| 91 |
"INSERT INTO $table_name VALUES (1);"); |
| 92 |
$node_publisher->wait_for_catchup($appname); |
| 93 |
my $result = |
| 94 |
$node_subscriber->safe_psql('postgres', "SELECT a FROM $table_name"); |
| 95 |
is($result, qq(1), |
| 96 |
'check replication worked well before renaming a publication'); |
| 97 |
|
| 98 |
# Swap the name of publications; $pubname <-> pub_empty |
| 99 |
$node_publisher->safe_psql( |
| 100 |
'postgres', qq[ |
| 101 |
ALTER PUBLICATION $pubname RENAME TO tap_pub_tmp; |
| 102 |
ALTER PUBLICATION pub_empty RENAME TO $pubname; |
| 103 |
ALTER PUBLICATION tap_pub_tmp RENAME TO pub_empty; |
| 104 |
]); |
| 105 |
|
| 106 |
# Insert the data again |
| 107 |
$node_publisher->safe_psql('postgres', |
| 108 |
"INSERT INTO $table_name VALUES (2);"); |
| 109 |
$node_publisher->wait_for_catchup($appname); |
| 110 |
|
| 111 |
# Confirms the second tuple won't be replicated because $pubname does not |
| 112 |
# contains relations anymore. |
| 113 |
$result = |
| 114 |
$node_subscriber->safe_psql('postgres', |
| 115 |
"SELECT a FROM $table_name ORDER BY a"); |
| 116 |
is($result, qq(1), |
| 117 |
'check the tuple inserted after the RENAME was not replicated'); |
| 118 |
|
| 119 |
# Restore the name of publications because it can be called several times |
| 120 |
$node_publisher->safe_psql( |
| 121 |
'postgres', qq[ |
| 122 |
ALTER PUBLICATION $pubname RENAME TO tap_pub_tmp; |
| 123 |
ALTER PUBLICATION pub_empty RENAME TO $pubname; |
| 124 |
ALTER PUBLICATION tap_pub_tmp RENAME TO pub_empty; |
| 125 |
]); |
| 126 |
} |
| 127 |
|
| 128 |
# Create another table |
| 129 |
$ddl = "CREATE TABLE test2 (a int, b text);"; |
| 130 |
$node_publisher->safe_psql('postgres', $ddl); |
| 131 |
$node_subscriber->safe_psql('postgres', $ddl); |
| 132 |
|
| 133 |
# Create publications and a subscription |
| 134 |
$node_publisher->safe_psql( |
| 135 |
'postgres', qq[ |
| 136 |
CREATE PUBLICATION pub_empty; |
| 137 |
CREATE PUBLICATION pub_for_tab FOR TABLE test1; |
| 138 |
CREATE PUBLICATION pub_for_all_tables FOR ALL TABLES; |
| 139 |
]); |
| 140 |
$node_subscriber->safe_psql('postgres', |
| 141 |
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr' PUBLICATION pub_for_tab" |
| 142 |
); |
| 143 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub'); |
| 144 |
|
| 145 |
# Confirms RENAME command works well for a publication |
| 146 |
test_swap('test1', 'pub_for_tab', 'tap_sub'); |
| 147 |
|
| 148 |
# Switches a publication which includes all tables |
| 149 |
$node_subscriber->safe_psql('postgres', |
| 150 |
"ALTER SUBSCRIPTION tap_sub SET PUBLICATION pub_for_all_tables;"); |
| 151 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub'); |
| 152 |
|
| 153 |
# Confirms RENAME command works well for ALL TABLES publication |
| 154 |
test_swap('test2', 'pub_for_all_tables', 'tap_sub'); |
| 155 |
|
| 156 |
# Cleanup |
| 157 |
$node_publisher->safe_psql( |
| 158 |
'postgres', qq[ |
| 159 |
DROP PUBLICATION pub_empty, pub_for_tab, pub_for_all_tables; |
| 160 |
DROP TABLE test1, test2; |
| 161 |
]); |
| 162 |
$node_subscriber->safe_psql( |
| 163 |
'postgres', qq[ |
| 164 |
DROP SUBSCRIPTION tap_sub; |
| 165 |
DROP TABLE test1, test2; |
| 166 |
]); |
| 167 |
|
| 168 |
$node_subscriber->stop; |
| 169 |
$node_publisher->stop; |
| 170 |
|
| 171 |
done_testing(); |