Review Cloud SQL PostgreSQL duration-based SQL logging

Balance diagnostic needs with the sensitivity of SQL content.

Description

PostgreSQL’s log_min_duration_statement logs the SQL text and duration of statements that run at least as long as the threshold. A positive number without a unit means milliseconds. 0 logs every completed statement.

Potential impact

A low threshold can record sensitive SQL values, increase log volume and add overhead. A high threshold can hide queries you need to diagnose.

Remediation

  • Choose a threshold that captures the slow statements you need. Use -1 only when you can turn off duration-based logging.
  • Check the resulting logs for diagnostic detail, volume and sensitive values. Review query performance and limit log access and retention.

Examples

Use a supported engine version and machine type, and fill in the remaining instance settings. You can change the threshold on the same PostgreSQL version.

Before

hcl
resource "google_sql_database_instance" "db" {
  name             = "postgres-instance"
  database_version = "POSTGRES_14"
  region           = "us-central1"

  settings {
    database_flags {
      name  = "log_min_duration_statement"
      value = "2"
    }
  }
}

After

hcl
resource "google_sql_database_instance" "db" {
  name             = "postgres-instance"
  database_version = "POSTGRES_15"
  region           = "us-central1"

  settings {
    tier = "db-f1-micro"

    database_flags {
      name  = "log_min_duration_statement"
      value = "-1"
    }
  }
}

The first example logs statements taking at least 2 ms. The second turns off this duration-based logging. Other logging settings still apply.

References