Skip to content

Revoke privileges per grantor, revoke depended-on grant options with CASCADE, report grants of other roles - #366

Merged
archlemon merged 2 commits into
mainfrom
pg19-grantors
Nov 21, 2026
Merged

archlemon merged 2 commits into
mainfrom
pg19-grantors

Conversation

@archlemon

Copy link
Copy Markdown
Member

Fixes #339
Fixes #362

PostgreSQL records a grantor for each privilege, and a plain REVOKE only takes away the grants of the role it runs as (of the owner, for the owner, a role with its privileges or a superuser). A grant that another role made, under SET ROLE or with GRANT ... GRANTED BY on 19, was revoked on every diff and stayed.

Revokes per grantor (#339)

  • Introspection reads the grantors of every grant on the objects whose ACL holds a grant of a role other than the owner (tables, views, sequences, columns, routines, types, schemas, servers, wrappers, languages; one batched catalog query, about 8 ms on a 1000-table schema with ACLs on every table and column), plus the roles the connecting role may SET ROLE to.
  • A grants block's revoke goes once per grantor: REVOKE ... GRANTED BY <grantor> on 19 when the connecting role is a superuser or has the grantor's privileges (19 requires inheriting them, checked), otherwise SET ROLE <grantor>; REVOKE ...; RESET ROLE; when it may SET ROLE to it (15 to 18 reject GRANTED BY another role with "grantor must be current user", even for a superuser). Grants it can do neither for are left out with a not_owner warning that gives the statement to run.
  • Revokes of grants that depend on a role's grant option go before it; the SET ROLE revokes go ahead of the plan's other revokes, as running as the grantor needs its USAGE on the schema.
  • REVOKE ... GRANTED BY from public gets the public_revoke warning too.
  • Objects holding only the owner's grants get the same plain REVOKE as before.

Grant options other grants depend on (#362)

  • Taking a grant option (or the privilege) from a role whose own grants depend on it failed at migrate with "dependent privileges exist". It is now revoked with CASCADE, and the dependent grants the block doesn't revoke are granted again.

Reporting

  • pgy pull warns about the grants a role other than the owner and the connecting role made, and pgy diff about those a grants block keeps: grants blocks don't record grantors, so a rebuilt database gets those privileges from the owner or the connecting role.
  • Default privileges are always granted by the role they are for, so they don't have this problem.

Not done: writing the grantor into the YAML and planning GRANT ... GRANTED BY would need a new YAML construct, not a small change to the grants model. That is left for a follow-up.

Tests: unit tests in internal/diff/grantors_test.go and internal/pull/grantors_test.go. Integration tests TestIntegrationGrantorRevokesConverge and TestIntegrationGrantorCascadeKeepsGrants pass on 15, 16, 18 and 19, and so do the full internal/cli and internal/integration suites. The probe fixtures (f15_base + f15_drift on 19, f15_drift18 on 15/16/18) now converge with matching pg_dump output.

…d under SET ROLE before, revoke grant options other grants depend on with CASCADE and give back the grants kept, report grants of other roles in pull and diff.
@archlemon
archlemon merged commit 13e331a into main Nov 21, 2026
8 checks passed
@archlemon
archlemon deleted the pg19-grantors branch November 21, 2026 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant