Part 13 / Topic 8 of 8
When not to put logic in the database
Everything this part built is powerful — which is exactly why the boundary matters. The honest trade-offs:
Database logic is harder to version, test and review. Application code lives in git with unit tests and code review built into the culture; functions and triggers can be managed that way (migration files), but teams drift into psql-edited production functions nobody can diff.
Triggers hide behaviour. An UPDATE that also writes three other tables is magic to the developer reading the application code. Magic is a cost — spend it only where the alternative (every app reimplementing the rule, one forgetting) is worse.
Debugging crosses a boundary. RAISE NOTICE is no stack trace, and a slow trigger shows up as "writes are slow" two layers from its cause.
The rule of thumb that holds up: the database owns data integrity — constraints first, triggers where constraints can't reach (audit trails, derived data). Applications own business workflow — "send the welcome email", "call the payment provider" — things with retries, external calls and product churn. And whatever lives in the database goes in migration files, reviewed like any other code.
🧪 Quick quiz — answer from memory, then check.
- Which belongs in the database: "order totals must be non-negative" or "email the customer on delivery"?
- What keeps database logic reviewable?
Show the answers
- The first (integrity — a constraint). The second is workflow: retries, external systems, product change — application territory.
- Living in version-controlled migration files, never edited live in psql.
Done here? The practice set for Part 13 — 6 exercises with solutions — is on the part page.