PostgreSQL EXPLAIN guide

PostgreSQL Disk Sort and work_mem

When a sort cannot fit in its available memory, PostgreSQL can spill to temporary disk. EXPLAIN ANALYZE reports the sort method and disk usage, making this one of the more directly measurable performance signals.

Example EXPLAIN output

Sort
  Sort Key: created_at DESC
  Sort Method: external merge  Disk: 84240kB
  Actual Rows: 1,201,844

What PostgreSQL is telling you

Sort Method can show external merge or another disk-backed method.

Disk usage shows spill size.

The sort node's timing and input rows reveal the cost.

What it can mean

Disk I/O can make large sorts substantially slower.

Increasing work_mem may help, but it applies per sort/hash operation and can multiply across concurrent queries.

Avoiding or reducing the sort can be better than simply allocating more memory.

What to check next

Sort Method and disk usage.

Rows entering the sort.

Current work_mem.

Concurrent query volume.

Whether an index can satisfy the desired order.

Whether earlier filtering can reduce sort input.

When this is not automatically a problem

  • • A small spill with low runtime may not matter.
  • • Globally increasing work_mem can create memory pressure.
  • • The safest fix depends on concurrency and query shape.

Check your actual plan

PlanCheck analyzes TEXT or JSON locally in your browser. Your SQL and plans are not uploaded.

Analyze EXPLAIN output