From 85b07c78042d151b78f7d4f3e997f3a7bfd07efc Mon Sep 17 00:00:00 2001 From: Shveta Malik Date: Fri, 13 Nov 2026 09:31:08 +0000 Subject: [PATCH v1] 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 | 28 ++++++++++ src/test/subscription/t/024_add_drop_pub.pl | 57 +++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c index 4015e861a64..b268ec8a92f 100644 --- a/src/backend/replication/logical/tablesync.c +++ b/src/backend/replication/logical/tablesync.c @@ -837,6 +837,20 @@ 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))); + /* + * 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 (tuplestore_tuple_count(pubres->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.")); + /* * We don't support the case where the column list is different for * the same table when combining publications. See comments atop @@ -1030,6 +1044,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..6d0848ceda6 100644 --- a/src/test/subscription/t/024_add_drop_pub.pl +++ b/src/test/subscription/t/024_add_drop_pub.pl @@ -135,6 +135,63 @@ 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); + +$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 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"); + # shutdown $node_subscriber->stop('fast'); $node_publisher->stop('fast'); -- 2.34.1