| 1 |
# Copyright (c) 2023-2025, PostgreSQL Global Development Group |
| 2 |
|
| 3 |
# Tests for upgrading logical replication slots |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
|
| 8 |
use File::Find qw(find); |
| 9 |
|
| 10 |
use PostgreSQL::Test::Cluster; |
| 11 |
use PostgreSQL::Test::Utils; |
| 12 |
use Test::More; |
| 13 |
|
| 14 |
# Can be changed to test the other modes |
| 15 |
my $mode = $ENV{PG_TEST_PG_UPGRADE_MODE} || '--copy'; |
| 16 |
|
| 17 |
# Initialize old cluster |
| 18 |
my $oldpub = PostgreSQL::Test::Cluster->new('oldpub'); |
| 19 |
$oldpub->init(allows_streaming => 'logical'); |
| 20 |
$oldpub->append_conf('postgresql.conf', 'autovacuum = off'); |
| 21 |
|
| 22 |
# Initialize new cluster |
| 23 |
my $newpub = PostgreSQL::Test::Cluster->new('newpub'); |
| 24 |
$newpub->init(allows_streaming => 'logical'); |
| 25 |
|
| 26 |
# During upgrade, when pg_restore performs CREATE DATABASE, bgwriter or |
| 27 |
# checkpointer may flush buffers and hold a file handle for the system table. |
| 28 |
# So, if later due to some reason we need to re-create the file with the same |
| 29 |
# name like a TRUNCATE command on the same table, then the command will fail |
| 30 |
# if OS (such as older Windows versions) doesn't remove an unlinked file |
| 31 |
# completely till it is open. The probability of seeing this behavior is |
| 32 |
# higher in this test because we use wal_level as logical via |
| 33 |
# allows_streaming => 'logical' which in turn set shared_buffers as 1MB. |
| 34 |
$newpub->append_conf( |
| 35 |
'postgresql.conf', q{ |
| 36 |
bgwriter_lru_maxpages = 0 |
| 37 |
checkpoint_timeout = 1h |
| 38 |
}); |
| 39 |
|
| 40 |
# Setup a common pg_upgrade command to be used by all the test cases |
| 41 |
my @pg_upgrade_cmd = ( |
| 42 |
'pg_upgrade', '--no-sync', |
| 43 |
'--old-datadir' => $oldpub->data_dir, |
| 44 |
'--new-datadir' => $newpub->data_dir, |
| 45 |
'--old-bindir' => $oldpub->config_data('--bindir'), |
| 46 |
'--new-bindir' => $newpub->config_data('--bindir'), |
| 47 |
'--socketdir' => $newpub->host, |
| 48 |
'--old-port' => $oldpub->port, |
| 49 |
'--new-port' => $newpub->port, |
| 50 |
$mode); |
| 51 |
|
| 52 |
# In a VPATH build, we'll be started in the source directory, but we want |
| 53 |
# to run pg_upgrade in the build directory so that any files generated finish |
| 54 |
# in it, like delete_old_cluster.{sh,bat}. |
| 55 |
chdir ${PostgreSQL::Test::Utils::tmp_check}; |
| 56 |
|
| 57 |
# ------------------------------ |
| 58 |
# TEST: Confirm pg_upgrade fails when the new cluster has wrong GUC values |
| 59 |
|
| 60 |
# Preparations for the subsequent test: |
| 61 |
# 1. Create two slots on the old cluster |
| 62 |
$oldpub->start; |
| 63 |
$oldpub->safe_psql( |
| 64 |
'postgres', qq[ |
| 65 |
SELECT pg_create_logical_replication_slot('test_slot1', 'pgoutput'); |
| 66 |
SELECT pg_create_logical_replication_slot('test_slot2', 'test_decoding'); |
| 67 |
]); |
| 68 |
$oldpub->stop(); |
| 69 |
|
| 70 |
# 2. Set 'max_replication_slots' to be less than the number of slots (2) |
| 71 |
# present on the old cluster. |
| 72 |
$newpub->append_conf('postgresql.conf', "max_replication_slots = 1"); |
| 73 |
|
| 74 |
# pg_upgrade will fail because the new cluster has insufficient |
| 75 |
# max_replication_slots |
| 76 |
command_checks_all( |
| 77 |
[@pg_upgrade_cmd], |
| 78 |
1, |
| 79 |
[ |
| 80 |
qr/"max_replication_slots" \(1\) must be greater than or equal to the number of logical replication slots \(2\) on the old cluster/ |
| 81 |
], |
| 82 |
[qr//], |
| 83 |
'run of pg_upgrade where the new cluster has insufficient "max_replication_slots"' |
| 84 |
); |
| 85 |
ok(-d $newpub->data_dir . "/pg_upgrade_output.d", |
| 86 |
"pg_upgrade_output.d/ not removed after pg_upgrade failure"); |
| 87 |
|
| 88 |
# Set 'max_replication_slots' to match the number of slots (2) present on the |
| 89 |
# old cluster. Both slots will be used for subsequent tests. |
| 90 |
$newpub->append_conf('postgresql.conf', "max_replication_slots = 2"); |
| 91 |
|
| 92 |
# ------------------------------ |
| 93 |
# TEST: Confirm pg_upgrade fails when slot plugins are prohibited by the new cluster |
| 94 |
|
| 95 |
$newpub->append_conf('postgresql.conf', |
| 96 |
"output_plugin_libraries = 'pgoutput'"); |
| 97 |
|
| 98 |
command_checks_all( |
| 99 |
[@pg_upgrade_cmd], |
| 100 |
1, |
| 101 |
[ |
| 102 |
qr/Your installation contains logical replication slots with plugins/, |
| 103 |
qr/that are not allowed by the new cluster's output_plugin_libraries/, |
| 104 |
], |
| 105 |
[qr//], |
| 106 |
'run of pg_upgrade where the old cluster has untrusted output plugins'); |
| 107 |
|
| 108 |
my $slots_filename; |
| 109 |
|
| 110 |
# Find a txt file that contains a list of logical replication slots that cannot |
| 111 |
# be upgraded. We cannot predict the file's path because the output directory |
| 112 |
# contains a milliseconds timestamp. File::Find::find must be used. |
| 113 |
find( |
| 114 |
sub { |
| 115 |
if ($File::Find::name =~ m/disallowed_output_plugins\.txt/) |
| 116 |
{ |
| 117 |
$slots_filename = $File::Find::name; |
| 118 |
} |
| 119 |
}, |
| 120 |
$newpub->data_dir . "/pg_upgrade_output.d"); |
| 121 |
|
| 122 |
# Check the report. |
| 123 |
my $content = slurp_file($slots_filename); |
| 124 |
like( |
| 125 |
$content, |
| 126 |
qr/The slot "test_slot2" uses plugin "test_decoding"/m, |
| 127 |
'the previous test failed due to prohibited plugins'); |
| 128 |
unlike($content, qr/test_slot1/m, 'allowed plugin is not reported'); |
| 129 |
|
| 130 |
# Fix things for the next tests. |
| 131 |
$newpub->append_conf('postgresql.conf', |
| 132 |
"output_plugin_libraries = 'pgoutput, test_decoding'"); |
| 133 |
|
| 134 |
|
| 135 |
# ------------------------------ |
| 136 |
# TEST: Confirm pg_upgrade fails when the slot still has unconsumed WAL records |
| 137 |
|
| 138 |
# Preparations for the subsequent test: |
| 139 |
# 1. Generate extra WAL records. At this point neither test_slot1 nor |
| 140 |
# test_slot2 has consumed them. |
| 141 |
# |
| 142 |
# 2. Advance the slot test_slot2 up to the current WAL location, but test_slot1 |
| 143 |
# still has unconsumed WAL records. |
| 144 |
# |
| 145 |
# 3. Emit a non-transactional message. This will cause test_slot2 to detect the |
| 146 |
# unconsumed WAL record. |
| 147 |
$oldpub->start; |
| 148 |
$oldpub->safe_psql( |
| 149 |
'postgres', qq[ |
| 150 |
CREATE TABLE tbl AS SELECT generate_series(1, 10) AS a; |
| 151 |
SELECT pg_replication_slot_advance('test_slot2', pg_current_wal_lsn()); |
| 152 |
SELECT count(*) FROM pg_logical_emit_message('false', 'prefix', 'This is a non-transactional message'); |
| 153 |
]); |
| 154 |
$oldpub->stop; |
| 155 |
|
| 156 |
# pg_upgrade will fail because there are slots still having unconsumed WAL |
| 157 |
# records |
| 158 |
command_checks_all( |
| 159 |
[@pg_upgrade_cmd], |
| 160 |
1, |
| 161 |
[ |
| 162 |
qr/Your installation contains logical replication slots that cannot be upgraded./ |
| 163 |
], |
| 164 |
[qr//], |
| 165 |
'run of pg_upgrade of old cluster with slots having unconsumed WAL records' |
| 166 |
); |
| 167 |
|
| 168 |
# Verify the reason why the logical replication slot cannot be upgraded |
| 169 |
find( |
| 170 |
sub { |
| 171 |
if ($File::Find::name =~ m/invalid_logical_slots\.txt/) |
| 172 |
{ |
| 173 |
$slots_filename = $File::Find::name; |
| 174 |
} |
| 175 |
}, |
| 176 |
$newpub->data_dir . "/pg_upgrade_output.d"); |
| 177 |
|
| 178 |
# Check the file content. Both slots should be reporting that they have |
| 179 |
# unconsumed WAL records. |
| 180 |
like( |
| 181 |
slurp_file($slots_filename), |
| 182 |
qr/The slot \"test_slot1\" has not consumed the WAL yet/m, |
| 183 |
'the previous test failed due to unconsumed WALs'); |
| 184 |
like( |
| 185 |
slurp_file($slots_filename), |
| 186 |
qr/The slot \"test_slot2\" has not consumed the WAL yet/m, |
| 187 |
'the previous test failed due to unconsumed WALs'); |
| 188 |
|
| 189 |
|
| 190 |
# ------------------------------ |
| 191 |
# TEST: Successful upgrade |
| 192 |
|
| 193 |
# Preparations for the subsequent test: |
| 194 |
# 1. Setup logical replication (first, cleanup slots from the previous tests) |
| 195 |
my $old_connstr = $oldpub->connstr . ' dbname=postgres'; |
| 196 |
|
| 197 |
$oldpub->start; |
| 198 |
$oldpub->safe_psql( |
| 199 |
'postgres', qq[ |
| 200 |
SELECT * FROM pg_drop_replication_slot('test_slot1'); |
| 201 |
SELECT * FROM pg_drop_replication_slot('test_slot2'); |
| 202 |
CREATE PUBLICATION regress_pub FOR ALL TABLES; |
| 203 |
]); |
| 204 |
|
| 205 |
# Initialize subscriber cluster |
| 206 |
my $sub = PostgreSQL::Test::Cluster->new('sub'); |
| 207 |
$sub->init(); |
| 208 |
|
| 209 |
$sub->start; |
| 210 |
$sub->safe_psql( |
| 211 |
'postgres', qq[ |
| 212 |
CREATE TABLE tbl (a int); |
| 213 |
CREATE SUBSCRIPTION regress_sub CONNECTION '$old_connstr' PUBLICATION regress_pub WITH (two_phase = 'true', failover = 'true') |
| 214 |
]); |
| 215 |
$sub->wait_for_subscription_sync($oldpub, 'regress_sub'); |
| 216 |
|
| 217 |
# Also wait for two-phase to be enabled |
| 218 |
my $twophase_query = |
| 219 |
"SELECT count(1) = 0 FROM pg_subscription WHERE subtwophasestate NOT IN ('e');"; |
| 220 |
$sub->poll_query_until('postgres', $twophase_query) |
| 221 |
or die "Timed out while waiting for subscriber to enable twophase"; |
| 222 |
|
| 223 |
# 2. Temporarily disable the subscription |
| 224 |
$sub->safe_psql('postgres', "ALTER SUBSCRIPTION regress_sub DISABLE"); |
| 225 |
$oldpub->stop; |
| 226 |
|
| 227 |
# pg_upgrade should be successful |
| 228 |
command_ok([@pg_upgrade_cmd], 'run of pg_upgrade of old cluster'); |
| 229 |
|
| 230 |
# Check that the slot 'regress_sub' has migrated to the new cluster |
| 231 |
$newpub->start; |
| 232 |
my $result = $newpub->safe_psql('postgres', |
| 233 |
"SELECT slot_name, two_phase, failover FROM pg_replication_slots"); |
| 234 |
is($result, qq(regress_sub|t|t), 'check the slot exists on new cluster'); |
| 235 |
|
| 236 |
# Update the connection |
| 237 |
my $new_connstr = $newpub->connstr . ' dbname=postgres'; |
| 238 |
$sub->safe_psql( |
| 239 |
'postgres', qq[ |
| 240 |
ALTER SUBSCRIPTION regress_sub CONNECTION '$new_connstr'; |
| 241 |
ALTER SUBSCRIPTION regress_sub ENABLE; |
| 242 |
]); |
| 243 |
|
| 244 |
# Check whether changes on the new publisher get replicated to the subscriber |
| 245 |
$newpub->safe_psql('postgres', |
| 246 |
"INSERT INTO tbl VALUES (generate_series(11, 20))"); |
| 247 |
$newpub->wait_for_catchup('regress_sub'); |
| 248 |
$result = $sub->safe_psql('postgres', "SELECT count(*) FROM tbl"); |
| 249 |
is($result, qq(20), 'check changes are replicated to the sub'); |
| 250 |
|
| 251 |
# Clean up |
| 252 |
$sub->stop(); |
| 253 |
$newpub->stop(); |
| 254 |
|
| 255 |
done_testing(); |