pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Support opt-in declarative roles, memberships, and ownership

Open Goldziher opened this issue 1 month ago • 6 comments

Summary

Add an opt-in declarative global-state surface for PostgreSQL roles, memberships, database/schema/object ownership, and global default privileges.

This intentionally requests a broader scope than #584 and #602. Referenced-role stubbing solves schema planning, and ignoring owners allows one schema file to work in environments with different owners, but neither supports installations where the role graph and least-privilege ownership topology are part of the versioned database contract.

Proposed shape

Keep schema files portable by putting cluster state in a separate explicit input, for example --global-file roles.sql or a structured manifest. Only objects declared in that input are managed.

The plan should support:

  • create/alter/drop of declared roles, including LOGIN, INHERIT, CREATEDB, CREATEROLE, REPLICATION, BYPASSRLS, connection limit, validity, and configuration;
  • memberships with PostgreSQL-version-aware ADMIN, INHERIT, and SET options;
  • externally provisioned roles that may be referenced and validated but are never created, altered, or dropped;
  • database, schema, table/view/materialized-view/sequence/function/procedure/type ownership;
  • global default privileges;
  • deterministic plan JSON/SQL, concurrent-change fingerprints, transactional grouping where PostgreSQL permits it, and zero-drift replanning.

Role passwords and other secrets should remain outside desired state.

Planning safety

Planning must never execute cluster-global desired-state statements against the target cluster or an external plan database. A plan-only run must leave the complete pg_roles, pg_auth_members, ownership, and default-ACL census unchanged.

One possible implementation is to model global state directly from catalogs rather than materializing it in the temporary database. If materialization is required, it must happen only in an isolated PostgreSQL instance, never a sibling database on the target cluster.

Safety defaults

  • Global-state management is opt-in.
  • Unmanaged roles and provider-created administrator relationships remain untouched.
  • System roles and the active session role cannot be dropped.
  • Destructive role/ownership operations are classified explicitly for policy gates.
  • Missing authority fails before mutation.
  • Managed-service external roles can be validated without requiring superuser access.

Acceptance

Test PostgreSQL 15 through 18, including the membership-option differences introduced in PostgreSQL 16.

A central integration fixture should deliberately drift:

  1. one role attribute;
  2. one membership option;
  3. one database owner;
  4. one schema owner;
  5. one object owner;
  6. one global default ACL.

Planning must report exactly one corresponding repair for each drift without changing the target. Applying the saved plan must converge, and the next plan must contain zero steps. Include externally managed administrator roles and a negative control proving an undeclared role is not modified.

This capability would allow consumers to remove parallel role/ownership convergence scripts and use pgschema as the complete PostgreSQL desired-state authority.

Goldziher avatar Oct 09 '26 08:10 Goldziher

@Goldziher to make sure I understand it accurately, can you provide a concrete example showing before/after

tianzhou avatar Oct 09 '26 08:10 tianzhou

A minimal concrete case, using simplified names from our deployment:

The target database initially has:

  • provider_admin, an externally managed administrator role that pgschema must validate but never alter;
  • app_login, an externally credentialed login role;
  • no app_owner or app_runtime roles;
  • the current database, schema public, and public.documents owned by provider_admin;
  • no app_runtime -> app_login membership;
  • PostgreSQL's database-wide default EXECUTE privilege for PUBLIC on newly created functions.

schema.sql contains the ordinary schema state:

CREATE TABLE documents (
    id bigint PRIMARY KEY
);

GRANT SELECT, INSERT ON TABLE documents TO app_runtime;

With pgschema today, app_runtime must be created separately before planning. Ownership statements are skipped, and this database-wide default-privilege change is also skipped:

ALTER TABLE public.documents OWNER TO app_owner;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner
    REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;

The proposed opt-in global input would logically declare the following. The serialization below is descriptive; the exact TOML/SQL shape is not essential to the request:

external roles:
  provider_admin
  app_login

managed roles:
  app_owner:   NOLOGIN, INHERIT
  app_runtime: NOLOGIN, INHERIT

managed memberships:
  GRANT app_runtime TO app_login
    WITH ADMIN FALSE, INHERIT TRUE, SET FALSE

managed ownership:
  current database       -> app_owner
  schema public          -> app_owner
  table public.documents -> app_owner

managed database-wide default privileges:
  creator app_owner, functions, PUBLIC -> no EXECUTE

