Skip to content

Restore role grants made by a superuser with a different name - #7

Merged
cerebrate merged 1 commit into
masterfrom
fix/superuser-grantor
Oct 31, 2026
Merged

cerebrate merged 1 commit into
masterfrom
fix/superuser-grantor

Conversation

@cerebrate

Copy link
Copy Markdown
Member

What

When restoring a PostgreSQL set's roles (globals.sql.zst), GRANTED BY is now dropped from any grant whose grantor is a superuser in the dump. The destination then records the grant as made by its own bootstrap superuser. Grants made by non-superusers keep their grantor. When this happens, the restore logs grants by superuser <name> attributed to this server's own superuser.

The rewrite is a pure function, without_superuser_grantors in engines/postgres.py, applied before the existing without_roles filter.

Why

The first restore test of the new postgres-memlord target passed, but with a warning:

restore: globals: ERROR:  permission denied to grant privileges as role "memlord"

How that happens:

  • Since PostgreSQL 16, every role grant a superuser makes is recorded as made by the bootstrap superuser, the one initdb created, and pg_dumpall names that role. memlord's container was set up with POSTGRES_USER=memlord, so its dump contains GRANT pg_read_all_data TO dbbackup WITH INHERIT TRUE GRANTED BY memlord;.
  • On a server whose bootstrap superuser is postgres, memlord is only an ordinary superuser, without the ADMIN option on pg_read_all_data. So the grant fails with "The grantor must have the ADMIN option on role pg_read_all_data".
  • As a result, the restored dbbackup role lost its pg_read_all_data membership. After a real recovery onto such a server, backups would fail or be incomplete until it was re-granted by hand.

Notes for review

  • Why dropping the clause is safe: a grant attributed to a superuser can only ever have been recorded under the bootstrap superuser. Dropping the clause records the same thing on the destination, under its own bootstrap superuser.
  • How superusers are detected: from the dump's own ALTER ROLE ... WITH ... lines, looking only at the attributes before PASSWORD. A password hash that happens to contain the word SUPERUSER can't cause a false match.
  • No integration coverage: the integration stack's source server has postgres as its bootstrap superuser, the same as the restore server, so it never produces this case. Reproducing it would mean renaming the source superuser throughout the tests. Coverage is a unit test, plus the manual check below against the real set.
  • Release: this changes the image, so it needs a release. It could go out as 0.3.1 together with Pull the db-backup image before restore testing; warn about sets from newer versions #6's warning about sets from a newer version.

Testing

  • make lint and make test pass. The new unit test covers superuser grantors with plain and quoted names, a non-superuser grantor that must be kept, and a password containing SUPERUSER.
  • make integration passes on a fresh stack: 17 tests. That stack's dumps contain GRANTED BY postgres, so it confirms the usual case still restores.
  • The real postgres-memlord set (made by 0.3.0), restore-tested with scripts/restore-test.sh --build, passed with 0 warnings (previously 1). On the kept scratch server, dbbackup is a member of pg_read_all_data, with grantor postgres and inherit_option true.

🤖 Generated with Claude Code

Since PostgreSQL 16, a superuser's role grants are recorded as made by the
bootstrap superuser, and pg_dumpall names that role in GRANTED BY. When the
original server's bootstrap superuser isn't called postgres (memlord's
container was set up with POSTGRES_USER=memlord), replaying

  GRANT pg_read_all_data TO dbbackup WITH INHERIT TRUE GRANTED BY memlord;

on a server whose bootstrap superuser is postgres fails: memlord is only an
ordinary superuser there, and has no ADMIN option on pg_read_all_data. The
restored backup role loses its membership.

When restoring globals, drop GRANTED BY from grants whose grantor is a
superuser in the dump, so they're recorded as made by this server's bootstrap
superuser, as they were on the original. Grants from non-superusers keep
their grantor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cerebrate
cerebrate merged commit f5675cc into master Oct 31, 2026
3 checks passed
@cerebrate
cerebrate deleted the fix/superuser-grantor branch October 31, 2026 22:07
@cerebrate cerebrate mentioned this pull request Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant