Senior 7 min · March 06, 2026

Denormalisation in Databases — Trigger Drift Pitfalls

A missing DELETE case caused $10K in silent order drift.

N
Naren · Founder
Plain-English first. Then code. Then the interview question.
About
 ● Production Incident 🔎 Debug Guide
Quick Answer
  • Denormalisation duplicates data across tables to eliminate expensive JOINs at read time
  • Five main techniques: flattening, stored aggregates, column duplication, vertical partitioning, materialised views
  • Read query speed can improve 1000x+ for complex JOINs — but write latency and consistency risk increase
  • Production teams must build reconciliation jobs: drift is inevitable, detection is mandatory
  • Biggest mistake: denormalising without profiling — a missing index is often the real bottleneck
Plain-English First

Imagine you run a library. Normally you keep one master card per book listing its location, author, and genre — that way you only ever update one card when something changes. But if a thousand people ask 'show me every sci-fi book by its author' every minute, you'd be exhausted flipping between cards. So you print a pre-made poster on the wall that lists everything together — redundant, yes, but blazing fast to read. Denormalisation is that poster: you deliberately duplicate data so reads are instant, accepting that you'll do extra work whenever data changes.

Every high-traffic system you've ever admired — Twitter's timeline, Amazon's product pages, Netflix's recommendation feed — is quietly violating database textbook rules at scale. Not by accident, but by design. Denormalisation is the deliberate, calculated decision to trade write complexity for read speed, and understanding when and how to do it separates engineers who can reason about production systems from those who are still copy-pasting stack overflow answers.

The problem denormalisation solves is deceptively simple: normalised schemas are optimised for data integrity and storage efficiency, but they force the database to perform expensive JOINs across multiple tables on every read. At low traffic this is invisible. At 50,000 reads per second it becomes the reason your on-call phone rings at 3am. When your query plan is joining six tables, sorting, and aggregating to serve a single page render, you have a structural mismatch between your data model and your access pattern.

By the end of this article you'll be able to identify which parts of a normalised schema are causing read bottlenecks, choose the right denormalisation technique for the situation (there are at least five distinct patterns), implement them safely with the SQL and application-layer strategies that production teams actually use, and know exactly which mistakes will silently corrupt your data if you get it wrong.

Here's the blunt truth: denormalisation doesn't fix lazy queries. It fixes structural read pressure. Profile first, then denormalise. If you skip profiling, you're guessing — and production doesn't forgive guesses.

Why Normalisation Breaks Down Under Real Read Loads

Third Normal Form (3NF) is beautiful in theory. Every fact lives in exactly one place, foreign keys enforce relationships, and your UPDATE anomalies vanish. The database as a single source of truth. But a normalised schema is an instruction manual — it tells you where all the pieces are, but you have to assemble the answer on every single read.

Consider an e-commerce order summary page. To render 'Order #4821 — 3 items — shipped to John Smith — via FedEx — total $127.50' from a 3NF schema, you'd typically JOIN orders, order_items, products, customers, addresses, and shipping_carriers. PostgreSQL or MySQL must load pages from each of those tables, build hash joins or nested loop joins in memory, and garbage-collect the intermediate result set — all before sending a single byte to your application.

The query planner is smart, but physics isn't. Each additional table multiplies I/O surface area. With millions of rows, even indexed JOINs produce enormous intermediate row sets that spill to disk. This is the fundamental tension: normalisation optimises for correctness and write performance; denormalisation optimises for read performance at the cost of write complexity and storage. Neither is universally correct — picking the wrong one for your workload is a production incident waiting to happen.

The inflection point is usually around a 10:1 read-to-write ratio. Below that, normalise aggressively. Above it, denormalisation starts paying for itself. Most consumer-facing applications live at 100:1 or higher.

normalised_order_query.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- ─────────────────────────────────────────────────────────────────
-- NORMALISED SCHEMA: 3NF compliant, six-table JOIN to render one
-- order summary row. Clean data model, painful at high read volume.
-- ─────────────────────────────────────────────────────────────────

CREATE TABLE customers (
    customer_id   SERIAL PRIMARY KEY,
    full_name     VARCHAR(120) NOT NULL,
    email         VARCHAR(255) NOT NULL UNIQUE
);

CREATE TABLE addresses (
    address_id    SERIAL PRIMARY KEY,
    customer_id   INT REFERENCES customers(customer_id),
    street        VARCHAR(200),
    city          VARCHAR(100),
    postcode      VARCHAR(20)
);

CREATE TABLE shipping_carriers (
    carrier_id    SERIAL PRIMARY KEY,
    carrier_name  VARCHAR(80) NOT NULL   -- e.g. 'FedEx', 'UPS'
);

