Repository navigation
Restore role grants made by a superuser with a different name - #7
Merged
Merged
Conversation
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>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
When restoring a PostgreSQL set's roles (
globals.sql.zst),GRANTED BYis 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 logsgrants by superuser <name> attributed to this server's own superuser.The rewrite is a pure function,
without_superuser_grantorsinengines/postgres.py, applied before the existingwithout_rolesfilter.Why
The first restore test of the new
postgres-memlordtarget passed, but with a warning:How that happens:
pg_dumpallnames that role. memlord's container was set up withPOSTGRES_USER=memlord, so its dump containsGRANT pg_read_all_data TO dbbackup WITH INHERIT TRUE GRANTED BY memlord;.postgres,memlordis only an ordinary superuser, without the ADMIN option onpg_read_all_data. So the grant fails with "The grantor must have the ADMIN option on role pg_read_all_data".dbbackuprole lost itspg_read_all_datamembership. After a real recovery onto such a server, backups would fail or be incomplete until it was re-granted by hand.Notes for review
ALTER ROLE ... WITH ...lines, looking only at the attributes beforePASSWORD. A password hash that happens to contain the wordSUPERUSERcan't cause a false match.postgresas 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.Testing
make lintandmake testpass. The new unit test covers superuser grantors with plain and quoted names, a non-superuser grantor that must be kept, and a password containingSUPERUSER.make integrationpasses on a fresh stack: 17 tests. That stack's dumps containGRANTED BY postgres, so it confirms the usual case still restores.postgres-memlordset (made by 0.3.0), restore-tested withscripts/restore-test.sh --build, passed with 0 warnings (previously 1). On the kept scratch server,dbbackupis a member ofpg_read_all_data, with grantorpostgresandinherit_optiontrue.🤖 Generated with Claude Code