From 59e72367c823ea3ace472da0103147ba780b1cef Mon Sep 17 00:00:00 2001
From: Peter Smith <peter.b.smith@fujitsu.com>
Date: Fri, 2 Jul 2021 12:38:47 +1000
Subject: [PATCH v17 3/5] PS tmp - add more comments for expected results

No test code is changed, but this patch adds lots more comments about reasons for the expected results.
---
 src/test/subscription/t/020_row_filter.pl | 89 ++++++++++++++++++-----
 1 file changed, 70 insertions(+), 19 deletions(-)

diff --git a/src/test/subscription/t/020_row_filter.pl b/src/test/subscription/t/020_row_filter.pl
index 35a41741d3..e018b0d08c 100644
--- a/src/test/subscription/t/020_row_filter.pl
+++ b/src/test/subscription/t/020_row_filter.pl
@@ -83,7 +83,12 @@ $node_publisher->safe_psql('postgres',
 	"ALTER PUBLICATION tap_pub_3 ADD TABLE tab_rowfilter_less_10k WHERE (a < 6000)"
 );
 
-# test row filtering
+# ----------------------------------------------------------
+# The following inserts come before the CREATE SUBSCRIPTION,
+# so these are for testing the initial table copy_data
+# replication.
+# ----------------------------------------------------------
+
 $node_publisher->safe_psql('postgres',
 	"INSERT INTO tab_rowfilter_1 (a, b) VALUES (1, 'not replicated')");
 $node_publisher->safe_psql('postgres',
@@ -96,20 +101,13 @@ $node_publisher->safe_psql('postgres',
 $node_publisher->safe_psql('postgres',
 	"INSERT INTO tab_rowfilter_2 (c) SELECT generate_series(1, 20)");
 $node_publisher->safe_psql('postgres',
-	"INSERT INTO tab_rowfilter_3 (a, b) SELECT x, (x % 3 = 0) FROM generate_series(1, 10) x"
-);
-# use partition row filter:
-# - replicate (1, 100) because 1 < 6000 is true
-# - don't replicate (8000, 101) because 8000 < 6000 is false
-# - replicate (15000, 102) because partition tab_rowfilter_greater_10k doesn't have row filter
+	"INSERT INTO tab_rowfilter_3 (a, b) SELECT x, (x % 3 = 0) FROM generate_series(1, 10) x");
+
+# insert into partitioned table and parttitions
 $node_publisher->safe_psql('postgres',
-	"INSERT INTO tab_rowfilter_partitioned (a, b) VALUES(1, 100),(8000, 101),(15000, 102)"
-);
-# insert directly into partition
-# use partition row filter: replicate (2, 200) because 2 < 6000 is true
+	"INSERT INTO tab_rowfilter_partitioned (a, b) VALUES(1, 100),(8000, 101),(15000, 102)");
 $node_publisher->safe_psql('postgres',
 	"INSERT INTO tab_rowfilter_less_10k (a, b) VALUES(2, 200)");
-# use partition row filter: replicate (5500, 300) because 5500 < 6000 is true
 $node_publisher->safe_psql('postgres',
 	"INSERT INTO tab_rowfilter_partitioned (a, b) VALUES(5500, 300)");
 
@@ -127,6 +125,12 @@ my $synced_query =
 $node_subscriber->poll_query_until('postgres', $synced_query)
   or die "Timed out while waiting for subscriber to synchronize data";
 
+# Check expected replicated rows for tap_row_filter_1
+# pub1 filter is: (a > 1000 AND b <> 'filtered')
+# - (1, 'not replicated') - no, because a not > 1000
+# - (1500, 'filtered') - no, because b == 'filtered'
+# - (1980, 'not filtered') - YES
+# - SELECT x, 'test ' || x FROM generate_series(990,1002) x" - YES, only for 1001,1002 because a > 1000
 my $result =
   $node_subscriber->safe_psql('postgres',
 	"SELECT a, b FROM tab_rowfilter_1 ORDER BY 1, 2");
@@ -134,16 +138,38 @@ is( $result, qq(1001|test 1001
 1002|test 1002
 1980|not filtered), 'check filtered data was copied to subscriber');
 
+# Check expected replicated rows for tab_row_filter_2
+# pub1 filter is: (c % 2 = 0)
+# pub2 filter is: (c % 3 = 0)
+# So only 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 should pass filter on pub1
+# So only 3, 6, 9, 12, 15, 18 should pass filter on pub2
+# So combined is 6, 12, 18, which is count 3, min 6, max 18
 $result =
   $node_subscriber->safe_psql('postgres',
 	"SELECT count(c), min(c), max(c) FROM tab_rowfilter_2");
 is($result, qq(3|6|18), 'check filtered data was copied to subscriber');
 
+# Check expected replicated rows for tab_row_filter_3
+# filter is null.
+# 10 rows are inserted, so 10 rows are replicated.
 $result =
   $node_subscriber->safe_psql('postgres',
 	"SELECT count(a) FROM tab_rowfilter_3");
 is($result, qq(10), 'check filtered data was copied to subscriber');
 