CREATE TABLE orders (
    order_id      SERIAL PRIMARY KEY,
    customer_id   INT REFERENCES customers(customer_id),
    address_id    INT REFERENCES addresses(address_id),
    carrier_id    INT REFERENCES shipping_carriers(carrier_id),
    ordered_at    TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE products (
    product_id    SERIAL PRIMARY KEY,
    product_name  VARCHAR(200) NOT NULL,
    unit_price    NUMERIC(10,2) NOT NULL
);

CREATE TABLE order_items (
    item_id       SERIAL PRIMARY KEY,
    order_id      INT REFERENCES orders(order_id),
    product_id    INT REFERENCES products(product_id),
    quantity      INT NOT NULL,
    line_total    NUMERIC(10,2) NOT NULL   -- quantity * unit_price at time of order
);

-- ─────────────────────────────────────────────────────────────────
-- The six-table JOIN required to render a single order summary.
-- EXPLAIN ANALYSE this on 1M orders and watch the planner sweat.
-- ─────────────────────────────────────────────────────────────────
SELECT
    o.order_id,
    c.full_name          AS customer_name,
    a.city               AS shipping_city,
    sc.carrier_name      AS carrier,
    COUNT(oi.item_id)    AS item_count,
    SUM(oi.line_total)   AS order_total
FROM orders            o
JOIN customers         c  ON c.customer_id  = o.customer_id
JOIN addresses         a  ON a.address_id   = o.address_id
JOIN shipping_carriers sc ON sc.carrier_id  = o.carrier_id
JOIN order_items       oi ON oi.order_id    = o.order_id
JOIN products          p  ON p.product_id   = oi.product_id
WHERE o.order_id = 4821
GROUP BY o.order_id, c.full_name, a.city, sc.carrier_name;
Output
order_id | customer_name | shipping_city | carrier | item_count | order_total
----------+---------------+---------------+---------+------------+-------------
4821 | John Smith | Austin | FedEx | 3 | 127.50
(1 row)
Time: 4.823 ms ← acceptable at low volume, devastating at 50k req/s
The 10:1 Rule of Thumb:
Start evaluating denormalisation seriously when your read-to-write ratio exceeds 10:1 on a given table cluster. Profile with EXPLAIN (ANALYSE, BUFFERS) in PostgreSQL or EXPLAIN FORMAT=JSON in MySQL to see actual buffer hits versus sequential scans before making any schema changes.
Production Insight
At 50k req/s, a six-table JOIN can cause connection pool exhaustion and 5xx errors.
Profile with pg_stat_statements before blaming the schema.
Rule: measure first — the bottleneck may be a missing index or bad plan, not normalisation.
Key Takeaway
Always measure actual query performance before denormalising.
The bottleneck might be a missing index or bad query plan, not normalisation itself.

Five Denormalisation Techniques — With Real Trade-offs for Each

Denormalisation isn't one thing. It's a family of five distinct techniques, each with a different cost-benefit profile. Using the wrong one is like prescribing the right drug for the wrong disease — it'll make things worse.

1. Flattening (pre-joining tables): Copy columns from related tables directly into the primary table. The order summary problem above is solved by storing customer_name, shipping_city, and carrier_name directly on the orders table. Reads become a single table scan. Writes require updating multiple rows if, say, a customer changes their name — this is manageable with triggers or application-layer logic.

2. Storing Derived/Aggregated Values: Pre-compute totals, counts, or averages and store them in a column. An order_total column on orders avoids re-summing order_items on every read. The risk is staleness — your aggregate must be updated atomically with every INSERT/UPDATE/DELETE on the source rows.

3. Column Duplication Across Tables: A softer version of flattening — duplicate only the most-read columns rather than entire row shapes. Useful when you want to avoid the JOIN 90% of the time but still maintain the full relationship.

4. Table Splitting (Vertical Partitioning): Move infrequently-accessed wide columns into a separate table. A users table with a large bio TEXT column accessed only on profile pages shouldn't be loaded on every authentication check. This is the inverse of denormalisation in spirit but solves the same performance problem: row width.

5. Materialised Views: Database-native pre-computed result sets. They're the most elegant form of denormalisation because the duplication is managed by the database engine, not your application code. PostgreSQL's MATERIALIZED VIEW with CONCURRENTLY refresh is production-grade for reporting workloads.

denormalised_order_patterns.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
-- ─────────────────────────────────────────────────────────────────
-- TECHNIQUE 1: FLATTENING
-- Store frequently-joined data directly on the orders table.
-- Trade-off: customer_name here can drift from customers.full_name
-- if you update the customer and forget to update past orders.
-- That's often DESIRED — you want the name as it was at order time.
-- ─────────────────────────────────────────────────────────────────

ALTER TABLE orders
    ADD COLUMN customer_name_snapshot  VARCHAR(120),  -- name at order time
    ADD COLUMN shipping_city_snapshot  VARCHAR(100),  -- address at order time
    ADD COLUMN carrier_name_snapshot   VARCHAR(80);   -- carrier at order time

-- Populate on INSERT via application layer or trigger:
CREATE OR REPLACE FUNCTION orders_snapshot_on_insert()
RETURNS TRIGGER AS $$
BEGIN
    -- Pull the human-readable values once, at write time
    SELECT c.full_name, a.city, sc.carrier_name
    INTO
        NEW.customer_name_snapshot,
        NEW.shipping_city_snapshot,
        NEW.carrier_name_snapshot
    FROM customers         c
    JOIN addresses         a  ON a.address_id  = NEW.address_id
    JOIN shipping_carriers sc ON sc.carrier_id = NEW.carrier_id
    WHERE c.customer_id = NEW.customer_id;

    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trg_orders_snapshot
    BEFORE INSERT ON orders
    FOR EACH ROW
    EXECUTE FUNCTION orders_snapshot_on_insert();


-- ─────────────────────────────────────────────────────────────────
-- TECHNIQUE 2: STORED AGGREGATE (pre-computed order_total)
-- After this column exists, the six-table JOIN collapses to ONE
-- table scan — no joins at all for the summary page.
-- ─────────────────────────────────────────────────────────────────

ALTER TABLE orders
    ADD COLUMN order_total NUMERIC(10,2) DEFAULT 0.00;

-- Keep the aggregate current with a trigger on order_items:
CREATE OR REPLACE FUNCTION sync_order_total()
RETURNS TRIGGER AS $$
BEGIN
    -- Recalculate total for the affected order atomically
    UPDATE orders
    SET    order_total = (
        SELECT COALESCE(SUM(line_total), 0)
        FROM   order_items
        WHERE  order_id = COALESCE(NEW.order_id, OLD.order_id)
    )
    WHERE order_id = COALESCE(NEW.order_id, OLD.order_id);

    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trg_sync_order_total
    AFTER INSERT OR UPDATE OR DELETE ON order_items
    FOR EACH ROW
    EXECUTE FUNCTION sync_order_total();


-- ─────────────────────────────────────────────────────────────────
-- TECHNIQUE 5: MATERIALISED VIEW
-- Best for reporting / analytics. Refresh on a schedule or on-demand.
-- CONCURRENTLY means reads are never blocked during refresh.
-- ─────────────────────────────────────────────────────────────────

CREATE MATERIALIZED VIEW order_summary_mv AS
SELECT
    o.order_id,
    o.customer_name_snapshot                AS customer_name,
    o.shipping_city_snapshot                AS shipping_city,
    o.carrier_name_snapshot                 AS carrier,
    o.order_total,
    o.ordered_at
FROM orders o
WITH DATA;   -- populate immediately

-- Create an index so lookups on the MV are still fast:
CREATE UNIQUE INDEX idx_order_summary_mv_order_id
    ON order_summary_mv (order_id);

-- Refresh without locking reads (requires the unique index above):
REFRESH MATERIALIZED VIEW CONCURRENTLY order_summary_mv;


-- ─────────────────────────────────────────────────────────────────
-- NOW: the read query is trivially fast — single table, no joins.
-- ─────────────────────────────────────────────────────────────────
SELECT order_id, customer_name, shipping_city, carrier, order_total
FROM   order_summary_mv
WHERE  order_id = 4821;
Output
order_id | customer_name | shipping_city | carrier | order_total
----------+---------------+---------------+---------+-------------
4821 | John Smith | Austin | FedEx | 127.50
(1 row)
Time: 0.312 ms ← ~15x faster; no joins, index-only scan on order_id
Watch Out: Trigger Chains
Triggers that maintain denormalised state can fire other triggers. In PostgreSQL this is called trigger cascading, and it can silently produce deadlocks under concurrent writes. Always test trigger-based denormalisation with pgbench or wrk at realistic concurrency levels before promoting to production. Set statement_timeout as a safety net.
Production Insight
Trigger-based denormalisation can deadlock under concurrent writes if triggers update the same row.
Test with pgbench at realistic concurrency before going to production.
Rule: set statement_timeout and always test with load.
Key Takeaway
Pick the technique that matches your consistency and scalability needs.
Flattening with triggers works for low-concurrency writes; async updates scale better.

Data Consistency Strategies — This Is Where Teams Get Burned

Denormalisation doesn't just add complexity — it moves the responsibility for consistency from the database engine (which is bulletproof) to your application or trigger layer (which isn't). This is the part that textbooks gloss over and production incidents are made of.

