Skip to content

[Docs]: Disabling a role with login: false does not close existing connections #11382

Open

Description

@melancholictheory

Is there an existing issue already for your request/idea?

  • I have searched for an existing issue, and could not find anything. I believe this is a new documentation enhancement to be evaluated.

What problem in the existing documentation this issue aims to solve?

Setting login: false on a DatabaseRole applies NOLOGIN, so new connections are refused. Sessions that were already open keep working, and the documentation does not mention this.

We hit it while writing an internal runbook for revoking access: we assumed disabling a user cuts off access, and it does not. An application that is already connected keeps reading and writing until it reconnects on its own.

Reproduced on 1.30.0 with PostgreSQL 18:

  1. Create a DatabaseRole with login: true, connect as that role and keep the session open, for example SELECT pg_sleep(180);.
  2. Set login: false and wait for status.applied: true (about 10 seconds for us).
  3. The open session still runs queries and returns data. New logins are refused with FATAL: role "myapp" is not permitted to log in.

For comparison, deleting the DatabaseRole with databaseRoleReclaimPolicy: delete does affect open sessions: the backend stays alive, but the next query fails with ERROR: invalid role OID: 16536. The two operations behave differently, and neither behaviour is documented.

Working around this from the outside is awkward. The database owner cannot call pg_terminate_backend on another role's backend without pg_signal_backend, and in a managed setup tenants have no superuser and no shell access to the pods.

Describe what additions need to be done to the documentation

A short note in declarative role management stating that disabling a role blocks new connections only, and that existing sessions survive until they reconnect. A sentence on what happens to open sessions when a role is dropped would help too.

It would also be useful to know your view on whether terminating sessions belongs in the operator at all. An opt-in field on DatabaseRole would make login: false usable for incident response. If you consider it out of scope, that is a fine answer, and worth stating in the docs so people do not build runbooks on the wrong assumption.

Describe what pages need to change in the documentation, if any

https://cloudnative-pg.io/docs/1.30/declarative_role_management/

