Repository navigation
Description
Two problems make a logical pg_dump restore of a database using extension_drop fail.
1. Install refuses when restoring into a non-public schema (from PR #55)
PR #55 makes CREATE EXTENSION and ALTER EXTENSION ... UPDATE refuse unless PUBLIC has USAGE on the target schema, since every role that drops an extension needs to call extension_drop__claim() there. pg_dump restores privileges after all other objects, so a dump of a database with extension_drop in schema xd restores as CREATE SCHEMA xd; (owner-only), then CREATE EXTENSION extension_drop WITH SCHEMA xd; (refused), and only much later GRANT USAGE ON SCHEMA xd TO PUBLIC;. With pg_dump -x the GRANT never appears. pg_upgrade and installs into public are unaffected.
- There is no reliable way for the install script to detect a restore. Most of pg_dump's SET header is masked inside an extension script, and the remaining fingerprints have false positives and negatives.
- Proposed fix: a placeholder setting
extension_drop.missing_public_usagewith valueserror(default) andgrant. Withgrant, install and update runGRANT USAGE ON SCHEMA ... TO PUBLICthemselves, with a WARNING. A restore sets it withPGOPTIONS='-c extension_drop.missing_public_usage=grant'. The REVOKE guard triggers must not read it, since any role can SET a placeholder. - A "just skip the error" value was rejected. The REVOKE guard would then refuse the dump's earlier REVOKEs, single-transaction restores abort, and
-xrestores end without PUBLIC USAGE. - Open problem: a GRANT run inside an extension script is recorded in
pg_init_privsas an initial privilege. Later dumps then omit it, and DROP EXTENSION leaves the row behind. Deleting the row from the install script works, but needs a true superuser, which managed services such as RDS don't provide. Alternatives are being investigated.
2. Duplicate key on extension_drop__commands during restore (pre-existing)
extension_drop__commands is marked with pg_extension_config_dump() with no filter. On restore, an extension that registers drop SQL does so again inside its own CREATE EXTENSION, and then the dump's COPY inserts the same row: duplicate key value violates unique constraint "extension_drop__commands_pkey". Every --single-transaction restore of a database with a registered extension aborts. A plain restore logs the error and continues. A fix needs a way to tell rows registered by an extension's install script from rows added later, and to filter the former out of the dump.
Activity
jnasbyupgrade commentedon Nov 4, 2026
Claude here. Findings on the
pg_init_privsproblem:CREATE EXTENSIONorALTER EXTENSION ... UPDATEexecutes its script is written topg_init_privs, whether or not the object belongs to the extension. The only gate inrecordExtensionInitPriviscreating_extension(PostgreSQL 12-17). A non-superuser has no way to remove the row afterwards:ALTER EXTENSION ... DROPwould, butALTER EXTENSION ... ADD SCHEMArefuses the extension's own schema, and later GRANT/REVOKEs only rewrite the row.pg_dumpuses that row as the baseline ACL for every schema exceptpublic(15+), so the grant is left out of later dumps.creating_extensionis cleared beforeCREATE EXTENSIONreturns, so a GRANT run at COMMIT is not recorded.Proposed direction: in
grantmode, the install and update scripts queue the grant instead of running it:DEFERRABLE INITIALLY DEFERREDconstraint trigger;GRANT USAGE ON SCHEMA ... TO PUBLICif PUBLIC still lacks it, then deletes the row.The schema ends up with an ordinary ACL that later dumps include.
DROP EXTENSIONleaves nopg_init_privsrow, an install intopublicdoesn't touch initdb's row, and nothing needs a catalog write. For--single-transactionrestores, the REVOKE guard has to treat a queued row as pending USAGE, because COMMIT comes after the dump's REVOKEs.Ruled out:
CREATE EXTENSION: it doesn't fire for the command that created it when no ddl_command_end/sql_drop/table_rewrite trigger existed beforehand, which is exactly the restore case.extension_drop__claim()into a schema the extension owns would make the recorded grant correct, but it hard-codes a schema name, which goes against this extension's schema-independence design.Verified on PostgreSQL 12 and 17 with a probe extension: an in-script GRANT leaves an
erow inpg_init_privsthat survives DROP EXTENSION and is omitted from pg_dump; the deferred-trigger GRANT leaves USAGE present, nopg_init_privsrow, an empty queue table, and the GRANT in pg_dump; inside an explicit transaction USAGE is absent before COMMIT and present after; plain and--single-transactionpg_restore both succeed with USAGE present and no row; an install intopublicafterREVOKE USAGE ... FROM PUBLICleaves initdb'sirow unchanged. The probe trigger function needsSET search_path FROM CURRENT(or qualified names), since the search_path at COMMIT no longer includes the extension schema.jnasbyupgrade commentedon Nov 4, 2026
Claude here. The design bullets in section 1 of this issue's description predate the findings comment above and are superseded. The shipped design (jnasbyupgrade#8) does not run the GRANT in the install or update script. With
extension_drop.missing_public_usage = grantthe script queues a row in the extension-owned tableextension_drop__grant_queue, and a deferred constraint trigger runsGRANT USAGE ON SCHEMA ... TO PUBLICat COMMIT if PUBLIC still lacks it. That avoids thepg_init_privsentry, so the "Open problem" bullet is resolved. Section 2 (duplicate keys onextension_drop__commands) is unchanged and still open.