PostgreSQL
medium 32 minPostgreSQL is an open-source relational database that never overwrites a row: every change adds a new version, readers pick the version their snapshot may see, and vacuum clears away versions nobody needs.
See it run
Row versions, the log, pooled connections, failover and index choice, one step at a time.
Operations
Update and read versions
One row, two readers and a vacuum. The old version stays until no snapshot can need it.
Step 1 of 7
One version
| value | xmin | xmax | |
|---|---|---|---|
| slot 1 | 2 boats | 10 | - |
| slot 2 | - | - | - |
| slot 3 | - | - | - |
One live tuple
Transaction 10 inserted the row and committed.
Step 1: One version
def update(table, xid, key, value): old = visible_version(table, xid, key) old.xmax = xid table.append(Version(key, value, xmin=xid, xmax=None))def visible(v, snapshot): return v.xmin in snapshot and v.xmax not in snapshotdef vacuum(table, snapshots): for v in table: if all(v.xmax in s for s in snapshots): table.free(v)- Cost
- Dead tuples
- You get
- No waiting
Say the price together with the benefit: no waiting for readers, work for vacuum.
In the real world
The same postgresql doing real jobs. Pick one and step through it.
Friday evening: a developer opens a transaction and goes home.
Step 1 of 4
18:00
| live | dead | held by | |
|---|---|---|---|
| orders | 1 million | 20,000 | session 7 |
A session runs BEGIN and one SELECT at repeatable read, then nothing more.
Step 1: 18:00
Common mistakes
The bugs interviewers watch for, with the fix beside each.
A connection per request
criticalEach connection is a process. A spike exhausts memory and the connection limit.
Wrongdef handle(req): conn = connect()Rightpool = Pool(max_size=20)def handle(req): with pool.connection() as conn: ...Leaving transactions open
criticalThe snapshot holds back vacuum in the whole database, and locks stay.
Wrongwith transaction(): row = select() call_payment_api() # 30 sRightrow = select()result = call_payment_api()with transaction(): save(result)Using a replica for your own write
majorThe replica may not have replayed it. The user's change seems lost.
Wrongprimary.save(profile)return replica.load(profile.id)Rightprimary.save(profile)return primary.load(profile.id)No retry after 40001
majorAt repeatable read and serializable the database may refuse a correct transaction. It must be run again.
Wrongrun(transfer)Rightfor attempt in range(5): try: return run(transfer) # whole transaction except SerializationFailure: backoff(attempt)
Interview tips
How to explain it
- Versions instead of overwriting.
- Log first, pages later.
- One process per connection, so pool.
- Vacuum pays the bill.
Say the trigger
- Replicas: reads exceed one server.
- Partitions: old data leaves in blocks.
- Shards: writes exceed the largest server.
Watch
- Oldest transaction and slot.
- Dead tuples and id age.
- Replication lag.
- Slowest queries by total time.
Do not promise
- Zero loss with asynchronous standbys.
- Exactly-once effects from a jobs table.
- Automatic failover without a tool.