Skip to main content

Featured

GPU Memory Sharding with FSDP

GPU Memory Sharding with FSDP

DISTRIBUTED DEEP LEARNING — PART 1/5 Day 1: Demystifying GPU Memory & The ZeRO Revolution — DeepSpeed, FSDP & State Sharding 25 min read Series: The Dharma of Development Distributed DL (Day 1 / 5) Level: Principal / Systems AI Engineer 💥 Context: You attempt to fine-tune or pretrain a 13-billion parameter dense transformer on an 80GB NVIDIA H100 or A100 GPU using standard PyTorch. You set your batch size to 1. You press enter. Within three seconds, your terminal explodes with the most dreaded error in artificial intelligence: torch.cuda.OutOfMemoryError: CUDA out of memory . How is this possible? In 16-bit precision, 13 billion parameters occupy only ~26 GB of disk space. Why can't an 80 GB state-of-the-art GPU train a 26 GB model? Because the naive mental model of machine learning memory is deeply flawed. Today, we demystify the true $16\Phi$ memory footprint of neural networks and master the architecture that made modern large language models possi...

PostgreSQL Indexing Mechanics — B-Trees, GIN Indexes, and EXPLAIN ANALYZE

BACKEND SERIES

Day 32: PostgreSQL Indexing Mechanics — B-Trees, GIN Indexes, and EXPLAIN ANALYZE

Series: Logic & Legacy
Day 32 / 50
Level: Senior / Database Reliability

⏳ Context: Adding an index to a slow database table is often treated as a silver bullet. But unguided indexing leads to write amplification, bloated disk usage, and unused indexes that slow down INSERT and UPDATE transactions. To write high-performance SQL queries in Python backends, software architects must understand how PostgreSQL stores indexes in memory, how B-Trees map to Heap Tuples (CTIDs), when GIN inverted indexes are required for JSONB, and how to read EXPLAIN (ANALYZE, BUFFERS) execution plans.




1. Anatomy of a B-Tree Index: Leaf Nodes & CTIDs

By default, CREATE INDEX in PostgreSQL builds a multi-level self-balancing B-Tree. B-Tree leaf nodes store ordered key values paired with ItemPointers (CTIDs)—a physical location tuple (page_number, tuple_index) pointing directly to the raw data row on disk in the heap file.

SQL B-Tree & Partial Index Creation
-- Standard B-Tree Composite Index for Range Queries
CREATE INDEX idx_orders_user_created 
ON orders (user_id, created_at DESC);

-- Partial Index: Ultra-lean index tracking only unprocessed tasks
CREATE INDEX idx_pending_jobs 
ON background_jobs (created_at) 
WHERE status = 'PENDING';

2. Indexing Semi-Structured Data: GIN (Generalized Inverted Index)

B-Trees cannot efficiently index internal keys inside a JSONB document or elements inside an array. A GIN (Generalized Inverted Index) breaks composite JSONB objects into individual key-value component entries, mapping each entry to an array of matching CTIDs.

GIN Indexing for Fast JSONB & Full-Text Search
-- Create GIN index on JSONB metadata payload
CREATE INDEX idx_users_metadata_gin 
ON users USING GIN (metadata jsonb_path_ops);

-- Fast $O(\log N)$ JSONB containment lookup using GIN
SELECT * FROM users 
WHERE metadata @> '{"role": "admin", "tier": "enterprise"}';

3. Dissecting EXPLAIN (ANALYZE, BUFFERS) Execution Plans

To verify whether PostgreSQL uses your indexes, wrap slow queries in EXPLAIN (ANALYZE, BUFFERS):

Reading Execution Plans
EXPLAIN (ANALYZE, BUFFERS) 
SELECT * FROM orders WHERE user_id = 'usr_9984' ORDER BY created_at DESC LIMIT 10;

-- Key Plan Indicators to Watch:
-- 1. Index Scan vs. Seq Scan: Confirms index usage
-- 2. Buffers: shared read=12 (Disk I/O) vs shared hit=450 (RAM Cache)
-- 3. Execution Time: Actual execution time in milliseconds

The Shareable Quote: "Do not guess why your query is slow; EXPLAIN ANALYZE tells you exact disk buffer reads and cost evaluations."

🛠️ Day 32 Project: 1 Million Row Query Benchmark

Generate a 1,000,000 row table in PostgreSQL and benchmark search performance before and after B-Tree & GIN indexing using Python's asyncpg.

  • Seed 1M rows with random user IDs, timestamps, and JSONB metadata payloads.
  • Run EXPLAIN (ANALYZE, BUFFERS) on an unindexed query and record execution time and buffer reads.
  • Add composite B-Tree and GIN indexes, re-run EXPLAIN ANALYZE, and calculate the speedup factor.
🔥 PRO UPGRADE / TEASER

Tomorrow, we explore database high availability: Day 33: PostgreSQL Replication & Read Replicas — Streaming Replication, Logical Replication, and Read-Write Splitting in Python.

Architectural Consulting

If you are building high-concurrency Python backends or microservices and need senior architectural guidance, I am available for direct contracting.

Explore Enterprise Engagements →

Comments