From 28bc549b8895931d6fa5a0083bedc3fbdcc1cbbd Mon Sep 17 00:00:00 2001
From: Peter Smith <peter.b.smith@fujitsu.com>
Date: Tue, 13 Jul 2021 12:48:29 +1000
Subject: [PATCH v18] tmp - Initial copy_data to use only DISTINCT filters.

If subscriber subscribes to multiple publications, and if those publications have filters for the same table, then when discovering all those filters we should only care if they are DISTINCT.

For example, there is no point to apply the same identical WHERE clause multiple times to the COPY command.
---
 src/backend/replication/logical/tablesync.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index 980826a..efe3ce8 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -804,12 +804,15 @@ fetch_remote_table_info(char *nspname, char *relname,
 
 	walrcv_clear_result(res);
 
-	/* Get relation qual */
+	/*
+	 * Get relation qual. Use SELECT DISTINCT because there is no point to apply
+	 * identical filters multiple times.
+	 */
 	if (walrcv_server_version(LogRepWorkerWalRcvConn) >= 150000)
 	{
 		resetStringInfo(&cmd);
 		appendStringInfo(&cmd,
-						 "SELECT pg_get_expr(prqual, prrelid) "
+						 "SELECT DISTINCT pg_get_expr(prqual, prrelid) "
 						 "  FROM pg_publication p "
 						 "  INNER JOIN pg_publication_rel pr "
 						 "       ON (p.oid = pr.prpubid) "
@@ -830,6 +833,8 @@ fetch_remote_table_info(char *nspname, char *relname,
 		}
 		appendStringInfoChar(&cmd, ')');
 
+		elog(LOG, "!!> fetch_remote_table_info: SQL:\n%s", cmd.data);
+
 		res = walrcv_exec(LogRepWorkerWalRcvConn, cmd.data, 1, qualRow);
 
 		if (res->status != WALRCV_OK_TUPLES)
@@ -932,6 +937,9 @@ copy_table(Relation rel)
 
 		appendStringInfoString(&cmd, ") TO STDOUT");
 	}
+
+	elog(LOG, "!!> copy_table SQL:\n%s", cmd.data);
+
 	res = walrcv_exec(LogRepWorkerWalRcvConn, cmd.data, 0, NULL);
 	pfree(cmd.data);
 	if (res->status != WALRCV_OK_COPY_OUT)
-- 
1.8.3.1

