In PostgreSQL, what do log_statement and log_min_duration_statement each log, and which one catches slow queries?
answer
- what kind vs how long
- none, ddl, mod, all
- a threshold on completed statements
- -1 off, 0 everything, unit ms
basics
~20 slog_statement logs statements by class (none, ddl, mod, all) before they run, with no duration. log_min_duration_statement logs every completed statement that ran at least its threshold, in milliseconds (-1 disables it). Only the second one catches slow queries.
solid answer
~40 s`log_statement` filters by the **kind** of statement: `none` (the default), `ddl`, `mod` (DDL plus `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`, `COPY FROM`) or `all`. It writes the text once the statement has been parsed, before it runs, so it tells you nothing about speed and at `all` it floods the log. `log_min_duration_statement` filters by **elapsed time**. Every statement that finishes after at least the threshold is logged as `duration: 2314.551 ms statement: ...`. It defaults to `-1` (off), `0` logs every duration, and a bare number means milliseconds. To find slow queries I set it to something like `500ms` and put user, database and PID into `log_line_prefix`. Both are superuser-level settings that take effect on a reload.
code
ini · 6 lines# postgresql.conf: slow-query logging, applied with a reload
log_statement = 'ddl'
log_min_duration_statement = 2s
log_min_duration_sample = 200ms
log_statement_sample_rate = 0.1
log_line_prefix = '%m [%p] %q%u@%d app=%a 'go deeper
Recall the split: log_statement picks statements by type, log_min_duration_statement by run time. Know -1 means off, 0 means everything, and that a bare number is milliseconds.
Explain why log_statement carries no duration: it is written after parsing and before execution. Show how log_line_prefix with PID or session ID pairs a statement line with its later duration line.
Size the threshold to the log volume the system can afford, add sampling with log_min_duration_sample, and say why per-statement logging misses many-fast-calls workloads that pg_stat_statements catches.
Treat the log as a budgeted signal: decide which outliers are worth full text and parameters, which belong in aggregated statistics, and who may change these superuser-level settings per role or database.
## Two filters that answer different questions PostgreSQL has two independent settings that put SQL text into the server log, and they filter on different things. **`log_statement`** filters on the *kind* of statement. **`log_min_duration_statement`** filters on *how long* the statement took. Confusing the two is the usual way someone turns on "query logging", fills a disk, and still cannot say which query is slow. Both settings have the `PGC_SUSET` context. A superuser, or a role granted `SET` on the parameter, can change them, and a change in `postgresql.conf` takes effect on a configuration reload, with no restart. They can also be scoped with `ALTER DATABASE ... SET` or `ALTER ROLE ... SET`, which helps when you want to log only one application role. ## log_statement: what kind of statement `log_statement` is an enum with four values: | value | what is logged | |---|---| | `none` | nothing (the default) | | `ddl` | data definition: `CREATE`, `ALTER`, `DROP` | | `mod` | everything in `ddl`, plus `INSERT`, `UPDATE`, `DELETE`, `TRUNCATE` and `COPY FROM` | | `all` | every statement, `SELECT` included | `PREPARE`, `EXECUTE` and `EXPLAIN ANALYZE` are logged when the statement inside them falls in the chosen class. The line is written once the server has parsed enough to know the statement's type, **before** the statement runs, so it carries **no duration**. That has consequences: - A statement with a plain syntax error is not logged, even at `all`, because classifying it needs a successful parse. On the extended query protocol, a statement that fails during parse analysis or planning is not logged either. The text of failing statements comes from `log_min_error_statement` (default `error`). - For clients on the extended query protocol, the line is written when the Execute message arrives, and it includes the bound parameter values. - At `all`, a busy server writes the text of every statement it runs. That can be gigabytes of log an hour, with real I/O cost. `log_statement = 'ddl'` is a reasonable production setting for a trail of schema changes. `mod` and `all` are for short investigations. ## log_min_duration_statement: how long it took `log_min_duration_statement` is an integer threshold, read as **milliseconds** when no unit is given. Every statement that *completes* after running at least that long is logged with its duration and its text: ```console 2026-10-06 09:14:02.118 UTC [48211] LOG: duration: 2314.551 ms statement: SELECT ... ``` The values that matter: 1. `-1`, the default, disables it. 2. `0` logs the duration of every statement. 3. A positive value such as `500ms` or `2s` logs only statements at or above it. This is the setting that answers "which statements are slow?". For extended-protocol clients, the Parse, Bind and Execute steps are timed and logged separately, so a slow execution shows up as its own line. ## Using both at once The two settings combine cleanly. If `log_statement` has already logged a statement, the later duration line does not repeat its text, so you need a way to pair the two lines. That is the job of `log_line_prefix`. Its default `'%m [%p] '` prints a millisecond timestamp and the process ID. Adding `%u` (user), `%d` (database), `%a` (application name) and `%c` (session ID) makes every line attributable without guessing. ## When even the slow statements are too many On a high-throughput system, even a low threshold can produce more output than you can store. PostgreSQL offers sampling for that case: - `log_min_duration_sample` sets a second, lower threshold (default `-1`, off). - `log_statement_sample_rate` (default `1.0`) is the fraction of the statements above that lower threshold that get logged. - Anything above `log_min_duration_statement` is always logged and never sampled. A common pattern logs every statement slower than two seconds, and one in ten of those slower than 200 ms. ## Where threshold logging stops being the right tool Threshold logging sees only statements that are slow one at a time. A query that takes 3 ms but runs two million times a day never crosses a 500 ms threshold, yet it can cost more server time than every logged statement put together. Aggregated per-query statistics from `pg_stat_statements` catch that pattern. The log catches the outliers, and it gives you the exact text, parameters and timestamp of each one.
- Why might a request the application reports as slow never appear with log_min_duration_statement = 500ms?The log times each statement inside the server. A slow request can be many fast statements: an N+1 loop of 300 queries at 2 ms each, or time spent waiting for a pooled connection or on network round trips. None of those statements crosses the threshold on its own. `pg_stat_statements`, which aggregates calls per normalised query, is what exposes that pattern.
- If log_statement = 'mod' and log_min_duration_statement = 1s are both set, how do you match a logged UPDATE to its duration?The duration line does not repeat text that `log_statement` already logged, so you pair the two lines by process. Put `%p` (PID) or `%c` (session ID) in `log_line_prefix`. The duration line from the same backend that follows the statement line belongs to it.
- Setting log_statement = 'all' logs each statement's duration, so you can sort by it.
- log_min_duration_statement = 0 turns slow-query logging off.
- A bare log_min_duration_statement = 2 means statements over two seconds.
- log_statement = 'mod' logs INSERT, UPDATE and DELETE but not DDL.
- Changing log_min_duration_statement needs a full server restart.