A slowly-changing-dimension SCD2 load doubles revenue and misdates changes — debug it
dim_customer keeps SCD Type 2 history on segment. Every CRM delivery is appended to stg_customer (customer_id, segment, changed_at), and the load below runs once a night in PostgreSQL. Three symptoms:
- 1,214 customers have two rows with
is_current = true, some with the same segment; - on days when a customer's segment changed,
fct_ordersrevenue for that customer is double the order system's; - customer C-001 moved to
enterpriseon 2026-03-02 in the CRM, but the dimension starts that version on 2026-03-05 — the nightly job was down for three days.
Requirements:
- name every defect behind these symptoms;
- fix the dimension load and the fact lookup;
- add checks that stop the history from going wrong again.
BEGIN;
UPDATE dim_customer d
SET valid_to = CURRENT_DATE, is_current = false
FROM stg_customer s
WHERE d.customer_id = s.customer_id
AND d.is_current
AND d.segment IS DISTINCT FROM s.segment;
INSERT INTO dim_customer (customer_id, segment, valid_from, valid_to, is_current)
SELECT s.customer_id, s.segment, CURRENT_DATE, DATE '9999-12-31', true
FROM stg_customer s
WHERE NOT EXISTS (
SELECT 1 FROM dim_customer d
WHERE d.customer_id = s.customer_id AND d.is_current
);
COMMIT;
-- fact load: each order takes the customer version valid on its date
INSERT INTO fct_orders (order_id, customer_key, order_date, amount)
SELECT o.order_id, d.customer_key, o.order_date, o.amount
FROM stg_orders o
JOIN dim_customer d
ON d.customer_id = o.customer_id
AND o.order_date BETWEEN d.valid_from AND d.valid_to;Find and fix the bugs.
Duplicate staging rows create two current versions, versions start on the load date, not the CRM change date, and BETWEEN matches two versions on the change day. Dedupe per key, date by the source, use half-open ranges, test overlaps.
- Feeding every delivery in staging to the load, so a redelivered or superseded row becomes a fake version
- Starting versions on the load date, so any delay in the pipeline shifts history and misattributes orders
- Joining facts with an inclusive
BETWEENon a closed-equals-next-start interval, so the change day matches two versions
- The CRM back-dates a change to before the current version started — what do you rewrite, and which facts are restated?
- How do you repair the 1,214 customers that already have two current rows, and their fact rows?
Three defects produce the three symptoms.
- Duplicates in staging.
stg_customeraccumulates every delivery, so one customer can have a redelivery or two changes in a row.NOT EXISTSis checked against the table as it stood before theINSERTbegan, so every staging row for the customer passes at once and two current versions appear. If staging holds an oldstarterrow and a newenterpriserow, theUPDATEcloses the current version on theenterpriserow and theINSERTadds both — that is the fake version with an unchanged segment. Both current versions stay open until 9999-12-31, so the join also doubles every later order of such a customer. - The version date is the load date.
CURRENT_DATEstarts the version on the day the load ran. Three days of downtime moved C-001's change to 2026-03-05, and orders from March 2–4 got the old segment. - Inclusive
BETWEEN. A closed version ends on the same day the new one starts.BETWEENincludes both bounds, so an order on the change day matches two versions and the join doubles its amount.
The fixed dimension load:
BEGIN;
-- one row per customer: the latest change in this batch
CREATE TEMP TABLE stg_latest ON COMMIT DROP AS
SELECT DISTINCT ON (customer_id)
customer_id, segment, changed_at::date AS change_date
FROM stg_customer
ORDER BY customer_id, changed_at DESC;
-- close the current version on the CRM change date
UPDATE dim_customer d
SET valid_to = s.change_date, is_current = false
FROM stg_latest s
WHERE d.customer_id = s.customer_id
AND d.is_current
AND d.segment IS DISTINCT FROM s.segment
AND s.change_date >= d.valid_from; -- an earlier date is handled separately
-- open the new version from the same date
INSERT INTO dim_customer (customer_id, segment, valid_from, valid_to, is_current)
SELECT s.customer_id, s.segment, s.change_date, DATE '9999-12-31', true
FROM stg_latest s
WHERE NOT EXISTS (
SELECT 1 FROM dim_customer d
WHERE d.customer_id = s.customer_id AND d.is_current
);
COMMIT;DISTINCT ON keeps only the batch's latest change. If intermediate changes inside one batch matter to reports, apply them one by one in changed_at order. A change dated before the current version started is a retroactive correction: it is not closed with the ordinary UPDATE; the intervals are rewritten and the affected facts restated.
The fact lookup uses a half-open interval, end excluded:
INSERT INTO fct_orders (order_id, customer_key, order_date, amount)
SELECT o.order_id, d.customer_key, o.order_date, o.amount
FROM stg_orders o
JOIN dim_customer d
ON d.customer_id = o.customer_id
AND o.order_date >= d.valid_from
AND o.order_date < d.valid_to;Guarding the history — constraints on the table itself and a check after each load:
-- at most one current version per customer
CREATE UNIQUE INDEX dim_customer_one_current
ON dim_customer (customer_id) WHERE is_current;
-- one customer's intervals never overlap; daterange is half-open [) by default
CREATE EXTENSION IF NOT EXISTS btree_gist;
ALTER TABLE dim_customer ADD CONSTRAINT dim_customer_no_overlap
EXCLUDE USING gist (customer_id WITH =, daterange(valid_from, valid_to) WITH &&);
-- gaps and overlaps between neighbouring versions
SELECT customer_id, valid_to, next_from
FROM (
SELECT customer_id, valid_to,
LEAD(valid_from) OVER (PARTITION BY customer_id ORDER BY valid_from) AS next_from
FROM dim_customer
) v
WHERE next_from IS NOT NULL AND next_from <> valid_to;The constraints will not build while broken rows remain: first keep the correct versions for each customer, re-point fact rows away from the fake keys, and restate the totals for the change days.