#buffer-cache-basics
How is the buffer cache built, and why have it if there is an OS file cache?
What to say
The buffer cache is an area in shared memory common to all backends, sized by `shared_buffers`, sliced into 8 KB slots. Every page read and write goes through it: a backend does not touch the file directly but asks the buffer manager for the page. If the page is already there, that is a hit and the disk is not touched. The OS cache also exists and works a layer below, but PostgreSQL's buffer cache knows about MVCC, dirty pages, and WAL, so it can guarantee the write-ahead rule and not flush to disk what has not yet been recorded in WAL.
What they want to hear
A senior should: - describe the cache as shared memory of 8 KB buffers through which all page access goes - explain why a separate cache over the OS cache: control of dirty pages and enforcing the WAL rule - understand double caching: one page can sit in both `shared_buffers` and the OS page cache - give a sizing guide (around a quarter of RAM as a start) and say why "more" is not always better
Pitfalls
- ✗ Setting `shared_buffers` to nearly all memory. The OS cache is needed too, and checkpoints start writing in a flood
- ✗ Thinking a backend reads the file directly. Everything goes through the buffer manager
- ✗ Confusing a buffer cache hit with an OS cache hit. They are two different layers
Follow-up
- ? Why is `shared_buffers` usually not set to 90% of memory?
- ? How does `pg_buffercache` show what is in the cache right now?
- ? What is double caching, and why is it bad?
Depth in knowledge base