From 668024d17016a42637a54a1e4df6f55407e443b5 Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Tue, 8 Jun 2021 14:30:41 -0700
Subject: [PATCH v1] Adding test to trigger logical replication assertion

---
 src/test/subscription/t/021_truncate.pl | 76 +++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100644 src/test/subscription/t/021_truncate.pl

diff --git a/src/test/subscription/t/021_truncate.pl b/src/test/subscription/t/021_truncate.pl
new file mode 100644
index 0000000000..d89290b8c0
--- /dev/null
+++ b/src/test/subscription/t/021_truncate.pl
@@ -0,0 +1,76 @@
+# Copyright (c) 2021, PostgreSQL Global Development Group
+
+# This test checks whether an after statement trigger fired as a consequence of
+# the logical replication of a truncate command will cause an Assertion.
+use strict;
+use warnings;
+use PostgresNode;
+use TestLib;
+use Test::More tests => 1;
+
+my $cmd;
+
+my $node_publisher = get_new_node('publisher');
+$node_publisher->init(allows_streaming => 'logical');
+$node_publisher->start;
+
+my $node_subscriber = get_new_node('subscriber');
+$node_subscriber->init(allows_streaming => 'logical');
+$node_subscriber->start;
+
+# Create schema on both publisher and subscriber
+$cmd = q(
+	CREATE TABLE public.t (i INTEGER);
+	ALTER TABLE public.t REPLICA IDENTITY FULL;
+);
+$node_publisher->safe_psql('postgres', $cmd);
+$node_subscriber->safe_psql('postgres', $cmd);
+
+# Create after statement trigger on the subscriber only
+$cmd = q(
+	CREATE FUNCTION public.truncate_trigger_func() RETURNS TRIGGER AS $$
+	BEGIN
+		INSERT INTO public.t (i) VALUES (1);
+		RETURN NULL;
+	END;
+	$$ LANGUAGE plpgsql;
+	CREATE TRIGGER truncate_trigger
+		AFTER TRUNCATE ON public.t
+		FOR EACH STATEMENT EXECUTE FUNCTION public.truncate_trigger_func();
+	ALTER TABLE public.t
+		ENABLE ALWAYS TRIGGER truncate_trigger;
+);
+$node_subscriber->safe_psql('postgres', $cmd);
+
+# Create publication and subscription
+my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
+$node_publisher->safe_psql('postgres', qq(
+CREATE PUBLICATION pub FOR TABLE public.t;
+));
+$node_subscriber->safe_psql('postgres', qq(
+CREATE SUBSCRIPTION sub CONNECTION '$publisher_connstr' PUBLICATION pub;
+));
+$node_publisher->wait_for_catchup('sub');
+
+# Wait for initial sync to finish as well
+my $synced_query =
+  "SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
+$node_subscriber->poll_query_until('postgres', $synced_query)
+  or die "Timed out while waiting for subscriber to synchronize data";
+
+#
+# Check replication of TRUNCATE command causing the firing of a statement
+# level trigger
+#
+$node_publisher->safe_psql('postgres', 'TRUNCATE public.t');
+$node_publisher->wait_for_catchup('sub');
+
+#
+# Not reached -- If the subscriber backend Asserts as expected, we'll be stuck
+# waiting forever, above
+#
+is( $node_subscriber->safe_psql(
+	'postgres', "SELECT 1"), 1, "still alive");
+
+$node_subscriber->stop;
+$node_publisher->stop;
-- 
2.21.1 (Apple Git-122.3)

