From 703d0e272d6469d3ae263d72682e9f9e4d879c37 Mon Sep 17 00:00:00 2001 From: Shlok Kyal Date: Fri, 20 Nov 2026 16:22:41 +0530 Subject: [PATCH v2 2/2] Reject initially immediate deferrable subscription target constraints Reject DEFERRABLE INITIALLY IMMEDIATE primary key, unique, and exclusion constraints on logical replication targets, including their partitions. Validate targets during subscription creation, publication refresh, and apply, and reject adding such constraints to registered target tables. Logical replication preserves transaction boundaries but not publisher statement boundaries. Initially immediate rechecks during apply could therefore reject transient conflicts within an otherwise valid multi-row change. --- doc/src/sgml/logical-replication.sgml | 5 +- src/backend/catalog/index.c | 33 ++++++++++++++ src/backend/executor/execReplication.c | 48 ++++++++++++++++++++ src/test/subscription/t/003_constraints.pl | 53 ++++++++++++++++++++++ src/test/subscription/t/035_conflicts.pl | 2 +- 5 files changed, 139 insertions(+), 2 deletions(-) diff --git a/doc/src/sgml/logical-replication.sgml b/doc/src/sgml/logical-replication.sgml index 4aff39ef1d6..f667bb46143 100644 --- a/doc/src/sgml/logical-replication.sgml +++ b/doc/src/sgml/logical-replication.sgml @@ -2491,7 +2491,10 @@ CONTEXT: processing remote data for replication origin "pg_16395" during "INSER Constraints declared as DEFERRABLE use recheck triggers that are created as ENABLE ALWAYS triggers, so they also - fire during logical replication. + fire during logical replication. Subscriber tables, including their + partitions, cannot have unique, primary key, or exclusion constraints + declared DEFERRABLE INITIALLY IMMEDIATE. Subscription + rejects tables with these constraints. diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 9fe67e2f6eb..ed67861bdca 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -71,6 +71,7 @@ #include "storage/predicate.h" #include "storage/smgr.h" #include "utils/builtins.h" +#include "utils/catcache.h" #include "utils/fmgroids.h" #include "utils/guc.h" #include "utils/inval.h" @@ -1945,6 +1946,38 @@ index_constraint_create(Relation heapRelation, /* constraint creation support doesn't work while bootstrapping */ Assert(!IsBootstrapProcessingMode()); + /* + * Reject DEFERRABLE INITIALLY IMMEDIATE constraints on replication + * targets: apply worker does not preserve publisher statement boundaries, + * so statement-end rechecks could reject valid transient conflicts. + */ + if (deferrable && !initdeferred) + { + List *relids = NIL; + + if (heapRelation->rd_rel->relispartition) + relids = get_partition_ancestors(RelationGetRelid(heapRelation)); + relids = lcons_oid(RelationGetRelid(heapRelation), relids); + + foreach_oid(relid, relids) + { + CatCList *subscriptions = SearchSysCacheList1(SUBSCRIPTIONRELMAP, + ObjectIdGetDatum(relid)); + bool issubscriber = subscriptions->n_members > 0; + + ReleaseSysCacheList(subscriptions); + + if (issubscriber) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot create constraint \"%s\" on logical replication target relation \"%s\"", + constraintName, RelationGetRelationName(heapRelation)), + errdetail("Deferrable initially immediate unique, primary key, and exclusion constraints are not supported."))); + } + + list_free(relids); + } + /* enforce system-table restriction */ if (!allow_system_table_mods && IsSystemRelation(heapRelation) && diff --git a/src/backend/executor/execReplication.c b/src/backend/executor/execReplication.c index 66a788e9e3d..c13a3e18669 100644 --- a/src/backend/executor/execReplication.c +++ b/src/backend/executor/execReplication.c @@ -19,12 +19,14 @@ #include "access/genam.h" #include "access/gist.h" #include "access/relscan.h" +#include "access/table.h" #include "access/tableam.h" #include "access/transam.h" #include "access/xact.h" #include "access/heapam.h" #include "catalog/catalog.h" #include "catalog/pg_am_d.h" +#include "catalog/pg_inherits.h" #include "commands/trigger.h" #include "executor/executor.h" #include "executor/nodeModifyTable.h" @@ -32,6 +34,7 @@ #include "replication/logicalrelation.h" #include "storage/lmgr.h" #include "utils/builtins.h" +#include "utils/fmgroids.h" #include "utils/lsyscache.h" #include "utils/rel.h" #include "utils/snapmgr.h" @@ -1280,4 +1283,49 @@ CheckSubscriptionTargetRel(Oid relid, char remoterelkind, nspname, relname, remoterelkind == RELKIND_SEQUENCE ? "sequence" : "table", localrelkind == RELKIND_SEQUENCE ? "sequence" : "table")); + + /* + * Reject tables with DEFERRABLE INITIALLY IMMEDIATE constraints on the + * relation. + */ + if (localrelkind != RELKIND_SEQUENCE) + { + List *relids; + + if (localrelkind == RELKIND_PARTITIONED_TABLE) + relids = find_all_inheritors(relid, AccessShareLock, NULL); + else + { + LockRelationOid(relid, AccessShareLock); + relids = list_make1_oid(relid); + } + + foreach_oid(targetrelid, relids) + { + Relation rel = table_open(targetrelid, NoLock); + TriggerDesc *trigdesc = rel->trigdesc; + + if (trigdesc != NULL) + { + for (int index = 0; index < trigdesc->numtriggers; index++) + { + Trigger *trigger = &trigdesc->triggers[index]; + + if (trigger->tgisinternal && + trigger->tgfoid == F_UNIQUE_KEY_RECHECK && + trigger->tgdeferrable && !trigger->tginitdeferred) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot use relation \"%s.%s\" as logical replication target", + get_namespace_name(RelationGetNamespace(rel)), + RelationGetRelationName(rel)), + errdetail("Deferrable initially immediate unique, primary key, and exclusion constraints are not supported."))); + } + } + + table_close(rel, AccessShareLock); + } + + list_free(relids); + } } diff --git a/src/test/subscription/t/003_constraints.pl b/src/test/subscription/t/003_constraints.pl index 2c22ff87035..5c1c07a6cbc 100644 --- a/src/test/subscription/t/003_constraints.pl +++ b/src/test/subscription/t/003_constraints.pl @@ -218,12 +218,65 @@ $node_subscriber->safe_psql( )); $node_publisher->wait_for_catchup('tap_sub'); +# Adding an initially immediate constraint to a subscribed table must fail. +my ($ret, $stdout, $stderr) = $node_subscriber->psql('postgres', + "ALTER TABLE tab_deferred ADD CONSTRAINT immediate_unique UNIQUE (val) DEFERRABLE INITIALLY IMMEDIATE" +); +like( + $stderr, + qr/cannot create constraint "immediate_unique" on logical replication target relation "tab_deferred"/, + 'constraint DDL reports subscription target restriction'); + # 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'); +# Create one initially immediate constraint per table on both nodes. +for my $node ($node_publisher, $node_subscriber) +{ + $node->safe_psql( + 'postgres', qq( +CREATE TABLE immediate_primary (val int PRIMARY KEY DEFERRABLE INITIALLY IMMEDIATE); +CREATE TABLE immediate_unique (val int UNIQUE DEFERRABLE INITIALLY IMMEDIATE); +CREATE TABLE immediate_exclusion (val int4range, EXCLUDE USING gist (val WITH &&) DEFERRABLE INITIALLY IMMEDIATE); +)); +} + +# Publish only one table at a time, and reject each subscription attempt. +$node_publisher->safe_psql('postgres', + 'CREATE PUBLICATION tap_pub FOR TABLE immediate_primary'); +($ret, $stdout, $stderr) = $node_subscriber->psql('postgres', + "CREATE SUBSCRIPTION rejected_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub" +); +like( + $stderr, + qr/Deferrable initially immediate unique, primary key, and exclusion constraints are not supported/, + 'primary key reports unsupported timing during subscription creation'); + +$node_publisher->safe_psql('postgres', + 'ALTER PUBLICATION tap_pub SET TABLE immediate_unique'); +($ret, $stdout, $stderr) = $node_subscriber->psql('postgres', + "CREATE SUBSCRIPTION rejected_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub" +); +like( + $stderr, + qr/Deferrable initially immediate unique, primary key, and exclusion constraints are not supported/, + 'unique constraint reports unsupported timing during subscription creation' +); + +$node_publisher->safe_psql('postgres', + 'ALTER PUBLICATION tap_pub SET TABLE immediate_exclusion'); +($ret, $stdout, $stderr) = $node_subscriber->psql('postgres', + "CREATE SUBSCRIPTION rejected_sub CONNECTION '$publisher_connstr' PUBLICATION tap_pub" +); +like( + $stderr, + qr/Deferrable initially immediate unique, primary key, and exclusion constraints are not supported/, + 'exclusion constraint reports unsupported timing during subscription creation' +); + $node_subscriber->stop('fast'); $node_publisher->stop('fast'); diff --git a/src/test/subscription/t/035_conflicts.pl b/src/test/subscription/t/035_conflicts.pl index 48ce95d9d13..2247b8db2f6 100644 --- a/src/test/subscription/t/035_conflicts.pl +++ b/src/test/subscription/t/035_conflicts.pl @@ -412,7 +412,7 @@ $node_A->poll_query_until('postgres', # Then delete a local row that has the same key but a different value. $node_A->safe_psql( 'postgres', " - ALTER TABLE tab_defer ADD PRIMARY KEY (a) DEFERRABLE; + ALTER TABLE tab_defer ADD PRIMARY KEY (a) DEFERRABLE INITIALLY DEFERRED; INSERT INTO tab_defer VALUES (1, 10); DELETE FROM tab_defer WHERE a = 1;"); -- 2.34.1