From a5147a464125159a734b39290a47ef49a748da62 Mon Sep 17 00:00:00 2001 From: wangw Date: Thu, 28 Apr 2022 11:28:35 +0800 Subject: [PATCH v8] Fix data replicated twice when specifying publish_via_partition_root option. If there are two publications, one of them publish a parent table with (publish_via_partition_root = true) and another publish child table, subscribing to both publications from one subscription results in two initial replications. It should only be copied once. To fix this, we exclude the partition table whose ancestor belongs to specified publications when getting the table list from publisher. --- src/backend/commands/subscriptioncmds.c | 35 ++++++++++++++++++------ src/test/subscription/t/013_partition.pl | 19 ++++--------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index c99c943a20..ee3cd3841c 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -1130,7 +1130,8 @@ static List * fetch_table_list(WalReceiverConn *wrconn, List *publications) { WalRcvExecResult *res; - StringInfoData cmd; + StringInfoData cmd, + pub_names; TupleTableSlot *slot; Oid tableRow[2] = {TEXTOID, TEXTOID}; ListCell *lc; @@ -1139,10 +1140,7 @@ fetch_table_list(WalReceiverConn *wrconn, List *publications) Assert(list_length(publications) > 0); - initStringInfo(&cmd); - appendStringInfoString(&cmd, "SELECT DISTINCT t.schemaname, t.tablename\n" - " FROM pg_catalog.pg_publication_tables t\n" - " WHERE t.pubname IN ("); + initStringInfo(&pub_names); first = true; foreach(lc, publications) { @@ -1151,14 +1149,35 @@ fetch_table_list(WalReceiverConn *wrconn, List *publications) if (first) first = false; else - appendStringInfoString(&cmd, ", "); + appendStringInfoString(&pub_names, ", "); - appendStringInfoString(&cmd, quote_literal_cstr(pubname)); + appendStringInfoString(&pub_names, quote_literal_cstr(pubname)); } - appendStringInfoChar(&cmd, ')'); + + /* + * Get the list of tables from publisher, the partition table whose + * ancestor is also in this list will be ignored, otherwise the initial + * data in the partition table would be replicated twice. + */ + initStringInfo(&cmd); + appendStringInfo(&cmd, "WITH pub_tabs AS(\n" + " SELECT DISTINCT N.nspname, C.oid, C.relname, C.relispartition\n" + " 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" + " 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", + pub_names.data); res = walrcv_exec(wrconn, cmd.data, 2, tableRow); pfree(cmd.data); + pfree(pub_names.data); if (res->status != WALRCV_OK_TUPLES) ereport(ERROR, diff --git a/src/test/subscription/t/013_partition.pl b/src/test/subscription/t/013_partition.pl index 4debbfe55f..f0574a52b4 100644 --- a/src/test/subscription/t/013_partition.pl +++ b/src/test/subscription/t/013_partition.pl @@ -3,7 +3,7 @@ use strict; use warnings; use PostgresNode; use TestLib; -use Test::More tests => 71; +use Test::More tests => 69; # setup @@ -468,12 +468,14 @@ $node_subscriber2->safe_psql('postgres', $node_subscriber2->safe_psql('postgres', "CREATE TABLE tab3_1 (a int PRIMARY KEY, c text DEFAULT 'sub2_tab3_1', b text)" ); + +# Note: We only create one table (tab4) here. We specified +# publish_via_partition_root = true (see pub_all and pub_lower_level above), so +# all data will be replicated to that table. $node_subscriber2->safe_psql('postgres', "CREATE TABLE tab4 (a int PRIMARY KEY)" ); -$node_subscriber2->safe_psql('postgres', - "CREATE TABLE tab4_1 (a int PRIMARY KEY)" -); + # Publication that sub2 points to now publishes via root, so must update # subscription target relations. $node_subscriber2->safe_psql('postgres', @@ -543,10 +545,6 @@ $result = $node_subscriber2->safe_psql('postgres', "SELECT a FROM tab4 ORDER BY 1"); is( $result, qq(0), 'inserts into tab4 replicated'); -$result = $node_subscriber2->safe_psql('postgres', - "SELECT a FROM tab4_1 ORDER BY 1"); -is( $result, qq(), 'inserts into tab4_1 replicated'); - # now switch the order of publications in the list, try again, the result # should be the same (no dependence on order of pulications) $node_subscriber2->safe_psql('postgres', @@ -571,11 +569,6 @@ $result = $node_subscriber2->safe_psql('postgres', is( $result, qq(0 1), 'inserts into tab4 replicated'); -$result = $node_subscriber2->safe_psql('postgres', - "SELECT a FROM tab4_1 ORDER BY 1"); -is( $result, qq(), 'inserts into tab4_1 replicated'); - - # update (replicated as update) $node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 6 WHERE a = 5"); $node_publisher->safe_psql('postgres', "UPDATE tab2 SET a = 6 WHERE a = 5"); -- 2.23.0.windows.1