history, audit and soft delete
deleted_at, history tables, and what 'as of last tuesday' costs you.
- address
- everything.design.history_and_soft_delete
- part
- part 7, schema design
- chapter
- chapter 9 of 13
- reading time
- 8 min
soft delete marks a row as gone (deleted_at) instead of removing it. it keeps the row for undo and audits, and makes every query, constraint and foreign key responsible for ignoring it. the classic trap: a UNIQUE constraint still sees deleted rows. a partial unique index WHERE deleted_at IS NULL fixes it.
for real history, keep versions: a history table where each row has a validity range (tstzrange), written by a trigger, so "what was the price last tuesday" is one @> query. an audit table records who changed what, as the changes happen.
the picture
you know the "are you sure? this can't be undone" dialog, and the support ticket the next morning asking to undo it? so the team adds deleted_at, and every DELETE becomes an UPDATE. a month later someone signs up again with the same email, and gets "email already taken" from an account that, as far as they know, is gone.
think of a library withdrawing a book. it doesn't shred the catalogue card (the row), it stamps it WITHDRAWN (deleted_at). good for history. but now the rule "one card per ISBN" counts the stamped cards too, and the library can't catalogue a new copy of a book it once withdrew.
| idint4 | emailtext | deleted_attimestamptz |
|---|---|---|
| 1 | ada@example.com | null |
| 2 | grace@example.com | null |
two live customers.
soft delete, honestly
the partial index from the figure is the fix for uniqueness:
SET TimeZone = 'UTC';
CREATE TABLE customers (
id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL,
name text NOT NULL,
deleted_at timestamptz
);
CREATE UNIQUE INDEX customers_email_live ON customers (email) WHERE deleted_at IS NULL;
INSERT INTO customers (email, name, deleted_at) VALUES
('ada@example.com', 'Ada Lovelace', '2026-03-10 09:00+00'),
('ada@example.com', 'Ada Lovelace', NULL),
('grace@example.com', 'Grace Okafor', NULL);
SELECT id, email, deleted_at FROM customers ORDER BY id;| idint4 | emailtext | deleted_attimestamptz |
|---|---|---|
| 1 | ada@example.com | 2026-03-10 09:00:00+00 |
| 2 | ada@example.com | null |
| 3 | grace@example.com | null |
the rest of the cost is everywhere else. every query that means "customers" now has to say WHERE deleted_at IS NULL, and the one that forgets shows deleted people. the usual guard is a view that does it once:
CREATE VIEW live_customers AS SELECT id, email, name FROM customers WHERE deleted_at IS NULL;
SELECT count(*) AS all_rows, (SELECT count(*) FROM live_customers) AS live FROM customers;| all_rowsint8 | liveint8 |
|---|---|
| 3 | 2 |
and point the app at the view (views). foreign keys are the part no view fixes: orders still reference a "deleted" customer, happily, because to postgres the row exists. that's often what you want (old orders keep their customer), but it means a soft delete never cascades, and "delete this customer's data" (a GDPR request, say) needs a real DELETE anyway.
-- customers has a deleted_at column and the partial index above.
-- a report someone wrote last year:
SELECT count(*) FROM customers WHERE email LIKE '%@example.com';when soft delete isn't worth it: if you only need undo for a few minutes, or an audit trail, move the row to an archive table in the same transaction (WITH gone AS (DELETE ... RETURNING *) INSERT INTO customers_archive SELECT * FROM gone) and keep the live table clean. deletes stay deletes, and history lives where only the people who want it look.
an audit table
an audit table answers "who changed this, from what, to what". a row trigger can write one entry per change, with the old and new row as jsonb:
CREATE TABLE accounts (id int PRIMARY KEY, owner text NOT NULL, balance numeric(12,2) NOT NULL);
CREATE TABLE audit_log (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
tbl text NOT NULL,
op text NOT NULL,
who text NOT NULL DEFAULT current_user,
old_row jsonb,
new_row jsonb
);
CREATE FUNCTION audit() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO audit_log (tbl, op, old_row, new_row)
VALUES (TG_TABLE_NAME, TG_OP,
CASE WHEN TG_OP <> 'INSERT' THEN to_jsonb(OLD) END,
CASE WHEN TG_OP <> 'DELETE' THEN to_jsonb(NEW) END);
RETURN NULL;
END $$;
CREATE TRIGGER accounts_audit AFTER INSERT OR UPDATE OR DELETE ON accounts
FOR EACH ROW EXECUTE FUNCTION audit();
INSERT INTO accounts VALUES (1, 'alice', 100.00);
UPDATE accounts SET balance = 80.00 WHERE id = 1;
DELETE FROM accounts WHERE id = 1;
SELECT id, op, who, old_row ->> 'balance' AS old_balance, new_row ->> 'balance' AS new_balance
FROM audit_log ORDER BY id;| idint8 | optext | whotext | old_balancetext | new_balancetext |
|---|---|---|---|---|
| 1 | INSERT | postgres | null | 100.00 |
| 2 | UPDATE | postgres | 100.00 | 80.00 |
| 3 | DELETE | postgres | 80.00 | null |
because it's a trigger, it catches every path, and because it runs in the same transaction, a rolled-back change leaves no entry. in production you'd add changed_at timestamptz DEFAULT now() and the application user, which the database only knows if the app tells it (SET app.user = ... per transaction, read with current_setting), since many apps connect as one role. for very high write rates, logical decoding can build the same log from the WAL without a trigger on the write path.
versions with a validity range
an audit log is a list of events. for "what was true at time T" you want versions: one row per state, each valid over a range of time. a range type holds the interval in one column, half-open, so consecutive versions meet without overlapping:
SET TimeZone = 'UTC';
CREATE TABLE prices (sku text PRIMARY KEY, price numeric(8,2) NOT NULL);
CREATE TABLE price_history (sku text NOT NULL, price numeric(8,2) NOT NULL, valid tstzrange NOT NULL);
-- close the current version and open a new one, on every insert or price change.
-- the clock comes from app.clock so this page's output is stable; in production, now().
CREATE FUNCTION keep_price_history() RETURNS trigger LANGUAGE plpgsql AS $$
DECLARE t timestamptz := coalesce(current_setting('app.clock', true)::timestamptz, now());
BEGIN
IF TG_OP = 'UPDATE' THEN
UPDATE price_history SET valid = tstzrange(lower(valid), t)
WHERE sku = OLD.sku AND upper_inf(valid);
END IF;
INSERT INTO price_history VALUES (NEW.sku, NEW.price, tstzrange(t, NULL));
RETURN NEW;
END $$;
CREATE TRIGGER prices_history AFTER INSERT OR UPDATE OF price ON prices
FOR EACH ROW EXECUTE FUNCTION keep_price_history();
SET app.clock = '2026-03-01 00:00+00'; INSERT INTO prices VALUES ('TEA-01', 6.50);
SET app.clock = '2026-03-12 00:00+00'; UPDATE prices SET price = 7.25 WHERE sku = 'TEA-01';
SET app.clock = '2026-03-20 00:00+00'; UPDATE prices SET price = 5.95 WHERE sku = 'TEA-01';
SET app.clock = '2026-04-01 00:00+00'; UPDATE prices SET price = 7.25 WHERE sku = 'TEA-01';
SELECT price, valid FROM price_history ORDER BY lower(valid);| pricenumeric | validtstzrange |
|---|---|
| 6.50 | ["2026-03-01 00:00:00+00","2026-03-12 00:00:00+00") |
| 7.25 | ["2026-03-12 00:00:00+00","2026-03-20 00:00:00+00") |
| 5.95 | ["2026-03-20 00:00:00+00","2026-04-01 00:00:00+00") |
| 7.25 | ["2026-04-01 00:00:00+00",) |
now "as of" is containment, valid @> timestamp: exactly one version contains any instant.
- 1SELECT sku, price FROM price_history WHERE sku = 'TEA-01' AND valid @> '2026-03-05 12:00+00'::timestamptz;
| skutext | pricenumeric |
|---|---|
| TEA-01 | 6.50 |
as of march 5: the range that contains that instant.
two things make this solid. an exclusion constraint, EXCLUDE USING gist (sku WITH =, valid WITH &&), guarantees no two versions of one sku overlap, and postgres 18 can declare the same thing as a key, PRIMARY KEY (sku, valid WITHOUT OVERLAPS) (temporal keys). and a GiST index on (sku, valid) makes the @> lookup fast once the history is long.
the SQL standard has system-versioned tables (PERIOD FOR SYSTEM_TIME, FOR SYSTEM_TIME AS OF), where the database keeps this history itself. postgres doesn't implement them, which is why the trigger above is the usual pattern (the temporal_tables extension packages it).
keylines
- soft delete is an UPDATE: every query, constraint and foreign key still sees the row.
- a partial unique index WHERE deleted_at IS NULL keeps uniqueness among live rows only.
- an audit trigger records every change in the same transaction, so rollbacks leave no trace.
- versions with a tstzrange validity make "as of" a single @> lookup.
- history only grows: decide retention and partition it before it's big.