# 4.4 Transactions (The Pinky Swear) ![Transactions: The Pinky Swear](assets/arch_transactions_pinky.png) In the chaos of the depot, mistakes are inevitable. You might start moving coins from one pocket to another, only to get distracted halfway through by a passing peanut. If the elephant forgets where those coins went, they vanish into the digital void! To prevent this, the elephant uses **Transactions**—a formal **Pinky Swear** that a set of actions is one single, indivisible "Unit of Work." The database guarantees absolute atomicity: either every action succeeds in its entirety, or none of them do. ## The Looking Glass Room (Isolation) When you start a transaction, the elephant leads you into a **Looking Glass Room**. While you are inside, you can scribble on your stone tablets, move your coins, and rearrange your suitcases as much as you like. Thanks to **[[Chapter 1/1.4 - The Sharpie Ledger (MVCC)|MVCC]]**, you are working on your own private versions of the data. To the ten other elephants currently looking at the depot, nothing has changed. They see the world as it was before you entered the room. This is **Isolation**: it ensures your half-finished, messy work doesn't confuse anybody else. No one likes a confused elephant! This is **Atomicity**. To the rest of the world, your work either happened completely or it didn't happen at all. There is no such thing as "halfway done" in the eyes of the elephant. It’s all or... well, nothing! ### The Cafe Ordering Session Let's watch a **Pinky Swear** in our restaurant. An animal wants to order a dish and a drink, but only if they can afford both: ```sql -- 1. Stepping into the Looking Glass Room BEGIN; -- 2. Making the first scribble INSERT INTO orders (animal_id, status) VALUES (1, 'Pending'); -- [Note: We get the new order_id back, let's say it's 500] -- 3. Making the second scribble INSERT INTO order_items (order_id, dish_id, quantity) VALUES (500, 10, 1); -- 4. THE GRAND REVEAL COMMIT; ``` If the animal halfway through realizes they forgot their wallet, they just yell **`ROLLBACK;`** instead of `COMMIT`, and the elephant wipes the slate clean. No half-finished orders ever reach the kitchen! ## The Waiting Game Pinky swears aren't free. While you are in your Looking Glass Room, you might be holding onto a cabinet that someone else needs. If you stay in there too long (**Long-Running Transactions**), other elephants start piling up in the hallway, sighing heavily and looking at their watches. Being a good elephant means making your promises, finishing your work, and getting out of the room as fast as possible so everyone else can get back to their naps and their tea. --- [[Chapter 4/4.3 - The Town Crier (Logical Replication)|← 4.3 - Publications & Subscriptions]] | [[Chapter 4/4.0 - Safety Without Sweating (The Write-Ahead Log)|↑ 4.0 - Safety Without Sweating]] | [[Chapter 4/4.5 - The Looking Glass Windows (Isolation)|4.5 - The Looking Glass Windows →]]