PostgreSQL
Overview
MVCC (Multi-Version Concurrency Control)
Core Concept
How It Works
Transaction IDs (XID)
Tuple Visibility
Transaction 100: INSERT INTO users VALUES (1, 'Alice');
→ Row: (id=1, name='Alice', xmin=100, xmax=NULL)
Transaction 101: UPDATE users SET name='Bob' WHERE id=1;
→ Old Row: (id=1, name='Alice', xmin=100, xmax=101)
→ New Row: (id=1, name='Bob', xmin=101, xmax=NULL)
Transaction 102 (started before 101): Sees 'Alice'
Transaction 103 (started after 101): Sees 'Bob'Visibility Rules
Write-Ahead Logging (WAL)
Purpose
How It Works
1. WAL Buffers
2. WAL Segments
3. Checkpoints
Crash Recovery
Storage: Pages & TOAST
Page Structure
TOAST (The Oversized Attribute Storage Technique)
Indexes
B-Tree (Default)
GiST (Generalized Search Tree)
GIN (Generalized Inverted Index)
BRIN (Block Range Index)
Query Execution
Planner/Optimizer
1. Parse SQL
2. Rewrite (Apply Rules)
3. Plan (Choose Execution Strategy)
4. Execute
Cost Estimation
Statistics
Vacuum & Autovacuum
Problem: Dead Tuples
Vacuum
1. VACUUM (Standard)
2. VACUUM FULL
Autovacuum
Tuning Autovacuum
Replication
Physical Replication (Streaming)
Logical Replication
Transaction Isolation Levels
Level
Dirty Read
Non-Repeatable Read
Phantom Read
Read Committed (Default)
Repeatable Read
Serializable
Locks
Row-Level Locks
Table-Level Locks
Deadlock Detection
Performance Tuning
Key Parameters
Memory
Checkpoints
Connections
Query Optimization
Use EXPLAIN ANALYZE
Index Best Practices
Common Pitfalls
❌ Not Running VACUUM
❌ Missing Indexes
❌ N+1 Query Problem
❌ Connection Pooling
Interview Questions
Last updated