The Decisions You Cannot Cheaply Reverse

The Schema Is The Decision You Cannot Reverse

Almost every technical choice is reversible at constant cost. The data model is the exception: its cost of change grows with rows, call sites and whatever outside your codebase depends on its shape.

Almost every technical decision you have made is reversible at roughly constant cost. You can replace a framework, move hosts, rewrite a page, swap a payment provider, and the work is about the same this year or next. The data model is the exception. Its cost of change grows with the number of rows, the number of call sites reading it, and the number of things outside your codebase quietly depending on its shape: a report, a webhook payload, a partner integration, a spreadsheet somebody in support built. Code refactors are invisible to your users. A migration is the only refactor they can see, and fixing a bad one means a schema change, a backfill of every row and a change to every call site at once, while the product stays online. The original decision took four seconds in a migration file.

Money, time and identity

Store money as integer minor units, or as numeric. Never as a float. Floating point cannot represent most decimal fractions exactly, so sums drift and the same total computed two ways disagrees, invisibly, until an accountant or a customer finds it. It is irreversible not because of the arithmetic but because by the time anyone notices you hold years of transactions at the wrong precision, and every historical figure in your accounts came from them.

Use timestamptz, never timestamp. A timestamp without a time zone stores a wall-clock reading with no record of which clock produced it. Harmless while one server in one region writes every row, and not harmless the moment a second region, a daylight saving boundary or a customer abroad enters data, because no later query can recover which reading meant what. Store the instant and convert at the edge.

Then identity. Sequential integer keys leak your volume and make your other records guessable: order number 1041 tells a competitor roughly how many orders you have taken and tells an attacker to try 1042. Random UUIDs fix both and introduce a different problem, because a version 4 UUID scatters inserts across the whole index instead of landing at the end of it, which means more pages touched per write, more page splits, more write amplification, and a working set that stops fitting in memory. UUIDv7 is the resolution: time-ordered, a 48-bit millisecond timestamp followed by random bits, so it keeps the unguessability and restores insert locality. Postgres 18 ships uuidv7() alongside uuidv4() and gen_random_uuid(), and the values look like 019535d9-3df7-79fb-b466-fa907fa17f9e.

uuidv7()

Postgres 18 ships a time-ordered UUID: unguessable like v4, but inserts land at the end of the index

The mainstream advice is to use UUIDs for primary keys. The instinct is right and the version is usually wrong. Random v4 keys are a common reason a table that was fast at one million rows is slow at fifty million, and nobody connects the two, because the query did not change.

Enums, flags and the tenant column

  • A Postgres enum type can gain values with ALTER TYPE ADD VALUE, but you cannot remove a value, ever. A lookup table is a row: rename it, retire it, reorder it, translate it, describe it. Use the type only for a set you are certain will never need editing, which is a shorter list than it feels like.
  • Soft deletion is sold as safety and bought as a permanent tax. From the moment you add the flag every query has to remember it, and one day one will not, which is how deleted records reappear in a report or a search result. It also collides with a right-to-erasure request, because a row marked deleted is still a row you hold. An append-only audit log plus a real delete is usually cleaner: the history without the thing itself.
  • Every nullable column is a three-state question: present, absent, not applicable. If absent and not applicable mean different things in your domain, say so with a status or a second column, because NULL cannot carry both meanings and every future reader will guess differently.
  • If your product will ever have organisations, teams or shared accounts, decide now whether every row knows which tenant it belongs to. Retrofitting a tenant identifier onto a schema built for single users touches every table, every query and every policy at once, which makes it the most expensive migration a successful product takes on.

JSONB is a decision, not a deferral

A JSONB column is the right tool for genuinely variable data: a third party's webhook payload you want verbatim, a user-defined form, settings whose shape you do not control. It becomes a problem when it is used to avoid deciding, because nothing validates the inside of a JSONB column. No constraint, no foreign key, no type. The schema did not disappear, it moved into application code where nothing enforces it. That is also the honest answer to the old argument that SQL does not scale so you should use a document store: the flexibility was real, the enforcement went away, and you traded a migration you can run in a transaction for a backfill script and a hope.

Before you remove the strange thing

A column you do not understand was added by somebody who did, so apply Chesterton's Fence: find out why the fence is there before you take it down. In a schema the reason is usually a specific customer, a tax rule, a regulator or a vendor's quirk, with the person who knew long gone. Search for the readers, and check whether anything outside the codebase selects it, before you touch it.

What to do differently

Spend an afternoon on the schema before you spend a month on the product. For each table write down what one row means in a sentence, what makes it unique in the real world, which tenant owns it, and what you would do if somebody asked you to delete it. Then pay one person who has operated a database at real scale to read it for an hour. Nearly every other mistake in this track can be fixed on a Tuesday afternoon. This one cannot.

Key takeaways

1The data model is the only decision whose cost of change grows with your success: fixing it means schema, backfill and every call site at once, live.
2Money as integer minor units or numeric, never float; timestamps as timestamptz, never timestamp. Neither is recoverable later.
3Prefer a lookup table to an enum, whose values can never be removed, and a real delete plus an audit log to a soft-delete flag every query must remember.
4Random v4 UUID keys scatter inserts across the index; UUIDv7, built into Postgres 18, keeps the unguessability and restores insert locality.
5Decide the tenant column before you need it. Retrofitting tenancy is the most expensive migration a successful product ever takes on.

This is one lesson of 541.

The whole Foundation is free — 16 subjects, beginner to advanced. Sign in to track what you have read, take the end-of-module quizzes, and use the terminal.