From 07db207328bba6a334f38943f5d487261d39e7b7 Mon Sep 17 00:00:00 2001 From: Huseyin Demir Date: Fri, 30 Oct 2026 08:03:39 +0100 Subject: [PATCH v4] Log changes discarded for relations not in the subscription When an apply worker receives a change for a relation that is not in SUBREL_STATE_READY, should_apply_changes_for_rel() returns false and the change is discarded with no log message, conflict or statistics counter. That is correct while a tablesync worker is still copying the table, but it is indistinguishable from a relation that has no pg_subscription_rel row at all, where the discard is permanent and both the replication origin and the publisher slot advance past the discarded rows. Report the discard at LOG when the relation is not part of the subscription, and at DEBUG2 for a relation that is merely not synchronized yet. The gate is reached once per change, so remember the last sync state reported in the relation map entry and report a relation once per sync state. The remembered state survives the entry being rebuilt when the publisher sends the relation again, as it does after DDL and for each streamed transaction, and is cleared once changes for the relation are applied, so a later discard is reported again. Author: Huseyin Demir --- src/backend/replication/logical/relation.c | 11 +++ src/backend/replication/logical/worker.c | 66 +++++++++++++++-- src/include/catalog/pg_subscription_rel.h | 2 + src/include/replication/logicalrelation.h | 8 +++ src/test/subscription/t/015_stream.pl | 68 +++++++++++++++++- src/test/subscription/t/024_add_drop_pub.pl | 78 +++++++++++++++++++++ 6 files changed, 228 insertions(+), 5 deletions(-) diff --git a/src/backend/replication/logical/relation.c b/src/backend/replication/logical/relation.c index 00e3dabdbc8..fb7320a7b1e 100644 --- a/src/backend/replication/logical/relation.c +++ b/src/backend/replication/logical/relation.c @@ -168,6 +168,7 @@ logicalrep_relmap_update(LogicalRepRelation *remoterel) LogicalRepRelMapEntry *entry; bool found; int i; + char last_unapplied_state = SUBREL_STATE_INIT_LAST_UNAPPLIED; if (LogicalRepRelMap == NULL) logicalrep_relmap_init(); @@ -179,9 +180,18 @@ logicalrep_relmap_update(LogicalRepRelation *remoterel) HASH_ENTER, &found); if (found) + { + /* + * The publisher sends the relation again after DDL and for each + * streamed transaction, so keep what has already been reported about + * skipped changes; see report_unapplied_change(). + */ + last_unapplied_state = entry->last_unapplied_state; logicalrep_relmap_free_entry(entry); + } memset(entry, 0, sizeof(LogicalRepRelMapEntry)); + entry->last_unapplied_state = last_unapplied_state; /* Make cached copy of the data */ oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext); @@ -681,6 +691,7 @@ logicalrep_partition_open(LogicalRepRelMapEntry *root, { memset(part_entry, 0, sizeof(LogicalRepPartMapEntry)); part_entry->partoid = partOid; + entry->last_unapplied_state = SUBREL_STATE_INIT_LAST_UNAPPLIED; } /* Release the no-longer-useful attrmap, if any. */ diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index 6d522573f90..438e4975bd1 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -677,6 +677,50 @@ ReplicationOriginNameForLogicalRep(Oid suboid, Oid relid, } } +/* + * Report that changes for a relation are being skipped. + * + * Logs at most once per sync state of the relation. Called from + * should_apply_changes_for_rel() each time it decides not to apply a change, + * so the last state reported is remembered in the relation map entry. It is + * kept when the entry is rebuilt for the same relation, and cleared when + * changes are applied again, so a later skip is reported again. + */ +static void +report_unapplied_change(LogicalRepRelMapEntry *rel) +{ + /* Quick return if no state change. */ + if (rel->state == rel->last_unapplied_state) + return; + + /* + * A relation with no pg_subscription_rel row is not being synchronized at + * all. Its changes are discarded for as long as that holds, yet are + * still acknowledged to the publisher as applied, so log at LOG; + * otherwise nothing shows that they were dropped. + */ + if (rel->state == SUBREL_STATE_UNKNOWN) + ereport(LOG, + errmsg("skipping changes for relation \"%s.%s\"", + rel->remoterel.nspname, rel->remoterel.relname), + errdetail("The published relation is not included in subscription \"%s\".", + MySubscription->name), + errhint("If this relation should be replicated, use ALTER SUBSCRIPTION ... REFRESH PUBLICATION.")); + else + { + /* + * The relation is still being synchronized by a tablesync worker, so + * skipping its changes is transient and expected. + */ + ereport(DEBUG2, + errmsg_internal("skipping changes for relation \"%s.%s\" in subscription \"%s\": synchronization state is \"%c\"", + rel->remoterel.nspname, rel->remoterel.relname, + MySubscription->name, rel->state)); + } + + rel->last_unapplied_state = rel->state; +} + /* * Should this worker apply changes for given relation. * @@ -720,12 +764,26 @@ should_apply_changes_for_rel(LogicalRepRelMapEntry *rel) MySubscription->name), errdetail("Cannot handle streamed replication transactions using parallel apply workers until all tables have been synchronized."))); - return rel->state == SUBREL_STATE_READY; + if (rel->state == SUBREL_STATE_READY) + { + rel->last_unapplied_state = SUBREL_STATE_INIT_LAST_UNAPPLIED; + return true; + } + + report_unapplied_change(rel); + return false; case WORKERTYPE_APPLY: - return (rel->state == SUBREL_STATE_READY || - (rel->state == SUBREL_STATE_SYNCDONE && - rel->statelsn <= remote_ctx.finish_lsn)); + if (rel->state == SUBREL_STATE_READY || + (rel->state == SUBREL_STATE_SYNCDONE && + rel->statelsn <= remote_ctx.finish_lsn)) + { + rel->last_unapplied_state = SUBREL_STATE_INIT_LAST_UNAPPLIED; + return true; + } + + report_unapplied_change(rel); + return false; case WORKERTYPE_SEQUENCESYNC: /* Should never happen. */ diff --git a/src/include/catalog/pg_subscription_rel.h b/src/include/catalog/pg_subscription_rel.h index 502640d3018..15c86d8d07f 100644 --- a/src/include/catalog/pg_subscription_rel.h +++ b/src/include/catalog/pg_subscription_rel.h @@ -76,6 +76,8 @@ MAKE_SYSCACHE(SUBSCRIPTIONRELMAP, pg_subscription_rel_srrelid_srsubid_index, 64) #define SUBREL_STATE_UNKNOWN '\0' /* unknown state */ #define SUBREL_STATE_SYNCWAIT 'w' /* waiting for sync */ #define SUBREL_STATE_CATCHUP 'c' /* catching up with apply */ +#define SUBREL_STATE_INIT_LAST_UNAPPLIED '?' /* nothing reported yet for + * skipped changes */ #endif /* EXPOSE_TO_CLIENT_CODE */ diff --git a/src/include/replication/logicalrelation.h b/src/include/replication/logicalrelation.h index 4063118d2b9..3055bae9cec 100644 --- a/src/include/replication/logicalrelation.h +++ b/src/include/replication/logicalrelation.h @@ -40,6 +40,14 @@ typedef struct LogicalRepRelMapEntry /* Sync state. */ char state; + + /* + * Sync state for which report_unapplied_change() last reported, or + * SUBREL_STATE_INIT_LAST_UNAPPLIED if nothing has been reported since + * changes were last applied. This must be set whenever the entry is + * zeroed, as zero is SUBREL_STATE_UNKNOWN. + */ + char last_unapplied_state; XLogRecPtr statelsn; } LogicalRepRelMapEntry; diff --git a/src/test/subscription/t/015_stream.pl b/src/test/subscription/t/015_stream.pl index ac96bc3f009..d26024aba3e 100644 --- a/src/test/subscription/t/015_stream.pl +++ b/src/test/subscription/t/015_stream.pl @@ -175,6 +175,39 @@ is($result, qq(2|2|2), 'check initial data was copied to subscriber'); test_streaming($node_publisher, $node_subscriber, $appname, 0); +# Verify that changes skipped for a relation that is not in the subscription +# are reported once per sync state, not once per streamed transaction. The +# publisher sends the relation again for each streamed transaction. +# +# A table that is published but never refreshed on the subscriber has no +# pg_subscription_rel row at all, so its changes are discarded. +$node_publisher->safe_psql('postgres', "CREATE TABLE test_tab_3 (a int)"); +$node_subscriber->safe_psql('postgres', "CREATE TABLE test_tab_3 (a int)"); +$node_publisher->safe_psql('postgres', + "ALTER PUBLICATION tap_pub ADD TABLE test_tab_3"); + +my $offset = -s $node_subscriber->logfile; + +# Each is large enough to be streamed, given logical_decoding_work_mem = 64kB. +foreach my $i (1 .. 3) +{ + $node_publisher->safe_psql('postgres', + "INSERT INTO test_tab_3 SELECT i FROM generate_series(1, 5000) s(i)"); +} +$node_publisher->wait_for_catchup($appname); + +my $log = + PostgreSQL::Test::Utils::slurp_file($node_subscriber->logfile, $offset); +my $count = () = + $log =~ + /LOG: ( [A-Z0-9]+:)? skipping changes for relation "public\.test_tab_3"/g; +is($count, 1, 'skipped streamed transactions are reported once'); + +$result = + $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tab_3"); +is($result, qq(0), + 'changes for a relation not in the subscription are discarded'); + ###################################### # Test using streaming mode 'parallel' ###################################### @@ -201,6 +234,39 @@ $node_subscriber->safe_psql('postgres', q{SELECT 1}); test_streaming($node_publisher, $node_subscriber, $appname, 1); +# Verify that a parallel apply worker reports changes it does not apply. +# +# test_tab_3 still has no pg_subscription_rel row. GetSubscriptionRelations() +# only returns rows that exist, so such a table is invisible to +# AllTablesyncsReady(), pa_can_start() still hands the streamed transaction to +# a parallel apply worker, and that worker discards every change for the table. +my $pa_offset = -s $node_subscriber->logfile; + +# Large enough to be streamed, given logical_decoding_work_mem = 64kB. +$node_publisher->safe_psql('postgres', + "INSERT INTO test_tab_3 SELECT i FROM generate_series(1, 5000) s(i)"); + +# The bgw_type in log_line_prefix's %b distinguishes the parallel apply worker +# ("logical replication parallel worker") from the leader ("logical replication +# apply worker"), so this pins the report to the parallel path specifically. +$node_subscriber->wait_for_log( + qr/logical replication parallel worker\[\d+\] LOG: ( [A-Z0-9]+:)? skipping changes for relation "public\.test_tab_3"/, + $pa_offset); + +ok( $node_subscriber->log_contains( + qr/DETAIL: ( [A-Z0-9]+:)? The published relation is not included in subscription "tap_sub"\./, + $pa_offset), + 'parallel apply worker names the relation it is not applying changes for' +); + +$node_publisher->wait_for_catchup($appname); + +$result = + $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM test_tab_3"); +is($result, qq(0), + 'parallel apply worker discards changes for a relation not in the subscription' +); + # Test that the deadlock is detected among the leader and parallel apply # workers. @@ -216,7 +282,7 @@ my $h = $node_publisher->background_psql('postgres', on_error_stop => 0); # Confirm if a deadlock between the leader apply worker and the parallel apply # worker can be detected. -my $offset = -s $node_subscriber->logfile; +$offset = -s $node_subscriber->logfile; $h->query_safe( q{ diff --git a/src/test/subscription/t/024_add_drop_pub.pl b/src/test/subscription/t/024_add_drop_pub.pl index 63adb1b464c..9ff0502a9fa 100644 --- a/src/test/subscription/t/024_add_drop_pub.pl +++ b/src/test/subscription/t/024_add_drop_pub.pl @@ -135,6 +135,84 @@ is( $result, qq(1 'check that the incremental data is replicated after the publication is created' ); +# Verify that the apply worker reports changes it does not apply because the +# relation is not part of the subscription. Such changes are discarded and +# still acknowledged to the publisher as applied, so the report is the only +# indication that they were received at all. +# +# A relation is reported once per sync state, however many changes arrive. +$node_publisher->safe_psql('postgres', "CREATE TABLE tab_4 (a int)"); +$node_subscriber->safe_psql('postgres', "CREATE TABLE tab_4 (a int)"); + +# Add the table to the publication, but do not refresh the subscription, so +# the subscriber has no pg_subscription_rel entry for it. +$node_publisher->safe_psql('postgres', + "ALTER PUBLICATION tap_pub_3 ADD TABLE tab_4"); + +$offset = -s $node_subscriber->logfile; + +$node_publisher->safe_psql('postgres', "INSERT INTO tab_4 VALUES (1)"); +$node_subscriber->wait_for_log( + qr/LOG: ( [A-Z0-9]+:)? skipping changes for relation "public\.tab_4"/, + $offset); + +ok( $node_subscriber->log_contains( + qr/DETAIL: ( [A-Z0-9]+:)? The published relation is not included in subscription "tap_sub"\./, + $offset), + 'relation not in the subscription is reported with the relation name'); + +# DDL makes the publisher send the relation again. The sync state has not +# changed, so this must not cause a second report. +$node_publisher->safe_psql('postgres', + "CREATE INDEX tab_4_a_idx ON tab_4 (a)"); +$node_publisher->safe_psql('postgres', "INSERT INTO tab_4 VALUES (2)"); +$node_publisher->wait_for_catchup('tap_sub'); + +my $log = + PostgreSQL::Test::Utils::slurp_file($node_subscriber->logfile, $offset); +my $count = () = $log =~ /skipping changes for relation "public\.tab_4"/g; +is($count, 1, 'relation is reported once, not again after DDL'); + +$result = + $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_4"); +is($result, qq(0), + 'changes for a relation not in the subscription are discarded'); + +# Once the relation is part of the subscription its changes are applied, and +# the initial copy brings across the rows that were discarded earlier. +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION"); +$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub'); +$node_publisher->safe_psql('postgres', "INSERT INTO tab_4 VALUES (3)"); +$node_publisher->wait_for_catchup('tap_sub'); + +$result = + $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_4"); +is($result, qq(3), + 'changes are applied once the relation is part of the subscription'); + +# Remove the relation from the subscription but keep it published. Changes +# were applied in between, so skipping them again must be reported again. +$node_publisher->safe_psql('postgres', + "ALTER PUBLICATION tap_pub_3 DROP TABLE tab_4"); +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION"); +$node_publisher->safe_psql('postgres', + "ALTER PUBLICATION tap_pub_3 ADD TABLE tab_4"); + +$offset = -s $node_subscriber->logfile; + +$node_publisher->safe_psql('postgres', "INSERT INTO tab_4 VALUES (4)"); +$node_subscriber->wait_for_log( + qr/LOG: ( [A-Z0-9]+:)? skipping changes for relation "public\.tab_4"/, + $offset); + +$result = + $node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_4"); +is($result, qq(3), + 'changes are discarded again once the relation is removed from the subscription' +); + # shutdown $node_subscriber->stop('fast'); $node_publisher->stop('fast'); -- 2.54.0 (Apple Git-157)