![Hunger of Resources](Postgres/assets/chap_5_resources.png) # Chapter 5: The Hunger of Resources A lazy elephant is a hungry elephant. While the engine desperately tries to avoid work, the physical reality of resolving your queries requires brutal, tangible resource consumption. In the world of Postgres, resources dictate the hierarchy of effort. ## The Clock of the Gods (Elephant Time) To understand why the elephant is so obsessed with his snacks and so terrified of the filing cabinet, we have to talk about the brutal reality of digital time. In the digital world, time moves so fast that it’s hard for a large, slow-moving mammal to comprehend. If we imagine that **one CPU cycle (0.3 ns) is equal to one second**, then the world of information looks like this: | Action | Digital Time | Elephant Time | Metaphor | | :--------------------- | :------------- | :-------------- | :---------------------------------------------------- | | **L1 Cache Reference** | 0.5 ns | **1.5 seconds** | A quick blink of the eye. | | **Branch Mispredict** | 5 ns | **15 seconds** | Walking into the wrong room and turning around. | | **L2 Cache Reference** | 7 ns | **21 seconds** | Checking your pocket for a snack. | | **Main Memory (RAM)** | 100 ns | **5 minutes** | Walking to the kitchen to boil a kettle. | | **SSD Random Read** | 150,000 ns | **1.7 days** | A weekend road trip to buy a single stamp. | | **Network (Same DC)** | 500,000 ns | **5.7 days** | Waiting a full week for a carrier pigeon to return. | | **HDD Seek (Disk)** | 10,000,000 ns | **4 months** | A grueling, season-long winter expedition. | | **Packet (CA to NL)** | 150,000,000 ns | **4.7 years** | Earning a full university degree before data arrives. | ## The Lesson of the Desk When you look at this list, the elephant’s "laziness" starts to look like extreme wisdom. The database will fight viciously to keep its most critical data (the [[Postgres/Resources/Memory#Shared Buffers|Shared Buffers]]) and active sorted lists (the [[Postgres/Resources/Memory#work_mem|work_mem]] "desk space") hovering safely inside **Main Memory**. Reachable in the time it takes to brew a cup of tea, memory is the warm, messy desk right in front of the elephant. But the alternative is the **[[Postgres/Resources/Disk IO|Filing Cabinet]]**. The moment the elephant has to touch the Disk, he is committing to a **4-month-long journey**. Every time the elephant has to get up, shiver in the cold, and yank open a heavy iron drawer to fetch a single [[Postgres/Chapter 1/1.2 - The Page|Page]], an immense punishment is enacted against performance. ![[Postgres/assets/ex_seek.svg|80]] The entire architecture of the database—from caching algorithms to the [[Postgres/Operations/BitmapHeapScan|Bitmap Heap Scan]]—is designed to violently rip data away from the frozen disk and hoarding it onto the warm desk of Memory. ## The Exhaustion Threshold Every soldier in the engine's army (Chapter 3) is given a strictly defined amount of desk space (`work_mem`). When a query becomes too massive—perhaps asking for a **Sort** of ten million rows—the list outgrows the desk. The elephant is then forced to "spill" his work. He frantically attempts to juggle the data by breaking it apart and gluing it back together in smaller chunks, resulting in spikes of **[[Postgres/Resources/CPU|CPU]]** exhaustion and the dreaded sound of the filing cabinet drawers slamming open and shut. Understanding the engine is simply understanding how fiercely the elephant guards its snacks. Doing the least work possible isn't just a lifestyle choice—it's the only way to survive when every mistake costs you a semester of your life.