From a04479d4e4ae665727ed41b7b58799f2e0242bab Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddy@enterprisedb.com>
Date: Sat, 16 Jan 2021 11:45:24 +0530
Subject: [PATCH v4 2/2] Test behaviour of ALTER PUBLICATION ... DROP TABLE

---
 src/test/subscription/t/100_bugs.pl | 83 ++++++++++++++++++++++++++++-
 1 file changed, 82 insertions(+), 1 deletion(-)

diff --git a/src/test/subscription/t/100_bugs.pl b/src/test/subscription/t/100_bugs.pl
index d1e407aacb..ac9cd39de6 100644
--- a/src/test/subscription/t/100_bugs.pl
+++ b/src/test/subscription/t/100_bugs.pl
@@ -3,7 +3,7 @@ use strict;
 use warnings;
 use PostgresNode;
 use TestLib;
-use Test::More tests => 5;
+use Test::More tests => 8;
 
 # Bug #15114
 
@@ -153,3 +153,84 @@ is($node_twoways->safe_psql('d2', "SELECT count(f) FROM t"),
 	$rows * 2, "2x$rows rows in t");
 is($node_twoways->safe_psql('d2', "SELECT count(f) FROM t2"),
 	$rows * 2, "2x$rows rows in t2");
+
+# Test behaviour of ALTER PUBLICATION ... DROP TABLE
+
+# When publisher drops a table from publication, it should also stop sending
+# its changes to subscriber. We look at the subscriber whether it receives
+# the rows that are inserted to the table in the publisher after it is dropped
+# from the publication.
+
+# Create publisher node
+$node_publisher = get_new_node('publisher3');
+$node_publisher->init(allows_streaming => 'logical');
+$node_publisher->start;
+
+# Create subscriber node
+$node_subscriber = get_new_node('subscriber3');
+$node_subscriber->init(allows_streaming => 'logical');
+$node_subscriber->start;
+
+# Create some preexisting content on publisher
+$node_publisher->safe_psql('postgres',
+	"CREATE TABLE test_tbl1 (a int)");
+$node_publisher->safe_psql('postgres',
+	"INSERT INTO test_tbl1 VALUES (1)");
+
+# Create some preexisting content on subscriber
+$node_subscriber->safe_psql('postgres',
+	"CREATE TABLE test_tbl1 (a int)");
+
+# Setup logical replication
+$publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
+$node_publisher->safe_psql('postgres',
+	"CREATE PUBLICATION test_pub1 FOR TABLE test_tbl1");
+
+$node_subscriber->safe_psql('postgres',
+	"CREATE SUBSCRIPTION test_sub1 CONNECTION '$publisher_connstr' PUBLICATION test_pub1"
+);
+
+$node_publisher->wait_for_catchup('test_sub1');
+
+# Also wait for initial table sync to finish
+$synced_query =
+  "SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
+$node_subscriber->poll_query_until('postgres', $synced_query)
+  or die "Timed out while waiting for subscriber to synchronize data";
+
+my $result =
+  $node_subscriber->safe_psql('postgres',
+	"SELECT count(*) FROM test_tbl1");
+is($result, qq(1), 'check initial data was copied to subscriber');
+
+# Insert one more row
+$node_publisher->safe_psql('postgres',
+	"INSERT INTO test_tbl1 VALUES (2)");
+
+$node_publisher->wait_for_catchup('test_sub1');
+
+# Subscriber receives the inserted row
+$result =
+  $node_subscriber->safe_psql('postgres',
+	"SELECT count(*) FROM test_tbl1");
+is($result, qq(2), 'check replicated insert on subscriber');
+
+# Drop the table from publicaiton
+$node_publisher->safe_psql('postgres',
+	"ALTER PUBLICATION test_pub1 DROP TABLE test_tbl1");
+
+# Insert another row, but publisher does not send this row to subscriber
+$node_publisher->safe_psql('postgres',
+	"INSERT INTO test_tbl1 VALUES (3)");
+
+$node_publisher->wait_for_catchup('test_sub1');
+
+# Subscriber should not receive the inserted row after table is dropped from
+# publication, so row count remains the same.
+$result =
+  $node_subscriber->safe_psql('postgres',
+	"SELECT count(*) FROM test_tbl1");
+is($result, qq(2), 'check insert on subscriber after table is dropped from publication');
+
+$node_subscriber->stop;
+$node_publisher->stop;
-- 
2.25.1

