AlgoViz

PostgreSQL

medium 32 min

PostgreSQL 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.

When it comes up

The moments this idea belongs in a design interview, and the moments it does not.

Signals

When to bring it up

You hearSayBecause
Orders, payments, bookingsPostgreSQL, with constraints and transactionsWrong data costs more than latency
Filters nobody has planned yetA relational store with indexesNew questions need no new data model
Nearby placesPostGISThe index understands distance
Background jobs, modest rateA jobs table with SKIP LOCKEDOne less system to run
Millions of writes a secondNot one primaryPartitioned log or wide-column store
Thousands of functionsA poolerEvery connection is a process

In the interview

  • Give one reason from the data, not from fame
  • Name the standby and the pool without being asked
  • State the trigger for replicas and for sharding
  • Mention vacuum when updates are heavy

Avoid

  • Sharding 200 GB
  • Calling a replica a backup
  • Promising zero loss with asynchronous standbys
  • One connection per request

Options

Explain your choices

A busy orders table has become slow and three times its normal size. What do you do?

BreaksTurn autovacuum off and shard
How
Stop the background load and split the table over servers.
Limit
Bloat grows on every shard and ids move towards wraparound.
Choose when
Never as a first step.
WorksVACUUM FULL tonight
How
Rewrite the table in a window.
Limit
It locks out everyone, and the bloat returns because the cause is still there.
Choose when
Once, after the cause is fixed, if a window exists.
StrongFind what holds vacuum back, then repack online
How
Find the oldest transaction or slot, end it, add timeouts, lower the table's scale factor, then use REPACK CONCURRENTLY.
Limit
Needs space for a second copy and version 19, or an extension on older ones.
Choose when
The normal answer.