The idea · Lab 3.6.3auto_explain hooks into query execution to automatically log an EXPLAIN plan for any statement exceeding a configured duration threshold — exactly what a query that only manifests under specific, unreproducible production load needs, since there is no need to catch it happening live or guess at reproducing it manually. Unlike pg_stat_statements from Lesson 3.6.1, auto_explain lives in session_preload_libraries and genuinely loads with nothing more than a configuration reload — a real, direct contrast worth confirming rather than assuming both preload mechanisms behave identically.
Seeing its output turns out to depend on something else entirely: this VM's logging_collector defaults to off, meaning log messages normally go straight to the server's console rather than any persistent file — a real, separate requirement (and a real restart, since logging_collector is postmaster-context) that has nothing to do with auto_explain itself. And since pgBadger, the curriculum's suggested report generator, is confirmed absent on this server (no network, no compiler), this lab substitutes real grep against the real log file — the same honest substitution pattern already used for every other absent tool this course has encountered.
Loading auto_explain, No Restart Required
sed -i "s/#session_preload_libraries = ''/session_preload_libraries = 'auto_explain'/" /var/lib/postgresql/19/data/postgresql.conf
SELECT pg_reload_conf();
SHOW session_preload_libraries; -- auto_explain
Genuinely applied with just a reload — a real, direct contrast with pg_stat_statements' shared_preload_libraries in Lesson 3.6.1, which needed a full restart.
A Separate, VM-Specific Requirement: logging_collector
# This VM's own postgresql.conf explicitly sets logging_collector = off by default —
# auto_explain's messages would otherwise go straight to the console, not any file
sed -i 's/logging_collector = off/logging_collector = on/' /var/lib/postgresql/19/data/postgresql.conf
pg_ctl restart -D /var/lib/postgresql/19/data -w
LOG: redirecting log output to logging collector process
HINT: Future log output will appear in directory "log".
logging_collector is postmaster-context — a genuine restart, entirely separate from auto_explain's own reload-only requirement a moment ago.
Only the Genuinely Slow Query Gets Captured
SET auto_explain.log_min_duration = 100;
SET auto_explain.log_buffers = on;
SET auto_explain.log_analyze = on;
SELECT pg_sleep(0.05); -- 50ms, below the threshold
SELECT b.name, r.score FROM beers b JOIN beer_ratings r ON r.beer_id = b.id ORDER BY r.score DESC LIMIT 5; -- genuinely slower
LOG: duration: 154.002 ms plan:
Query Text: SELECT b.name, r.score FROM beers b JOIN beer_ratings r ON r.beer_id = b.id ORDER BY r.score DESC LIMIT 5;
Limit (cost=210.85..210.86 rows=5 width=44) (actual time=154.002..154.002 rows=5.00 loops=1)
Buffers: shared hit=3 read=98
The 50ms pg_sleep never appears in the log at all — only the query that genuinely exceeded the 100ms threshold, with its real execution plan and real buffer counts attached automatically.
pgBadger Confirmed Absent — Real grep Instead
which pgbadger # nothing
grep -E 'duration:|Buffers:' /var/lib/postgresql/19/data/log/postgresql*.log
LOG: duration: 158.389 ms plan:
Buffers: shared hit=3 read=98
Buffers: shared hit=3 read=98
Buffers: shared read=98
read=98 is the largest single buffer-read count in this plan — the direct evidence for exactly where an index would help most, without needing pgBadger's formatted HTML report to find it.
Key concepts
session_preload_libraries vs shared_preload_libraries
Both preload modules before a session starts using them, but shared_preload_libraries loads a module into shared memory at postmaster startup (needed by modules like pg_stat_statements that maintain cluster-wide state, requiring a genuine restart), while session_preload_libraries loads a module fresh for each new session's own backend process, which is why a plain configuration reload is enough — no shared memory allocation is involved.
auto_explain.log_min_duration
The threshold, in milliseconds, above which auto_explain automatically logs a query's execution plan. Queries completing faster than this are not logged at all — set too low, it floods the log with routine fast queries; set at a genuinely meaningful threshold (matching what "slow" actually means for the workload), it captures exactly the outliers worth investigating, with zero need to reproduce them manually first.
What to remember
- auto_explain lives in session_preload_libraries, which loads fresh per session and needs only a reload — a real, direct contrast with shared_preload_libraries modules like pg_stat_statements, which need a restart
- Seeing any log output at all depends on logging_collector being on — a separate, postmaster-context setting requiring its own genuine restart, unrelated to auto_explain's own requirements
- auto_explain.log_min_duration determines exactly which queries get captured — genuinely below the threshold means genuinely never logged, confirmed directly rather than assumed
- auto_explain.log_buffers attaches real buffer hit/read counts to the captured plan automatically, pinpointing exactly where the most I/O in a slow query actually happened
- When a suggested tool (pgBadger here) is confirmed absent, plain grep against the real log file answers the same essential question directly — the same honest substitution pattern used for every other absent tool in this course