# 5.1 The Warming Rack (Shared Buffers)

In Chapter 1, we saw how every suitcase (row) is packed into an 8KB **[[Chapter 1/1.3 - The Shipping Container (The Page)|Shipping Container (Page)]]**. But the elephant is far too lazy to carry these pages up from the **frozen pantry** every time someone asks a question.
Instead, he keeps a **Warming Rack** right in the middle of the room. This rack is called the **Shared Buffers**. If a block of data is going to be accessed frequently, it must be kept warm and ready.
## The Microwave Mentality
The Warming Rack is the most expensive piece of furniture in the depot. It is where the most popular pages live, kept warm and ready for instant access.
When a staff member needs a page, he first checks the rack. If it's there (a **Buffer Hit**), he can read it in the time it takes to blink. If it's not (a **Buffer Miss**), he has to put on his winter coat and head toward the basement.
This is why we usually only give the elephant **25% of the room's RAM** for his Warming Rack (`shared_buffers`). We leave the rest for the Building Manager to manage the Back Hallway!
**The Causal Account**: Why not give the Elephant 100% of the RAM? Because the Building Manager (the OS) is actually much better at managing the file-level plumbing—the literal pipes and wires that connect the Frozen Pantry to the kitchen. If the Elephant takes everything, the Manager can't do his job, and the whole building gets sluggish with "Double Buffering" (storing the same page twice).
**The Shape of the Idea**: Shared Buffers is for the **"Hot Plates"** you are eating right now; the OS cache is for the **"Cold Ingredients"** that might be needed for the next table.
## The Clock Sweep (Who Stays Warm?)
Because the rack is small and the depot is infinite, the elephant must eventually decide which pages to kick off the rack to make room for new ones. He uses a clever game called the **Clock Sweep Algorithm**.
It is a strict recipe for rotation:
1. Every page on the rack has a **Usage Count** (a tiny number from 0 to 5). Think of it as how many tea-party invitations the page has.
2. The elephant walks around the rack like a clock hand.
3. Every time he passes a page, he decrements its count by 1.
4. If he finds a page with a count of **0**, and the page isn't "Dirty" (currently being written to), he kicks it off the rack and puts a new page there.
5. But if a staff member accesses a page, its count is instantly bumped back up (to a max of 5)!
This ensures that "Hot" pages (the ones everyone is looking at) stay warm and cozy, while legacy data slowly cools down and returns to the frozen pantry.
### The Size of the Rack
You can ask the elephant exactly how large his Warming Rack is with a simple command:
```sql
-- How many pages can we keep warm?
SHOW shared_buffers;
-- Results:
-- shared_buffers
-- ----------------
-- 128MB
```
If you want to see exactly *which* suitcases are currently warm, you can use the **`pg_buffercache`** extension. It’s like a thermal camera for the depot!
---
| ← Previous | ↑ Table of Contents | Next → |
| :--- | :---: | ---: |
| [[Chapter 5/5.0 - The Hunger of Resources (Memory & Disk)\|Chapter 5 - The Hunger of Resources (Memory & Disk)]] | [[Learn You a Postgres for Great Good\|Home]] | [[Chapter 5/5.2 - The Private Desk (Work Mem)\|5.2 The Private Desk (Work Mem)]] |