Assuming the connected deployment role already has the authority required for those ownership transfers, planning should report deterministic steps equivalent to:

CREATE ROLE app_owner
    NOLOGIN INHERIT NOCREATEDB NOCREATEROLE
    NOREPLICATION NOBYPASSRLS;

CREATE ROLE app_runtime
    NOLOGIN INHERIT NOCREATEDB NOCREATEROLE
    NOREPLICATION NOBYPASSRLS;

GRANT app_runtime TO app_login
    WITH ADMIN FALSE, INHERIT TRUE, SET FALSE;

ALTER DATABASE current_database_name OWNER TO app_owner;
ALTER SCHEMA public OWNER TO app_owner;
ALTER TABLE public.documents OWNER TO app_owner;

ALTER DEFAULT PRIVILEGES FOR ROLE app_owner
    REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;

On PostgreSQL 15, only the membership ADMIN option exists. A declaration requiring per-membership INHERIT or SET behavior that PostgreSQL 15 cannot represent should fail clearly rather than silently changing its meaning.

Planning must obtain this state from the target catalogs without executing any of the statements against either the target or an external planning database. Applying the saved plan should perform the transition, and replanning should produce zero steps.

The management boundary is explicit:

  • provider_admin and app_login are validated but never altered.
  • Roles omitted from the global input are untouched.
  • A role is dropped only through an explicit absent/delete declaration, never merely because it is omitted.
  • Passwords remain outside pgschema.
  • Ownership applies only to explicitly declared managed objects, excluding extension members and ignored schemas unless separately supported.
  • Missing authority is detected before the first mutation.
  • The saved-plan fingerprint includes the selected role, membership, ownership, and default-ACL catalog state so concurrent drift rejects the apply.

This replaces our separate stable-state convergence scripts. One-time data migrations, backfills, and ownership transitions that cannot be represented as desired state remain ordinary migrations.

Goldziher avatar Oct 09 '26 08:10 Goldziher

Implementation testing confirmed eight independent correctness requirements that need to be closed before #627 is safe to merge:

  • #628 — a planned SET membership cannot currently unblock schema inspection
  • #629 — ownership transfer must execute under the current owner's authority
  • #630 — the applying role must not lose authority before dependent steps
  • #631 — PostgreSQL 16+ multi-grantor memberships need deterministic reconciliation
  • #632 — generated role-configuration literals must be independent of standard_conforming_strings
  • #633 — duplicate default privileges must not create perpetual drift
  • #634 — roles declared absent cannot be referenced by other desired state
  • #635 — tuple identities must be collision-free for quoted identifiers containing /

Each was reproduced against the implementation branch before production fixes: #628 and #629 on PostgreSQL 15–18, #631 on PostgreSQL 16–18, and the remaining planner/validation defects with focused regressions. No temporary grants or plan-time mutations will be used to solve them. The implementation will stay out of review until these regressions pass alongside the PostgreSQL 15–18 plan/apply/zero-drift matrix.

Goldziher avatar Oct 09 '26 09:10 Goldziher

One additional PostgreSQL-specific authority defect was confirmed while independently reviewing the ownership fix:

  • #636 — schema ownership preflight checks database CREATE on the wrong role.

The two failure directions were reproduced on PostgreSQL 15–18 before the fix: valid transfers were rejected when only the effective current owner had CREATE, and invalid transfers were accepted when only the destination owner had it. PostgreSQL’s AlterSchemaOwner_internal explicitly treats schema ownership differently from other owner changes, so the correction is scoped to schemas and keeps destination-owner checks for tables and routines.

Goldziher avatar Oct 09 '26 11:10 Goldziher

PR #637 is now green at 5c24071 after the live PostgreSQL 15-18 regression matrix and an independent full review.

The final coverage includes role/membership authority ordering, mixed object owners, concurrent DDL under execution roles, SQL export parity, cluster-wide role dependency preflight, quoted/dotted identifiers, same- and cross-schema custom argument types, overloaded routines, routine privileges/default revokes, and normal, zero-argument, ordered-set, and hypothetical-set aggregates. All review threads are resolved; the PR remains open for maintainer review.

Goldziher avatar Oct 09 '26 14:10 Goldziher

@tianzhou any updates on this?

Goldziher avatar Nov 26 '26 05:11 Goldziher