In plain words: when the same fact is stored in two places, sooner or later the two places disagree, and then nobody knows which one is true. Normal forms are a 50-year-old checklist for finding those places. I'd built my schema fast, one feature at a time, so I went back to the basics to see what had crept in.
Hearback (it drafts replies to comments on my posts) runs on about 25 Postgres tables. I asked my agent to audit them using the original sources rather than a blog summary: Codd's papers, Fagin's papers, and Kent's 1983 "A Simple Guide to Five Normal Forms". Kent's rule fits on one line: every column is a fact about the key, the whole key, and nothing but the key.
It found more than I expected:
- 2NF, "the whole key". A link table keyed by (idea, item) also stored the person, who depends on the item alone. My "how many people asked for this" counter read that copy.
- 3NF, "nothing but the key". Text chunks repeated their document's kind, label and URL. A boolean
is_publicwas justlabel = 'quotable'written twice. - Derived values written by hand. Token totals stored next to the breakdown they come from; seven rows already disagreed in meaning. "Last touched" dates were updated by read-then-write in app code, and one writer forgot to update them at all.
- 1NF. An array of allowed models was searched with
= anyin three functions: a table in disguise.
Every fix was one of three moves: drop the copy and read through a join, let Postgres compute it (generated columns), or make a trigger the only writer of a cached value. Before adding any constraint, the agent counted would-be violations on live data (zero), then tested on a local Supabase, then deployed and ran the smoke suite.
What I got is confidence: each fact now has one home, and the database itself refuses the rest. Some copies I kept on purpose for security and speed; that's the second story.
Check if this applies to you
Applies if: a Postgres database you designed or grew yourself, with migrations you can read. Steps (read-only):
- Composite keys (2NF):
select conrelid::regclass, pg_get_constraintdef(oid) from pg_constraint where contype = 'p' and array_length(conkey, 1) > 1 and connamespace = 'public'::regnamespace;For each one, ask of every other column: does it depend on only one part of the key? - Copies of a parent (3NF): for each foreign key
child.parent_id, list child columns with the same name as a parent column (kind,label,url,platform,project_id), then count rows where they differ. - Hand-written derived values: search the server code for writes of columns that are a function of other columns (totals, hashes, booleans, "last_*_at").
Has the problem: any column found in steps 1–3 that the database doesn't keep consistent itself (no generated column, trigger or composite foreign key). Doesn't have it: every copy found is either gone or enforced by the database.
Reproduce this
Prerequisites: Postgres 12+ (generated columns), migrations in version control, read-only SQL access to production, and a local or staging copy to test on (for Supabase, supabase start needs Docker). Your agent will need from you: your OK before applying to production, and a decision for each copy it finds (remove, compute, cache, or keep). Steps:
- Read Kent, "A Simple Guide to Five Normal Forms in Relational Database Theory", CACM 26(2), 1983: https://www.bkent.net/Doc/simple5.htm. Sources if needed: Codd 1970 (doi:10.1145/362384.362685) and Fagin 1977 (doi:10.1145/320557.320571).
- Audit table by table: 1NF (arrays or JSON that queries search inside), 2NF (columns depending on part of a composite key), 3NF (columns determined by another non-key column or by a parent row).
- For each finding pick one move: drop the copy and join;
generated always as (...) storedfor a function of the same row; a trigger as the only writer for aggregates over other rows; a child table for an array that is really a relation. - Before writing the migration, count the rows that would change or fail on production with read-only queries. Add a guard in the migration (
do $$ ... raise exception ... $$) that stops if the data disagrees. - Apply on the local copy and check behaviour with real-shaped rows: triggers fire, generated values match, every API query still resolves.
- Update writers (inserts that set a generated column fail) and remove app code that maintained trigger-kept values.
- Deploy the migration and the code together, then run end-to-end tests.
Verify: the read-only checks from step 4 return 0, the end-to-end suite passes, and writing a generated column directly is refused by Postgres.