Sort Spill (Temp Files from Sorts)
pg_views: pg_stat_statements, pg_stat_database
Full detection logic, thresholds, and guided fixes ship in the product.
Book a demo →Overview
When Postgres sorts rows for ORDER BY, GROUP BY, or merge joins and the intermediate result exceeds work_mem, it spills to disk as a temporary file. Disk I/O is orders of magnitude slower than memory—a sort that spills is often 10-100x slower than one that fits in work_mem. Temp file spills also consume disk space and increase I/O pressure on all concurrent queries.
insightral detects spills at both the database level (pg_stat_database.temp_files) and per query (pg_stat_statements.temp_blks_written). The preferred fix is usually an index that allows an index scan to return rows in sorted order rather than requiring a sort step. Increasing work_mem is a secondary lever—it helps for queries where a sort is unavoidable.
See this rule in action
Full detection logic, thresholds, and guided fixes ship in the product. The licensed agent runs this check — and all others — continuously against your database, surfaces findings with AI explanations, and surfaces a guided remediation path inside the dashboard.
Book a demo →Common False Positives
- Intentional large batch exports or ETL jobs that always spill by design — consider using a dedicated read replica with higher work_mem for these.
- Temp files from hash joins (pg-r09) show up in the same counters — check EXPLAIN to distinguish sort spills from hash spills.
Sort Spill (Temp Files from Sorts) — and 100+ more checks — in the licensed product
Full detection logic, thresholds, and guided fixes ship in the product.
Book a demo →Last updated: 2026-05-13 · View the rule library