+# Check expected replicated rows for partitions
+# PUBLICATION option "publish_via_partition_root" is default, so use the filter at table level
+# tab_rowfilter_partitioned filter: (a < 5000)
+# tab_rowfilter_less_10k filter: (a < 6000)
+# tab_rowfilter_greater_10k filter: null
+# INSERT INTO tab_rowfilter_partitioned (a, b) VALUES(1, 100),(8000, 101),(15000, 102)
+# - (1,100) YES, because 1 < 6000
+# - (8000, 101) NO, because fails 8000 < 6000
+# - (15000, 102) YES, because tab_rowfilter_greater_10k has null filter
+# INSERT INTO tab_rowfilter_less_10k (a, b) VALUES(2, 200)
+# - (2, 200) YES, because 2 < 6000
+# INSERT INTO tab_rowfilter_partitioned (a, b) VALUES(5500, 300)
+# - (5500, 300) YES, because 5500 < 6000 (Note: using the filter at the table, not the partition root)
 $result =
   $node_subscriber->safe_psql('postgres',
 	"SELECT a, b FROM tab_rowfilter_less_10k ORDER BY 1, 2");
@@ -156,6 +182,11 @@ $result =
 	"SELECT a, b FROM tab_rowfilter_greater_10k ORDER BY 1, 2");
 is($result, qq(15000|102), 'check filtered data was copied to subscriber');
 
+# ------------------------------------------------------------
+# The following operations come after the CREATE SUBSCRIPTION,
+# so these are for testing normal replication behaviour.
+# -----------------------------------------------------------
+
 # test row filter (INSERT, UPDATE, DELETE)
 $node_publisher->safe_psql('postgres',
 	"INSERT INTO tab_rowfilter_1 (a, b) VALUES (800, 'test 800')");
@@ -165,18 +196,26 @@ $node_publisher->safe_psql('postgres',
 	"INSERT INTO tab_rowfilter_1 (a, b) VALUES (1601, 'test 1601')");
 $node_publisher->safe_psql('postgres',
 	"INSERT INTO tab_rowfilter_1 (a, b) VALUES (1700, 'test 1700')");
-# UPDATE is not replicated ; row filter evaluates to false when b = NULL
 $node_publisher->safe_psql('postgres',
 	"UPDATE tab_rowfilter_1 SET b = NULL WHERE a = 1600");
 $node_publisher->safe_psql('postgres',
 	"UPDATE tab_rowfilter_1 SET b = 'test 1601 updated' WHERE a = 1601");
-# DELETE is not replicated ; b is not part of the PK or replica identity and
-# old tuple contains b = NULL, hence, row filter evaluates to false
 $node_publisher->safe_psql('postgres',
 	"DELETE FROM tab_rowfilter_1 WHERE a = 1700");
 
 $node_publisher->wait_for_catchup($appname);
 
+# Check expected replicated rows for tap_row_filter_1
+# pub1 filter is: (a > 1000 AND b <> 'filtered')
+# - 1001, 1002, 1980 already exist from previous inserts
+# - (800, 'test 800') NO because 800 < 1000
+# - (1600, 'test 1600') YES
+# - (1601, 'test 1601') YES
+# - (1700, 'test 1700') YES
+# UPDATE (1600, NULL) NO. row filter evaluates to false when b = NULL
+# UPDATE (1601, 'test 1601 updated') YES
+# DELETE (1700), NO. b is not part of the PK or replica identity and
+# old tuple contains b = NULL, hence, row filter evaluates to false
 $result =
   $node_subscriber->safe_psql('postgres',
 	"SELECT a, b FROM tab_rowfilter_1 ORDER BY 1, 2");
@@ -194,21 +233,33 @@ $node_subscriber->safe_psql('postgres',
 	"TRUNCATE TABLE tab_rowfilter_partitioned");
 $node_subscriber->safe_psql('postgres',
 	"ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION WITH (copy_data = true)");
-# use partitioned table row filter: replicate, 4000 < 5000 is true
 $node_publisher->safe_psql('postgres',
 	"INSERT INTO tab_rowfilter_partitioned (a, b) VALUES(4000, 400)");
-# use partitioned table row filter: replicate, 4500 < 5000 is true
 $node_publisher->safe_psql('postgres',
 	"INSERT INTO tab_rowfilter_less_10k (a, b) VALUES(4500, 450)");
-# use partitioned table row filter: don't replicate, 5600 < 5000 is false
 $node_publisher->safe_psql('postgres',
 	"INSERT INTO tab_rowfilter_less_10k (a, b) VALUES(5600, 123)");
-# use partitioned table row filter: don't replicate, 16000 < 5000 is false
 $node_publisher->safe_psql('postgres',
 	"INSERT INTO tab_rowfilter_greater_10k (a, b) VALUES(16000, 1950)");
 
 $node_publisher->wait_for_catchup($appname);
 
+# Check expected replicated rows for partitions
+# PUBLICATION option "publish_via_partition_root = true" is default, so use the filter at root level
+# tab_rowfilter_partitioned filter: (a < 5000)
+# tab_rowfilter_less_10k filter: (a < 6000)
+# tab_rowfilter_greater_10k filter: null
+# Existing INSERTS (copied because of copy_data=true option)
+# - (1,100) YES, 1 < 5000
+# - (8000, 101) NO, fails 8000 < 5000
+# - (15000, 102) NO, fails 15000 < 5000
+# - (2, 200) YES, 2 < 6000
+# - (5500, 300) NO, fails 5500 < 5000
+# New INSERTS replicated (after the initial copy_data)?
+# - VALUES(4000, 400) YES, 4000 < 5000
+# - VALUES(4500, 450) YES, 4500 < 5000
+# - VALUES(5600, 123) NO fails 5600 < 5000
+# - VALUES(16000, 1950) NO fails 16000 < 5000
 $result =
   $node_subscriber->safe_psql('postgres',
 	"SELECT a, b FROM tab_rowfilter_partitioned ORDER BY 1, 2");
-- 
2.27.0

