Difficulty: Advanced
What is an append-only (immutable) data pattern, and when should you use it instead of updating rows in place?
In an append-only design you never UPDATE or DELETE existing rows; every change is recorded by inserting a new row (a new version or an event) with a timestamp. The current state is derived from the latest version or by replaying the events. This gives a complete audit trail and the ability to see the data as it was at any point in time, at the cost of more storage and more complex reads.
product_prices(product_id, price_paise, valid_from). The current price is the latest row. Data warehouses call this Slowly Changing Dimension Type 2, often with valid_from and valid_to columns.AccountOpened, MoneyDeposited, MoneyWithdrawn. The balance is computed by folding events. Taken to an architectural level, this is event sourcing.A double-entry accounting ledger is the classic append-only system. You never edit a posted entry; a mistake is fixed by a new reversing entry. Payment systems such as wallets and UPI settlement ledgers follow the same rule.
| Concern | Impact | Mitigation |
|---|---|---|
| Storage growth | Every change adds a row | Partitioning, archiving old versions |
| Read complexity | Need "latest version" queries | Views, DISTINCT ON, a current-state projection table |
| Replay cost (event sourcing) | Rebuilding state from many events is slow | Periodic snapshots |
| Corrections | Cannot just fix a wrong row | Compensating or reversing entries |
| Right to erasure | Personal data cannot simply be deleted | Store PII separately, or crypto-shredding (delete the encryption key) |
The last row matters under privacy laws like the EU GDPR and India's DPDP Act 2023: an immutable log must still support deleting personal data on request.
UPDATE and DELETE privileges from the application role on the log table.CREATE TABLE wallet_ledger (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
wallet_id BIGINT NOT NULL,
entry_type TEXT NOT NULL CHECK (entry_type IN ('CREDIT','DEBIT','REVERSAL')),
amount_paise BIGINT NOT NULL, -- signed: credits +, debits -
reference_id TEXT NOT NULL UNIQUE, -- idempotency key
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- Block edits at the database level
CREATE FUNCTION forbid_change() RETURNS trigger AS $
BEGIN RAISE EXCEPTION 'wallet_ledger is append-only'; END $ LANGUAGE plpgsql;
CREATE TRIGGER ledger_immutable BEFORE UPDATE OR DELETE ON wallet_ledger
FOR EACH ROW EXECUTE FUNCTION forbid_change();
-- Current balance and balance as of a past date
SELECT sum(amount_paise) FROM wallet_ledger WHERE wallet_id = 7;
SELECT sum(amount_paise) FROM wallet_ledger
WHERE wallet_id = 7 AND created_at < '2026-03-01';
The trigger rejects updates and deletes, mistakes are fixed with REVERSAL entries, and point-in-time balances come from filtering on created_at.
SELECT DISTINCT ON (product_id)
product_id, price_paise, valid_from
FROM product_prices
ORDER BY product_id, valid_from DESC;
DISTINCT ON keeps the newest version per product, a common way to read current state from versioned rows in PostgreSQL.
Append-Only, Immutable Data, Event Sourcing, Audit Trail, Ledger