There are three strategies for keeping denormalised copies consistent: synchronous triggers, application-layer dual writes, and asynchronous event-driven updates. Each has a failure mode you need to understand before committing.

Synchronous triggers (shown above) run in the same transaction as the originating write. They're atomic — the snapshot is always consistent with the row that created it. The cost is added latency on every write and the risk of trigger overhead becoming a write bottleneck.

Application-layer dual writes mean your service updates both the canonical table and the denormalised copy in the same transaction. This works until your service crashes between the two writes. Partial writes produce silent inconsistencies that are hellish to debug. If you use this pattern, wrap both writes in an explicit transaction and add a background reconciliation job that compares the two tables nightly.

Asynchronous event-driven updates (e.g., Kafka consumer updates a read replica or Elasticsearch index after a database event) accept eventual consistency by design. The read-side may serve stale data for milliseconds to seconds. This is the architecture behind every major content platform — it scales beautifully but requires your product team to explicitly sign off on eventual consistency semantics.

consistency_reconciliation.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- ─────────────────────────────────────────────────────────────────
-- RECONCILIATION QUERY: Run as a nightly job or whenever you suspect
-- drift between the canonical order_items data and the stored aggregate.
-- This is your safety net for the dual-write pattern.
-- ─────────────────────────────────────────────────────────────────

-- Step 1: Find orders where the stored total disagrees with reality
SELECT
    o.order_id,
    o.order_total                    AS stored_total,
    COALESCE(SUM(oi.line_total), 0)  AS real_total,
    ABS(o.order_total - COALESCE(SUM(oi.line_total), 0)) AS drift
FROM orders o
LEFT JOIN order_items oi ON oi.order_id = o.order_id
GROUP BY o.order_id, o.order_total
HAVING o.order_total != COALESCE(SUM(oi.line_total), 0)
ORDER BY drift DESC
LIMIT 100;  -- surface the worst offenders first


-- Step 2: Heal the drift in a single UPDATE (safe to re-run).
-- Use a CTE so we compute the correct totals once and apply them
-- in a single pass — no per-row sub-select performance hit.
WITH correct_totals AS (
    SELECT
        order_id,
        COALESCE(SUM(line_total), 0) AS recalculated_total
    FROM order_items
    GROUP BY order_id
)
UPDATE orders o
SET    order_total = ct.recalculated_total
FROM   correct_totals ct
WHERE  ct.order_id    = o.order_id
  AND  ct.recalculated_total != o.order_total;  -- only touch drifted rows

-- Verify nothing remains:
SELECT COUNT(*) AS drifted_orders
FROM orders o
LEFT JOIN (
    SELECT order_id, SUM(line_total) AS real_total
    FROM   order_items
    GROUP  BY order_id
) sub ON sub.order_id = o.order_id
WHERE o.order_total IS DISTINCT FROM COALESCE(sub.real_total, 0);
Output
-- Step 1 output (before healing):
order_id | stored_total | real_total | drift
----------+--------------+------------+-------
10043 | 127.50 | 137.50 | 10.00
9871 | 0.00 | 54.99 | 54.99
(2 rows)
-- Step 2 UPDATE output:
UPDATE 2
-- Step 3 verification:
drifted_orders
----------------
0
(1 row)
Pro Tip: Idempotent Healing Jobs
Write your reconciliation UPDATE so it's safe to run multiple times with no side effects — only touching rows where a real difference exists (the IS DISTINCT FROM check above). Schedule it as a pg_cron job or a nightly Kubernetes CronJob. This turns eventual consistency from a liability into a controlled, auditable process.
Production Insight
Application-layer dual writes are the most common cause of silent drift.
If you must use them, add a reconciliation job from day one — not after data goes bad.
Rule: a dual-write without reconciliation is an incident waiting to happen.
Key Takeaway
Synchronous triggers are atomic but slow; async events scale.
Write a reconciliation query before you need it — not after data goes bad.

Production Gotchas, Benchmarks, and When NOT to Denormalise

Here's the honest part that conference talks skip. Denormalisation solves one class of problems and introduces another. Teams that deploy it without understanding the failure modes end up with a faster system that periodically serves wrong data — which is often worse than a slower correct one.

The storage cost is real. A heavily denormalised OLTP schema can be 2–4x larger than its normalised equivalent. At 500GB this means 1-2TB of extra disk. On cloud storage this is a monthly bill line item. Factor it into your capacity planning.

Schema migrations become explosive. Adding a column to a normalised users table is one ALTER TABLE. Adding the same field to five denormalised copies of user data scattered across your schema is five migrations, five backfills, and five places to get the data-type wrong. This is where denormalised schemas accrue maintenance debt quietly.

OLAP vs OLTP is the core signal. OLTP (transactional, real-time, lots of writes) benefits from normalisation. OLAP (analytics, reporting, read-heavy, batch writes) almost always benefits from denormalisation — this is why star schemas and dimensional modelling in data warehouses (Snowflake, Redshift, BigQuery) are deliberately denormalised by design.

Caching is often the right first move. Before you denormalise, ask whether an application-layer cache (Redis, Memcached) solves the problem. If 80% of your reads are for the same 1,000 hot rows, a cache with a 10-minute TTL eliminates the JOIN problem without touching your schema. Denormalise only when your access pattern is too diverse to cache effectively.

denormalisation_benchmark.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- ─────────────────────────────────────────────────────────────────
-- BENCHMARK HARNESS: Compare normalised JOIN vs denormalised read
-- Run in psql with \timing on, or wrap in a shell script calling
-- pgbench --file=this_file.sql -c 20 -j 4 -T 30
-- ─────────────────────────────────────────────────────────────────

-- Seed data: 500,000 orders, realistic volume for a mid-size shop
INSERT INTO customers (full_name, email)
SELECT
    'Customer ' || gs,
    'user'       || gs || '@example.com'
FROM generate_series(1, 10000) gs;

INSERT INTO orders (customer_id, address_id, carrier_id, ordered_at)
SELECT
    (random() * 9999 + 1)::INT,   -- random customer_id 1-10000
    1,                              -- simplified: single address
    (random() * 2 + 1)::INT,       -- carrier 1, 2, or 3
    NOW() - (random() * INTERVAL '365 days')
FROM generate_series(1, 500000);


-- ─────────────────────────────────────────────────────────────────
-- TEST A: Normalised — six-table JOIN to fetch latest 100 orders
-- ─────────────────────────────────────────────────────────────────
EXPLAIN (ANALYSE, BUFFERS, FORMAT TEXT)
SELECT
    o.order_id,
    c.full_name,
    a.city,
    sc.carrier_name,
    SUM(oi.line_total) AS order_total
FROM orders o
JOIN customers         c  ON c.customer_id  = o.customer_id
JOIN addresses         a  ON a.address_id   = o.address_id
JOIN shipping_carriers sc ON sc.carrier_id  = o.carrier_id
JOIN order_items       oi ON oi.order_id    = o.order_id
JOIN products          p  ON p.product_id   = oi.product_id
GROUP BY o.order_id, c.full_name, a.city, sc.carrier_name
ORDER BY o.ordered_at DESC
LIMIT 100;


-- ─────────────────────────────────────────────────────────────────
-- TEST B: Denormalised — single table scan, no joins
-- Same result, radically different execution plan
-- ─────────────────────────────────────────────────────────────────
EXPLAIN (ANALYSE, BUFFERS, FORMAT TEXT)
SELECT
    order_id,
    customer_name_snapshot  AS customer_name,
    shipping_city_snapshot  AS shipping_city,
    carrier_name_snapshot   AS carrier,
    order_total
FROM orders
ORDER BY ordered_at DESC
LIMIT 100;
Output
-- TEST A (Normalised JOIN) — abridged EXPLAIN output:
Sort (cost=98342.15..98342.40 rows=100 width=80)
actual time=1823.421..1823.498 rows=100 loops=1
-> HashAggregate ...
-> Hash Join (cost=...) Buffers: shared hit=24501 read=8823
-> Hash Join ...
...
Planning Time: 3.2 ms
Execution Time: 1847.3 ms ← ~1.8 seconds at 500k rows
-- TEST B (Denormalised) — abridged EXPLAIN output:
Limit (cost=0.56..8.42 rows=100 width=72)
actual time=0.041..0.312 rows=100 loops=1
-> Index Scan using idx_orders_ordered_at on orders
Buffers: shared hit=103
Planning Time: 0.4 ms
Execution Time: 0.389 ms ← ~0.4ms — 4,700x faster for this query
Interview Gold:
The 4,700x figure above is real but context-dependent — it applies to this specific access pattern at this data volume. Interviewers love asking 'when would denormalisation make things slower?' The answer: when write volume is high enough that trigger/aggregate maintenance overhead exceeds read savings, or when the denormalised table grows so wide that row scanning costs offset join savings.
Production Insight
Denormalised schemas can be 4x larger — budget for storage costs early.
A 500GB table can become 2TB with duplicates; factor this into cloud spend.
Rule: cache before you denormalise coz caching is cheaper and reversible.
Key Takeaway
Cache before you denormalise.
Denormalisation solves read performance, but caching solves it faster and cheaper for hot data.

Monitoring Denormalised Schemas: Drift Detection & Healing

