Video in progress
What do you look at when you code-review the database schema of a new service?
Types (money, time, id), constraints (NOT NULL, FK, unique, CHECK), indexes on FK columns, indexes that match the main queries, the growth and cleanup of big tables, and the lock risk of migrations. The goal is to catch decisions that will be expensive to change later, now.
📖 Answer
In review I go through this list, roughly in order of importance:
Types. Money is numeric, not double. Time is timestamptz. ID is bigint or UUID, not int (converting a table that reaches 2 billion from int to bigint means rewriting the whole table). Text is text or a justified varchar(n).
Constraints. A required field is NOT NULL. Business uniqueness (email, order number) is a unique constraint, not only an application check. A status is a CHECK or an FK. Are the FKs there, and was the ON DELETE behavior chosen deliberately.
Indexes. Does every FK column have an index (needed when a parent row is deleted, and in JOINs). I look at the main queries in the PR and check whether there is a matching index. Also redundant indexes: each one slows down writes.
Growth. How much will the table grow per year? How will old data be cleaned up? If there are millions of rows a day, partitioning must be planned from the start, because adding it later is hard.
Migration safety. ALTER TABLE locks, CREATE INDEX without CONCURRENTLY, a volatile default on a big table, DDL and a big data change in one migration.
Concurrency. "Check, then write" patterns, exists checks instead of a unique constraint, hot rows.
-- what I often send back in review
CREATE TABLE payment (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
order_id bigint NOT NULL REFERENCES orders(id),
amount numeric(19,2) NOT NULL CHECK (amount > 0),
status text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX ON payment (order_id); -- for the FK
Much of this list can be automated (migration linters like squawk, checks in CI), and the rest needs a human eye.
Sign up to mark studied questions and save your progress.