Activity

  1. ehtishammubarik commented on Aug 30, 2026

    @ehtishammubarik

    Confirmed on the code path, and I ran your two comparisons against a stock PostgreSQL 18.6 so the docs note can say exactly what happens. Line numbers below are from main (8179beb). roles/postgres.go, api/v1/databaserole_types.go and the docs page are byte-identical on release-1.30 (2c518dc), which is what you are running; where a file has shifted between the two I give both.

    login: false cannot affect open sessions, by construction

    DatabaseRole reconciliation ends in roles.Update, which builds one ALTER ROLE statement from appendRoleOptions — the only place login is translated, and it writes NOLOGIN (internal/management/controller/roles/postgres.go:366). Nothing in the role path terminates backends: the only two pg_terminate_backend call sites in the tree are dropStaleReplicationConnections (internal/management/controller/instance_controller.go:1554, :1551 on 1.30 — replication connections only) and Instance.DropConnections (pkg/management/postgres/instance.go:1560), whose single caller is the promotion path at instance_controller.go:1270. So this is plain PostgreSQL NOLOGIN semantics, and a docs note is the right fix.

    Replaying the exact statement the operator issues:

    ALTER ROLE "myapp" NOBYPASSRLS NOCREATEDB NOCREATEROLE INHERIT NOLOGIN
      NOREPLICATION NOSUPERUSER CONNECTION LIMIT -1;

    A new connection is refused with FATAL: role "myapp" is not permitted to log in, while the session opened beforehand keeps answering (still alive | myapp | 415). Same as your report.

    The workaround does not need superuser — inRoles already covers it

    DatabaseRoleSpec embeds RoleConfiguration inline (api/v1/databaserole_types.go:69), so inRoles (api/v1/cluster_types.go:2757, :2698 on 1.30) is a DatabaseRole field, and it is in the generated CRD at config/crd/bases/postgresql.cnpg.io_databaseroles.yaml:138. Membership changes are applied to existing roles too, via roles.UpdateMembership, which emits GRANT "<role>" TO "<name>" (postgres.go:266) on the reconciler's superuser connection (internal/management/controller/databaserole_controller.go:522) — so the tenant never needs one of their own:

    apiVersion: postgresql.cnpg.io/v1
    kind: DatabaseRole
    spec:
      name: runbook
      login: true
      inRoles:
        - pg_signal_backend

    Measured with a plain non-superuser role, terminating the still-open myapp backend:

    • before the grant: ERROR: permission denied to terminate process — with PostgreSQL's own DETAIL pointing at pg_signal_backend
    • after GRANT "pg_signal_backend" TO "runbook": pg_terminate_backend returns t, and pg_stat_activity drops to 0 myapp client backends

    That makes login: false plus one pg_terminate_backend a complete revoke, with no superuser and no pod shell. The page already uses pg_monitor in the DatabaseRole example (docs/src/declarative_role_management.md:113) and pg_signal_backend in the inline-roles example (:359), but never connects either to cutting off access.

    The drop comparison is weaker than it looks

    Dropping does not close the session either. After DROP ROLE, the backend stays connected and only statements that resolve the role OID fail:

    SELECT 1 AS plain;                    ->  1
    SELECT count(*) FROM pg_class;        ->  415
    CREATE TEMP TABLE t(x int);           ->  ERROR: role 16392 was concurrently dropped
    SELECT current_user;                  ->  ERROR: invalid role OID: 16392
    SELECT 2 AS after_error;              ->  2
    

    The session survives the error and keeps running. What actually stopped data access in my run was the REVOKE that had to happen before the drop, not the drop itself — roles.Delete issues a bare DROP ROLE IF EXISTS (postgres.go:191), so a role holding a live table grant fails to drop at all:

    ERROR:  role "appx" cannot be dropped because some objects depend on it
    DETAIL:  privileges for table appdata
    

    And a REVOKE on its own, with the role kept and still LOGIN, hits the open session immediately: the next SELECT returns permission denied for table revdata.

    So the rule worth writing down is that privilege changes take effect on live sessions at the next statement, while login: false and dropping the role do not. A databaseRoleReclaimPolicy: delete on a role that actually holds privileges is also worth a caveat, since it will not drop cleanly.

    On whether termination belongs in the operator: with inRoles: [pg_signal_backend] there is already a supported path that needs no new API, so it may be enough to document that instead — your call. Happy to send a docs PR for the note if you want it.

  2. melancholictheory commented on Aug 30, 2026

    @melancholictheory
    Author

    Thanks for digging into this. The NOLOGIN trace matches what we see, and inRoles: [pg_signal_backend] is a good find. I hadn't considered that the GRANT goes over the reconciler's own superuser connection, so the tenant never needs one.

    It doesn't reach our tenants as things stand. Our platform API wraps DatabaseRole and exposes an access: ro|rw enum that maps to pg_read_all_data / pg_write_all_data, so inRoles isn't user-settable. That's our limitation, not the operator's.

    One correction on the drop comparison, though. Whether the drop cuts off data access depends on where the privileges come from.

    Our roles hold no direct grants. All access is membership in pg_read_all_data / pg_write_all_data, and in that model the drop does end data access, at the next statement. Same setup as yours, with a session opened before the change and a table owned by another role:

    before  SELECT count(*) FROM secret_data;   ->  1
    
    (DatabaseRole removed, databaseRoleReclaimPolicy: delete, role gone from pg_roles)
    
    after   SELECT 1;                            ->  1
    after   SELECT count(*) FROM secret_data;    ->  ERROR: permission denied for table secret_data
    after   SELECT current_user;                 ->  ERROR: invalid role OID: 16536
    

    SELECT count(*) FROM pg_class doesn't show this, because pg_class is readable by PUBLIC and keeps answering for a dropped role too. A table the role could only reach through membership stops being readable as soon as the membership goes with the role.

    The DROP ROLE caveat splits along the same line. A role that owns nothing and holds no direct grants drops cleanly, so databaseRoleReclaimPolicy: delete is fine in the membership model: the bare DROP ROLE IF EXISTS went through in our run with no REVOKE before it. The dependency error you hit needs a direct grant to exist in the first place.

    So if you write the note, the line worth drawing is direct grants versus membership:

    • login: false never touches an open session, in either model.
    • With direct grants, the drop fails until the grants are revoked, and it's the REVOKE that lands on the live session.
    • With membership, the drop succeeds and access ends at the next statement.

    Yes to the docs PR, please. Happy to review it.

  3. github-actions commented on Nov 22, 2026

    @github-actions
    Contributor

    This issue is stale because it has been open for 60 days with no activity.

  4. seshikanthguduri commented on Nov 24, 2026

    @seshikanthguduri

    Hi, I'd like to work on this documentation update.

    From the issue and the verification above, the note for the declarative role management page would state that setting login: false applies NOLOGIN: new connections are refused, but sessions that are already open keep working until they disconnect, so disabling a role does not by itself revoke access for connected clients.

    If that matches what the maintainers have in mind, I'm happy to prepare a PR with the change. Please let me know if you'd prefer the note placed or worded differently.

    Assisted with Muse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions