When an agent encounters this scenario, Schema provides these diagnostic steps automatically.
When investigating excessive temporary file creation in PostgreSQL, start by quantifying the problem through pg_stat_database metrics, then identify the specific queries creating temp files. Analyze their execution plans to understand whether the issue is insufficient work_mem for legitimate operations or inefficient queries that need optimization. Test per-query work_mem increases before making global changes, and always weigh the memory allocation risk against the number of concurrent connections.
1Quantify temporary file activity and confirm the problem
Check `postgresql-temp-bytes` and `postgresql-temp-files` in pg_stat_database to establish a baseline. If you're seeing hundreds of GB in temp_bytes or thousands of temp files per hour, you have a real problem. The `excessive-temporary-file-creation` pattern confirms this is causing disk I/O instead of memory-based operations. Look at the rate of growth — is it consistent or spiking with specific workloads?
2Identify which specific queries are creating temporary files
Query pg_stat_statements ordered by temp_blks_written to find the offenders. Look at `postgresql-queries-temp-blks-written` to see which queries are writing the most temporary blocks. A single query writing millions of blocks is a different problem than many queries each writing a few thousand. Also check `postgresql-queries-duration-max` to correlate slow queries with temp file usage — queries that both run long and write temp files are your primary targets.
3Analyze execution plans to understand what operations spill to disk
Run EXPLAIN (ANALYZE, BUFFERS) on the problematic queries. Look for 'Sort Method: external merge' or 'Hash Buckets: ... Batches: X' where X > 1 — these indicate disk-based operations. The `insufficient-work-mem` insight explains that sorts, aggregations, and hash joins spill to disk when work_mem is too low. Pay attention to the 'Disk:' line in EXPLAIN output showing exactly how much temp space was used.
4Test per-query work_mem increases to verify memory solves it
Before changing anything globally, SET work_mem = '128MB' (or higher) for a specific session and re-run the problematic query with EXPLAIN (ANALYZE, BUFFERS). If the temp file usage drops to zero and the query speeds up significantly, you've confirmed work_mem is the issue. The `need-for-per-query-work-mem-tuning` pattern suggests this is often the right solution — tune work_mem per-query or per-session rather than globally. Reset work_mem afterward to avoid session memory bloat.
5Look for query optimization opportunities to reduce temp file need
Even if increasing work_mem helps, check whether the query itself is inefficient. Missing indexes on JOIN or WHERE clauses force large hash joins or nested loops that consume memory. Unnecessary ORDER BY clauses or redundant sorts can be eliminated. The `queries-need-optimization-to-reduce-temp-usage` insight reminds us that fixing the root cause — the query — is better than just throwing memory at it. Can you add an index to avoid a sort, or rewrite a CTE to reduce the working set?
6Calculate memory allocation risk before increasing work_mem globally
The danger of increasing work_mem is that PostgreSQL can allocate up to work_mem × max_connections in the worst case. If you have 200 connections and set work_mem to 256MB, you could theoretically need 50GB of RAM. The `risk-of-memory-over-allocation-if-work-mem-increased` insight highlights this tradeoff. For OLTP workloads with many connections, keep work_mem modest (16-64MB) and use per-query tuning. For OLAP workloads with few connections, you can safely go higher (128MB+).
7Monitor disk I/O impact from temporary files
Check `postgresql-io-write-time` to see if temp file creation is causing I/O contention. High write times correlate with temp file spikes indicate your storage is struggling to keep up. The `disk-i-o-impact-from-temp-files` pattern shows that excessive temp files don't just slow the query itself — they can degrade overall system performance by saturating disk bandwidth. If I/O wait times are high, prioritizing work_mem increases or query optimization becomes even more urgent.