From f034c6a33264a7bc19763de8a81ef8f02b17e48e Mon Sep 17 00:00:00 2001 From: Nisha Moond Date: Wed, 25 Nov 2026 10:12:07 +0530 Subject: [PATCH v2] Fix and document conflict log table handling in subscriptions. Previously, the conflict log table was dropped with DROP_CASCADE, both by DROP SUBSCRIPTION and by ALTER SUBSCRIPTION ... SET (conflict_log_destination = 'log'). So any user objects that depend on the table, such as views or functions referencing it or its row type, were silently dropped as well. These objects need not be owned by the subscription owner, and the ALTER case looks like a mere logging setting change to the user. This patch makes DROP SUBSCRIPTION pass its CASCADE | RESTRICT behavior down to the deletion of the conflict log table, and ALTER SUBSCRIPTION always uses RESTRICT, so both fail if other objects depend on the table, as DROP TABLE does. In DROP SUBSCRIPTION, the table is now dropped after the workers are stopped and the prepared-transaction check is done, but before the tablesync origins are dropped. Dropping an origin resets its progress in shared memory, which is not undone on rollback, so a failure after that would leave the tablesync origins without their progress. In The docs claimed that CASCADE and RESTRICT have no effect for DROP SUBSCRIPTION, which was no longer true after a5918fddf1. Update them accordingly. Also document the pg_subscription catalog columns subconflictlogrelid and subconflictlogdest in catalogs.sgml, and use proper markup for the term "conflict schema" in ddl.sgml. While at it, explain once, above IsConflictLogTableNamespace(), why operations on conflict log tables are restricted, and remove the repeated comments at each call site. Oversight in a5918fddf1. Author: Zsolt Parragi Author: Nisha Moond Author: Dilip Kumar Reviewed-by: Peter Smith Discussion: https://postgr.es/m/CAFiTN-sHeA%3DA%2B054KcOGue_q4tiT65rRvKxfdmJL3xjMwa2Kow%40mail.gmail.com Discussion: https://postgr.es/m/CAN4CZFOqiZtHOHAgd1mA325XQrsShj0C%3DvK2SBboUTFEmdoPZg%40mail.gmail.com --- doc/src/sgml/catalogs.sgml | 26 ++++++++ doc/src/sgml/ddl.sgml | 2 +- doc/src/sgml/ref/alter_subscription.sgml | 3 +- doc/src/sgml/ref/drop_subscription.sgml | 15 ++++- src/backend/catalog/catalog.c | 13 +++- src/backend/commands/policy.c | 5 -- src/backend/commands/subscriptioncmds.c | 74 +++++++++++++++++----- src/backend/commands/tablecmds.c | 20 ------ src/backend/commands/trigger.c | 10 --- src/backend/replication/logical/conflict.c | 8 ++- src/backend/rewrite/rewriteDefine.c | 10 --- src/test/regress/expected/subscription.out | 18 +++++- src/test/regress/sql/subscription.sql | 11 +++- 13 files changed, 143 insertions(+), 72 deletions(-) diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index 742388d355b..8c6112b45e1 100644 --- a/doc/src/sgml/catalogs.sgml +++ b/doc/src/sgml/catalogs.sgml @@ -8250,6 +8250,32 @@ SCRAM-SHA-256$<iteration count>:&l + + + subconflictlogrelid oid + (references pg_class.oid) + + + OID of the subscription conflict log table created in the + pg_conflict namespace, or zero if + conflict_log_destination + is set to log. + + + + + + subconflictlogdest text + + + The destination for conflict logging: log (server + log only), table (conflict log table only), or + all (both). See + conflict_log_destination + for details. + + + subconninfo text diff --git a/doc/src/sgml/ddl.sgml b/doc/src/sgml/ddl.sgml index d78d986756e..99b70bec652 100644 --- a/doc/src/sgml/ddl.sgml +++ b/doc/src/sgml/ddl.sgml @@ -3776,7 +3776,7 @@ REVOKE CREATE ON SCHEMA public FROM PUBLIC; The pg_conflict schema (sometimes referred to as the - conflict schema) contains system-managed conflict + conflict schema) contains system-managed conflict log tables used for logical replication conflict tracking. These tables are created and maintained by the system and are not intended for direct user manipulation. Unlike pg_catalog, the diff --git a/doc/src/sgml/ref/alter_subscription.sgml b/doc/src/sgml/ref/alter_subscription.sgml index 070a0d05d58..8751a18aba9 100644 --- a/doc/src/sgml/ref/alter_subscription.sgml +++ b/doc/src/sgml/ref/alter_subscription.sgml @@ -367,7 +367,8 @@ ALTER SUBSCRIPTION name RENAME TO < parameter is set to table or all, the system automatically creates the conflict log table. Conversely, if the destination is changed to log, logging to the table stops and the conflict log - table is automatically dropped. + table is automatically dropped. This fails if any other objects depend on + the conflict log table. diff --git a/doc/src/sgml/ref/drop_subscription.sgml b/doc/src/sgml/ref/drop_subscription.sgml index 909f6d1bf43..850711ac2f9 100644 --- a/doc/src/sgml/ref/drop_subscription.sgml +++ b/doc/src/sgml/ref/drop_subscription.sgml @@ -70,12 +70,21 @@ DROP SUBSCRIPTION [ IF EXISTS ] name CASCADE - RESTRICT + + + Automatically drop objects that depend on the subscription's conflict + log table (such as views), and in turn all objects that depend on those + objects (see ). + + + + + RESTRICT - These key words do not have any effect, since there are no dependencies - on subscriptions. + Refuse to drop the subscription if any objects depend on its conflict + log table. This is the default. diff --git a/src/backend/catalog/catalog.c b/src/backend/catalog/catalog.c index ad96dab0381..6248e0f3ce9 100644 --- a/src/backend/catalog/catalog.c +++ b/src/backend/catalog/catalog.c @@ -277,7 +277,9 @@ IsToastClass(Form_pg_class reltuple) * IsConflictLogTableClass * True iff pg_class tuple represents a Conflict Log Table. * - * Does not perform any catalog accesses. + * Does not perform any catalog accesses. See + * IsConflictLogTableNamespace() for why operations on these tables are + * restricted. */ bool IsConflictLogTableClass(Form_pg_class reltuple) @@ -326,6 +328,15 @@ IsToastNamespace(Oid namespaceId) * True iff namespace is pg_conflict. * * Does not perform any catalog accesses. + * + * Tables in the pg_conflict namespace are conflict log tables, created and + * maintained by the system to record logical replication conflicts. The + * apply worker's inserts depend on their fixed structure, so they are treated + * as system-managed: call sites across the backend use this (and + * IsConflictLogTableClass()) to reject operations that could disrupt logging, + * such as DDL, manual INSERT/UPDATE/MERGE, and row locking. SELECT, DELETE + * and TRUNCATE are the only data operations allowed, so that users can still + * query and prune conflict rows. */ bool IsConflictLogTableNamespace(Oid namespaceId) diff --git a/src/backend/commands/policy.c b/src/backend/commands/policy.c index 093af147416..1da5857e2af 100644 --- a/src/backend/commands/policy.c +++ b/src/backend/commands/policy.c @@ -79,11 +79,6 @@ RangeVarCallbackForPolicy(const RangeVar *rv, Oid relid, Oid oldrelid, if (!object_ownercheck(RelationRelationId, relid, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(get_rel_relkind(relid)), rv->relname); - /* - * Conflict log tables are used internally for logical replication - * conflict logging and should not be modified directly, as it could - * disrupt conflict logging. - */ if (IsConflictLogTableClass(classform)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index 52d32f436e6..cb06018b0d8 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -150,7 +150,9 @@ static bool alter_sub_conflict_log_dest(Subscription *sub, ConflictLogDest newlogdest, Oid *conflicttablerelid); static void drop_sub_conflict_log_table(Oid subid, char *subname, - Oid subconflictlogrelid); + Oid subconflictlogrelid, + DropBehavior behavior, + bool is_drop_sub); /* * Common option parsing function for CREATE and ALTER SUBSCRIPTION commands. @@ -1628,7 +1630,8 @@ alter_sub_conflict_log_dest(Subscription *sub, ConflictLogDest oldlogdest, if (!want_table) { drop_sub_conflict_log_table(sub->oid, sub->name, - sub->conflictlogrelid); + sub->conflictlogrelid, DROP_RESTRICT, + false); update_relid = true; } } @@ -2625,9 +2628,13 @@ construct_subserver_conninfo(Oid subserver, Oid subowner, char **err) * Drop subscription's conflict log table * * The conflict log table is registered as an internal dependency of the - * subscription. This function removes the dependency by performing a - * cascading deletion on the subscription object, which in turn drops the - * associated conflict log table. + * subscription, so it can't be dropped on its own. With is_drop_sub, the + * table is dropped through the subscription object, which keeps the + * subscription itself. Otherwise, it removes the internal dependency and + * drops the table directly. + * + * With DROP_RESTRICT, this fails if other objects depend on the conflict log + * table. * * This is used to clean up conflict log tables that are no longer required, * preventing accumulation of stale or orphaned relations. @@ -2637,7 +2644,8 @@ construct_subserver_conninfo(Oid subserver, Oid subowner, char **err) * mechanism. */ static void -drop_sub_conflict_log_table(Oid subid, char *subname, Oid subconflictlogrelid) +drop_sub_conflict_log_table(Oid subid, char *subname, Oid subconflictlogrelid, + DropBehavior behavior, bool is_drop_sub) { /* Drop any dependent conflict log table */ if (OidIsValid(subconflictlogrelid)) @@ -2650,14 +2658,39 @@ drop_sub_conflict_log_table(Oid subid, char *subname, Oid subconflictlogrelid) elog(ERROR, "cache lookup failed for relation %u", subconflictlogrelid); - /* - * By using PERFORM_DELETION_SKIP_ORIGINAL, we ensure that only the - * conflict log table is deleted while the subscription remains. - */ - ObjectAddressSet(object, SubscriptionRelationId, subid); - performDeletion(&object, DROP_CASCADE, - PERFORM_DELETION_INTERNAL | - PERFORM_DELETION_SKIP_ORIGINAL); + if (is_drop_sub) + { + /* + * The subscription is being dropped, so drop the table through it + * as its internal part. + * + * By using PERFORM_DELETION_SKIP_ORIGINAL, we ensure that only + * the conflict log table is deleted while the subscription + * remains. + */ + ObjectAddressSet(object, SubscriptionRelationId, subid); + performDeletion(&object, behavior, + PERFORM_DELETION_INTERNAL | + PERFORM_DELETION_SKIP_ORIGINAL); + } + else + { + /* + * The subscription stays, so drop only the table, and a RESTRICT + * failure names the table rather than the subscription. Remove + * the internal dependency first, or the table can't be dropped + * without the subscription. + */ + deleteDependencyRecordsForSpecific(RelationRelationId, + subconflictlogrelid, + DEPENDENCY_INTERNAL, + SubscriptionRelationId, + subid); + CommandCounterIncrement(); + + ObjectAddressSet(object, RelationRelationId, subconflictlogrelid); + performDeletion(&object, behavior, PERFORM_DELETION_INTERNAL); + } ereport(NOTICE, errmsg("dropped conflict log table \"%s\" for subscription \"%s\"", @@ -2829,6 +2862,16 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel) errdetail("A prepared transaction of the subscription may hold a lock on the subscription's conflict log table."), errhint("Resolve these transactions and try again.")); + /* + * Drop the conflict log table. This waits for any lock on the table, so + * do it only after the workers are stopped and prepared transactions are + * ruled out above. With RESTRICT it fails if other objects depend on the + * table, so do it before the non-transactional cleanup of origins and the + * slot below. + */ + drop_sub_conflict_log_table(subid, subname, subconflictlogrelid, + stmt->behavior, true); + /* * Remove the no-longer-useful entry in the launcher's table of apply * worker start times. @@ -2870,9 +2913,6 @@ DropSubscription(DropSubscriptionStmt *stmt, bool isTopLevel) replorigin_drop_by_name(originname, true, false); } - /* Drop subscription's conflict log table */ - drop_sub_conflict_log_table(subid, subname, subconflictlogrelid); - /* Clean up dependencies */ deleteDependencyRecordsFor(SubscriptionRelationId, subid, false); deleteSharedDependencyRecordsFor(SubscriptionRelationId, subid, 0); diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index e67c9c3768b..442d5233c1f 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -3888,11 +3888,6 @@ renameatt_check(Oid myrelid, Form_pg_class classform, bool recursing) aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(get_rel_relkind(myrelid)), NameStr(classform->relname)); - /* - * Conflict log tables are used internally for logical replication - * conflict logging and should not be modified directly, as it could - * disrupt conflict logging. - */ if (IsConflictLogTableClass(classform)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), @@ -10817,11 +10812,6 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel, errmsg("referenced relation \"%s\" is not a table", RelationGetRelationName(pkrel)))); - /* - * Conflict log tables are used internally for logical replication - * conflict logging and should not be referenced by foreign keys, as it - * could disrupt conflict logging. - */ if (IsConflictLogTableClass(pkrel->rd_rel)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), @@ -20957,11 +20947,6 @@ RangeVarCallbackOwnsRelation(const RangeVar *relation, aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(get_rel_relkind(relId)), relation->relname); - /* - * Conflict log tables are used internally for logical replication - * conflict logging and should not be modified directly, as it could - * disrupt conflict logging. - */ if (IsConflictLogTableClass((Form_pg_class) GETSTRUCT(tuple))) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), @@ -21004,11 +20989,6 @@ RangeVarCallbackForAlterRelation(const RangeVar *rv, Oid relid, Oid oldrelid, if (!object_ownercheck(RelationRelationId, relid, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(get_rel_relkind(relid)), rv->relname); - /* - * Conflict log tables are used internally for logical replication - * conflict logging and should not be altered directly, as it could - * disrupt conflict logging. - */ if (IsConflictLogTableClass(classform)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index 838eae31660..df267427c25 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -315,11 +315,6 @@ CreateTriggerFiringOn(const CreateTrigStmt *stmt, const char *queryString, RelationGetRelationName(rel)), errdetail_relkind_not_supported(rel->rd_rel->relkind))); - /* - * Conflict log tables are used internally for logical replication - * conflict logging and should not have triggers, as it could disrupt - * conflict logging. - */ if (IsConflictLogTableClass(rel->rd_rel)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), @@ -1471,11 +1466,6 @@ RangeVarCallbackForRenameTrigger(const RangeVar *rv, Oid relid, Oid oldrelid, if (!object_ownercheck(RelationRelationId, relid, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(get_rel_relkind(relid)), rv->relname); - /* - * Conflict log tables are used internally for logical replication - * conflict logging and should not have triggers, as it could disrupt - * conflict logging. - */ if (IsConflictLogTableClass(form)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), diff --git a/src/backend/replication/logical/conflict.c b/src/backend/replication/logical/conflict.c index 0eede891aff..73c4077d05a 100644 --- a/src/backend/replication/logical/conflict.c +++ b/src/backend/replication/logical/conflict.c @@ -212,10 +212,14 @@ create_conflict_log_table(Oid subid, char *subname, Oid subowner) /* Build the tuple descriptor for the new table. */ tupdesc = create_conflict_log_table_tupdesc(); - /* Create conflict log table. */ + /* + * Create conflict log table. Place it in the database's default + * tablespace, ignoring default_tablespace, so its placement does not + * depend on the session that creates it. + */ relid = heap_create_with_catalog(relname, PG_CONFLICT_NAMESPACE, - 0, /* tablespace */ + InvalidOid, /* tablespace */ InvalidOid, /* relid */ InvalidOid, /* reltypeid */ InvalidOid, /* reloftypeid */ diff --git a/src/backend/rewrite/rewriteDefine.c b/src/backend/rewrite/rewriteDefine.c index 043ddd8970a..542f91638a0 100644 --- a/src/backend/rewrite/rewriteDefine.c +++ b/src/backend/rewrite/rewriteDefine.c @@ -264,11 +264,6 @@ DefineQueryRewrite(const char *rulename, RelationGetRelationName(event_relation)), errdetail_relkind_not_supported(event_relation->rd_rel->relkind))); - /* - * Conflict log tables are used internally for logical replication - * conflict logging and should not have rules, as it could disrupt - * conflict logging. - */ if (IsConflictLogTableClass(event_relation->rd_rel)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), @@ -771,11 +766,6 @@ RangeVarCallbackForRenameRule(const RangeVar *rv, Oid relid, Oid oldrelid, errmsg("relation \"%s\" cannot have rules", rv->relname), errdetail_relkind_not_supported(form->relkind))); - /* - * Conflict log tables are used internally for logical replication - * conflict logging and should not have rules, as it could disrupt - * conflict logging. - */ if (IsConflictLogTableClass(form)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), diff --git a/src/test/regress/expected/subscription.out b/src/test/regress/expected/subscription.out index 1b525fc37fb..e09c81b3b2a 100644 --- a/src/test/regress/expected/subscription.out +++ b/src/test/regress/expected/subscription.out @@ -863,10 +863,20 @@ ERROR: 42501 -- CLEANUP: DROP SUBSCRIPTION reaps the table ALTER SUBSCRIPTION regress_conflict_test1 DISABLE; ALTER SUBSCRIPTION regress_conflict_test1 SET (slot_name = NONE); +-- fail - objects depending on the conflict log table need CASCADE +CREATE VIEW regress_clt_view AS SELECT relid FROM :clt1; +\set VERBOSITY sqlstate +ALTER SUBSCRIPTION regress_conflict_test1 SET (conflict_log_destination = 'log'); +ERROR: 2BP01 +DROP SUBSCRIPTION regress_conflict_test1; +ERROR: 2BP01 +DROP SUBSCRIPTION regress_conflict_test1 RESTRICT; +ERROR: 2BP01 +\set VERBOSITY default -- Verify the table OID for reap check SELECT 'pg_conflict.pg_conflict_log_' || oid AS internal_tablename FROM pg_subscription WHERE subname = 'regress_conflict_test1' \gset SET client_min_messages = WARNING; -DROP SUBSCRIPTION regress_conflict_test1; +DROP SUBSCRIPTION regress_conflict_test1 CASCADE; -- should return NULL, meaning the conflict log table was reaped via dependency SELECT to_regclass(:'internal_tablename'); to_regclass @@ -874,6 +884,12 @@ SELECT to_regclass(:'internal_tablename'); (1 row) +SELECT to_regclass('regress_clt_view'); + to_regclass +------------- + +(1 row) + -- -- Additional Namespace and Table Protection Tests -- diff --git a/src/test/regress/sql/subscription.sql b/src/test/regress/sql/subscription.sql index 0701294ed85..4ccce5c56f5 100644 --- a/src/test/regress/sql/subscription.sql +++ b/src/test/regress/sql/subscription.sql @@ -661,14 +661,23 @@ DROP TABLE :clt1; ALTER SUBSCRIPTION regress_conflict_test1 DISABLE; ALTER SUBSCRIPTION regress_conflict_test1 SET (slot_name = NONE); +-- fail - objects depending on the conflict log table need CASCADE +CREATE VIEW regress_clt_view AS SELECT relid FROM :clt1; +\set VERBOSITY sqlstate +ALTER SUBSCRIPTION regress_conflict_test1 SET (conflict_log_destination = 'log'); +DROP SUBSCRIPTION regress_conflict_test1; +DROP SUBSCRIPTION regress_conflict_test1 RESTRICT; +\set VERBOSITY default + -- Verify the table OID for reap check SELECT 'pg_conflict.pg_conflict_log_' || oid AS internal_tablename FROM pg_subscription WHERE subname = 'regress_conflict_test1' \gset SET client_min_messages = WARNING; -DROP SUBSCRIPTION regress_conflict_test1; +DROP SUBSCRIPTION regress_conflict_test1 CASCADE; -- should return NULL, meaning the conflict log table was reaped via dependency SELECT to_regclass(:'internal_tablename'); +SELECT to_regclass('regress_clt_view'); -- -- Additional Namespace and Table Protection Tests -- 2.54.0 (Apple Git-157)