Why this matters
The database outlives every service written against it. Bad data models cause bugs no amount of application code can fix, and missing indexes are the most common cause of slow endpoints. For AI work, Postgres also becomes your vector store (8.4).
Core ideas
- Tables, rows, columns; a primary key identifies a row; a foreign key references another table's key.
- Constraints (
NOT NULL,UNIQUE,CHECK, foreign keys) make invalid data impossible. Put rules in the database when they must always hold. - Normalisation: store each fact once. Denormalise deliberately, for read speed, with a plan to keep copies in sync.
- Index: a sorted structure (B-tree by default) that makes lookups fast and writes slightly slower.
- Transaction: a group of statements that commit together or not at all (ACID).
- Isolation levels: Postgres defaults to read committed; repeatable read and serializable prevent more anomalies at more cost.
How to do it
CREATE TABLE customers (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL UNIQUE,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id bigint NOT NULL REFERENCES customers (id),
status text NOT NULL CHECK (status IN ('open','paid','cancelled')),
total_cents integer NOT NULL CHECK (total_cents >= 0),
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX orders_customer_created ON orders (customer_id, created_at DESC);
The index matches the query it serves: "this customer's latest orders".
EXPLAIN ANALYZE
SELECT id, total_cents FROM orders
WHERE customer_id = 42 ORDER BY created_at DESC LIMIT 20;
Read the plan: a Seq Scan on a large table for a selective filter means a missing index; compare estimated and actual rows to spot stale statistics.
Avoiding race conditions
Two requests buy the last item at once. Options:
- An atomic update with a condition:
UPDATE stock SET qty = qty - 1 WHERE sku = $1 AND qty > 0and check the row count. SELECT ... FOR UPDATEinside a transaction to lock the row.- A unique constraint so the second insert fails, and handle the error.
Why this way
Constraints catch bugs from every code path, including scripts and future services. Composite indexes ordered like your WHERE and ORDER BY let Postgres read just the rows it needs. Atomic conditional updates avoid locks entirely when they fit.
Common mistakes
- Indexing every column "just in case" (slower writes, wasted memory).
- Storing money as floats.
timestampwithout time zone; usetimestamptz.- Read-then-write logic in application code without a transaction or lock.
- Running migrations that rewrite large tables during peak traffic (2.5).
Interview follow-ups
- "Your index isn't being used. Why?" Possible causes: a function on the column (
lower(email)needs an expression index), a type mismatch, low selectivity (the planner prefers a scan), stale statistics (ANALYZE), or a column order that doesn't match the filter. - "Explain isolation levels briefly." Read committed (Postgres default) sees committed data per statement; repeatable read sees one snapshot per transaction; serializable makes concurrent transactions behave as if run one by one, at the cost of retries on conflicts.
- "How do you add a NOT NULL column to a 100M-row table?" Add it nullable (or with a constant default, which is cheap in modern Postgres), backfill in batches, add a
CHECK … NOT VALIDthenVALIDATE, then set NOT NULL, avoiding a long lock. - "SQL or NoSQL?" Default to Postgres for relational data and transactions; choose a document or key-value store for a specific access pattern or scale need, not by fashion.
Check yourself
An endpoint filters by `tenant_id` and sorts by `created_at`. Which index?
A composite index on (tenant_id, created_at DESC), matching the filter then the sort, so Postgres reads rows in order and stops at the limit.
How do you stop two concurrent requests from overselling stock?
Use an atomic conditional update (... WHERE qty > 0) and check the affected row count, or lock the row with SELECT ... FOR UPDATE in a transaction.
Build it
Create the schema above in a local Postgres (Docker), insert 1M orders with generate_series, time the customer query with and without the index using EXPLAIN ANALYZE.