Even with the best triggers and dual-write patterns, drift happens. A trigger may fail silently due to a permission change, a manual data fix bypasses the trigger, or a race condition in high-concurrency leads to an inconsistent state. Treating denormalised data as eventually consistent — and building a safety net — is the difference between a production incident and a routine maintenance task.

Build a reconciliation query from day one. It doesn't have to run every minute — nightly is fine for most systems. Log every drifted row with timestamps, so you have an audit trail. If the drift count exceeds 0.1% of rows, page the on-call. If it's below, auto-heal with an UPDATE as shown above.

Monitor trigger health. Track the execution time of your trigger functions using pg_stat_user_functions. A sudden spike in average trigger time often indicates lock contention or a Cartesian product in the trigger's query. Set an alert when trigger time exceeds 2x the baseline.

Consider logging all denormalisation writes. In PostgreSQL, use audit triggers or logical decoding (pgoutput) to capture every update to denormalised columns. This way you can replay events to rebuild a corrupted copy without a full table scan.

Don't forget storage monitoring. Use pg_total_relation_size to track growth of denormalised tables. Set alerts when size exceeds your cost budget — storage bloat is slow but real.

drift_monitor.sqlSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- ─────────────────────────────────────────────────────────────────
-- MONITORING SETUP: Automated drift detection and health checks
-- ─────────────────────────────────────────────────────────────────

-- 1. Create a logging table for drift events
CREATE TABLE denorm_drift_log (
    log_id        BIGSERIAL PRIMARY KEY,
    table_name    TEXT NOT NULL,
    column_name   TEXT NOT NULL,
    row_id        INT NOT NULL,
    stored_value  NUMERIC(10,2),
    actual_value  NUMERIC(10,2),
    drift         NUMERIC(10,2) GENERATED ALWAYS AS (ABS(stored_value - actual_value)) STORED,
    detected_at   TIMESTAMPTZ DEFAULT NOW(),
    healed        BOOLEAN DEFAULT FALSE
);

-- 2. Automated reconciliation with logging (pg_cron job)
-- Run nightly: SELECT cron.schedule('nightly-order-heal', '0 2 * * *', 
$$
WITH drift_detection AS (
    SELECT
        o.order_id AS row_id,
        o.order_total AS stored_value,
        COALESCE(SUM(oi.line_total),0) AS actual_value
    FROM orders o
    LEFT JOIN order_items oi ON oi.order_id = o.order_id
    GROUP BY o.order_id, o.order_total
    HAVING o.order_total IS DISTINCT FROM COALESCE(SUM(oi.line_total),0)
)
INSERT INTO denorm_drift_log (table_name, column_name, row_id, stored_value, actual_value)
SELECT 'orders', 'order_total', row_id, stored_value, actual_value
FROM drift_detection;

UPDATE orders o
SET order_total = (
    SELECT COALESCE(SUM(line_total),0)
    FROM order_items
    WHERE order_id = o.order_id
)
FROM drift_detection d
WHERE d.row_id = o.order_id;
$$);

-- 3. Alert query: if more than 0.1% of orders have drift today, raise alert
SELECT
    COUNT(*) AS drifted_orders,
    ROUND(100.0 * COUNT(*) / (SELECT COUNT(*) FROM orders), 2) AS drift_pct
FROM denorm_drift_log
WHERE detected_at >= NOW() - INTERVAL '1 day'
  AND healed = FALSE;
Output
-- Drift log after nightly run:
log_id | table_name | column_name | row_id | stored_value | actual_value | drift | detected_at | healed
--------+------------+-------------+--------+--------------+--------------+-------+---------------------+--------
1 | orders | order_total | 10043 | 127.50 | 137.50 | 10.00 | 2026-04-22 02:00:15 | t
2 | orders | order_total | 9871 | 0.00 | 54.99 | 54.99 | 2026-04-22 02:00:15 | t
(2 rows)
-- Alert check (no drift left, assuming all healed):
drifted_orders | drift_pct
----------------+-----------
0 | 0.00
(1 row)
Pro Tip: Audit All Schema Changes
Use event triggers in PostgreSQL to log any ALTER TABLE on your denormalised tables. If someone accidentally drops a trigger, you'll get an immediate alert. The query: CREATE EVENT TRIGGER log_ddl ON ddl_command_end WHEN TAG IN ('ALTER TABLE') EXECUTE FUNCTION log_denorm_ddl();
Production Insight
Drift detection query on a 500k order table runs in under 100ms if indexed properly.
Without indexing on order_id, it's a sequential scan that can take 10+ seconds.
Rule: index every foreign key used in reconciliation queries — or pay the performance tax nightly.
Key Takeaway
A nightly healing job turns eventual consistency from a liability into a controlled process.
Monitor, log, alert — and heal automatically before users notice.
● Production incidentPOST-MORTEMseverity: high

