skip to content

In PostgreSQL 18, how would you configure auto_explain to log plans of slow queries in production without heavy instrumentation overhead?

level: seniorimportance: should knowfreq 40%

answer

  1. the plan, captured after the fact
  2. it cannot know in advance
  3. rows without the clock
  4. sample, then log

basics

~20 s

Set auto_explain.log_min_duration to a threshold. With log_analyze on, instrumentation runs for every sampled statement, slow or not, so use log_timing = off and a sample_rate below 1. Turn on log_buffers yourself: it stays off by default in 18.

solid answer

~40 s

`auto_explain` logs the plan of any statement that runs longer than `auto_explain.log_min_duration` (milliseconds, default `-1`, off). Plain plans cost almost nothing. `auto_explain.log_analyze = on` adds actual rows and times, but the module cannot know a statement will be slow until it ends. So it switches on per-node instrumentation at executor start for **every statement it samples**, and the documentation warns this can hurt performance badly. I limit that cost two ways. `auto_explain.log_timing = off` keeps actual row counts without clock reads, and `auto_explain.sample_rate`, say `0.05`, means only that fraction of statements is instrumented and eligible for logging. A higher threshold reduces log volume but not instrumentation cost. In 18, plain `EXPLAIN ANALYZE` shows buffers by default, but `auto_explain.log_buffers` is still `off`, so I enable it, and I use `log_format = json` for tooling.

code

ini · 7 lines
ini
# postgresql.conf, with auto_explain preloaded
auto_explain.log_min_duration = '2s'
auto_explain.log_analyze = on
auto_explain.log_timing = off
auto_explain.log_buffers = on
auto_explain.sample_rate = 0.05
auto_explain.log_format = 'json'

go deeper

for a junior

Know that auto_explain writes the plan of a slow statement to the log once it crosses a millisecond threshold, and that it is off until a threshold is set.

for a middle

Explain the defaults: log_analyze off, log_timing on, log_buffers off, sample_rate 1. Know which settings only take effect when log_analyze is on.

for a senior

Explain why instrumentation cost falls on every sampled statement, not just the slow ones, and how log_timing off and sample_rate bring it down. Flag the 18 log_buffers trap.

for a principal

Weigh plan coverage against overhead: decide the sampling rate from how often the problem queries recur, and measure the cost on the real workload before rolling it out across the fleet.

