Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 15 / Topic 4 of 7

Memory

Three numbers explain most memory behaviour:

SHOW shared_buffers;
SHOW work_mem;
SHOW maintenance_work_mem;
 shared_buffers 
----------------
 128MB
(1 row)

 work_mem 
----------
 4MB
(1 row)

 maintenance_work_mem 
----------------------
 64MB
(1 row)
  • shared_buffers — Postgres's own page cache, shared by all backends (the default 128 MB is deliberately modest; production commonly gets ~25% of RAM). Pages are read into it, modified in it, flushed from it.
  • work_mem — the per-operation budget for sorts and hashes. A query with four sorts may take four helpings; a hundred connections doing that is how servers run out of memory. When an operation outgrows it, Postgres spills to temporary disk files — Part 16 shows you the tell-tale in a plan.
  • maintenance_work_mem — the bigger allowance for occasional heavy jobs: VACUUM, CREATE INDEX.

The subtlety worth knowing: Postgres also benefits from the operating system's file cache — data not in shared_buffers is often still in RAM. Which is why a "cold" first query can be slow and its repeat instant, and why Part 16's BUFFERS option distinguishes cache hits from real reads.

🧪 Quick quiz — answer from memory, then check.

  1. Is work_mem per connection?
  2. What happens when a sort exceeds work_mem?
Show the answers
  1. Per operation — one query can consume several multiples of it.
  2. It spills to temporary files on disk and carries on, slower.

Done here? The practice set for Part 15 — 5 exercises with solutions — is on the part page.

Search the guide