Skip to content

[Feature]: Allow setting per-role default values for configuration parameters (ALTER ROLE ... SET) #11951

Description

@gbartolini

Is there an existing issue already for this feature request/idea?

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

What problem is this feature going to solve? Why should it be added?

Allow users to declare, on the DatabaseRole resource, default values for run-time configuration parameters that Postgres applies to a role's sessions, as defined by ALTER ROLE ... SET:

ALTER ROLE { role_specification | ALL } [ IN DATABASE database_name ]
    SET configuration_parameter { TO | = } { value | DEFAULT }

Problem

Declarative role management (DatabaseRole, and inline .spec.managed.roles[], both built on RoleConfiguration in api/v1/cluster_types.go) covers attributes such as login, superuser, connectionLimit, inRoles, and so on, but there is no way to set per-role defaults for parameters like search_path, statement_timeout, work_mem, idle_in_transaction_session_timeout or log_statement.

Today users must run ALTER ROLE ... SET by hand (or through postInitSQL, which only runs at bootstrap), and the setting is neither reconciled nor visible in the cluster spec. It also drifts silently if changed out of band.

Describe the solution you'd like

Add a generic list of settings to DatabaseRoleSpec (api/v1/databaserole_types.go). Per the docs ("new capabilities land on DatabaseRole"), do not add it to the shared RoleConfiguration, which would also expose it on inline managed.roles. The exact shape is open for discussion, for example:

apiVersion: postgresql.cnpg.io/v1
kind: DatabaseRole
metadata:
  name: app
spec:
  name: app
  login: true
  cluster:
    name: cluster-example
  settings:
    - name: search_path
      value: app, public
    - name: statement_timeout
      value: "30s"
    - name: work_mem
      value: 64MB
      database: appdb   # optional, maps to IN DATABASE

The operator reconciles these in the DatabaseRole controller, issuing ALTER ROLE ... [IN DATABASE ...] SET ... for added/changed entries and ALTER ROLE ... RESET ... (or SET ... TO DEFAULT) for removed ones, comparing against pg_db_role_setting.

Describe alternatives you've considered

Imperative only.

Additional context

Allowed parameters (to be verified)

The feature must be generic, but it must not let users set arbitrary parameters. Part of this issue is to define and enforce the rules:

  • Only parameters that Postgres allows to be set per role. Check pg_settings.context: only user and superuser contexts (plus superuser-backend/backend where Postgres permits it) are valid; postmaster, sighup and internal are rejected by Postgres. Decide whether to validate in the webhook, at reconcile time, or both.
  • Reject parameters that CloudNativePG manages itself or that would break the operator's assumptions (for example session_replication_role, role/session_authorization, default_transaction_read_only on roles the operator uses, and anything in the existing fixed/blocked parameter lists in pkg/postgres/configuration.go). Reuse those lists where possible rather than creating a new one.
  • Privilege: SET of superuser-context parameters needs the right privileges (or GRANT SET ON PARAMETER on PG 15+). Define what the operator does when it can't apply a setting, and surface it in the DatabaseRole status conditions.
  • Version differences: the allowed set varies across supported PG versions (extensions' custom GUCs such as pg_stat_statements.* can only be validated at apply time). Decide how to handle unknown parameters and DEFAULT/RESET semantics.
  • ALL roles and IN DATABASE scope: decide whether ALL (a cluster-wide default for all roles) is in scope or is better served by the cluster .spec.postgresql.parameters. Recommend leaving ALL out of the first iteration.

Acceptance criteria

  • API field on DatabaseRoleSpec, with CRD/doc generation updated.
  • Webhook validation for obviously invalid or blocked parameters, with clear errors.
  • Reconciliation (create, update, remove) against pg_db_role_setting, idempotent and drift-correcting.
  • Status reporting when a setting cannot be applied.
  • Unit and e2e tests; documentation in docs/src/declarative_role_management.md.

Out of scope

  • Server-wide defaults (already covered by .spec.postgresql.parameters).
  • Per-database defaults: tracked in a separate issue for ALTER DATABASE ... SET.

Backport?

No

Are you willing to actively contribute to this feature?

Yes

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Labels

enhancement 🪄

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions