Find your slow queries, and fix them!
summary
Shows how to find slow queries by enabling log_min_duration_statement (0 logs everything; recommendation: start at 2000 ms, then 100 ms) and a pg-style log_line_prefix, plus log_lock_waits, log_temp_files and log_autovacuum_min_duration to correlate pauses. Uses pg_badger for HTML reports and installs pg_stat_statements (shared_preload_libraries, create extension) with track_io_timing and pg_test_timing. Diagnoses plans via EXPLAIN/ANALYZE and auto_explain, then fixes by adding indexes (including FK refs), increasing work_mem/maintenance_work_mem/effective_cache_size, tuning checkpoints and vacuuming. Example: pg_badger shows a 23s top query; external sorts happen when work_mem is too small.
AI-written summary · about
chapters (23)
- 0:00Intro
- 0:29Stephen Frost
- 1:35Finding Slow Queries
- 3:57log min duration statement
- 7:49log line prefix Pre-pended to every log message
- 11:57log lock waits Logs information when query waits on a lock
- 14:05log temp files
- 17:35Log Analysis
- 18:37Pg stat statements
- 28:11Understanding Why Queries Are Slow
- 32:17PostgreSQL Configuration - work mem
- 33:17PostgreSQL Configuration - maintenance work mem
- 33:38PostgreSQL Configuration - effective cache size
- 34:12PostgreSQL Configuration - shared buffers
- 35:05PostgreSQL Configuration - checkpoints, wal size
- 36:17Dead Tuples / Bloat
- 37:56Putting things together (Joins)
- 39:16What's the best plan?
- 39:55Automating collection of plans
- 40:20Analyzing plans
- 40:35Fixing Slow Queries
- 42:08Prepared Queries
- 46:02Questions?
