Review PostgreSQL statement-duration logging thresholds

Choose a PostgreSQL duration-logging threshold that balances diagnostic needs and log volume, and protect logs containing SQL text.

Description

log_min_duration_statement records the text and duration of SQL statements that finish at or above the threshold. Set a positive threshold in milliseconds to find slow queries.

Potential impact

Logging every statement can add load and expose sensitive values in SQL text. Too high a threshold can hide queries you need to investigate.

Remediation

Set the threshold in settings.database_flags. Check slow queries against the resulting logs and monitor volume and database performance. Limit who can read the logs and how long they are kept.

Examples

This google.cloud 1.14.0 example creates an instance. Supply a supported version and tier, project, instance name and credentials. For an existing instance, edit its flags in Cloud SQL and keep the other required values.

Before

yaml
- name: Cloud SQL 인스턴스 생성
  google.cloud.gcp_sql_instance:
    auth_kind: serviceaccount
    name: "{{ resource_name }}-db"
    project: test_project
    region: us-central1
    database_version: POSTGRES_13
    settings:
      tier: db-custom-1-3840
      database_flags:
        - name: log_min_duration_statement
          value: 0
    state: present

0 logs every completed statement.

After

yaml
- name: Cloud SQL 인스턴스 생성
  google.cloud.gcp_sql_instance:
    auth_kind: serviceaccount
    name: "{{ resource_name }}-db"
    project: test_project
    region: us-central1
    database_version: POSTGRES_13
    settings:
      tier: db-custom-1-3840
      database_flags:
        - name: log_min_duration_statement
          value: -1
    state: present

-1 turns off this threshold. Settings such as log_min_duration_sample can still log SQL statements and their duration.

References