Well-configured PostgreSQL logs are often the fastest way to diagnose slow queries, lock contention, checkpoint storms, and autovacuum lag.

A practical production logging configuration:

logging_collector = on
log_destination = 'stderr'         # also supports 'csvlog', 'jsonlog', 'syslog'
log_directory = 'log'
log_filename = 'postgresql-%Y-%m-%d.log'
log_rotation_age = '1d'
log_rotation_size = '100MB'
log_truncate_on_rotation = on

log_timezone = 'UTC'
lc_messages = 'C'
log_line_prefix = '%m [%p] %q%u@%d/%a '

log_min_messages = warning
log_min_duration_statement = '1s'  # log queries slower than 1s
log_checkpoints = on
log_connections = on
log_disconnections = on
log_lock_waits = on                # log waits exceeding deadlock_timeout
log_temp_files = 0                 # log all temp file spills to disk
log_autovacuum_min_duration = '1s'

Use log_destination = 'jsonlog' (PostgreSQL 15+) or 'csvlog' if you ship logs to Loki, Elasticsearch, or OpenSearch for structured parsing.

Key parameters explained#

ParameterWhat it captures
log_min_duration_statementLogs statements taking longer than the threshold (0 logs all, -1 disables).
log_statementControls SQL logging by type: none, ddl, mod (DDL + DML), or all.
log_durationLogs the duration of every completed statement (can be noisy under high QPS).
log_checkpointsShows checkpoint trigger reason, buffers written, sync time, and WAL activity.
log_lock_waitsLogs any session waiting longer than deadlock_timeout (default 1s) for a lock.
log_temp_filesLogs temporary files created for sorts/hashes that exceed work_mem (0 = all).
log_autovacuum_min_durationLogs slow autovacuum runs (0 logs every run).
log_error_verbosityDetail level: terse, default, or verbose.

Common log_line_prefix escapes#

  • %m — Timestamp with milliseconds
  • %p — Process ID (PID)
  • %u — User name
  • %d — Database name
  • %a — Application name
  • %h — Remote host
  • %x — Transaction ID
  • %q — Stops output for non-session (background) processes

Applying changes without restarting#

Most logging parameters (except logging_collector) can be reloaded live:

SELECT pg_reload_conf();

Check which parameters require a restart vs reload:

SELECT name, setting, context
FROM pg_settings
WHERE name LIKE 'log_%'
ORDER BY name;