| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test for replication slot limit |
| 5 |
# Ensure that max_slot_wal_keep_size limits the number of WAL files to |
| 6 |
# be kept by replication slots. |
| 7 |
use strict; |
| 8 |
use warnings FATAL => 'all'; |
| 9 |
|
| 10 |
use PostgreSQL::Test::Utils; |
| 11 |
use PostgreSQL::Test::Cluster; |
| 12 |
use Test::More; |
| 13 |
use Time::HiRes qw(usleep); |
| 14 |
|
| 15 |
# Initialize primary node, setting wal-segsize to 1MB |
| 16 |
my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 17 |
$node_primary->init(allows_streaming => 1, extra => ['--wal-segsize=1']); |
| 18 |
$node_primary->append_conf( |
| 19 |
'postgresql.conf', qq( |
| 20 |
min_wal_size = 2MB |
| 21 |
max_wal_size = 4MB |
| 22 |
log_checkpoints = yes |
| 23 |
)); |
| 24 |
$node_primary->start; |
| 25 |
$node_primary->safe_psql('postgres', |
| 26 |
"SELECT pg_create_physical_replication_slot('rep1')"); |
| 27 |
|
| 28 |
# The slot state and remain should be null before the first connection |
| 29 |
my $result = $node_primary->safe_psql('postgres', |
| 30 |
"SELECT restart_lsn IS NULL, wal_status is NULL, safe_wal_size is NULL FROM pg_replication_slots WHERE slot_name = 'rep1'" |
| 31 |
); |
| 32 |
is($result, "t|t|t", 'check the state of non-reserved slot is "unknown"'); |
| 33 |
|
| 34 |
|
| 35 |
# Take backup |
| 36 |
my $backup_name = 'my_backup'; |
| 37 |
$node_primary->backup($backup_name); |
| 38 |
|
| 39 |
# Create a standby linking to it using the replication slot |
| 40 |
my $node_standby = PostgreSQL::Test::Cluster->new('standby_1'); |
| 41 |
$node_standby->init_from_backup($node_primary, $backup_name, |
| 42 |
has_streaming => 1); |
| 43 |
$node_standby->append_conf('postgresql.conf', "primary_slot_name = 'rep1'"); |
| 44 |
|
| 45 |
$node_standby->start; |
| 46 |
|
| 47 |
# Wait until standby has replayed enough data |
| 48 |
$node_primary->wait_for_catchup($node_standby); |
| 49 |
|
| 50 |
# Stop standby |
| 51 |
$node_standby->stop; |
| 52 |
|
| 53 |
# Preparation done, the slot is the state "reserved" now |
| 54 |
$result = $node_primary->safe_psql('postgres', |
| 55 |
"SELECT wal_status, safe_wal_size IS NULL FROM pg_replication_slots WHERE slot_name = 'rep1'" |
| 56 |
); |
| 57 |
is($result, "reserved|t", 'check the catching-up state'); |
| 58 |
|
| 59 |
# Advance WAL by five segments (= 5MB) on primary |
| 60 |
$node_primary->advance_wal(1); |
| 61 |
$node_primary->safe_psql('postgres', "CHECKPOINT;"); |
| 62 |
|
| 63 |
# The slot is always "safe" when fitting max_wal_size |
| 64 |
$result = $node_primary->safe_psql('postgres', |
| 65 |
"SELECT wal_status, safe_wal_size IS NULL FROM pg_replication_slots WHERE slot_name = 'rep1'" |
| 66 |
); |
| 67 |
is($result, "reserved|t", |
| 68 |
'check that it is safe if WAL fits in max_wal_size'); |
| 69 |
|
| 70 |
$node_primary->advance_wal(4); |
| 71 |
$node_primary->safe_psql('postgres', "CHECKPOINT;"); |
| 72 |
|
| 73 |
# The slot is always "safe" when max_slot_wal_keep_size is not set |
| 74 |
$result = $node_primary->safe_psql('postgres', |
| 75 |
"SELECT wal_status, safe_wal_size IS NULL FROM pg_replication_slots WHERE slot_name = 'rep1'" |
| 76 |
); |
| 77 |
is($result, "reserved|t", 'check that slot is working'); |
| 78 |
|
| 79 |
# The standby can reconnect to primary |
| 80 |
$node_standby->start; |
| 81 |
|
| 82 |
$node_primary->wait_for_catchup($node_standby); |
| 83 |
|
| 84 |
$node_standby->stop; |
| 85 |
|
| 86 |
# Set max_slot_wal_keep_size on primary |
| 87 |
my $max_slot_wal_keep_size_mb = 6; |
| 88 |
$node_primary->append_conf( |
| 89 |
'postgresql.conf', qq( |
| 90 |
max_slot_wal_keep_size = ${max_slot_wal_keep_size_mb}MB |
| 91 |
)); |
| 92 |
$node_primary->reload; |
| 93 |
|
| 94 |
# The slot is in safe state. |
| 95 |
|
| 96 |
$result = $node_primary->safe_psql('postgres', |
| 97 |
"SELECT wal_status FROM pg_replication_slots WHERE slot_name = 'rep1'"); |
| 98 |
is($result, "reserved", 'check that max_slot_wal_keep_size is working'); |
| 99 |
|
| 100 |
# Advance WAL again then checkpoint, reducing remain by 2 MB. |
| 101 |
$node_primary->advance_wal(2); |
| 102 |
$node_primary->safe_psql('postgres', "CHECKPOINT;"); |
| 103 |
|
| 104 |
# The slot is still working |
| 105 |
$result = $node_primary->safe_psql('postgres', |
| 106 |
"SELECT wal_status FROM pg_replication_slots WHERE slot_name = 'rep1'"); |
| 107 |
is($result, "reserved", |
| 108 |
'check that safe_wal_size gets close to the current LSN'); |
| 109 |
|
| 110 |
# The standby can reconnect to primary |
| 111 |
$node_standby->start; |
| 112 |
$node_primary->wait_for_catchup($node_standby); |
| 113 |
$node_standby->stop; |
| 114 |
|
| 115 |
# wal_keep_size overrides max_slot_wal_keep_size |
| 116 |
$result = $node_primary->safe_psql('postgres', |
| 117 |
"ALTER SYSTEM SET wal_keep_size to '8MB'; SELECT pg_reload_conf();"); |
| 118 |
# Advance WAL again then checkpoint, reducing remain by 6 MB. |
| 119 |
$node_primary->advance_wal(6); |
| 120 |
$result = $node_primary->safe_psql('postgres', |
| 121 |
"SELECT wal_status as remain FROM pg_replication_slots WHERE slot_name = 'rep1'" |
| 122 |
); |
| 123 |
is($result, "extended", |
| 124 |
'check that wal_keep_size overrides max_slot_wal_keep_size'); |
| 125 |
# restore wal_keep_size |
| 126 |
$result = $node_primary->safe_psql('postgres', |
| 127 |
"ALTER SYSTEM SET wal_keep_size to 0; SELECT pg_reload_conf();"); |
| 128 |
|
| 129 |
# The standby can reconnect to primary |
| 130 |
$node_standby->start; |
| 131 |
$node_primary->wait_for_catchup($node_standby); |
| 132 |
$node_standby->stop; |
| 133 |
|
| 134 |
# Advance WAL again without checkpoint, reducing remain by 6 MB. |
| 135 |
$node_primary->advance_wal(6); |
| 136 |
|
| 137 |
# Slot gets into 'reserved' state |
| 138 |
$result = $node_primary->safe_psql('postgres', |
| 139 |
"SELECT wal_status FROM pg_replication_slots WHERE slot_name = 'rep1'"); |
| 140 |
is($result, "extended", 'check that the slot state changes to "extended"'); |
| 141 |
|
| 142 |
# do checkpoint so that the next checkpoint runs too early |
| 143 |
$node_primary->safe_psql('postgres', "CHECKPOINT;"); |
| 144 |
|
| 145 |
# Advance WAL again without checkpoint; remain goes to 0. |
| 146 |
$node_primary->advance_wal(1); |
| 147 |
|
| 148 |
# Slot gets into 'unreserved' state and safe_wal_size is negative |
| 149 |
$result = $node_primary->safe_psql('postgres', |
| 150 |
"SELECT wal_status, safe_wal_size <= 0 FROM pg_replication_slots WHERE slot_name = 'rep1'" |
| 151 |
); |
| 152 |
is($result, "unreserved|t", |
| 153 |
'check that the slot state changes to "unreserved"'); |
| 154 |
|
| 155 |
# The standby still can connect to primary before a checkpoint |
| 156 |
$node_standby->start; |
| 157 |
|
| 158 |
$node_primary->wait_for_catchup($node_standby); |
| 159 |
|
| 160 |
$node_standby->stop; |
| 161 |
|
| 162 |
ok( !$node_standby->log_contains( |
| 163 |
"requested WAL segment [0-9A-F]+ has already been removed"), |
| 164 |
'check that required WAL segments are still available'); |
| 165 |
|
| 166 |
# Create one checkpoint, to improve stability of the next steps |
| 167 |
$node_primary->safe_psql('postgres', "CHECKPOINT;"); |
| 168 |
|
| 169 |
# Prevent other checkpoints from occurring while advancing WAL segments |
| 170 |
$node_primary->safe_psql('postgres', |
| 171 |
"ALTER SYSTEM SET max_wal_size='40MB'; SELECT pg_reload_conf()"); |
| 172 |
|
| 173 |
# Advance WAL again. The slot loses the oldest segment by the next checkpoint |
| 174 |
my $logstart = -s $node_primary->logfile; |
| 175 |
$node_primary->advance_wal(7); |
| 176 |
|
| 177 |
# Now create another checkpoint and wait until the WARNING is issued |
| 178 |
$node_primary->safe_psql('postgres', |
| 179 |
'ALTER SYSTEM RESET max_wal_size; SELECT pg_reload_conf()'); |
| 180 |
$node_primary->safe_psql('postgres', "CHECKPOINT;"); |
| 181 |
my $invalidated = 0; |
| 182 |
for (my $i = 0; $i < 10 * $PostgreSQL::Test::Utils::timeout_default; $i++) |
| 183 |
{ |
| 184 |
if ($node_primary->log_contains( |
| 185 |
'invalidating obsolete replication slot "rep1"', $logstart)) |
| 186 |
{ |
| 187 |
$invalidated = 1; |
| 188 |
last; |
| 189 |
} |
| 190 |
usleep(100_000); |
| 191 |
} |
| 192 |
ok($invalidated, 'check that slot invalidation has been logged'); |
| 193 |
|
| 194 |
$result = $node_primary->safe_psql( |
| 195 |
'postgres', |
| 196 |
qq[ |
| 197 |
SELECT slot_name, active, restart_lsn IS NULL, wal_status, safe_wal_size |
| 198 |
FROM pg_replication_slots WHERE slot_name = 'rep1']); |
| 199 |
is($result, "rep1|f|t|lost|", |
| 200 |
'check that the slot became inactive and the state "lost" persists'); |
| 201 |
|
| 202 |
# Wait until current checkpoint ends |
| 203 |
my $checkpoint_ended = 0; |
| 204 |
for (my $i = 0; $i < 10 * $PostgreSQL::Test::Utils::timeout_default; $i++) |
| 205 |
{ |
| 206 |
if ($node_primary->log_contains("checkpoint complete: ", $logstart)) |
| 207 |
{ |
| 208 |
$checkpoint_ended = 1; |
| 209 |
last; |
| 210 |
} |
| 211 |
usleep(100_000); |
| 212 |
} |
| 213 |
ok($checkpoint_ended, 'waited for checkpoint to end'); |
| 214 |
|
| 215 |
# The invalidated slot shouldn't keep the old-segment horizon back; |
| 216 |
# see bug #17103: https://postgr.es/m/[email protected] |
| 217 |
# Test for this by creating a new slot and comparing its restart LSN |
| 218 |
# to the oldest existing file. |
| 219 |
my $redoseg = $node_primary->safe_psql('postgres', |
| 220 |
"SELECT pg_walfile_name(lsn) FROM pg_create_physical_replication_slot('s2', true)" |
| 221 |
); |
| 222 |
my $oldestseg = $node_primary->safe_psql('postgres', |
| 223 |
"SELECT pg_ls_dir AS f FROM pg_ls_dir('pg_wal') WHERE pg_ls_dir ~ '^[0-9A-F]{24}\$' ORDER BY 1 LIMIT 1" |
| 224 |
); |
| 225 |
$node_primary->safe_psql('postgres', |
| 226 |
qq[SELECT pg_drop_replication_slot('s2')]); |
| 227 |
is($oldestseg, $redoseg, "check that segments have been removed"); |
| 228 |
|
| 229 |
# The standby no longer can connect to the primary |
| 230 |
$logstart = -s $node_standby->logfile; |
| 231 |
$node_standby->start; |
| 232 |
|
| 233 |
my $failed = 0; |
| 234 |
for (my $i = 0; $i < 10 * $PostgreSQL::Test::Utils::timeout_default; $i++) |
| 235 |
{ |
| 236 |
if ($node_standby->log_contains( |
| 237 |
"This replication slot has been invalidated due to \"wal_removed\".", |
| 238 |
$logstart)) |
| 239 |
{ |
| 240 |
$failed = 1; |
| 241 |
last; |
| 242 |
} |
| 243 |
usleep(100_000); |
| 244 |
} |
| 245 |
ok($failed, 'check that replication has been broken'); |
| 246 |
|
| 247 |
$node_primary->stop; |
| 248 |
$node_standby->stop; |
| 249 |
|
| 250 |
my $node_primary2 = PostgreSQL::Test::Cluster->new('primary2'); |
| 251 |
$node_primary2->init(allows_streaming => 1); |
| 252 |
$node_primary2->append_conf( |
| 253 |
'postgresql.conf', qq( |
| 254 |
min_wal_size = 32MB |
| 255 |
max_wal_size = 32MB |
| 256 |
log_checkpoints = yes |
| 257 |
)); |
| 258 |
$node_primary2->start; |
| 259 |
$node_primary2->safe_psql('postgres', |
| 260 |
"SELECT pg_create_physical_replication_slot('rep1')"); |
| 261 |
$backup_name = 'my_backup2'; |
| 262 |
$node_primary2->backup($backup_name); |
| 263 |
|
| 264 |
$node_primary2->stop; |
| 265 |
$node_primary2->append_conf( |
| 266 |
'postgresql.conf', qq( |
| 267 |
max_slot_wal_keep_size = 0 |
| 268 |
)); |
| 269 |
$node_primary2->start; |
| 270 |
|
| 271 |
$node_standby = PostgreSQL::Test::Cluster->new('standby_2'); |
| 272 |
$node_standby->init_from_backup($node_primary2, $backup_name, |
| 273 |
has_streaming => 1); |
| 274 |
$node_standby->append_conf('postgresql.conf', "primary_slot_name = 'rep1'"); |
| 275 |
$node_standby->start; |
| 276 |
$node_primary2->advance_wal(1); |
| 277 |
$result = $node_primary2->safe_psql( |
| 278 |
'postgres', |
| 279 |
"CHECKPOINT; SELECT 'finished';", |
| 280 |
timeout => $PostgreSQL::Test::Utils::timeout_default); |
| 281 |
is($result, 'finished', 'check if checkpoint command is not blocked'); |
| 282 |
|
| 283 |
$node_primary2->stop; |
| 284 |
$node_standby->stop; |
| 285 |
|
| 286 |
# The next test depends on Perl's `kill`, which apparently is not |
| 287 |
# portable to Windows. (It would be nice to use Test::More's `subtest`, |
| 288 |
# but that's not in the ancient version we require.) |
| 289 |
if ($PostgreSQL::Test::Utils::windows_os) |
| 290 |
{ |
| 291 |
done_testing(); |
| 292 |
exit; |
| 293 |
} |
| 294 |
|
| 295 |
# Get a slot terminated while the walsender is active |
| 296 |
# We do this by sending SIGSTOP to the walsender. Skip this on Windows. |
| 297 |
my $node_primary3 = PostgreSQL::Test::Cluster->new('primary3'); |
| 298 |
$node_primary3->init(allows_streaming => 1, extra => ['--wal-segsize=1']); |
| 299 |
$node_primary3->append_conf( |
| 300 |
'postgresql.conf', qq( |
| 301 |
min_wal_size = 2MB |
| 302 |
max_wal_size = 2MB |
| 303 |
log_checkpoints = yes |
| 304 |
max_slot_wal_keep_size = 1MB |
| 305 |
)); |
| 306 |
$node_primary3->start; |
| 307 |
$node_primary3->safe_psql('postgres', |
| 308 |
"SELECT pg_create_physical_replication_slot('rep3')"); |
| 309 |
# Take backup |
| 310 |
$backup_name = 'my_backup'; |
| 311 |
$node_primary3->backup($backup_name); |
| 312 |
# Create standby |
| 313 |
my $node_standby3 = PostgreSQL::Test::Cluster->new('standby_3'); |
| 314 |
$node_standby3->init_from_backup($node_primary3, $backup_name, |
| 315 |
has_streaming => 1); |
| 316 |
$node_standby3->append_conf('postgresql.conf', "primary_slot_name = 'rep3'"); |
| 317 |
$node_standby3->start; |
| 318 |
$node_primary3->wait_for_catchup($node_standby3); |
| 319 |
|
| 320 |
my $senderpid; |
| 321 |
|
| 322 |
# We've seen occasional cases where multiple walsender pids are still active |
| 323 |
# at this point, apparently just due to process shutdown being slow. To avoid |
| 324 |
# spurious failures, retry a couple times. |
| 325 |
my $i = 0; |
| 326 |
while (1) |
| 327 |
{ |
| 328 |
my ($stdout, $stderr); |
| 329 |
|
| 330 |
$senderpid = $node_primary3->safe_psql('postgres', |
| 331 |
"SELECT pid FROM pg_stat_activity WHERE backend_type = 'walsender'"); |
| 332 |
|
| 333 |
last if $senderpid =~ qr/^[0-9]+$/; |
| 334 |
|
| 335 |
diag "multiple walsenders active in iteration $i"; |
| 336 |
|
| 337 |
# show information about all active connections |
| 338 |
$node_primary3->psql( |
| 339 |
'postgres', |
| 340 |
"\\a\\t\nSELECT * FROM pg_stat_activity", |
| 341 |
stdout => \$stdout, |
| 342 |
stderr => \$stderr); |
| 343 |
diag $stdout, $stderr; |
| 344 |
|
| 345 |
if ($i++ == 10 * $PostgreSQL::Test::Utils::timeout_default) |
| 346 |
{ |
| 347 |
# An immediate shutdown may hide evidence of a locking bug. If |
| 348 |
# retrying didn't resolve the issue, shut down in fast mode. |
| 349 |
$node_primary3->stop('fast'); |
| 350 |
$node_standby3->stop('fast'); |
| 351 |
die "could not determine walsender pid, can't continue"; |
| 352 |
} |
| 353 |
|
| 354 |
usleep(100_000); |
| 355 |
} |
| 356 |
|
| 357 |
like($senderpid, qr/^[0-9]+$/, "have walsender pid $senderpid"); |
| 358 |
|
| 359 |
my $receiverpid = $node_standby3->safe_psql('postgres', |
| 360 |
"SELECT pid FROM pg_stat_activity WHERE backend_type = 'walreceiver'"); |
| 361 |
like($receiverpid, qr/^[0-9]+$/, "have walreceiver pid $receiverpid"); |
| 362 |
|
| 363 |
$logstart = -s $node_primary3->logfile; |
| 364 |
# freeze walsender and walreceiver. Slot will still be active, but walreceiver |
| 365 |
# won't get anything anymore. |
| 366 |
kill 'STOP', $senderpid, $receiverpid; |
| 367 |
$node_primary3->advance_wal(2); |
| 368 |
|
| 369 |
my $msg_logged = 0; |
| 370 |
my $max_attempts = $PostgreSQL::Test::Utils::timeout_default; |
| 371 |
while ($max_attempts-- >= 0) |
| 372 |
{ |
| 373 |
if ($node_primary3->log_contains( |
| 374 |
"terminating process $senderpid to release replication slot \"rep3\"", |
| 375 |
$logstart)) |
| 376 |
{ |
| 377 |
$msg_logged = 1; |
| 378 |
last; |
| 379 |
} |
| 380 |
sleep 1; |
| 381 |
} |
| 382 |
ok($msg_logged, "walsender termination logged"); |
| 383 |
|
| 384 |
# Now let the walsender continue; slot should be killed now. |
| 385 |
# (Must not let walreceiver run yet; otherwise the standby could start another |
| 386 |
# one before the slot can be killed) |
| 387 |
kill 'CONT', $senderpid; |
| 388 |
$node_primary3->poll_query_until('postgres', |
| 389 |
"SELECT wal_status FROM pg_replication_slots WHERE slot_name = 'rep3'", |
| 390 |
"lost") |
| 391 |
or die "timed out waiting for slot to be lost"; |
| 392 |
|
| 393 |
$msg_logged = 0; |
| 394 |
$max_attempts = $PostgreSQL::Test::Utils::timeout_default; |
| 395 |
while ($max_attempts-- >= 0) |
| 396 |
{ |
| 397 |
if ($node_primary3->log_contains( |
| 398 |
'invalidating obsolete replication slot "rep3"', $logstart)) |
| 399 |
{ |
| 400 |
$msg_logged = 1; |
| 401 |
last; |
| 402 |
} |
| 403 |
sleep 1; |
| 404 |
} |
| 405 |
ok($msg_logged, "slot invalidation logged"); |
| 406 |
|
| 407 |
# Now let the walreceiver continue, so that the node can be stopped cleanly |
| 408 |
kill 'CONT', $receiverpid; |
| 409 |
|
| 410 |
$node_primary3->stop; |
| 411 |
$node_standby3->stop; |
| 412 |
|
| 413 |
# ============================================================================= |
| 414 |
# Testcase start: Check inactive_since property of the streaming standby's slot |
| 415 |
# |
| 416 |
|
| 417 |
# Initialize primary node |
| 418 |
my $primary4 = PostgreSQL::Test::Cluster->new('primary4'); |
| 419 |
$primary4->init(allows_streaming => 'logical'); |
| 420 |
$primary4->start; |
| 421 |
|
| 422 |
# Take backup |
| 423 |
$backup_name = 'my_backup4'; |
| 424 |
$primary4->backup($backup_name); |
| 425 |
|
| 426 |
# Create a standby linking to the primary using the replication slot |
| 427 |
my $standby4 = PostgreSQL::Test::Cluster->new('standby4'); |
| 428 |
$standby4->init_from_backup($primary4, $backup_name, has_streaming => 1); |
| 429 |
|
| 430 |
my $sb4_slot = 'sb4_slot'; |
| 431 |
$standby4->append_conf('postgresql.conf', "primary_slot_name = '$sb4_slot'"); |
| 432 |
|
| 433 |
my $slot_creation_time = $primary4->safe_psql( |
| 434 |
'postgres', qq[ |
| 435 |
SELECT current_timestamp; |
| 436 |
]); |
| 437 |
|
| 438 |
$primary4->safe_psql( |
| 439 |
'postgres', qq[ |
| 440 |
SELECT pg_create_physical_replication_slot(slot_name := '$sb4_slot'); |
| 441 |
]); |
| 442 |
|
| 443 |
# Get inactive_since value after the slot's creation. Note that the slot is |
| 444 |
# still inactive till it's used by the standby below. |
| 445 |
my $inactive_since = |
| 446 |
$primary4->validate_slot_inactive_since($sb4_slot, $slot_creation_time); |
| 447 |
|
| 448 |
$standby4->start; |
| 449 |
|
| 450 |
# Wait until standby has replayed enough data |
| 451 |
$primary4->wait_for_catchup($standby4); |
| 452 |
|
| 453 |
# Now the slot is active so inactive_since value must be NULL |
| 454 |
is( $primary4->safe_psql( |
| 455 |
'postgres', |
| 456 |
qq[SELECT inactive_since IS NULL FROM pg_replication_slots WHERE slot_name = '$sb4_slot';] |
| 457 |
), |
| 458 |
't', |
| 459 |
'last inactive time for an active physical slot is NULL'); |
| 460 |
|
| 461 |
# Stop the standby to check its inactive_since value is updated |
| 462 |
$standby4->stop; |
| 463 |
|
| 464 |
# Let's restart the primary so that the inactive_since is set upon loading the |
| 465 |
# slot from the disk. |
| 466 |
$primary4->restart; |
| 467 |
|
| 468 |
is( $primary4->safe_psql( |
| 469 |
'postgres', |
| 470 |
qq[SELECT inactive_since > '$inactive_since'::timestamptz FROM pg_replication_slots WHERE slot_name = '$sb4_slot' AND inactive_since IS NOT NULL;] |
| 471 |
), |
| 472 |
't', |
| 473 |
'last inactive time for an inactive physical slot is updated correctly'); |
| 474 |
|
| 475 |
$standby4->stop; |
| 476 |
|
| 477 |
# Testcase end: Check inactive_since property of the streaming standby's slot |
| 478 |
# ============================================================================= |
| 479 |
|
| 480 |
# ============================================================================= |
| 481 |
# Testcase start: Check inactive_since property of the logical subscriber's slot |
| 482 |
my $publisher4 = $primary4; |
| 483 |
|
| 484 |
# Create subscriber node |
| 485 |
my $subscriber4 = PostgreSQL::Test::Cluster->new('subscriber4'); |
| 486 |
$subscriber4->init; |
| 487 |
|
| 488 |
# Setup logical replication |
| 489 |
my $publisher4_connstr = $publisher4->connstr . ' dbname=postgres'; |
| 490 |
$publisher4->safe_psql('postgres', "CREATE PUBLICATION pub FOR ALL TABLES"); |
| 491 |
|
| 492 |
$slot_creation_time = $publisher4->safe_psql( |
| 493 |
'postgres', qq[ |
| 494 |
SELECT current_timestamp; |
| 495 |
]); |
| 496 |
|
| 497 |
my $lsub4_slot = 'lsub4_slot'; |
| 498 |
$publisher4->safe_psql('postgres', |
| 499 |
"SELECT pg_create_logical_replication_slot(slot_name := '$lsub4_slot', plugin := 'pgoutput');" |
| 500 |
); |
| 501 |
|
| 502 |
# Get inactive_since value after the slot's creation. Note that the slot is |
| 503 |
# still inactive till it's used by the subscriber below. |
| 504 |
$inactive_since = |
| 505 |
$publisher4->validate_slot_inactive_since($lsub4_slot, $slot_creation_time); |
| 506 |
|
| 507 |
$subscriber4->start; |
| 508 |
$subscriber4->safe_psql('postgres', |
| 509 |
"CREATE SUBSCRIPTION sub CONNECTION '$publisher4_connstr' PUBLICATION pub WITH (slot_name = '$lsub4_slot', create_slot = false)" |
| 510 |
); |
| 511 |
|
| 512 |
# Wait until subscriber has caught up |
| 513 |
$subscriber4->wait_for_subscription_sync($publisher4, 'sub'); |
| 514 |
|
| 515 |
# Now the slot is active so inactive_since value must be NULL |
| 516 |
is( $publisher4->safe_psql( |
| 517 |
'postgres', |
| 518 |
qq[SELECT inactive_since IS NULL FROM pg_replication_slots WHERE slot_name = '$lsub4_slot';] |
| 519 |
), |
| 520 |
't', |
| 521 |
'last inactive time for an active logical slot is NULL'); |
| 522 |
|
| 523 |
# Stop the subscriber to check its inactive_since value is updated |
| 524 |
$subscriber4->stop; |
| 525 |
|
| 526 |
# Let's restart the publisher so that the inactive_since is set upon |
| 527 |
# loading the slot from the disk. |
| 528 |
$publisher4->restart; |
| 529 |
|
| 530 |
is( $publisher4->safe_psql( |
| 531 |
'postgres', |
| 532 |
qq[SELECT inactive_since > '$inactive_since'::timestamptz FROM pg_replication_slots WHERE slot_name = '$lsub4_slot' AND inactive_since IS NOT NULL;] |
| 533 |
), |
| 534 |
't', |
| 535 |
'last inactive time for an inactive logical slot is updated correctly'); |
| 536 |
|
| 537 |
# Testcase end: Check inactive_since property of the logical subscriber's slot |
| 538 |
# ============================================================================= |
| 539 |
|
| 540 |
$publisher4->stop; |
| 541 |
$subscriber4->stop; |
| 542 |
|
| 543 |
done_testing(); |