## What auto_explain does `auto_explain` is a contrib module with no SQL functions. It hooks into the executor. When a statement's run time reaches `auto_explain.log_min_duration`, the module writes that statement's plan to the server log, in the form `EXPLAIN` would print it. A slow-query line in the log tells you *that* a statement was slow. The plan logged next to it tells you *how* it ran with the parameters and data of that moment, which you often cannot reproduce later. It can be loaded for one session with `LOAD 'auto_explain'` (superuser only), or preloaded for all sessions through `session_preload_libraries` or `shared_preload_libraries`. The mechanics of preloading belong to the extensions topic, reading the logged plan belongs to the EXPLAIN topic, and rendering it belongs to PEV2. ## The settings and their defaults | setting | default | effect | |---|---|---| | `auto_explain.log_min_duration` | `-1` (off) | threshold in ms; `0` logs every plan | | `auto_explain.log_analyze` | `off` | log `EXPLAIN ANALYZE` output: actual rows and times | | `auto_explain.log_timing` | `on` | per-node timing; only matters with `log_analyze` | | `auto_explain.log_buffers` | `off` | buffer counts; only with `log_analyze` | | `auto_explain.log_wal` | `off` | WAL usage; only with `log_analyze` | | `auto_explain.log_format` | `text` | `text`, `xml`, `json` or `yaml` | | `auto_explain.log_nested_statements` | `off` | also consider statements run inside functions | | `auto_explain.log_parameter_max_length` | `-1` | log bound parameter values in full | | `auto_explain.sample_rate` | `1` | fraction of statements considered, per session | Only superusers can change these settings. ## Why log_analyze is the expensive part A plain plan is cheap: the planner has already built it, and printing it costs little. Actual row counts and timings are different, because they need **instrumentation**: counters on every plan node, updated for every row that passes through it. The module cannot know at the start whether a statement will end up over the threshold. So with `log_analyze` on, it turns instrumentation on at executor start for **every statement it has sampled**, including the 99.9% that finish in a millisecond and are never logged. The manual warns that this can have an extremely negative effect on performance. That leaves three controls, and they act on different costs: 1. **`auto_explain.log_timing = off`.** Timing means reading the system clock around node execution, which is expensive on some platforms. Turning it off keeps actual row counts, and estimate-versus-actual rows is usually what explains a bad plan, at much lower cost. 2. **`auto_explain.sample_rate`.** For each top-level statement, the module decides at executor start whether to sample it. Statements that are not sampled get no instrumentation and are never logged. Nested statements follow their top-level statement, so either all are explained or none are. A rate of `0.05` cuts instrumentation by about 95%, and a slow query that recurs will still be caught within a few occurrences. 3. **`auto_explain.log_min_duration`.** Raising it reduces how many plans get **written**. It does **not** reduce instrumentation, which has already happened by the time the duration is known. ## A production configuration ```ini auto_explain.log_min_duration = '2s' auto_explain.log_analyze = on auto_explain.log_timing = off auto_explain.log_buffers = on auto_explain.sample_rate = 0.05 auto_explain.log_format = 'json' auto_explain.log_parameter_max_length = 256 ``` This logs, for about one statement in twenty, a JSON plan with actual rows and buffer counts whenever that statement takes two seconds or more. Bound parameter values are capped at 256 bytes, so a large JSON payload does not flood the log. ## The 18 trap: buffers PostgreSQL 18 made `EXPLAIN ANALYZE` include `BUFFERS` by default. It is easy to assume `auto_explain` followed suit. It did not: `auto_explain.log_buffers` still defaults to `off` (and needs `log_analyze`). A team that upgrades and expects buffer counts in logged plans gets none unless the setting is written down. ## Other choices worth making - **`log_nested_statements = on`** when the slow work happens inside PL/pgSQL functions. Otherwise only the function call's top-level plan appears. - **`log_verbose = on`** adds output columns and, when `compute_query_id` is active, the `Query Identifier`. That lets you join a logged plan to its `pg_stat_statements` row through `queryid`. - **`log_format = 'json'`** keeps plans machine-readable for log reports and plan visualisers. - Measure the overhead on your own workload before and after enabling `log_analyze`. The cost varies with the platform's clock source and the shape of the queries.

  • Why not just set auto_explain.log_min_duration = 0 on a staging server with log_analyze on?
    On staging that can be fine: you get every plan with actual rows, which suits load tests. The cost is instrumentation on every statement plus a plan in the log for each one, so the throughput you measure there is not what production would see.
  • How do you connect a plan auto_explain logged to the matching pg_stat_statements entry?
    With `auto_explain.log_verbose = on` and query IDs being computed (`compute_query_id` at `on`, or `auto` with pg_stat_statements loaded), the logged plan carries a `Query Identifier`. That value equals `pg_stat_statements.queryid`, and `%Q` in `log_line_prefix` puts it on the log line too.

  • Raising auto_explain.log_min_duration removes the cost of log_analyze on fast queries.
  • auto_explain.log_analyze only instruments queries that end up over the threshold.
  • In PostgreSQL 18, auto_explain logs buffer counts by default, just like EXPLAIN ANALYZE.
  • auto_explain.log_timing = off removes the actual row counts as well.
  • auto_explain.sample_rate picks the slowest fraction of statements to log.

Assumes PostgreSQL 18: EXPLAIN ANALYZE includes BUFFERS by default since 18, while auto_explain.log_buffers still defaults to off.