LOAD-BEARINGfree
FoundationsTier 3 · unit 7 of 12 · 13 min read

Storing history: soft deletes, audit tables, temporal data

UPDATE and DELETE destroy information. That is what they are for, and for most of a system's life it is exactly right. Then someone asks what a customer's address was when the order shipped, or an auditor asks who changed a price, or a user asks you to undo something — and you discover the answer was overwritten in place two years ago.

Deciding what history to keep is a data-model decision, made early, expensive to reverse. Four different questions hide behind "we should keep history", and they have four different answers:

  • Can this be undone? → soft delete
  • Who changed this, when, and why? → audit table
  • What did this look like on 3 March? → temporal data
  • What are we legally required to retain, and for how long? → retention policy, which is a different question again

Soft delete, and its four sharp edges

Add deleted_at, filter it out of every query, and nothing is ever really gone. It is the cheapest possible answer and it works, provided you know what you have taken on:

  • Every query must remember the filter. One that forgets shows deleted rows to a user, and it will pass code review because the missing clause looks like no clause at all. Push the rule below hand-written SQL — a view, a default scope, a row-level policy — so that forgetting is not an available option. Lesson 12.04 made the same argument about tenant filters, for the same reason.
  • Unique constraints stop working. UNIQUE(email) now blocks a user from re-registering with an address that belongs to a deleted account. The fix is a partial index — unique where deleted_at IS NULL — and it is not the sort of thing you discover until a real user complains.
  • Foreign keys still point at deleted rows. Which is usually what you want, and means "deleted" is not a state the database understands. It is a convention your application maintains.
  • A soft-deleted record is still stored. When a user exercises a right to erasure, deleted_at = now() does not satisfy it. Lesson 12.07 takes this up properly; for now, note that soft delete and deletion are different operations that look identical in the UI.

Audit tables: who, when, and the field everyone omits

A separate table recording each change — the row before, the row after, the actor, the timestamp. Two ways to populate it:

  • Database triggers, or change data capture. Complete by construction: nothing can write to the table without being recorded, including the migration script and the engineer with a psql session. That completeness is the whole value, and the cost is that the logic is invisible from the application code and awkward to test.
  • The application layer. Visible, testable, and one forgotten code path away from a hole. Left alone, this is the weaker option.

In practice: use triggers or CDC for the record of what changed, because completeness is not optional in an audit trail — and use the application for why. The reason for a change is the field auditors actually ask about and the one nobody stores, because a trigger cannot know that this price change was a promotion approved in ticket OPS-4471. Pass the intent down with the write.

Temporal data: two different clocks

The idea worth carrying out of this lesson, because it explains a whole category of "the numbers changed retroactively" confusion. There are two time axes, and mature systems track both:

  • Valid time — when the fact was true in the world. The price was ₹500 from 1 March.
  • Transaction time — when your system recorded it. You were told on 12 April.

Keep only one and you cannot answer the question that matters after any correction: what did we believe on the day we made this decision? Insurance, payroll, billing and anything with backdated corrections needs both — a report run in March and re-run in May will disagree, and being able to explain why is the difference between a bug and a correction. SQL:2011 system-versioned tables give you transaction time for free; valid time is almost always modelled by hand as a validity range on the row.

The analytics plane meets the same problem under a different name. A slowly changing dimension handled as Type 1 overwrites the old value and loses history; Type 2 writes a new row with a validity range and keeps it. If you want to know which customer segment someone was in at the time they ordered — and that is what almost every cohort analysis means — you need Type 2. The operational database rarely provides it, which is precisely why the pipeline in Tier 7 has to build it.

At the far end of this spectrum sits event sourcing (7.04), where history is not a feature bolted onto state but the only thing stored. Everything here is a partial version of that trade, bought in smaller increments.

History is unbounded growth

Every option above makes tables grow forever. Audit tables routinely become the largest in the database, and dead soft-deleted rows sit inside every index on the table, making every query slightly worse in perpetuity. So retention is part of the design, not an afterthought: partition by time so that expiry is dropping a partition rather than a DELETE of forty million rows, decide how long each class of history lives, and price "keep everything forever" honestly. It is a legitimate choice. It is not a free one.

The trap

Making soft delete the default for every table, on the reasoning that keeping data is safer than losing it. What you get is an ever-growing table where every index carries dead rows, a filter that every future query must remember, and records that are legally still stored while everyone believes they were deleted. Soft delete is the right tool when undo is a product feature users can see. An audit table is the right tool when you need the record of who changed what. They solve different problems, and using one where the other belongs is where nearly all of this pain comes from.