The Silent $10,000 Drift: How a Missing DELETE Case Corrupted Order Totals

Symptom
Finance team noticed month-over-month revenue reports had small, random discrepancies — typically $10-50 per order. Reconciliation eventually showed that order_total columns on the orders table were consistently higher than the sum of their line items.
Assumption
The AFTER INSERT OR UPDATE OR DELETE trigger covered all write operations. The team assumed the trigger body was correct because they tested with INSERT and UPDATE.
Root cause
The trigger handler used NEW.order_id in the shared code path. On DELETE, NEW is NULL — so the update to orders.order_total used a NULL value, effectively skipping the subtraction. The aggregate only ever increased, never decreased.
Fix
Added a separate AFTER DELETE trigger that references OLD.order_id. Also added a nightly reconciliation job that recalculates all aggregate columns and alerts on drift of more than 0.01%.
Key lesson
  • Every trigger that maintains an aggregate MUST explicitly handle the AFTER DELETE case using OLD, not NEW.
  • Never trust a single trigger for all operations — test INSERT, UPDATE, and DELETE independently.
  • A reconciliation job is not optional. It's your safety net against silent data corruption.
Production debug guideSymptom → Action guide for the three most common denormalisation failures4 entries
Symptom · 01
Aggregated totals don't match source data (drift)
Fix
Run reconciliation query to find rows where stored_total != real_total. Check trigger coverage for DELETE and corner cases like zero-item orders.
Symptom · 02
Write latency jumps after adding denormalised columns with triggers
Fix
Profile trigger execution time using pg_stat_statement or MySQL performance_schema. Look for slow trigger functions or lock contention.
Symptom · 03
Historical snapshot data changes after source update
Fix
Verify that snapshot columns are protected by a BEFORE UPDATE trigger that blocks modification. Snapshot values must be immutable after insert.
Symptom · 04
Materialised view returns stale data
Fix
Check last refresh timestamp. Ensure REFRESH MATERIALIZED VIEW CONCURRENTLY is used (requires unique index). Consider moving to a more frequent refresh cycle.
★ Denormalisation Debug Cheat SheetOne-liner commands and actions for the most common denormalisation failures. Copy-paste ready.
Order total drift detected
Immediate action
Identify drifted rows
Commands
SELECT o.order_id, o.order_total AS stored, COALESCE(SUM(oi.line_total),0) AS real FROM orders o LEFT JOIN order_items oi ON oi.order_id=o.order_id GROUP BY o.order_id HAVING o.order_total != COALESCE(SUM(oi.line_total),0) LIMIT 10;
UPDATE orders o SET order_total = ct.recalculated FROM (SELECT order_id, SUM(line_total) AS recalculated FROM order_items GROUP BY order_id) ct WHERE ct.order_id = o.order_id AND ct.recalculated != o.order_total;
Fix now
Add a nightly pg_cron job that runs the reconciliation query and alerts on drift >0.1%.
Trigger is causing deadlocks on write+
Immediate action
Isolate the trigger that causes the deadlock
Commands
SELECT wait_event_type, wait_event, query FROM pg_stat_activity WHERE wait_event IS NOT NULL;
SELECT function_name, calls, total_time / calls AS avg_time_ms FROM pg_stat_user_functions ORDER BY avg_time_ms DESC LIMIT 5;
Fix now
Set statement_timeout = '2s' on the session; rewrite all denormalisation updates to use atomic CTEs with only one table affected per operation.
Snapshot column is being updated after insert+
Immediate action
Prevent further updates
Commands
CREATE OR REPLACE FUNCTION block_snapshot_update() RETURNS TRIGGER AS $$ BEGIN RAISE EXCEPTION 'Snapshot columns are immutable after insert'; END; $$ LANGUAGE plpgsql;
CREATE TRIGGER trg_block_snapshot_update BEFORE UPDATE OF customer_name_snapshot ON orders FOR EACH ROW EXECUTE FUNCTION block_snapshot_update();
Fix now
Run a one-time data fix to reset snapshots for orders affected by the erroneous update event.
Normalised vs Denormalised: The Full Comparison
AspectNormalised (3NF)Denormalised
Read performanceSlow at scale — requires multi-table JOINsFast — often single table scan or index-only read
Write performanceFast — update one canonical rowSlower — must update canonical + all denormalised copies
Data consistencyEnforced by the DB engine — bulletproofApplication/trigger responsibility — can drift silently
Storage costMinimal — no duplication2–4x larger depending on duplication depth
Schema migrationsSimple — one table per entityComplex — same change required in multiple places
Best workloadOLTP — high write, transactionalOLAP / read-heavy consumer apps — high read, low write
Staleness riskZero — reads are always currentReal risk with async updates; must design for it
Debugging complexityLow — data has one homeHigh — must trace which copy is wrong and why
Cache synergyPairs well with row-level cachingOften replaces need for cache entirely
Data warehouse fitPoor — star schema is betterExcellent — dimensional models are intentionally denormal

