The diff is green. The tests pass. Somewhere in the middle, a line now updates a row that used to be write-once. You scroll past it. You approve.
Review is a person reading, and people get tired. An agent can produce the next diff as quickly as the first. Which rules can you make the database check on every write?
Give retries a durable key
Try this in a disposable Postgres database:
CREATE TABLE sends (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
idempotency_key text NOT NULL UNIQUE,
recipient text NOT NULL,
status text NOT NULL CHECK (status IN ('queued', 'sent', 'failed'))
);
INSERT INTO sends (idempotency_key, recipient, status)
VALUES ('order-42-receipt', 'a@example.com', 'queued');
INSERT INTO sends (idempotency_key, recipient, status)
VALUES ('order-42-receipt', 'a@example.com', 'queued')
ON CONFLICT (idempotency_key) DO NOTHING;
SELECT count(*) FROM sends;
The count is one. Without the conflict clause, the second insert raises a unique-constraint violation. An insert with status 'delivered' raises a check-constraint violation. Both rules apply even if another caller skips the application validator.
For a real API, scope the key to the caller and operation. When a key already exists, check that the request matches the original before returning its result. A database constraint covers the local record. If a provider call times out, use provider idempotency or reconcile the ambiguous outcome before retrying.
Give history a separate write path
A send changes status. Its history can record each transition as a new row:
CREATE TABLE send_events (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
send_id bigint NOT NULL REFERENCES sends(id),
event text NOT NULL,
recorded_at timestamptz NOT NULL DEFAULT now()
);
CREATE FUNCTION forbid_event_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'send_events is append-only';
END;
$$;
CREATE TRIGGER send_events_append_only
BEFORE UPDATE OR DELETE ON send_events
FOR EACH ROW EXECUTE FUNCTION forbid_event_change();
INSERT INTO send_events (send_id, event)
SELECT id, 'queued' FROM sends WHERE idempotency_key = 'order-42-receipt';
Try updating or deleting that event. The trigger rejects the change. In production, keep the application role separate from the owner, grant only the needed permissions, and withhold TRUNCATE on the history table. Row-level update and delete triggers do not fire on TRUNCATE. Review migrations that can change constraints, triggers, or grants.
A test worth adding
Run two attempts with the same operation key and verify how many records survive. Then try mutating the history using the same role your app uses. You’ll learn which parts of the design are enforced today and which still depend on the next reviewer noticing.
Top comments (6)
My two cents will be an append-only history protects the entries that exist while an omitted transition leaves the audit trail looking just as clean. Also btw would love to see some content on building journey of rootmail :) humble request
an append-only history protects the entries that exist. the omitted transition is the hole, because the audit trail looks just as clean.
The TRUNCATE caveat is the part most append only write ups skip, so it's good to see it spelled out. One thing I'd add to the test is the same mutation from the table owner role, since an owner can just disable or drop the trigger. That's presumably why you split the application role from the owner. Do you put an event trigger on DDL as well, or is migration review the only guard there?
an event trigger on DDL would catch the drop the review waves through. the review stays the only guard that knows whether the change was meant to happen.
Ha, until the tired reviewer waves through the migration that drops the trigger. Do you flag constraint and grant changes somehow so they can't hide in a normal looking diff?
that is the exact gap. a grant change reads like a normal migration in the diff, so it needs its own flag, not just a tired reviewer.