From 10d4cab23486d04004114ef415cbd4b79ff31d0e Mon Sep 17 00:00:00 2001
From: Shlok Kyal <shlok.kyal.oss@gmail.com>
Date: Wed, 22 Jan 2025 20:59:17 +0530
Subject: [PATCH v8 2/2] Test for Improve logging for data origin discrepancies
 in table synchronization

---
 src/test/subscription/t/030_origin.pl | 104 ++++++++++++++++++++++++++
 1 file changed, 104 insertions(+)

diff --git a/src/test/subscription/t/030_origin.pl b/src/test/subscription/t/030_origin.pl
index eb1dcb7271..390623d2da 100644
--- a/src/test/subscription/t/030_origin.pl
+++ b/src/test/subscription/t/030_origin.pl
@@ -255,6 +255,110 @@ $node_A->safe_psql('postgres', "DROP TABLE tab_new");
 $node_B->safe_psql('postgres', "DROP TABLE tab_new");
 $node_A->safe_psql('postgres', "DROP SUBSCRIPTION $subname_AB2");
 
+###############################################################################
+# Specifying origin = NONE and copy_data = on must raise WARNING if we subscribe
+# to a partitioned table and this table contains any remotely originated data.
+#
+#           node_B
+#  __________________________
+# |       tab_main           | --------------> node_C (tab_main)
+# |__________________________|
+# | tab_part1  | tab_part2   | <-------------- node_A (tab_part2)
+# |____________|_____________|
+#              | tab_part2_1 |
+#              |_____________|
+#
+#           node_B
+#  __________________________
+# |       tab_main           |
+# |__________________________|
+# | tab_part1  | tab_part2   | <-------------- node_A (tab_part2)
+# |____________|_____________|
+#              | tab_part2_1 | --------------> node_C (tab_part2_1)
+#              |_____________|
+###############################################################################
+
+# create a table on node A which will act as a source for a partition on node B
+$node_A->safe_psql(
+	'postgres', qq(
+CREATE TABLE tab_part2(a int);
+CREATE PUBLICATION pub_a_b FOR TABLE tab_part2;
+));
+
+# create a partition table on node B
+$node_B->safe_psql(
+	'postgres', qq(
+CREATE TABLE tab_main(a int) PARTITION BY RANGE(a);
+CREATE TABLE tab_part1 PARTITION OF tab_main FOR VALUES FROM (0) TO (5);
+CREATE TABLE tab_part2(a int) PARTITION BY RANGE(a);
+CREATE TABLE tab_part2_1 PARTITION OF tab_part2 FOR VALUES FROM (5) TO (10);
+ALTER TABLE tab_main ATTACH PARTITION tab_part2 FOR VALUES FROM (5) to (10);
+CREATE SUBSCRIPTION sub_a_b CONNECTION '$node_A_connstr' PUBLICATION pub_a_b;
+));
+
+# create a table on node C
+$node_C->safe_psql(
+	'postgres', qq(
+CREATE TABLE tab_main(a int);
+CREATE TABLE tab_part2_1(a int);
+));
+
+# create a logical replication setup between node B and node C with
+# subscription on node C having origin = NONE and copy_data = on
+$node_B->safe_psql(
+	'postgres', qq(
+CREATE PUBLICATION pub_b_c FOR TABLE tab_main WITH (publish_via_partition_root);
+CREATE PUBLICATION pub_b_c_2 FOR TABLE tab_part2_1;
+));
+
+($result, $stdout, $stderr) = $node_C->psql(
+	'postgres', "
+	CREATE SUBSCRIPTION sub_b_c CONNECTION '$node_B_connstr' PUBLICATION pub_b_c WITH (origin = none, copy_data = on);
+");
+
+# A warning must be logged as a partition 'tab_part2' in node B is subscribed to
+# node A so partition 'tab_part2' can have remotely originated data
+like(
+	$stderr,
+	qr/WARNING: ( [A-Z0-9]+:)? subscription "sub_b_c" requested copy_data with origin = NONE but might copy data that had a different origin/,
+	"Create subscription with origin = none and copy_data when the publisher's partition is subscribing from different origin"
+);
+$node_C->safe_psql('postgres', "DROP SUBSCRIPTION sub_b_c");
+
+($result, $stdout, $stderr) = $node_C->psql(
+	'postgres', "
+	CREATE SUBSCRIPTION sub_b_c CONNECTION '$node_B_connstr' PUBLICATION pub_b_c_2 WITH (origin = none, copy_data = on);
+");
+
+# A warning must be logged as ancestor of table 'tab_part2_1' in node A have
+# been subscribed to node B so table 'tab_part2_1' can have remotely originated
+# data
+like(
+	$stderr,
+	qr/WARNING: ( [A-Z0-9]+:)? subscription "sub_b_c" requested copy_data with origin = NONE but might copy data that had a different origin/,
+	"Create subscription with origin = none and copy_data when the publisher's ancestor is subscribing from different origin"
+);
+
+# clear the operations done by this test
+$node_C->safe_psql(
+	'postgres', qq(
+DROP SUBSCRIPTION sub_b_c;
+DROP TABLE tab_main;
+DROP TABLE tab_part2_1;
+));
+$node_B->safe_psql(
+	'postgres', qq(
+DROP SUBSCRIPTION sub_a_b;
+DROP PUBLICATION pub_b_c;
+DROP PUBLICATION pub_b_c_2;
+DROP TABLE tab_main;
+));
+$node_A->safe_psql(
+	'postgres', qq(
+DROP PUBLICATION pub_a_b;
+DROP TABLE tab_part2;
+));
+
 # shutdown
 $node_B->stop('fast');
 $node_A->stop('fast');
-- 
2.34.1