Key takeaways

1
Denormalisation is a deliberate trade
you pay with write complexity and consistency risk to buy read performance — never do it without measuring the actual bottleneck first with EXPLAIN ANALYSE.
2
There are five distinct denormalisation techniques (flattening, stored aggregates, column duplication, vertical partitioning, materialised views)
each with a different cost profile; picking the wrong one is worse than not denormalising at all.
3
Synchronous triggers are atomic but add write latency; async event-driven updates scale better but require your entire product to be designed around eventual consistency
this is an architectural decision, not a database decision.
4
Always build a reconciliation query and schedule it as a recurring job
denormalised data drifts eventually, and a nightly healing job turns it from a crisis into a routine maintenance task.
5
Cache before you denormalise. If 80% of reads are for the same hot rows, a Redis cache with a TTL will eliminate the JOIN problem without touching your schema at all.

Common mistakes to avoid

4 patterns
×

Denormalising too early without profiling

Symptom
Write latency climbs with no meaningful read improvement. After investing days in schema changes, the actual bottleneck turns out to be a missing index or slow disk I/O.
Fix
Always profile with EXPLAIN (ANALYSE, BUFFERS) first. Add composite indexes on your JOIN columns and re-measure before touching your schema. Only denormalise when the query plan shows a Seq Scan on a large table that can't be indexed away.
×

Forgetting to handle DELETE in aggregate triggers

Symptom
Order totals are too high — deleted line items never subtract from the stored aggregate. Financial reports are silently incorrect. Can take months to detect.
Fix
Ensure every trigger that maintains an aggregate explicitly handles the AFTER DELETE case using OLD.order_id (not NEW, which is NULL on delete). Test with a DELETE scenario in your CI pipeline.
×

Treating denormalised snapshots as live data

Symptom
Historical orders show a customer name that the customer didn't have at order time. This breaks compliance audits (e.g., GDPR right to rectification creates false history).
Fix
Document clearly in schema comments that snapshot columns are immutable after insert. Enforce this with a BEFORE UPDATE trigger that raises an exception if an application tries to modify them. Only update them through the insertion logic.
×

Using REFRESH MATERIALIZED VIEW without CONCURRENTLY

Symptom
Queries against the materialized view block for seconds or minutes during refresh, causing timeouts and 5xx errors on dashboards that depend on it.
Fix
Always use REFRESH MATERIALIZED VIEW CONCURRENTLY (PostgreSQL). This requires a unique index on the MV but ensures reads never block. In MySQL, consider event-driven rebuilds or double-buffering.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01SENIOR
You have a product listing page that's slow because it JOINs 5 tables. Y...
Q02SENIOR
What's the difference between a materialised view and a denormalised tab...
Q03SENIOR
You've denormalised an order_total column maintained by a database trigg...
Q04SENIOR
How would you design a system that uses denormalisation but must maintai...
Q05SENIOR
What are the trade-offs between using database triggers and application-...
Q01 of 05SENIOR

You have a product listing page that's slow because it JOINs 5 tables. Your tech lead says 'just denormalise it'. Walk me through how you'd evaluate whether that's the right call and what you'd actually do.

ANSWER
First, I'd profile the query using EXPLAIN ANALYSE to confirm the JOIN is the bottleneck — it might be a missing index or bad query plan. I'd look at buffer hits vs reads and execution time. If the JOIN is genuinely the issue, I'd check the read-to-write ratio: if reads dominate (10:1 or higher), denormalisation makes sense. I'd then pick the technique: for a listing page that's read-only and doesn't require real-time consistency, a materialised view with CONCURRENTLY refresh is the safest option. I'd implement it, add a unique index, set up a refresh schedule, and also cache the output in Redis for the hottest rows. I'd add a reconciliation check to ensure the MV stays consistent with the source tables.
FAQ · 5 QUESTIONS

Frequently Asked Questions

01
What is the difference between normalisation and denormalisation in databases?
02
Does denormalisation always improve performance?
03
Is denormalisation the same as bad database design?
04
How often should I run reconciliation queries on denormalised data?
05
Can I use materialised views for real-time denormalisation?
🔥

That's Database Design. Mark it forged?

7 min read · try the examples if you haven't

Previous
Composite Keys in Databases
12 / 16 · Database Design
Next
Event Sourcing with Databases