Skip to content

[Feature]: Allow setting per-database default values for configuration parameters (ALTER DATABASE ... SET) #11952

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, in the Database CRD, default values for run-time configuration parameters that Postgres applies to new sessions in that database, as defined by ALTER DATABASE ... SET:

ALTER DATABASE name SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER DATABASE name RESET configuration_parameter

Problem

The Database resource (api/v1/database_types.go) already manages attributes such as owner, encoding, isTemplate, allowConnections and connectionLimit, but not per-database parameter defaults such as search_path, work_mem, statement_timeout, default_transaction_isolation or timezone.

Users must run ALTER DATABASE ... SET manually. The setting is not declared in the spec, not reconciled, and drifts silently if changed out of band.

Describe the solution you'd like

Add a generic list of settings to DatabaseSpec. The exact shape is open for discussion, for example:

apiVersion: postgresql.cnpg.io/v1
kind: Database
metadata:
  name: app
spec:
  name: appdb
  owner: app
  cluster:
    name: cluster-example
  settings:
    - name: search_path
      value: app, public
    - name: statement_timeout
      value: "30s"

The Database controller reconciles these by issuing ALTER DATABASE ... SET for added/changed entries and ALTER DATABASE ... RESET for removed ones, comparing against pg_db_role_setting (rows with setrole = 0).

Allowed parameters (to be verified)

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

  • Only parameters that Postgres allows to be set per database. Check pg_settings.context: postmaster, sighup and internal parameters 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. Reuse the fixed/blocked parameter lists in pkg/postgres/configuration.go rather than creating a new one.
  • Privilege: superuser-context parameters require elevated privileges (or GRANT SET ON PARAMETER on PG 15+). Define the behaviour and error reporting in status when a setting can't be applied.
  • Version and extension differences: the allowed set varies across supported PG versions, and extension GUCs (e.g. pg_stat_statements.*) can only be validated at apply time. Decide how to handle unknown parameters.
  • Precedence: role-in-database settings override database settings, which override server defaults. Document this and keep it consistent with the companion issue for roles (ALTER ROLE ... [IN DATABASE ...] SET), ideally sharing the same validation code and settings type.

Describe alternatives you've considered

Imperative only.

Additional context

Acceptance criteria

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

Out of scope

  • Server-wide defaults (already covered by .spec.postgresql.parameters).
  • Per-role defaults: tracked in a separate issue for ALTER ROLE ... 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement 🪄

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions