Skip to content

Declare the grants of roles other than the owner in grantedBy blocks - #371

Merged
archlemon merged 1 commit into
mainfrom
granted-by
Nov 21, 2026
Merged

archlemon merged 1 commit into
mainfrom
granted-by

Conversation

@archlemon

Copy link
Copy Markdown
Member

Fixes #367

Since #366 pgy revokes per grantor, but a grants block can't say who granted a privilege. A rebuilt database got every grant from the owner or the connecting role, so pg_dump of the source and of the rebuild differed (p19_reader=r/p19_admin vs r/pgy). This adds a grantedBy: block that declares those grants.

YAML

  • Every object kind with a grants: block now also takes grantedBy:: tables, foreign tables, columns, views, materialized views, sequences, functions, procedures, aggregates, types, domains, schemas (schema x: blocks and schemas: entries), servers, foreign-data wrappers and languages. A view's columns take columnGrantedBy:. The shape is grantor → grantee → privileges, spelled as in a grants block (with grant option and all included).
  • schemas/pgy.schema.json has a new grantedBy definition and accepts the key in all of these places. A languages: entry now needs grants or grantedBy.
  • docs/schema.md has a new "Grants of other roles" section and property-table rows. docs/feature_support.md is updated.

Semantics

  • The block is authoritative for each grantor it lists. Missing grants are made. A listed grantor's live grants that the block doesn't declare are revoked, or just their grant option when the block declares the privilege without it. grantor: {} revokes all of that grantor's grants.
  • The grants block works as before for every grantor the grantedBy block doesn't list. It no longer sees privileges that only listed grantors gave, so it never revokes or regrants them (no flapping). A privilege a grantee holds from both the owner and a listed grantor appears in both blocks.
  • With no grants block, only the listed grantors' grants are managed.
  • An entry for the owner as grantor counts as part of the grants block. pgy validate warns about it.
  • Revoke privileges per grantor, revoke depended-on grant options with CASCADE, report grants of other roles #366's diff warning about grants a grants block keeps is no longer given for grantors a grantedBy block lists.
  • A table-level REVOKE ... GRANTED BY g also takes away g's grants on the table's columns. A column's grantedBy makes those grants again. I found this on real servers, and afterTableRevokes handles it.

Emission

  • PostgreSQL 19: GRANT ... GRANTED BY g when the connecting role is a superuser or inherits g's privileges.
  • PostgreSQL 15–18: SET ROLE g; GRANT ...; RESET ROLE; when the connecting role is a superuser or may SET ROLE to g. On these versions GRANTED BY must name the current user, even for a superuser (checked on 18).
  • If the connecting role is the grantor and is neither the owner nor a superuser, it runs a plain GRANT.
  • Otherwise the grant is left out with a not_owner warning that gives the statement to run.
  • --from-empty plans the SET ROLE form, which runs on every version.
  • Revokes reuse Revoke privileges per grantor, revoke depended-on grant options with CASCADE, report grants of other roles #366's revoker. The statements are ordered so that the grants block's grants (which give the grantor its grant option) come first, then each grantor's grants after those of the grantors that give it its options. Grants that a REVOKE ... CASCADE takes away are made again.
  • Simulate applies grantedBy blocks to the simulated grantors and ACLs, so split plans converge. ALL is expanded in grantedBy blocks too.

pull

  • pgy pull writes grantedBy for grants whose grantor is neither the owner nor the connecting role, and takes them out of the grants block unless the owner granted the same privilege. Before, it warned about these grants.
  • It still warns about grants it couldn't write a block for. An example is column privileges on an extension's table, which pull already reports.

validate

  • Error: PUBLIC or an empty name as a grantor.
  • Error: a privilege the object's kind doesn't have.
  • Warning: the owner listed as a grantor.
  • Warning: on an object with a grants block, a grantor that neither block gives the grant option it needs. A column's grantor may hold the option on its table. PostgreSQL grants nothing in that case and only warns.

Decided against / left open

  • A grantor that is the connecting role, when that role is a superuser, can't be reproduced before 19, because a superuser's GRANT records the owner as the grantor. That grant is left out with a warning. Pull keeps writing such grants to the grants block, as before.
  • The maintain-before-17 version warning still looks only at grants blocks, not at grantedBy blocks.

Tests

  • Parse tests for every kind and all three YAML formats, plus a JSON Schema fixture (internal/schema/granted_by_test.go).
  • Diff tests (internal/diff/granted_by_test.go):
    • create on 19 vs 15/16/18 spellings
    • grant-option chain order
    • skip when live matches
    • revoke an undeclared grant of a listed grantor
    • grant-option add and revoke
    • interplay with the grants block, including unlisted grantors
    • no grants block
    • owner entry
    • not_owner fallback with hints
    • member via GRANTED BY / SET ROLE
    • connecting role as grantor
    • columns, routines, types, schemas
    • ALL expansion
    • Simulate convergence
    • table revoke regranting a column grant
  • Pull tests (internal/pull/grantors_test.go) and validate tests (internal/validate/granted_by_test.go).
  • New pull fixture internal/integration/testdata/pull/granted_by.sql. It covers grantor chains, PUBLIC granted by a non-owner, a column, a view, a sequence, a function, a type, the schema and plpgsql, and is checked with the pg_dump comparison.
  • New internal/integration/grantedby_test.go: a superuser converge, drift revoke and pull round trip, plus a non-superuser member of the grantor, with a not_owner warning for a grantor it isn't a member of.
  • TestIntegrationGrantorRevokesConverge now expects grantedBy blocks instead of pull warnings.
  • Full internal/cli and internal/integration suites pass on PostgreSQL 15, 16, 18 and 19 (PGY_TEST_REQUIRE_PL=1, pg_dump comparison on). make test, go vet and golangci-lint are clean.
  • Probe oracles: f14_grants (19) and f14b_grants18 (15/16/18) now produce matching dumps with --roles. Before this change, 19 differed only in the grantor. f15_base + f15_drift / f15_drift18 with driftcmp match on all four versions, and so does the reverse direction (pull the drifted database, apply it to the base one).

… made GRANTED BY the grantor on PostgreSQL 19 and under SET ROLE before, and pull them.
@archlemon
archlemon merged commit b197ed7 into main Nov 21, 2026
8 checks passed
@archlemon
archlemon deleted the granted-by branch November 21, 2026 03:42
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.

Proposal: declare grantors of object privileges (grantedBy, GRANT ... GRANTED BY on PostgreSQL 19)

1 participant