From 336fb13086d9b05211b48b0e1202fb3b659d9f23 Mon Sep 17 00:00:00 2001 From: Shlok Kyal Date: Tue, 24 Nov 2026 13:51:38 +0530 Subject: [PATCH v3 1/2] Fire deferrable constraint recheck triggers in replica mode Deferrable unique, primary key, and exclusion constraints rely on an internal trigger to recheck potential conflicts. Replica mode skipped that trigger by default, allowing logical replication to commit rows that violated a subscriber constraint. Allow the internal recheck trigger to fire in replica mode. For subscription with run_as_owner = false and if the table owner cannot SET ROLE to the subscription owner, the apply worker runs the change in a security-restricted operation and cannot defer a recheck until commit. This can stop replication even when a later change in the same transaction would resolve the conflict. --- doc/src/sgml/config.sgml | 6 ++ doc/src/sgml/logical-replication.sgml | 14 ++++ src/backend/commands/trigger.c | 8 ++- src/test/subscription/t/003_constraints.pl | 80 ++++++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 77cc523ce50..9fd63395925 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -10835,6 +10835,12 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv; to replica also disables all foreign key checks, which can leave data in an inconsistent state if improperly used. + + + Recheck triggers for unique, primary key, and exclusion constraints + declared DEFERRABLE are invoked even when this + parameter is set to replica. + diff --git a/doc/src/sgml/logical-replication.sgml b/doc/src/sgml/logical-replication.sgml index 66dbb65aa48..0225901a666 100644 --- a/doc/src/sgml/logical-replication.sgml +++ b/doc/src/sgml/logical-replication.sgml @@ -2776,6 +2776,20 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER and statement triggers for INSERT. + + Constraints declared as DEFERRABLE use recheck triggers + that fired by default during logical replication. + + + + When a subscription is configured with run_as_owner = false, + the apply worker applies changes as the table owner. If the table owner + cannot SET ROLE to the subscription owner, the worker + applies those changes in a security-restricted operation. Such an operation + cannot defer a constraint recheck trigger until commit, so replication stops + if a change requires a deferred recheck. + + Initial Snapshot diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index df267427c25..156c5462dc7 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -3526,8 +3526,12 @@ TriggerEnabled(EState *estate, ResultRelInfo *relinfo, /* Check replication-role-dependent enable state */ if (SessionReplicationRole == SESSION_REPLICATION_ROLE_REPLICA) { - if (trigger->tgenabled == TRIGGER_FIRES_ON_ORIGIN || - trigger->tgenabled == TRIGGER_DISABLED) + if (trigger->tgenabled == TRIGGER_DISABLED) + return false; + + /* The uniqueness recheck must run in replica mode. */ + if (trigger->tgenabled == TRIGGER_FIRES_ON_ORIGIN && + !(trigger->tgisinternal && trigger->tgfoid == F_UNIQUE_KEY_RECHECK)) return false; } else /* ORIGIN or LOCAL role */ diff --git a/src/test/subscription/t/003_constraints.pl b/src/test/subscription/t/003_constraints.pl index 237e113013b..6742593d5bc 100644 --- a/src/test/subscription/t/003_constraints.pl +++ b/src/test/subscription/t/003_constraints.pl @@ -135,6 +135,86 @@ $result = $node_subscriber->safe_psql('postgres', is($result, qq(2|1|2), 'check column trigger applied even on update for other column'); +# Create all three initially deferred constraints on both nodes. +for my $node ($node_publisher, $node_subscriber) +{ + $node->safe_psql( + 'postgres', qq( +CREATE TABLE tab_deferred (id int, val int, span int4range, + CONSTRAINT deferred_primary PRIMARY KEY (id) DEFERRABLE INITIALLY DEFERRED, + CONSTRAINT deferred_unique UNIQUE (val) DEFERRABLE INITIALLY DEFERRED, + CONSTRAINT deferred_exclusion EXCLUDE USING gist (span WITH &&) DEFERRABLE INITIALLY DEFERRED); +)); +} + +# Stop apply after a constraint violation so the failed insert can be checked. +$node_subscriber->safe_psql( + 'postgres', qq( + ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION; + ALTER SUBSCRIPTION tap_sub SET (disable_on_error = true); +)); +my $disabled_query = + "SELECT NOT subenabled FROM pg_subscription WHERE subname = 'tap_sub'"; +$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub'); + +# Insert the subscriber-only rows for all three conflicts at once. +$node_subscriber->safe_psql( + 'postgres', + "INSERT INTO tab_deferred VALUES + (1, 99, '[90,91)'), (98, 20, '[91,92)'), (99, 98, '[3,4)')"); + +# The incoming row duplicates the primary key of a subscriber-only row. +my $log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "INSERT INTO tab_deferred VALUES (1, 10, '[1,2)')"); +$node_subscriber->wait_for_log( + qr/duplicate key value violates unique constraint "deferred_primary"/, + $log_offset); +$node_subscriber->poll_query_until('postgres', $disabled_query) + or die 'Timed out waiting for subscription to be disabled'; +$node_subscriber->safe_psql( + 'postgres', qq( + DELETE FROM tab_deferred WHERE id = 1; + ALTER SUBSCRIPTION tap_sub ENABLE; +)); +$node_publisher->wait_for_catchup('tap_sub'); + +# The incoming row has a different primary key but duplicates the unique value. +$node_publisher->safe_psql('postgres', + "INSERT INTO tab_deferred VALUES (2, 20, '[2,3)')"); +$node_subscriber->wait_for_log( + qr/duplicate key value violates unique constraint "deferred_unique"/, + $log_offset); +$node_subscriber->poll_query_until('postgres', $disabled_query) + or die 'Timed out waiting for subscription to be disabled'; +$node_subscriber->safe_psql( + 'postgres', qq( + DELETE FROM tab_deferred WHERE id = 98; + ALTER SUBSCRIPTION tap_sub ENABLE; +)); +$node_publisher->wait_for_catchup('tap_sub'); + +# Only the range overlaps with the subscriber-only row. +$node_publisher->safe_psql('postgres', + "INSERT INTO tab_deferred VALUES (3, 30, '[3,4)')"); +$node_subscriber->wait_for_log( + qr/conflicting key value violates exclusion constraint "deferred_exclusion"/, + $log_offset); +$node_subscriber->poll_query_until('postgres', $disabled_query) + or die 'Timed out waiting for subscription to be disabled'; +$node_subscriber->safe_psql( + 'postgres', qq( + DELETE FROM tab_deferred WHERE id = 99; + ALTER SUBSCRIPTION tap_sub ENABLE; +)); +$node_publisher->wait_for_catchup('tap_sub'); + +# Cleanup +$node_subscriber->safe_psql('postgres', + 'DROP SUBSCRIPTION tap_sub; DROP TABLE tab_deferred'); +$node_publisher->safe_psql('postgres', + 'DROP TABLE tab_deferred; DROP PUBLICATION tap_pub'); + $node_subscriber->stop('fast'); $node_publisher->stop('fast'); -- 2.34.1