From ca3d27234106e2edfbb2ca5bd2d952471f6be6cd Mon Sep 17 00:00:00 2001 From: Shlok Kyal Date: Fri, 20 Nov 2026 16:22:41 +0530 Subject: [PATCH v2 1/2] Fire deferrable constraint rechecks during logical replication Create internal recheck triggers for deferrable primary key, unique, and exclusion constraints as ENABLE ALWAYS so they also fire in replica mode. For initially deferred constraints, this permits transient conflicts while rejecting violations that remain at transaction end. --- doc/src/sgml/config.sgml | 7 ++ doc/src/sgml/logical-replication.sgml | 6 ++ src/backend/catalog/index.c | 12 ++- src/test/subscription/t/003_constraints.pl | 89 ++++++++++++++++++++++ 4 files changed, 111 insertions(+), 3 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 28440932a73..83870eaa766 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -10831,6 +10831,13 @@ 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 created as + ENABLE ALWAYS triggers, so they also fire when this + parameter is set to replica. + diff --git a/doc/src/sgml/logical-replication.sgml b/doc/src/sgml/logical-replication.sgml index 4701a3d9d18..4aff39ef1d6 100644 --- a/doc/src/sgml/logical-replication.sgml +++ b/doc/src/sgml/logical-replication.sgml @@ -2488,6 +2488,12 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER and statement triggers for INSERT. + + Constraints declared as DEFERRABLE use recheck triggers + that are created as ENABLE ALWAYS triggers, so they also + fire during logical replication. + + Initial Snapshot diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 99413556572..9fe67e2f6eb 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -2073,9 +2073,15 @@ index_constraint_create(Relation heapRelation, trigger->initdeferred = initdeferred; trigger->constrrel = NULL; - (void) CreateTrigger(trigger, NULL, RelationGetRelid(heapRelation), - InvalidOid, conOid, indexRelationId, InvalidOid, - InvalidOid, NULL, true, false); + /* + * Create trigger with TRIGGER_FIRES_ALWAYS, so the recheck trigger is + * fired in replica mode too, so logical replication cannot leave + * constraint violations undetected. + */ + (void) CreateTriggerFiringOn(trigger, NULL, RelationGetRelid(heapRelation), + InvalidOid, conOid, indexRelationId, InvalidOid, + InvalidOid, NULL, true, false, + TRIGGER_FIRES_ALWAYS); } /* diff --git a/src/test/subscription/t/003_constraints.pl b/src/test/subscription/t/003_constraints.pl index 237e113013b..2c22ff87035 100644 --- a/src/test/subscription/t/003_constraints.pl +++ b/src/test/subscription/t/003_constraints.pl @@ -135,6 +135,95 @@ $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'); + +# Check the firing mode of each constraint's internal recheck trigger. +$result = $node_subscriber->safe_psql( + 'postgres', + "SELECT tgenabled FROM pg_trigger + WHERE tgrelid = 'tab_deferred'::regclass + AND tgfoid = 'unique_key_recheck'::regproc"); +is($result, qq(A\nA\nA), + 'all three recheck triggers are always enabled on subscriber'); + +# 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