Write a slowly-changing-dimension SCD Type 2 load for customers in PostgreSQL
dim_customer keeps history as SCD Type 2. Each night stg_customer holds one row per customer_id with the CRM's current plan, city and their hashdiff. Customer C-001 moved from starter to enterprise on 2026-04-15 (:load_date).
Requirements (PostgreSQL 15+):
- a changed customer gets its current row closed and a new current row;
- an unchanged customer gets nothing; a new customer gets its first row;
- re-running the load for the same date changes nothing.
CREATE TABLE dim_customer (
customer_sk bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id text NOT NULL,
plan text,
city text,
hashdiff text NOT NULL,
valid_from date NOT NULL,
valid_to date NOT NULL DEFAULT '9999-12-31',
is_current boolean NOT NULL DEFAULT true
);
-- your code hereWrite the implementation, then show how fct_orders picks the customer_sk for each order_ts. Write :load_date as DATE '2026-04-15': a closed row gets valid_to equal to the load date and is_current = false, a new row starts at valid_from equal to the load date; give the fct_orders lookup in a comment, since the checks read only dim_customer.
Each version gets its own surrogate key; customer_id repeats. In one transaction close the current row whose hashdiff changed, then insert the new one — one MERGE action can't do both. Facts take the key of the version valid at the event time.
- Joining facts on
is_current = true, which relabels old orders with the customer's current version - Expecting one MERGE action to close the old row and insert the new version for the same source row
- Using an inclusive
valid_to, so an event on the change date matches two versions
- Why does the warehouse mint its own key when the CRM already has
customer_id? - How does the fact load handle an order whose customer is not in the dimension yet?
customer_id repeats in every version of a customer, so it cannot be the dimension's key: the warehouse mints its own surrogate key customer_sk per version. The same key also helps when two sources use different or reused ids.
A changed customer must be both closed and inserted again. In MERGE one source row fires one action — WHEN MATCHED or WHEN NOT MATCHED. The simplest form is therefore two statements in one transaction:
BEGIN;
-- 1. close the current version of customers whose hashdiff changed
UPDATE dim_customer d
SET valid_to = :load_date,
is_current = false
FROM stg_customer s
WHERE d.customer_id = s.customer_id
AND d.is_current
AND d.hashdiff <> s.hashdiff;
-- 2. insert a current version for new and just-closed customers
INSERT INTO dim_customer (customer_id, plan, city, hashdiff, valid_from)
SELECT s.customer_id, s.plan, s.city, s.hashdiff, :load_date
FROM stg_customer s
LEFT JOIN dim_customer d
ON d.customer_id = s.customer_id
AND d.is_current
WHERE d.customer_id IS NULL;
COMMIT;A re-run changes nothing: C-001 already has a current row with the new hashdiff, step 1 leaves it alone, and step 2 sees the current row and inserts nothing.
The same result in a single MERGE uses the doubled-source trick: changed customers are fed a second time with NULL as the join key, so that row never matches and goes to the insert. The source is evaluated against the table as it was before the MERGE, so the join inside it still sees the not-yet-closed current rows:
MERGE INTO dim_customer d
USING (
SELECT s.customer_id AS merge_key, s.* FROM stg_customer s
UNION ALL
SELECT NULL::text, s.*
FROM stg_customer s
JOIN dim_customer c
ON c.customer_id = s.customer_id AND c.is_current AND c.hashdiff <> s.hashdiff
) src
ON d.customer_id = src.merge_key AND d.is_current
WHEN MATCHED AND d.hashdiff <> src.hashdiff THEN
UPDATE SET valid_to = :load_date, is_current = false
WHEN NOT MATCHED THEN
INSERT (customer_id, plan, city, hashdiff, valid_from)
VALUES (src.customer_id, src.plan, src.city, src.hashdiff, :load_date);The intervals are half-open: the closed version ends at valid_to = '2026-04-15' and the new one starts at valid_from = '2026-04-15', so an event on the change date matches exactly one. A fact looks up the version valid at the event time, not the current one:
SELECT o.order_id, o.order_ts, o.amount, d.customer_sk
FROM stg_orders o
JOIN dim_customer d
ON d.customer_id = o.customer_id
AND o.order_ts::date >= d.valid_from
AND o.order_ts::date < d.valid_to;An order whose customer is not in the dimension yet would vanish in an inner join. Route it to an "unknown customer" row, or create a placeholder row with the natural key and fill it in when the CRM data arrives.