On a PostgreSQL 18 server, what do log_lock_waits, log_temp_files and log_connections add to the log, and how should log_line_prefix support them?
answer
- waits, spills and arrivals
- deadlock_timeout as the trigger
- kilobytes, logged on deletion
- 18 turned a boolean into a list
- %Q needs compute_query_id
basics
~20 slog_lock_waits logs any lock wait longer than deadlock_timeout, with holders and queue. log_temp_files logs temporary files above a size in kB. log_connections, a list since 18, logs connection stages and setup time. log_line_prefix adds user, database, application and query ID.
solid answer
~40 s`log_lock_waits` (default `off`) logs `process N still waiting for ShareLock on transaction X after 1000.07 ms` when a wait outlasts `deadlock_timeout` (1 s), with the holders and the wait queue, then an `acquired` line when the lock is granted. `log_temp_files` (default `-1`, value in kB) logs each temporary file at or above the threshold when it is deleted, with its size in bytes, so you see which statements spill. In 18, `log_connections` takes a list: `receipt`, `authentication`, `authorization`, `setup_durations` or `all`. The old `on` still means the first three. `setup_durations` times fork and authentication, which helps during connection storms. I extend the default `'%m [%p] '` prefix with `%u`, `%d`, `%a`, `%h` and `%Q`. `%Q` prints the query ID, which joins log lines to `pg_stat_statements`, but only while `compute_query_id` is active.
code
ini · 7 lines# postgresql.conf (PostgreSQL 18)
log_lock_waits = on
log_temp_files = 10MB
log_connections = 'authorization,setup_durations'
log_disconnections = on
compute_query_id = on
log_line_prefix = '%m [%p] %q%u@%d app=%a client=%h qid=%Q 'go deeper
Recall that lock waits, temp files and connections each have their own log setting, all off by default, and that log_line_prefix controls what appears before every line.
Explain the triggers and units: deadlock_timeout for lock waits, kilobytes with logging at deletion for temp files, and the 18 list values for log_connections.
Build a prefix that ties lines to user, application and query ID, explain why %Q depends on compute_query_id, and use setup_durations to diagnose slow connects.
Treat logging as part of incident readiness: agree which signals are always on, which are switched on during an investigation, and how log lines join to query statistics.
## Three questions the slow-query log cannot answer A statement-duration log shows *that* something was slow. It does not show whether the time went to waiting for a lock, to spilling a sort to disk, or to setting up the connection before the query ran. PostgreSQL has a separate log setting for each of those, and all three are off by default. | setting | default | context | logs | |---|---|---|---| | `log_lock_waits` | `off` | superuser, reload | lock waits longer than `deadlock_timeout` | | `log_temp_files` | `-1` (off) | superuser, reload | temp files at or above N kB, on deletion | | `log_connections` | `''` (off) | superuser, new sessions only | chosen stages of each connection | | `log_line_prefix` | `'%m [%p] '` | reload | the prefix on every line | ## log_lock_waits When a backend has waited for a lock for `deadlock_timeout` (default `1s`), it runs the deadlock check. With `log_lock_waits = on` it also writes a line: ```console LOG: process 48211 still waiting for ShareLock on transaction 9120044 after 1000.072 ms DETAIL: Process holding the lock: 48190. Wait queue: 48211, 48230. ``` If the lock is granted later, a second line reports `acquired ... after N ms`. Together they give you the holder's PID, the queue behind it and how long the wait lasted, after the fact, when it is too late to query live views. Two points matter: - The threshold is `deadlock_timeout`, so lowering that setting to see shorter waits also makes deadlock checks run more often. - 18 added `log_lock_failures` (default `off`), which logs the detail when a `SELECT ... NOWAIT` fails to get its lock. Live blocker triage with `pg_blocking_pids()` belongs to the activity topic, and what each lock mode conflicts with belongs to the locking topic. ## log_temp_files Sorts, hashes and materialised intermediate results that outgrow their memory budget write temporary files. `log_temp_files` sets a size threshold in **kilobytes** (`0` logs every file). Each qualifying file is logged when it is **deleted**, with its size in **bytes**: ```console LOG: temporary file: path "base/pgsql_tmp/pgsql_tmp48211.0", size 268435456 ``` Because `log_min_error_statement` defaults to `error` and LOG-level messages count as more severe than that for the server log, the statement that created the file is usually printed on a `STATEMENT:` line after it. That is how you attribute spills to queries. Sizing `work_mem` to stop them belongs to the memory-tuning topic. ## log_connections in 18 Before 18, `log_connections` was a boolean. In 18 it is a **list**: 1. `receipt`: the connection arrived. 2. `authentication`: the identity the auth method established. Failed authentication is always logged anyway. 3. `authorization`: the session was authorised, with user, database and application name. 4. `setup_durations`: total setup time, fork time and authentication time. 5. `all`: every stage. For backward compatibility, `on`, `true`, `yes` and `1` still work and mean `receipt,authentication,authorization`. They do not include `setup_durations`. The context is `PGC_SU_BACKEND`, so a change applies to new sessions and cannot be made inside a running one. `log_disconnections` is still a separate boolean that logs session end and session duration. `setup_durations` is the useful new part when an application's connection storm or a slow authentication backend makes connects slow. ## log_line_prefix: make every line attributable The default prefix, `'%m [%p] '`, gives a timestamp and a PID. For diagnosis, extend it: - `%u` user and `%d` database, so you know whose query it was - `%a` application name, so you know which service sent it - `%h` or `%r` client host (with port for `%r`) - `%c` session ID, to follow one session across lines - `%Q` **query identifier**, which is zero unless `compute_query_id` is enabled (`on`, or `auto` with a module such as `pg_stat_statements` that requires it) - `%q` ends the prefix early for background processes that have no user or database `%Q` is the useful link. A slow statement's line, its temp-file line and its `pg_stat_statements` row can all be joined on the same `queryid`. `compute_query_id` defaults to `auto`. Set it to `on` if you want IDs in the log without loading `pg_stat_statements`. `log_line_prefix` is a reload-time setting. ```ini log_lock_waits = on log_temp_files = 10MB log_connections = 'authorization,setup_durations' log_line_prefix = '%m [%p] %q%u@%d app=%a client=%h qid=%Q ' ```
- Should you lower deadlock_timeout to 100ms to see shorter lock waits?It works, because `log_lock_waits` fires at `deadlock_timeout`, but every wait that long now also runs the deadlock check, which is not free under heavy contention. Most teams keep 1 s and lower it only during an investigation, or only for the roles involved, since it is a superuser-settable parameter.
- Why does %Q print 0 on every line after you add it to log_line_prefix?No query ID is being computed. `compute_query_id = auto` computes one only when a module such as `pg_stat_statements` asks for it. Load that module or set `compute_query_id = on`. Lines that are not tied to a query, such as connection messages, show zero regardless.
- log_lock_waits logs a wait the moment a session starts waiting.
- log_temp_files = 1024 means one gigabyte of temp file.
- In PostgreSQL 18, log_connections = on also logs setup durations.
- log_connections can be switched on inside a running session with SET.
- %Q in log_line_prefix always shows a query ID, whatever compute_query_id says.
Assumes PostgreSQL 18: log_connections changed from a boolean to a list of stages in 18; on 17 and earlier it is on/off only.