From 4a7bf67f53d5a714c3bef159b05896936b94ab4b Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Thu, 1 Oct 2020 20:53:04 -0300
Subject: [PATCH] Restore double command completion tags

---
 src/backend/replication/walsender.c |  3 ++-
 src/test/subscription/t/100_bugs.pl | 29 ++++++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c
index 7c9d1b67df..f6d5875b7d 100644
--- a/src/backend/replication/walsender.c
+++ b/src/backend/replication/walsender.c
@@ -1656,7 +1656,8 @@ exec_replication_command(const char *cmd_string)
 				else
 					StartLogicalReplication(cmd);
 
-				/* callees already sent their own completion message */
+				/* necessary per libpqrcv_endstreaming expectation */
+				//EndReplicationCommand(cmdtag);
 
 				Assert(xlogreader != NULL);
 				break;
diff --git a/src/test/subscription/t/100_bugs.pl b/src/test/subscription/t/100_bugs.pl
index 366a7a9435..af5d1f3bed 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 => 3;
+use Test::More tests => 4;
 
 # Bug #15114
 
@@ -100,3 +100,30 @@ is( $node_publisher->psql(
 );
 
 $node_publisher->stop('fast');
+
+# Bug #16643
+# Initial sync doesn't complete; the protocol was not being followed per
+# expectations after commit 07082b08cc5d.
+my $node_twoways = get_new_node('twoways');
+$node_twoways->init(allows_streaming => 'logical');
+$node_twoways->start;
+for my $db (qw(d1 d2))
+{
+	$node_twoways->safe_psql('postgres', "CREATE DATABASE $db");
+	$node_twoways->safe_psql($db, "CREATE TABLE t (f int)");
+}
+
+$node_twoways->safe_psql('d1', qq{
+	INSERT INTO t SELECT * FROM generate_series(1, 5000);
+	CREATE PUBLICATION testpub FOR TABLE t;
+	SELECT pg_create_logical_replication_slot('testslot', 'pgoutput');
+	});
+
+$node_twoways->safe_psql('d2',
+	"CREATE SUBSCRIPTION testsub CONNECTION \$\$".
+	$node_twoways->connstr('d1').
+	"\$\$ PUBLICATION testpub WITH (create_slot=false, ".
+	"slot_name='testslot')");
+$node_twoways->wait_for_catchup('testsub');
+is($node_twoways->safe_psql('d2', "SELECT sum(f) > 0 FROM t"),
+	"t", "table is synced");
-- 
2.20.1

