# 3.3 The Soldiers (Scanning & Joining)

The General has drawn up the perfect battle plan. But someone still has to go
out into the mud and execute it. In Postgres, the actual physical labor is
performed by a mismatched army of highly specialized **Operations**.
## The Demand-Driven Army (The Pull Model)
To understand the Soldiers, you must first understand how they communicate. The
Postgres Execution Engine does not "push" data from the bottom up. Instead, it
is a **Demand-Driven Army**.
Imagine the top-most soldier (the [[Postgres/Operations/Result|Result]] node,
let's call him the **Admiral**) standing at the very top of a hill. Every
second, he shouts down to the soldier below him: _"Give me one row!"_
That soldier then shouts down to the soldier below him, and so on, until the
request reaches the **Scouts** at the very bottom. Only then does a single
suitcase (a [[Postgres/Chapter 1/1.1 - The Tuple|Tuple]]) get pulled out of a
shipping container and passed back up the chain.
This "Pull" model ensures the elephant never does more work than he absolutely
has to. If you only asked for the first 10 rows (a
[[Postgres/Operations/Limit|Limit]]), the Admiral stops shouting after the 10th
row arrives, and the entire army immediately puts down its tools, even if there
are millions of suitcases left to scan.
## The Specialized Units
The army is divided into companies, each with its own quirks, strengths, and
heavy machinery:
### 1. The Scouts (Data Retrieval)
The Scouts are the only ones who actually touch the shipping containers (the
[[Postgres/Chapter 1/1.2 - The Page|Pages]]).
- **[[Postgres/Operations/SeqScan|Sequential Scan]]**: The "Brute Force
Company." They walk every single aisle and open every single container. It is
the most honest, yet most exhausting way to find a suitcase.
- **[[Postgres/Operations/IndexScan|Index Scan]]**: The "Scouts on Stilts." They
use the Librarian's Map (Chapter 2) to walk directly to the one container they
need, grab the suitcase, and vanish.
- **[[Postgres/Operations/BitmapHeapScan|Bitmap Heap Scan]]**: The "Whiteboard
Team." If they need to grab many suitcases from different containers, they
scan the Index first, mark the locations on a whiteboard to avoid
backtracking, and then visit each container exactly once.
### 2. The Matchmakers (Joining)
Once two different lists of suitcases have been pulled, the Matchmakers must
find the pairs that belong together.
- **[[Postgres/Operations/NestedLoop|Nested Loop]]**: The "Hamster Wheel." For
every suitcase in List A, a worker walks through every single suitcase in List
B looking for a match. It is fine for tiny lists, but if both lists are large,
the worker will eventually collapse from exhaustion.
- **[[Postgres/Operations/HashJoin|Hash Join]]**: The "Jellybean Wall." This
worker takes List A and sorts all its suitcases into buckets based on their
ID. Then, he simply walks through List B and checks the corresponding bucket.
It's incredibly fast, but it requires a **Desk** big enough to hold the whole
wall.
- **[[Postgres/Operations/MergeJoin|Merge Join]]**: The "Parallel Walkers." If
both lists are already sorted, two workers simply walk down the paths in sync,
high-fiving as they pass matching IDs. It is the most elegant of all joins.
## The Logistics of Battle (Resources)
Every soldier in the army needs a place to work. In Chapter 5, we will explore
**[[Postgres/Chapter 5 - The Hunger of Resources|The Hunger of Resources]]**,
but for now, know this: every soldier is given a small **Desk** (a memory buffer
called `work_mem`).
If a **Sort** or a **Hash Join** is given a desk that is too small for the data
it's holding, the soldier is forced to scream in frustration and start dumping
data into a slow, heavy metal **Filing Cabinet** on the floor (the Disk). This
"Spill to Disk" can turn a lightning-fast battle into a multi-hour siege.
## The Fog of War (Wait Events)
Even the best-organized army eventually runs into traffic. Sometimes, two
soldiers are trying to use the same narrow bridge at the same time, and one must
wait. In Chapter 6, we will delve into **[[Postgres/Chapter 6 - The Waiting
Game|The Waiting Game]]**, where we'll see how these soldiers spend their time
bumping into each other (Locks) or waiting for the slow elevator to return from
the storage basement (I/O Wait).
Every time you run a query, you can use the
[[Postgres/Operations/EXPLAIN|EXPLAIN ANALYZE]] command. This isn't just the
General reading his orders; it's the Admiral hand-delivering a **Battle Report**
that tells you exactly how many rows were pulled, how much each soldier sweated,
and exactly how many times someone had to go to the filing cabinet.