From 951f7e9d8e80dda7c8acbaf339b513459e47c3c1 Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Fri, 4 Dec 2026 20:16:35 +0800 Subject: [PATCH v1 3/5] Track conflict log table ownership only through its subscription. Subscriptions are stored in a shared catalog and recorded in pg_shdepend with dbid = InvalidOid, so DROP OWNED BY skips them (just like databases and tablespaces). However, heap_create_with_catalog() recorded a database-local SHARED_DEPENDENCY_OWNER entry (dbid = MyDatabaseId) for a subscription's conflict log table. As a result, DROP OWNED BY (even with CASCADE) ignored the subscription itself and attempted to drop the conflict log table directly, failing because of its DEPENDENCY_INTERNAL link to the subscription. In addition, REASSIGN OWNED BY updated the conflict log table's owner directly via pg_shdepend before visiting the subscription. Fix this by skipping initial dependency creation for conflict log tables in heap_create_with_catalog(), matching TOAST tables, so that conflict log table ownership is managed solely through the owning subscription. Also ignore default privileges for conflict log tables in heap_create_with_catalog() even when use_user_acl is true (such as CREATE TABLE in binary-upgrade mode), matching create_conflict_log_table() and ensuring relacl is NULL when skipping recordDependencyOnNewAcl(). Because users can still grant explicit privileges on a conflict log table, remove any SHARED_DEPENDENCY_ACL entries for the new owner in ATExecChangeOwner(). Discussion: https://postgr.es/m/CALdSSPhJJsrpVFroxSNLYCN=Pxj6Son6RzSLzxwYzmQwxWh4Rw@mail.gmail.com --- src/backend/catalog/heap.c | 24 +++++++++++++------ src/backend/catalog/pg_shdepend.c | 25 ++++++++++++++++++++ src/backend/commands/tablecmds.c | 15 ++++++++---- src/include/catalog/dependency.h | 3 +++ src/test/regress/expected/subscription.out | 27 ++++++++++++++++++++++ src/test/regress/sql/subscription.sql | 20 ++++++++++++++++ 6 files changed, 102 insertions(+), 12 deletions(-) diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 49ef6652203..8d83405c462 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -1554,8 +1554,17 @@ heap_create_with_catalog(const char *relname, case RELKIND_MATVIEW: case RELKIND_FOREIGN_TABLE: case RELKIND_PARTITIONED_TABLE: - relacl = get_user_default_acl(OBJECT_TABLE, ownerid, - relnamespace); + + /* + * Conflict log tables, like TOAST tables, never get default + * ACL entries, even when restored via CREATE TABLE during + * binary upgrade. + */ + if (IsConflictLogTableNamespace(relnamespace)) + relacl = NULL; + else + relacl = get_user_default_acl(OBJECT_TABLE, ownerid, + relnamespace); break; case RELKIND_SEQUENCE: relacl = get_user_default_acl(OBJECT_SEQUENCE, ownerid, @@ -1729,17 +1738,18 @@ heap_create_with_catalog(const char *relname, * dependencies for any roles mentioned in the default ACL. * * For composite types, these dependencies are tracked for the pg_type - * entry, so we needn't record them here. Likewise, TOAST tables don't - * need a namespace dependency (they live in a pinned namespace) nor an - * owner dependency (they depend indirectly through the parent table), nor - * should they have any ACL entries. The same applies for extension - * dependencies. + * entry, so we needn't record them here. Likewise, TOAST tables and + * conflict log tables don't need a namespace dependency (they live in + * pinned namespaces) nor an owner dependency (they depend indirectly + * through the parent table or owning subscription), nor should they have + * any initial ACL entries. The same applies for extension dependencies. * * Also, skip this in bootstrap mode, since we don't make dependencies * while bootstrapping. */ if (relkind != RELKIND_COMPOSITE_TYPE && relkind != RELKIND_TOASTVALUE && + !IsConflictLogTableNamespace(relnamespace) && !IsBootstrapProcessingMode()) { ObjectAddress myself, diff --git a/src/backend/catalog/pg_shdepend.c b/src/backend/catalog/pg_shdepend.c index 8a5d6e957a4..1c90c689375 100644 --- a/src/backend/catalog/pg_shdepend.c +++ b/src/backend/catalog/pg_shdepend.c @@ -359,6 +359,31 @@ changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId) table_close(sdepRel, RowExclusiveLock); } +/* + * deleteOwnerAclDependencies + * + * Remove any SHARED_DEPENDENCY_ACL entries for newOwnerId on an object (and + * its sub-objects) whose ownership is tracked through another catalog entry + * rather than its own SHARED_DEPENDENCY_OWNER record. See comments in + * changeDependencyOnOwner(). + */ +void +deleteOwnerAclDependencies(Oid classId, Oid objectId, Oid newOwnerId) +{ + Relation sdepRel; + + if (IsPinnedObject(AuthIdRelationId, newOwnerId)) + return; + + sdepRel = table_open(SharedDependRelationId, RowExclusiveLock); + + shdepDropDependency(sdepRel, classId, objectId, 0, true, + AuthIdRelationId, newOwnerId, + SHARED_DEPENDENCY_ACL); + + table_close(sdepRel, RowExclusiveLock); +} + /* * recordDependencyOnTablespace * diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 9755943f110..87acafff997 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -17580,12 +17580,17 @@ ATExecChangeOwner(Oid relationOid, Oid newOwnerId, bool recursing, LOCKMODE lock /* * Update owner dependency reference, if any. A composite type has * none, because it's tracked for the pg_type entry instead of here; - * indexes and TOAST tables don't have their own entries either. + * indexes, TOAST tables, and conflict log tables don't have their own + * entries either. Conflict log tables can still carry explicit ACL + * grants, so drop any ACL dependency for the new owner. */ - if (tuple_class->relkind != RELKIND_COMPOSITE_TYPE && - tuple_class->relkind != RELKIND_INDEX && - tuple_class->relkind != RELKIND_PARTITIONED_INDEX && - tuple_class->relkind != RELKIND_TOASTVALUE) + if (IsConflictLogTableClass(tuple_class)) + deleteOwnerAclDependencies(RelationRelationId, relationOid, + newOwnerId); + else if (tuple_class->relkind != RELKIND_COMPOSITE_TYPE && + tuple_class->relkind != RELKIND_INDEX && + tuple_class->relkind != RELKIND_PARTITIONED_INDEX && + tuple_class->relkind != RELKIND_TOASTVALUE) changeDependencyOnOwner(RelationRelationId, relationOid, newOwnerId); diff --git a/src/include/catalog/dependency.h b/src/include/catalog/dependency.h index 9782d0df2a7..bbe1d05da19 100644 --- a/src/include/catalog/dependency.h +++ b/src/include/catalog/dependency.h @@ -216,6 +216,9 @@ extern void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner); extern void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId); +extern void deleteOwnerAclDependencies(Oid classId, Oid objectId, + Oid newOwnerId); + extern void recordDependencyOnTablespace(Oid classId, Oid objectId, Oid tablespace); diff --git a/src/test/regress/expected/subscription.out b/src/test/regress/expected/subscription.out index e09c81b3b2a..b2fad184369 100644 --- a/src/test/regress/expected/subscription.out +++ b/src/test/regress/expected/subscription.out @@ -762,7 +762,34 @@ SELECT COUNT(*) FROM :conflict_log_table; 0 (1 row) +-- Grant a privilege to the new owner first to verify its ACL dependency on +-- the conflict log table is removed when ownership changes, and verify +-- REASSIGN OWNED BY handles conflict log tables through the subscription +-- rather than a separate table owner entry. +GRANT DELETE ON :conflict_log_table TO regress_subscription_user_dummy; RESET ROLE; +REASSIGN OWNED BY regress_subscription_user2 TO regress_subscription_user_dummy; +SELECT s.subowner::regrole, c.relowner::regrole, + (SELECT count(*) FROM pg_catalog.pg_shdepend d + WHERE d.classid = 'pg_catalog.pg_class'::regclass + AND d.objid = c.oid) AS clt_shdeps +FROM pg_catalog.pg_subscription s +JOIN pg_catalog.pg_class c ON c.oid = s.subconflictlogrelid +WHERE s.subname = 'regress_conflict_test1'; + subowner | relowner | clt_shdeps +---------------------------------+---------------------------------+------------ + regress_subscription_user_dummy | regress_subscription_user_dummy | 0 +(1 row) + +-- Verify DROP OWNED BY (even with CASCADE) skips subscriptions and their +-- conflict log tables rather than failing to drop the table directly. +DROP OWNED BY regress_subscription_user_dummy CASCADE; +SELECT COUNT(*) FROM :conflict_log_table; + count +------- + 0 +(1 row) + -- Restore the original subscription owner. ALTER SUBSCRIPTION regress_conflict_test1 OWNER TO regress_subscription_user; -- diff --git a/src/test/regress/sql/subscription.sql b/src/test/regress/sql/subscription.sql index 4ccce5c56f5..9bc053d0158 100644 --- a/src/test/regress/sql/subscription.sql +++ b/src/test/regress/sql/subscription.sql @@ -576,8 +576,28 @@ TRUNCATE TABLE :conflict_log_table; DELETE FROM :conflict_log_table; SELECT COUNT(*) FROM :conflict_log_table; +-- Grant a privilege to the new owner first to verify its ACL dependency on +-- the conflict log table is removed when ownership changes, and verify +-- REASSIGN OWNED BY handles conflict log tables through the subscription +-- rather than a separate table owner entry. +GRANT DELETE ON :conflict_log_table TO regress_subscription_user_dummy; + RESET ROLE; +REASSIGN OWNED BY regress_subscription_user2 TO regress_subscription_user_dummy; +SELECT s.subowner::regrole, c.relowner::regrole, + (SELECT count(*) FROM pg_catalog.pg_shdepend d + WHERE d.classid = 'pg_catalog.pg_class'::regclass + AND d.objid = c.oid) AS clt_shdeps +FROM pg_catalog.pg_subscription s +JOIN pg_catalog.pg_class c ON c.oid = s.subconflictlogrelid +WHERE s.subname = 'regress_conflict_test1'; + +-- Verify DROP OWNED BY (even with CASCADE) skips subscriptions and their +-- conflict log tables rather than failing to drop the table directly. +DROP OWNED BY regress_subscription_user_dummy CASCADE; +SELECT COUNT(*) FROM :conflict_log_table; + -- Restore the original subscription owner. ALTER SUBSCRIPTION regress_conflict_test1 OWNER TO regress_subscription_user; -- 2.54.0 (Apple Git-157)