| 1 |
# Copyright (c) 2023-2025, PostgreSQL Global Development Group |
| 2 |
|
| 3 |
# Test for pg_upgrade of logical subscription. Note that after the successful |
| 4 |
# upgrade test, we can't use the old cluster to prevent failing in --link mode. |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
|
| 8 |
use File::Find qw(find); |
| 9 |
use File::Path qw(rmtree); |
| 10 |
|
| 11 |
use PostgreSQL::Test::Cluster; |
| 12 |
use PostgreSQL::Test::Utils; |
| 13 |
use Test::More; |
| 14 |
|
| 15 |
# Can be changed to test the other modes. |
| 16 |
my $mode = $ENV{PG_TEST_PG_UPGRADE_MODE} || '--copy'; |
| 17 |
|
| 18 |
# Initialize publisher node |
| 19 |
my $publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 20 |
$publisher->init(allows_streaming => 'logical'); |
| 21 |
$publisher->start; |
| 22 |
|
| 23 |
# Initialize the old subscriber node |
| 24 |
my $old_sub = PostgreSQL::Test::Cluster->new('old_sub'); |
| 25 |
$old_sub->init; |
| 26 |
$old_sub->start; |
| 27 |
my $oldbindir = $old_sub->config_data('--bindir'); |
| 28 |
|
| 29 |
# Initialize the new subscriber |
| 30 |
my $new_sub = PostgreSQL::Test::Cluster->new('new_sub'); |
| 31 |
$new_sub->init; |
| 32 |
my $newbindir = $new_sub->config_data('--bindir'); |
| 33 |
|
| 34 |
# In a VPATH build, we'll be started in the source directory, but we want |
| 35 |
# to run pg_upgrade in the build directory so that any files generated finish |
| 36 |
# in it, like delete_old_cluster.{sh,bat}. |
| 37 |
chdir ${PostgreSQL::Test::Utils::tmp_check}; |
| 38 |
|
| 39 |
# Remember a connection string for the publisher node. It would be used |
| 40 |
# several times. |
| 41 |
my $connstr = $publisher->connstr . ' dbname=postgres'; |
| 42 |
|
| 43 |
# ------------------------------------------------------ |
| 44 |
# Check that pg_upgrade fails when max_active_replication_origins configured |
| 45 |
# in the new cluster is less than the number of subscriptions in the old |
| 46 |
# cluster. |
| 47 |
# ------------------------------------------------------ |
| 48 |
# It is sufficient to use disabled subscription to test upgrade failure. |
| 49 |
$publisher->safe_psql('postgres', "CREATE PUBLICATION regress_pub1"); |
| 50 |
$old_sub->safe_psql('postgres', |
| 51 |
"CREATE SUBSCRIPTION regress_sub1 CONNECTION '$connstr' PUBLICATION regress_pub1 WITH (enabled = false)" |
| 52 |
); |
| 53 |
|
| 54 |
$old_sub->stop; |
| 55 |
|
| 56 |
$new_sub->append_conf('postgresql.conf', |
| 57 |
"max_active_replication_origins = 0"); |
| 58 |
|
| 59 |
# pg_upgrade will fail because the new cluster has insufficient |
| 60 |
# max_active_replication_origins. |
| 61 |
command_checks_all( |
| 62 |
[ |
| 63 |
'pg_upgrade', |
| 64 |
'--no-sync', |
| 65 |
'--old-datadir' => $old_sub->data_dir, |
| 66 |
'--new-datadir' => $new_sub->data_dir, |
| 67 |
'--old-bindir' => $oldbindir, |
| 68 |
'--new-bindir' => $newbindir, |
| 69 |
'--socketdir' => $new_sub->host, |
| 70 |
'--old-port' => $old_sub->port, |
| 71 |
'--new-port' => $new_sub->port, |
| 72 |
$mode, |
| 73 |
'--check', |
| 74 |
], |
| 75 |
1, |
| 76 |
[ |
| 77 |
qr/"max_active_replication_origins" \(0\) must be greater than or equal to the number of subscriptions \(1\) on the old cluster/ |
| 78 |
], |
| 79 |
[qr//], |
| 80 |
'run of pg_upgrade where the new cluster has insufficient max_active_replication_origins' |
| 81 |
); |
| 82 |
|
| 83 |
# Reset max_active_replication_origins |
| 84 |
$new_sub->append_conf('postgresql.conf', |
| 85 |
"max_active_replication_origins = 10"); |
| 86 |
|
| 87 |
# Cleanup |
| 88 |
$publisher->safe_psql('postgres', "DROP PUBLICATION regress_pub1"); |
| 89 |
$old_sub->start; |
| 90 |
$old_sub->safe_psql('postgres', "DROP SUBSCRIPTION regress_sub1;"); |
| 91 |
|
| 92 |
# ------------------------------------------------------ |
| 93 |
# Check that pg_upgrade refuses to run if: |
| 94 |
# a) there's a subscription with tables in a state other than 'r' (ready) or |
| 95 |
# 'i' (init) and/or |
| 96 |
# b) the subscription has no replication origin. |
| 97 |
# ------------------------------------------------------ |
| 98 |
$publisher->safe_psql( |
| 99 |
'postgres', qq[ |
| 100 |
CREATE TABLE tab_primary_key(id serial PRIMARY KEY); |
| 101 |
INSERT INTO tab_primary_key values(1); |
| 102 |
CREATE PUBLICATION regress_pub2 FOR TABLE tab_primary_key; |
| 103 |
]); |
| 104 |
|
| 105 |
# Insert the same value that is already present in publisher to the primary key |
| 106 |
# column of subscriber so that the table sync will fail. |
| 107 |
$old_sub->safe_psql( |
| 108 |
'postgres', qq[ |
| 109 |
CREATE TABLE tab_primary_key(id serial PRIMARY KEY); |
| 110 |
INSERT INTO tab_primary_key values(1); |
| 111 |
CREATE SUBSCRIPTION regress_sub2 CONNECTION '$connstr' PUBLICATION regress_pub2; |
| 112 |
]); |
| 113 |
|
| 114 |
# Table will be in 'd' (data is being copied) state as table sync will fail |
| 115 |
# because of primary key constraint error. |
| 116 |
my $started_query = |
| 117 |
"SELECT count(1) = 1 FROM pg_subscription_rel WHERE srsubstate = 'd'"; |
| 118 |
$old_sub->poll_query_until('postgres', $started_query) |
| 119 |
or die |
| 120 |
"Timed out while waiting for the table state to become 'd' (datasync)"; |
| 121 |
|
| 122 |
# Setup another logical replication and drop the subscription's replication |
| 123 |
# origin. |
| 124 |
$publisher->safe_psql('postgres', "CREATE PUBLICATION regress_pub3"); |
| 125 |
$old_sub->safe_psql('postgres', |
| 126 |
"CREATE SUBSCRIPTION regress_sub3 CONNECTION '$connstr' PUBLICATION regress_pub3 WITH (enabled = false)" |
| 127 |
); |
| 128 |
my $sub_oid = $old_sub->safe_psql('postgres', |
| 129 |
"SELECT oid FROM pg_subscription WHERE subname = 'regress_sub3'"); |
| 130 |
my $reporigin = 'pg_' . qq($sub_oid); |
| 131 |
$old_sub->safe_psql('postgres', |
| 132 |
"SELECT pg_replication_origin_drop('$reporigin')"); |
| 133 |
|
| 134 |
$old_sub->stop; |
| 135 |
|
| 136 |
command_checks_all( |
| 137 |
[ |
| 138 |
'pg_upgrade', |
| 139 |
'--no-sync', |
| 140 |
'--old-datadir' => $old_sub->data_dir, |
| 141 |
'--new-datadir' => $new_sub->data_dir, |
| 142 |
'--old-bindir' => $oldbindir, |
| 143 |
'--new-bindir' => $newbindir, |
| 144 |
'--socketdir' => $new_sub->host, |
| 145 |
'--old-port' => $old_sub->port, |
| 146 |
'--new-port' => $new_sub->port, |
| 147 |
$mode, |
| 148 |
'--check', |
| 149 |
], |
| 150 |
1, |
| 151 |
[ |
| 152 |
qr/\QYour installation contains subscriptions without origin or having relations not in i (initialize) or r (ready) state\E/ |
| 153 |
], |
| 154 |
[], |
| 155 |
'run of pg_upgrade --check for old instance with relation in \'d\' datasync(invalid) state and missing replication origin' |
| 156 |
); |
| 157 |
|
| 158 |
# Verify the reason why the subscriber cannot be upgraded |
| 159 |
my $sub_relstate_filename; |
| 160 |
|
| 161 |
# Find a txt file that contains a list of tables that cannot be upgraded. We |
| 162 |
# cannot predict the file's path because the output directory contains a |
| 163 |
# milliseconds timestamp. File::Find::find must be used. |
| 164 |
find( |
| 165 |
sub { |
| 166 |
if ($File::Find::name =~ m/subs_invalid\.txt/) |
| 167 |
{ |
| 168 |
$sub_relstate_filename = $File::Find::name; |
| 169 |
} |
| 170 |
}, |
| 171 |
$new_sub->data_dir . "/pg_upgrade_output.d"); |
| 172 |
|
| 173 |
# Check the file content which should have tab_primary_key table in an invalid |
| 174 |
# state. |
| 175 |
like( |
| 176 |
slurp_file($sub_relstate_filename), |
| 177 |
qr/The table sync state \"d\" is not allowed for database:\"postgres\" subscription:\"regress_sub2\" schema:\"public\" relation:\"tab_primary_key\"/m, |
| 178 |
'the previous test failed due to subscription table in invalid state'); |
| 179 |
|
| 180 |
# Check the file content which should have regress_sub3 subscription. |
| 181 |
like( |
| 182 |
slurp_file($sub_relstate_filename), |
| 183 |
qr/The replication origin is missing for database:\"postgres\" subscription:\"regress_sub3\"/m, |
| 184 |
'the previous test failed due to missing replication origin'); |
| 185 |
|
| 186 |
# Cleanup |
| 187 |
$old_sub->start; |
| 188 |
$publisher->safe_psql( |
| 189 |
'postgres', qq[ |
| 190 |
DROP PUBLICATION regress_pub2; |
| 191 |
DROP PUBLICATION regress_pub3; |
| 192 |
DROP TABLE tab_primary_key; |
| 193 |
]); |
| 194 |
$old_sub->safe_psql( |
| 195 |
'postgres', qq[ |
| 196 |
DROP SUBSCRIPTION regress_sub2; |
| 197 |
DROP SUBSCRIPTION regress_sub3; |
| 198 |
DROP TABLE tab_primary_key; |
| 199 |
]); |
| 200 |
rmtree($new_sub->data_dir . "/pg_upgrade_output.d"); |
| 201 |
|
| 202 |
# Verify that the upgrade should be successful with tables in 'ready'/'init' |
| 203 |
# state along with retaining the replication origin's remote lsn, |
| 204 |
# subscription's running status, and failover option. Use multiple tables to |
| 205 |
# verify deterministic pg_dump ordering of subscription relations during |
| 206 |
# --binary-upgrade. |
| 207 |
$publisher->safe_psql( |
| 208 |
'postgres', qq[ |
| 209 |
CREATE TABLE tab_upgraded(id int); |
| 210 |
CREATE TABLE tab_upgraded1(id int); |
| 211 |
CREATE PUBLICATION regress_pub4 FOR TABLE tab_upgraded, tab_upgraded1; |
| 212 |
]); |
| 213 |
|
| 214 |
$old_sub->safe_psql( |
| 215 |
'postgres', qq[ |
| 216 |
CREATE TABLE tab_upgraded(id int); |
| 217 |
CREATE TABLE tab_upgraded1(id int); |
| 218 |
CREATE SUBSCRIPTION regress_sub4 CONNECTION '$connstr' PUBLICATION regress_pub4 WITH (failover = true); |
| 219 |
]); |
| 220 |
|
| 221 |
# Wait till the tables tab_upgraded and tab_upgraded1 reach 'ready' state |
| 222 |
my $synced_query = |
| 223 |
"SELECT count(1) = 2 FROM pg_subscription_rel WHERE srsubstate = 'r'"; |
| 224 |
$old_sub->poll_query_until('postgres', $synced_query) |
| 225 |
or die "Timed out while waiting for the table to reach ready state"; |
| 226 |
|
| 227 |
$publisher->safe_psql('postgres', |
| 228 |
"INSERT INTO tab_upgraded1 VALUES (generate_series(1,50))"); |
| 229 |
$publisher->wait_for_catchup('regress_sub4'); |
| 230 |
|
| 231 |
# Change configuration to prepare a subscription table in init state |
| 232 |
$old_sub->append_conf('postgresql.conf', |
| 233 |
"max_logical_replication_workers = 0"); |
| 234 |
$old_sub->restart; |
| 235 |
|
| 236 |
# Setup another logical replication |
| 237 |
$publisher->safe_psql( |
| 238 |
'postgres', qq[ |
| 239 |
CREATE TABLE tab_upgraded2(id int); |
| 240 |
CREATE PUBLICATION regress_pub5 FOR TABLE tab_upgraded2; |
| 241 |
]); |
| 242 |
$old_sub->safe_psql( |
| 243 |
'postgres', qq[ |
| 244 |
CREATE TABLE tab_upgraded2(id int); |
| 245 |
CREATE SUBSCRIPTION regress_sub5 CONNECTION '$connstr' PUBLICATION regress_pub5; |
| 246 |
]); |
| 247 |
|
| 248 |
# The table tab_upgraded2 will be in the init state as the subscriber's |
| 249 |
# configuration for max_logical_replication_workers is set to 0. |
| 250 |
my $result = $old_sub->safe_psql('postgres', |
| 251 |
"SELECT count(1) = 1 FROM pg_subscription_rel WHERE srsubstate = 'i'"); |
| 252 |
is($result, qq(t), "Check that the table is in init state"); |
| 253 |
|
| 254 |
# Get the replication origin's remote_lsn of the old subscriber |
| 255 |
my $remote_lsn = $old_sub->safe_psql('postgres', |
| 256 |
"SELECT remote_lsn FROM pg_replication_origin_status os, pg_subscription s WHERE os.external_id = 'pg_' || s.oid AND s.subname = 'regress_sub4'" |
| 257 |
); |
| 258 |
# Have the subscription in disabled state before upgrade |
| 259 |
$old_sub->safe_psql('postgres', "ALTER SUBSCRIPTION regress_sub5 DISABLE"); |
| 260 |
|
| 261 |
my $tab_upgraded_oid = $old_sub->safe_psql('postgres', |
| 262 |
"SELECT oid FROM pg_class WHERE relname = 'tab_upgraded'"); |
| 263 |
my $tab_upgraded1_oid = $old_sub->safe_psql('postgres', |
| 264 |
"SELECT oid FROM pg_class WHERE relname = 'tab_upgraded1'"); |
| 265 |
my $tab_upgraded2_oid = $old_sub->safe_psql('postgres', |
| 266 |
"SELECT oid FROM pg_class WHERE relname = 'tab_upgraded2'"); |
| 267 |
|
| 268 |
$old_sub->stop; |
| 269 |
|
| 270 |
# Change configuration so that initial table sync does not get started |
| 271 |
# automatically |
| 272 |
$new_sub->append_conf('postgresql.conf', |
| 273 |
"max_logical_replication_workers = 0"); |
| 274 |
|
| 275 |
# ------------------------------------------------------ |
| 276 |
# Check that pg_upgrade is successful when all tables are in ready or in |
| 277 |
# init state (tab_upgraded and tab_upgraded1 tables are in ready state and |
| 278 |
# tab_upgraded2 table is in init state) along with retaining the replication |
| 279 |
# origin's remote lsn, subscription's running status, and failover option. |
| 280 |
# ------------------------------------------------------ |
| 281 |
command_ok( |
| 282 |
[ |
| 283 |
'pg_upgrade', |
| 284 |
'--no-sync', |
| 285 |
'--old-datadir' => $old_sub->data_dir, |
| 286 |
'--new-datadir' => $new_sub->data_dir, |
| 287 |
'--old-bindir' => $oldbindir, |
| 288 |
'--new-bindir' => $newbindir, |
| 289 |
'--socketdir' => $new_sub->host, |
| 290 |
'--old-port' => $old_sub->port, |
| 291 |
'--new-port' => $new_sub->port, |
| 292 |
$mode |
| 293 |
], |
| 294 |
'run of pg_upgrade for old instance when the subscription tables are in init/ready state' |
| 295 |
); |
| 296 |
ok( !-d $new_sub->data_dir . "/pg_upgrade_output.d", |
| 297 |
"pg_upgrade_output.d/ removed after successful pg_upgrade"); |
| 298 |
|
| 299 |
# ------------------------------------------------------ |
| 300 |
# Check that the data inserted to the publisher when the new subscriber is down |
| 301 |
# will be replicated once it is started. Also check that the old subscription |
| 302 |
# states and relations origins are all preserved. |
| 303 |
# ------------------------------------------------------ |
| 304 |
$publisher->safe_psql( |
| 305 |
'postgres', qq[ |
| 306 |
INSERT INTO tab_upgraded1 VALUES(51); |
| 307 |
INSERT INTO tab_upgraded2 VALUES(1); |
| 308 |
]); |
| 309 |
|
| 310 |
$new_sub->start; |
| 311 |
|
| 312 |
# The subscription's running status and failover option should be preserved |
| 313 |
# in the upgraded instance. So regress_sub4 should still have subenabled and |
| 314 |
# subfailover set to true, while regress_sub5 should have both set to false. |
| 315 |
$result = $new_sub->safe_psql('postgres', |
| 316 |
"SELECT subname, subenabled, subfailover FROM pg_subscription ORDER BY subname" |
| 317 |
); |
| 318 |
is( $result, qq(regress_sub4|t|t |
| 319 |
regress_sub5|f|f), |
| 320 |
"check that the subscription's running status and failover are preserved" |
| 321 |
); |
| 322 |
|
| 323 |
# Subscription relations should be preserved |
| 324 |
$result = $new_sub->safe_psql('postgres', |
| 325 |
"SELECT srrelid, srsubstate FROM pg_subscription_rel ORDER BY srrelid"); |
| 326 |
is( $result, qq($tab_upgraded_oid|r |
| 327 |
$tab_upgraded1_oid|r |
| 328 |
$tab_upgraded2_oid|i), |
| 329 |
"there should be 3 rows in pg_subscription_rel(representing tab_upgraded, tab_upgraded1 and tab_upgraded2)" |
| 330 |
); |
| 331 |
|
| 332 |
# The replication origin's remote_lsn should be preserved |
| 333 |
$sub_oid = $new_sub->safe_psql('postgres', |
| 334 |
"SELECT oid FROM pg_subscription WHERE subname = 'regress_sub4'"); |
| 335 |
$result = $new_sub->safe_psql('postgres', |
| 336 |
"SELECT remote_lsn FROM pg_replication_origin_status WHERE external_id = 'pg_' || $sub_oid" |
| 337 |
); |
| 338 |
is($result, qq($remote_lsn), "remote_lsn should have been preserved"); |
| 339 |
|
| 340 |
# Resume the initial sync and wait until all tables of subscription |
| 341 |
# 'regress_sub5' are synchronized |
| 342 |
$new_sub->append_conf('postgresql.conf', |
| 343 |
"max_logical_replication_workers = 10"); |
| 344 |
$new_sub->restart; |
| 345 |
$new_sub->safe_psql('postgres', "ALTER SUBSCRIPTION regress_sub5 ENABLE"); |
| 346 |
$new_sub->wait_for_subscription_sync($publisher, 'regress_sub5'); |
| 347 |
|
| 348 |
# wait for regress_sub4 to catchup as well |
| 349 |
$publisher->wait_for_catchup('regress_sub4'); |
| 350 |
|
| 351 |
# Rows on tab_upgraded1 and tab_upgraded2 should have been replicated |
| 352 |
$result = |
| 353 |
$new_sub->safe_psql('postgres', "SELECT count(*) FROM tab_upgraded1"); |
| 354 |
is($result, qq(51), "check replicated inserts on new subscriber"); |
| 355 |
$result = |
| 356 |
$new_sub->safe_psql('postgres', "SELECT count(*) FROM tab_upgraded2"); |
| 357 |
is($result, qq(1), |
| 358 |
"check the data is synced after enabling the subscription for the table that was in init state" |
| 359 |
); |
| 360 |
|
| 361 |
done_testing(); |