Schema ownership preflight checks CREATE on the wrong role
Parent: #627
Schema ownership has a PostgreSQL-specific authority rule that differs from other ownership transfers. PostgreSQL checks database CREATE on the effective current role for ALTER SCHEMA ... OWNER, while table and routine ownership check the destination owner’s CREATE authority. The implementation currently inspects the desired new owner for every ownership transfer.
Reproduced on PostgreSQL 15, 16, 17, and 18 with deployer using SET ROLE old_owner for the emitted transfer:
managedis owned byold_owner.deployercan SET ROLE toold_ownerandnew_owner.old_ownercan SET ROLE tonew_owner.- Desired state transfers
managedtonew_owner.
Both error directions reproduce:
- When only
old_ownerhas databaseCREATE, the emittedSET ROLE old_owner; ALTER SCHEMA managed OWNER TO new_ownersucceeds, but preflight rejects the valid plan becausenew_ownerlacksCREATE. - When only
new_ownerhas databaseCREATE, preflight accepts the plan, but apply fails withpermission denied for databasebecause the effective executorold_ownerlacksCREATE.
PostgreSQL documents the implementation exception directly in AlterSchemaOwner_internal: it checks GetUserId() and notes that this differs from other alter-owner checks:
https://github.com/postgres/postgres/blob/master/src/backend/commands/schemacmds.c#L252-L272
Required behavior:
- For schema ownership only, inspect and fingerprint database
CREATEauthority for the effective executor: the current owner when pgschema emitsSET ROLE, otherwise the session role. - Preserve destination-owner
CREATEchecks for tables, routines, and other schema-contained objects. - Reject missing executor authority during planning before any mutation.
- Preserve schema-before-database ordering when both transfer in one manifest, because transferring the database first can remove the old database owner’s implicit
CREATEauthority before the schema transfer.
Tracked under #627. A proposed implementation is in #637, which is still under discussion. Closing this sub-issue since it is covered by the #627 umbrella.