From de613d6bd763aba20055fe4ece224e951b6185fa Mon Sep 17 00:00:00 2001 From: Wang Wei Date: Fri, 17 Mar 2023 13:16:00 +0800 Subject: [PATCH v20 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 f90ce0539f..1598c3ef85 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.31.1