From 9367185796549a5f216a6157325df6014d51eb16 Mon Sep 17 00:00:00 2001 From: Shveta Malik Date: Fri, 13 Nov 2026 09:31:08 +0000 Subject: [PATCH v2] Do not copy the whole table in tablesync when no publication publishes it fetch_remote_table_info() took an empty result of its column-list and row-filter queries as "no column list" and "no row filter", so if the publication was dropped, or the table removed from it, before the table's initial sync, the whole table was copied and marked ready without any error. A table published without a column list or row filter returns a row with NULL values, so no rows means that no publication of the subscription publishes the table anymore. To reproduce: create a publication FOR TABLE t(c) WHERE (c > 3), create a subscription to it with enabled = false, drop the publication on the publisher, and enable the subscription. All columns and rows of t are copied. Raise an error instead when either query returns no rows. The table sync worker then exits and retries without copying anything, until the table is published again or ALTER SUBSCRIPTION ... REFRESH PUBLICATION removes it from the subscription. --- src/backend/replication/logical/tablesync.c | 35 ++++++++++- src/test/subscription/t/024_add_drop_pub.pl | 67 +++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c index 4015e861a64..57fa1d53845 100644 --- a/src/backend/replication/logical/tablesync.c +++ b/src/backend/replication/logical/tablesync.c @@ -788,6 +788,7 @@ fetch_remote_table_info(char *nspname, char *relname, LogicalRepRelation *lrel, WalRcvExecResult *pubres; TupleTableSlot *tslot; Oid attrsRow[] = {INT2VECTOROID}; + int64 nrows; /* Build the pub_names comma-separated string. */ pub_names = makeStringInfo(); @@ -837,6 +838,22 @@ fetch_remote_table_info(char *nspname, char *relname, LogicalRepRelation *lrel, errmsg("could not fetch column list info for table \"%s.%s\" from publisher: %s", nspname, relname, pubres->err))); + nrows = tuplestore_tuple_count(pubres->tuplestore); + + /* + * No rows means that none of the subscribed publications publishes + * the table anymore, for instance because a publication was dropped + * or the table was removed from it after the subscription was created + * or refreshed. Don't take that as "no column list", which would + * copy all columns. + */ + if (nrows == 0) + ereport(ERROR, + errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("table \"%s.%s\" is not published by any of the publications of subscription \"%s\"", + nspname, relname, MySubscription->name), + errhint("Use ALTER SUBSCRIPTION ... REFRESH PUBLICATION to remove the table from the subscription, or publish the table again.")); + /* * We don't support the case where the column list is different for * the same table when combining publications. See comments atop @@ -845,12 +862,14 @@ fetch_remote_table_info(char *nspname, char *relname, LogicalRepRelation *lrel, * still need to check here in case the column list was changed after * creating the subscription and before the sync worker is started. */ - if (tuplestore_tuple_count(pubres->tuplestore) > 1) + if (nrows > 1) ereport(ERROR, errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cannot use different column lists for table \"%s.%s\" in different publications", nspname, relname)); + Assert(nrows == 1); + /* * Get the column list and build a single bitmap with the attnums. * @@ -1030,6 +1049,20 @@ fetch_remote_table_info(char *nspname, char *relname, LogicalRepRelation *lrel, (errmsg("could not fetch table WHERE clause info for table \"%s.%s\" from publisher: %s", nspname, relname, res->err))); + /* + * No rows means that none of the subscribed publications publishes + * the table anymore. As the column-list query above did return rows, + * this can only happen if a publication was dropped, or the table was + * removed from it, after that query. Don't take that as "no row + * filter", which would copy all rows. + */ + if (tuplestore_tuple_count(res->tuplestore) == 0) + ereport(ERROR, + errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), + errmsg("table \"%s.%s\" is not published by any of the publications of subscription \"%s\"", + nspname, relname, MySubscription->name), + errhint("Use ALTER SUBSCRIPTION ... REFRESH PUBLICATION to remove the table from the subscription, or publish the table again.")); + /* * Multiple row filter expressions for the same table will be combined * by COPY using OR. If any of the filter expressions for this table diff --git a/src/test/subscription/t/024_add_drop_pub.pl b/src/test/subscription/t/024_add_drop_pub.pl index 63adb1b464c..2040f89d94b 100644 --- a/src/test/subscription/t/024_add_drop_pub.pl +++ b/src/test/subscription/t/024_add_drop_pub.pl @@ -135,6 +135,73 @@ is( $result, qq(1 'check that the incremental data is replicated after the publication is created' ); +# Ensure that the initial sync of a table that is no longer published by any +# of the subscription's publications fails instead of copying the whole table, +# ignoring its column list and row filter. Test both a dropped publication +# and a table removed from the publication before the sync starts. +$node_publisher->safe_psql( + 'postgres', qq( + CREATE TABLE tab_4 (a int, b int, c int); + INSERT INTO tab_4 SELECT i, i, i FROM generate_series(1, 5) i; + CREATE TABLE tab_5 (a int, b int, c int); + INSERT INTO tab_5 SELECT i, i, i FROM generate_series(1, 5) i; + CREATE PUBLICATION tap_pub_4 FOR TABLE tab_4 (a, c) WHERE (a > 3); + CREATE PUBLICATION tap_pub_5 FOR TABLE tab_5 (a, c) WHERE (a > 3); +)); +$node_subscriber->safe_psql( + 'postgres', qq( + CREATE TABLE tab_4 (a int, b int, c int); + CREATE TABLE tab_5 (a int, b int, c int); +)); +$node_subscriber->safe_psql('postgres', + "CREATE SUBSCRIPTION tap_sub_2 CONNECTION '$publisher_connstr' PUBLICATION tap_pub_4, tap_pub_5 WITH (enabled = false)" +); + +$node_publisher->safe_psql( + 'postgres', qq( + DROP PUBLICATION tap_pub_4; + ALTER PUBLICATION tap_pub_5 DROP TABLE tab_5; +)); + +$offset = -s $node_subscriber->logfile; + +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub_2 ENABLE"); + +$node_subscriber->wait_for_log( + qr/ERROR: ( [A-Z0-9]+:)? table "public.tab_4" is not published by any of the publications of subscription "tap_sub_2"/, + $offset); +$node_subscriber->wait_for_log( + qr/ERROR: ( [A-Z0-9]+:)? table "public.tab_5" is not published by any of the publications of subscription "tap_sub_2"/, + $offset); + +# Changes made on the publisher after that are not replicated either. +$node_publisher->safe_psql( + 'postgres', qq( + INSERT INTO tab_4 VALUES (6, 6, 6); + INSERT INTO tab_5 VALUES (6, 6, 6); +)); +$node_publisher->wait_for_catchup('tap_sub_2'); + +$result = $node_subscriber->safe_psql('postgres', + "SELECT (SELECT count(*) FROM tab_4), (SELECT count(*) FROM tab_5)"); +is($result, qq(0|0), + 'check that no data is copied or replicated for tables that are no longer published' +); + +# REFRESH PUBLICATION removes the tables from the subscription. +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub_2 REFRESH PUBLICATION"); + +$result = $node_subscriber->safe_psql('postgres', + "SELECT count(*) FROM pg_subscription_rel WHERE srrelid IN ('tab_4'::regclass, 'tab_5'::regclass)" +); +is($result, qq(0), + 'check that REFRESH PUBLICATION removes the tables from the subscription'); + +$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_2"); +$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_5"); + # shutdown $node_subscriber->stop('fast'); $node_publisher->stop('fast'); -- 2.34.1