v10-0002-Added-tests-for-verification-of-logical-replicat.patch

text/x-patch

Filename: v10-0002-Added-tests-for-verification-of-logical-replicat.patch
Type: text/x-patch
Part: 1
Message: Re: Replication slot stats misgivings

Patch

Format: format-patch
Series: patch v10-0002
Subject: Added tests for verification of logical replication statistics.
File+
contrib/test_decoding/Makefile 2 0
contrib/test_decoding/t/001_repl_stats.pl 76 0
src/backend/catalog/system_views.sql 4 1
From 8e78a79588a38137bc8ba15daf75a4b417b1c86b Mon Sep 17 00:00:00 2001
From: vignesh <vignesh21@gmail.com>
Date: Mon, 5 Apr 2021 18:17:21 +0530
Subject: [PATCH v10 2/4] Added tests for verification of logical replication
 statistics.

Added tests for verification of logical replication statistics after
restart of server.
---
 contrib/test_decoding/Makefile            |  2 +
 contrib/test_decoding/t/001_repl_stats.pl | 76 +++++++++++++++++++++++
 src/backend/catalog/system_views.sql      |  5 +-
 3 files changed, 82 insertions(+), 1 deletion(-)
 create mode 100644 contrib/test_decoding/t/001_repl_stats.pl

diff --git a/contrib/test_decoding/Makefile b/contrib/test_decoding/Makefile
index c5e28ce5cc..9a31e0b879 100644
--- a/contrib/test_decoding/Makefile
+++ b/contrib/test_decoding/Makefile
@@ -17,6 +17,8 @@ ISOLATION_OPTS = --temp-config $(top_srcdir)/contrib/test_decoding/logical.conf
 # typical installcheck users do not have (e.g. buildfarm clients).
 NO_INSTALLCHECK = 1
 
+TAP_TESTS = 1
+
 ifdef USE_PGXS
 PG_CONFIG = pg_config
 PGXS := $(shell $(PG_CONFIG) --pgxs)
diff --git a/contrib/test_decoding/t/001_repl_stats.pl b/contrib/test_decoding/t/001_repl_stats.pl
new file mode 100644
index 0000000000..11b6cd9b9c
--- /dev/null
+++ b/contrib/test_decoding/t/001_repl_stats.pl
@@ -0,0 +1,76 @@
+# Test replication statistics data in pg_stat_replication_slots is sane after
+# drop replication slot and restart.
+use strict;
+use warnings;
+use PostgresNode;
+use TestLib;
+use Test::More tests => 1;
+
+# Test set-up
+my $node = get_new_node('test');
+$node->init(allows_streaming => 'logical');
+$node->append_conf('postgresql.conf', 'synchronous_commit = on');
+$node->start;
+
+# Create table.
+$node->safe_psql('postgres',
+        "CREATE TABLE test_repl_stat(col1 int)");
+
+# Create replication slots.
+$node->safe_psql(
+	'postgres', qq[
+	SELECT pg_create_logical_replication_slot('regression_slot1', 'test_decoding');
+	SELECT pg_create_logical_replication_slot('regression_slot2', 'test_decoding');
+	SELECT pg_create_logical_replication_slot('regression_slot3', 'test_decoding');
+	SELECT pg_create_logical_replication_slot('regression_slot4', 'test_decoding');
+]);
+
+# Insert some data.
+$node->safe_psql('postgres', "INSERT INTO test_repl_stat values(generate_series(1, 5));");
+
+$node->safe_psql(
+	'postgres', qq[
+	SELECT data FROM pg_logical_slot_get_changes('regression_slot1', NULL,
+	NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+	SELECT data FROM pg_logical_slot_get_changes('regression_slot2', NULL,
+	NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+	SELECT data FROM pg_logical_slot_get_changes('regression_slot3', NULL,
+	NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+	SELECT data FROM pg_logical_slot_get_changes('regression_slot4', NULL,
+	NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+]);
+
+# Wait for the statistics to be updated.
+$node->poll_query_until(
+	'postgres', qq[
+	SELECT count(slot_name) >= 4 FROM pg_stat_replication_slots
+	WHERE slot_name ~ 'regression_slot'
+	AND total_txns > 0 AND total_bytes > 0;
+]) or die "Timed out while waiting for statistics to be updated";
+
+# Test to drop one of the replication slot and verify replication statistics data is
+# fine after restart.
+$node->safe_psql('postgres', "SELECT pg_drop_replication_slot('regression_slot4')");
+
+$node->stop;
+$node->start;
+
+# Verify statistics data present in pg_stat_replication_slots are sane after
+# restart.
+my $result = $node->safe_psql('postgres',
+	"SELECT slot_name, total_txns > 0 AS total_txn,
+	total_bytes > 0 AS total_bytes FROM pg_stat_replication_slots
+	ORDER BY slot_name"
+);
+is($result, qq(regression_slot1|t|t
+regression_slot2|t|t
+regression_slot3|t|t), 'check replication statistics are updated');
+
+# cleanup
+$node->safe_psql('postgres', "DROP TABLE test_repl_stat");
+$node->safe_psql('postgres', "SELECT pg_drop_replication_slot('regression_slot1')");
+$node->safe_psql('postgres', "SELECT pg_drop_replication_slot('regression_slot2')");
+$node->safe_psql('postgres', "SELECT pg_drop_replication_slot('regression_slot3')");
+
+# shutdown
+$node->stop;
diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql
index 6d78b33590..9fdf445579 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -878,7 +878,10 @@ CREATE VIEW pg_stat_replication_slots AS
             s.total_txns,
             s.total_bytes,
             s.stats_reset
-    FROM pg_stat_get_replication_slots() AS s;
+    FROM pg_replication_slots as r,
+        LATERAL pg_stat_get_replication_slot(slot_name) as s
+    WHERE r.datoid IS NOT NULL; -- excluding physical slots
+
 
 CREATE VIEW pg_stat_slru AS
     SELECT
-- 
2.25.1