From 51c3463611f03f2879683ba04aed96ee85c5dc3a Mon Sep 17 00:00:00 2001
From: Wang Wei <wangw.fnst@fujitsu.com>
Date: Fri, 17 Mar 2023 13:16:00 +0800
Subject: [PATCH v21 2/2] Fix this problem for back branches

---
 src/backend/commands/subscriptioncmds.c | 45 +++++++++++++++++--------
 1 file changed, 31 insertions(+), 14 deletions(-)

diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index 02bd1e81ce..a71404ba03 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -1936,23 +1936,22 @@ static List *
 fetch_table_list(WalReceiverConn *wrconn, List *publications)
 {
 	WalRcvExecResult *res;
-	StringInfoData cmd;
+	StringInfoData cmd,
+				pub_names;
 	TupleTableSlot *slot;
 	Oid			tableRow[3] = {TEXTOID, TEXTOID, NAMEARRAYOID};
 	List	   *tablelist = NIL;
 	int			server_version = walrcv_server_version(wrconn);
 	bool		check_columnlist = (server_version >= 150000);
 
+	initStringInfo(&pub_names);
+	get_publications_str(publications, &pub_names, true);
+
 	initStringInfo(&cmd);
 
 	/* Get the list of tables from the publisher. */
 	if (server_version >= 160000)
 	{
-		StringInfoData pub_names;
-
-		initStringInfo(&pub_names);
-		get_publications_str(publications, &pub_names, true);
-
 		/*
 		 * From version 16, we allowed passing multiple publications to the
 		 * function pg_get_publication_tables. This helped to filter out the
@@ -1975,24 +1974,42 @@ fetch_table_list(WalReceiverConn *wrconn, List *publications)
 						 "          WHERE pubname IN ( %s )) as gpt\n"
 						 "       ON gpt.relid = c.oid\n",
 						 pub_names.data);
-
-		pfree(pub_names.data);
 	}
 	else
 	{
-		appendStringInfoString(&cmd, "SELECT DISTINCT t.schemaname, t.tablename \n");
+		appendStringInfoString(&cmd, "WITH pub_tabs AS(\n"
+							   " SELECT DISTINCT n.nspname, c.oid, c.relname, c.relispartition\n");
+
+		/* Get column lists for each relation if the publisher supports it */
+		if (check_columnlist)
+			appendStringInfoString(&cmd, ", ( SELECT array_agg(a.attname ORDER BY a.attnum)\n"
+								   "          FROM pg_attribute a\n"
+								   "          WHERE a.attrelid = gpt.relid AND a.attnum > 0 AND\n"
+								   "                NOT a.attisdropped AND\n"
+								   "                (a.attnum = ANY(gpt.attrs) OR gpt.attrs IS NULL)\n"
+								   "        ) AS attnames\n");
+
+		appendStringInfo(&cmd, " FROM pg_publication p,\n"
+						 "      LATERAL pg_get_publication_tables(p.pubname) gpt,\n"
+						 "      pg_class c JOIN pg_namespace n ON (n.oid = c.relnamespace)\n"
+						 "  WHERE c.oid = gpt.relid AND p.pubname IN ( %s )\n"
+						 ")\n"
+						 "SELECT DISTINCT pub_tabs.nspname, pub_tabs.relname\n",
+						 pub_names.data);
 
 		/* Get column lists for each relation if the publisher supports it */
 		if (check_columnlist)
-			appendStringInfoString(&cmd, ", t.attnames\n");
+			appendStringInfoString(&cmd, ", pub_tabs.attnames\n");
 
-		appendStringInfoString(&cmd, "FROM pg_catalog.pg_publication_tables t\n"
-							   " WHERE t.pubname IN (");
-		get_publications_str(publications, &cmd, true);
-		appendStringInfoChar(&cmd, ')');
+		appendStringInfoString(&cmd, "FROM pub_tabs\n"
+							   " WHERE (pub_tabs.relispartition IS FALSE\n"
+							   "  OR NOT EXISTS (SELECT 1 FROM pg_partition_ancestors(pub_tabs.oid) as pa\n"
+							   "                  WHERE pa.relid IN (SELECT pub_tabs.oid FROM pub_tabs)\n"
+							   "                   AND pa.relid != pub_tabs.oid))\n");
 	}
 
 	res = walrcv_exec(wrconn, cmd.data, check_columnlist ? 3 : 2, tableRow);
+	pfree(pub_names.data);
 	pfree(cmd.data);
 
 	if (res->status != WALRCV_OK_TUPLES)
-- 
2.39.1.windows.1

