Learn SQL
-- security.audit_history_triggers

Audit History & Triggers in SQL

Append-only who-changed-what — compliance backbone

--concept

History tables (row versions with valid-from/to + actor) fed by triggers or disciplined app writes answer who changed what, when. Temporal/system-versioned tables automate where supported. Audit writes must be append-only (revoke UPDATE/DELETE on history) to stay trustworthy. Event sourcing replays full state at replay cost.

--how it works
1

Version every mutation

History rows carry who/when/old/new — actor from session, diff as JSONB.

2

Trigger the capture

AFTER UPDATE/DELETE fans history for every writer — consoles included.

3

Append-only trust

REVOKE UPDATE/DELETE on history — tamper-evident by privilege.

4

Partition + temporal

Monthly partitions with retention; SYSTEM_TIME as-of where supported.

--visualization
APP WRITECONSOLE FIXAPPEND-ONLY
Stage 1 / 3
App write → history row (actor + diff as JSONB, same txn).
--example
query.sql
1-- trigger-maintained history:
2-- CREATE TRIGGER audit AFTER UPDATE OR DELETE ON orders FOR EACH ROW EXECUTE FUNCTION log_change();
Result
layerguarantee
historyappend-only versions
triggercannot be skipped by app bugs

Every mutation fans a history row with actor + timestamp + diff. App-path audits miss console fixes; trigger-path catches all writers uniformly.

--variants

Trigger fan-out (writer-proof)

No app path can skip it.

query.sql
1CREATE TRIGGER audit AFTER UPDATE OR DELETE ON orders FOR EACH ROW EXECUTE FUNCTION log_change();
Result
writercapture
app writehistory row
console fixhistory row
1Trigger fires inside the same txn — atomic with the change.
2Actor from session_user / app setting.
3App-only auditing misses the console path.

As-of read (portable)

Half-open validity answers June-1st.

query.sql
1SELECT * FROM prices WHERE product_id = 7 AND valid_from <= '2024-06-01' AND '2024-06-01' < valid_to;
Result
product_idprice
799
1Exactly one validity row contains the instant.
2Closed ends would gap/double-count boundaries.
3Temporal tables automate this where installed.
--key facts
Append-onlyrevoke UPDATE/DELETE on history
Triggerswriter-independent capture
Temporalas-of queries where supported
--use cases
Compliance trails: who-changed-what proofs
Billing disputes: as-of price/order reconstruction
--under the hood
Triggers add per-row write cost + WAL — batch history inserts; async replicas carry the read load.
Temporal SYSTEM_TIME needs history partitioning or it grows unbounded.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.