diff --git a/contrib/test_decoding/t/001_repl_stats.pl b/contrib/test_decoding/t/001_repl_stats.pl index daf03ff147..513cebf2ad 100644 --- a/contrib/test_decoding/t/001_repl_stats.pl +++ b/contrib/test_decoding/t/001_repl_stats.pl @@ -1,8 +1,7 @@ # Copyright (c) 2021-2022, PostgreSQL Global Development Group -# Test replication statistics data in pg_stat_replication_slots is sane after -# drop replication slot and restart. +# Test replication statistics data in pg_stat_replication_slots use strict; use warnings; use File::Path qw(rmtree); @@ -10,6 +9,9 @@ use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; +# Test if statistics data in pg_stat_replication_slots is sane after drop replication +# slot and restart. + # Test set-up my $node = PostgreSQL::Test::Cluster->new('test'); $node->init(allows_streaming => 'logical'); @@ -118,4 +120,78 @@ $node->safe_psql('postgres', # shutdown $node->stop; +# Test if resetting statistics data in pg_stat_replication_slots while decoding is +# ongoing works correctly. +$node = PostgreSQL::Test::Cluster->new('test2'); +$node->init(allows_streaming => 'logical'); +$node->start; + +$node->safe_psql('postgres', "CREATE TABLE test(i int)"); + +# The walsender process creates and acquires the replication slot. +my $pg_recvlogical = IPC::Run::start( + [ + 'pg_recvlogical', '-S', + 'regression_slot', '-d', + $node->connstr('postgres'), '--start', + '--create-slot', '--no-loop', + '-f', '-' + ]); + +# Wait for the replication slot to become active. +$node->poll_query_until('postgres', + "SELECT EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = 'regression_slot' AND active_pid IS NOT NULL)" +) or die "slot never became active"; + +# Reset the replication slot statistics. +$node->safe_psql('postgres', + "SELECT pg_stat_reset_replication_slot('regression_slot');"); +my $result = $node->safe_psql('postgres', + "SELECT * FROM pg_stat_replication_slots WHERE slot_name = 'regrssion_slot'" +); +is($result, "", "replication slot statistics are reset"); + +# Test if the walsender properly updates the statistics. +$node->safe_psql('postgres', "INSERT INTO test VALUES (1)"); +$node->poll_query_until('postgres', + "SELECT total_txns > 0 FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot'" +); + +# Teardown the node so the statistics is removed. +$pg_recvlogical->kill_kill; +$node->teardown_node; +$node->start; + +# Start pg_recvlogical again. +$pg_recvlogical = IPC::Run::start( + [ + 'pg_recvlogical', '-S', + 'regression_slot', '-d', + $node->connstr('postgres'), '--start', + '--no-loop', '-f', + '-' + ]); + +# Wait for the slot to become active. +$node->poll_query_until('postgres', + "SELECT EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = 'regression_slot' AND active_pid IS NOT NULL)" +) or die "slot never became active"; + +# Check if the replication slot statistics have been removed. +$result = $node->safe_psql('postgres', + "SELECT * FROM pg_stat_replication_slots WHERE slot_name = 'regrssion_slot'" +); +is($result, "", "replication slot statistics are removed"); + +# Test if the replication slot staistics continue to be accumulated even after +# statistics have been removed. +$node->safe_psql('postgres', "INSERT INTO test VALUES (1)"); +$node->poll_query_until('postgres', + "SELECT total_txns > 0 FROM pg_stat_replication_slots WHERE slot_name = 'regression_slot'" +); + +# shutdown +$pg_recvlogical->kill_kill; +$node->stop; + done_testing();