Slow query logging turns intermittent slowness into actionable evidence by recording the SQL statements that exceed an execution-time threshold.
PostgreSQL can emit a duration: log line for slow statements when log_min_duration_statement is set, capturing both the elapsed time and the statement text. A sensible threshold keeps the signal-to-noise ratio high compared to logging every statement.
Slow-statement logs can grow quickly and may include sensitive literals embedded in SQL text, so threshold choice and retention policies matter. Server-wide configuration via ALTER SYSTEM requires superuser privileges and takes effect on reload, while managed services often expose the same parameter via provider settings instead of systemd.
Related: How to analyze slow queries in PostgreSQL \\
Related: How to optimize PostgreSQL performance
$ sudo -u postgres psql -c "ALTER SYSTEM SET log_min_duration_statement = '500ms';" ALTER SYSTEM
Use a higher threshold first when log volume is unknown. Lower it once noisy statements are filtered. Set log_min_duration_statement to -1 to disable slow statement logging.
$ sudo -u postgres psql -c "SELECT pg_reload_conf();" pg_reload_conf ---------------- t (1 row)
$ sudo -u postgres psql -Atc "SHOW log_min_duration_statement;" 500ms
$ sudo -u postgres psql -d postgres -c "SELECT pg_sleep(0.6);" pg_sleep ---------- (1 row)
$ sudo grep -hF "SELECT pg_sleep(0.6)" /var/log/postgresql/postgresql-2025-12-29.log | tail -n 1 2025-12-29 08:45:36.367 UTC [5666] 69523faf.1622 4 postgres@postgres [local] app=psql LOG: duration: 610.600 ms statement: SELECT pg_sleep(0.6);
On hosts that write logs to /var/log/postgresql, search those files or the systemd journal instead.