Skip to content

pg_dump/restore: install-time PUBLIC USAGE check and duplicate extension_drop__commands rows #56

Open

Description

@jnasbyupgrade

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_usage with values error (default) and grant. With grant, install and update run GRANT USAGE ON SCHEMA ... TO PUBLIC themselves, with a WARNING. A restore sets it with PGOPTIONS='-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 -x restores end without PUBLIC USAGE.
  • Open problem: a GRANT run inside an extension script is recorded in pg_init_privs as 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

  1. jnasbyupgrade commented on Nov 4, 2026

    @jnasbyupgrade
    ContributorAuthor

    Claude here. Findings on the pg_init_privs problem:

    • Any GRANT run while CREATE EXTENSION or ALTER EXTENSION ... UPDATE executes its script is written to pg_init_privs, whether or not the object belongs to the extension. The only gate in recordExtensionInitPriv is creating_extension (PostgreSQL 12-17). A non-superuser has no way to remove the row afterwards: ALTER EXTENSION ... DROP would, but ALTER EXTENSION ... ADD SCHEMA refuses the extension's own schema, and later GRANT/REVOKEs only rewrite the row.
    • pg_dump uses that row as the baseline ACL for every schema except public (15+), so the grant is left out of later dumps.
    • creating_extension is cleared before CREATE EXTENSION returns, so a GRANT run at COMMIT is not recorded.

    Proposed direction: in grant mode, the install and update scripts queue the grant instead of running it:

    • they insert a row into an extension-owned table that carries a DEFERRABLE INITIALLY DEFERRED constraint trigger;
    • at COMMIT the trigger runs GRANT USAGE ON SCHEMA ... TO PUBLIC if PUBLIC still lacks it, then deletes the row.

    The schema ends up with an ordinary ACL that later dumps include. DROP EXTENSION leaves no pg_init_privs row, an install into public doesn't touch initdb's row, and nothing needs a catalog write. For --single-transaction restores, the REVOKE guard has to treat a queued row as pending USAGE, because COMMIT comes after the dump's REVOKEs.

    Ruled out:

    • An event trigger on 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.
    • A grant step the user runs after install: the install has to stop refusing first, which is the "skip" mode rejected above.
    • A TEMP helper table: temp tables created in an extension script are still recorded as members.
    • Moving 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 e row in pg_init_privs that survives DROP EXTENSION and is omitted from pg_dump; the deferred-trigger GRANT leaves USAGE present, no pg_init_privs row, 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-transaction pg_restore both succeed with USAGE present and no row; an install into public after REVOKE USAGE ... FROM PUBLIC leaves initdb's i row unchanged. The probe trigger function needs SET search_path FROM CURRENT (or qualified names), since the search_path at COMMIT no longer includes the extension schema.

  2. jnasbyupgrade commented on Nov 4, 2026

    @jnasbyupgrade
    ContributorAuthor

    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 = grant the script queues a row in the extension-owned table extension_drop__grant_queue, and a deferred constraint trigger runs GRANT USAGE ON SCHEMA ... TO PUBLIC at COMMIT if PUBLIC still lacks it. That avoids the pg_init_privs entry, so the "Open problem" bullet is resolved. Section 2 (duplicate keys on extension_drop__commands) is unchanged and still open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions