PostgreSQL data modeling

Normalization vs pragmatic denormalization, foreign keys, unique and partial indexes, migrations. Schema questions expose real seniority fast.

About 28 min read3-question knowledge check

The mental model

The schema is the load-bearing structure of your application. UI gets repainted every quarter, API routes come and go, but the tables you design in week one are still holding the building up in year five — and changing them later is renovation with tenants living inside. That is why schema questions expose seniority so fast in interviews: they reveal whether you think in terms of invariants the system must never violate or just "shapes I need for this screen".

The mental model that organizes everything: **the database is the only component that sees all writers.** Your application code, your admin scripts, your queue workers, next year's rewritten service — all of them write to the same tables. An invariant enforced in application code ("every order has a customer") is a convention; an invariant enforced by the schema (a foreign key with NOT NULL) is a law. Constraints are not pedantry — they are the difference between "bugs corrupt data" and "bugs get rejected with an error you can alert on."

What breaks without deliberate modeling: orphaned rows referencing deleted users, duplicate signups from race conditions, NULLs in columns that "can never be null", money stored as floats drifting by paise, and — the expensive one — a data shape that makes tomorrow's feature require a table rewrite under live traffic. Normalization exists to make each fact live in exactly one place, so an update cannot leave two copies disagreeing; pragmatic denormalization is choosing, consciously and rarely, to duplicate a fact because reading it together is worth the burden of keeping copies in sync.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision