A quick overview of Slowly Changing Dimensions
A discussion of pros and cons of the different forms of SCDs
Using Postgres to manage SCD Type 6
Problem 1: Managing the boundaries between ranges
Problem 2: Find out what the value was on a given day
Problem 3: Avoiding overlaps between ranges
Problem 4: Identifying gaps in ranges
Slowly Changing Dimensions (SCD) are a powerful method for modeling reference data that evolves over time. For instance, price lists frequently change, and tracking these historical changes is essential for analytics and transactions. With SCDs, we can determine which price was in effect at any given time, allowing us to analyze how price adjustments impact revenue trends. Some types of SCDs also allow staging data changes that will become effective.
SCDs are based on the work of Ralph Kimball (https://www.kimballgroup.com/) and were introduced in the early 2000s. The literature distinguishes six ways of handling reference data that changes over time.
In this article, I will quickly review the six types of SCDs and illustrate them using a simple example, before discussing pros and cons and recommending how some of them could be implemented in Postgres by taking advantage of key innovative features.
The sample code is available at https://github.com/marclinster/SCD-example.
A quick overview of Slowly Changing Dimensions
These examples are intended for illustration only. Later on, I will show how to take advantage of Postgres to create optimized implementations.
Type 1: Changes are considered corrections, and there is no value in tracking when the new change has become effective. Addressing a typo in the product name is a typical example of a Type 1 change. Type 1s are also often referred to as overwrites.
CREATE TABLE product_price_type_1 (product_id INTEGER,price NUMERIC,PRIMARY KEY (product_id));
| product_id | price |
| 12345 | 20.99 |
Type 2: Every change gets assigned a new row in the data table, and the primary key is extended with the start date and end date columns. NULL can be used to denote that no end date has been set – but in that case the column cannot participate in the primary key definition and I suggest adding a surrogate key, like a UUID to simplify the references.
CREATE TABLE product_price_type_2 (product_id INTEGER,price NUMERIC,start_date date,end_date date,--- SQL standard does not allow NULL values in columns that participate in a primary keyUNIQUE (product_id,start_date, end_date));
| product_id | price | start_date | end_date |
|---|---|---|---|
| 12345 | 19.99 | 2025-01-01 | 2025-01-31 |
| 12345 | 20.99 | 2025-02-01 | NULL |
Type 3: Type 3 SCDs add additional columns to track the prior value and the date of last change. This only allows for tracking of one prior version.
CREATE TABLE product_price_type_3 (product_id INTEGER,price NUMERIC,last_update date,prior_price NUMERIC,PRIMARY KEY (product_id));
| product_id | price | last_update | prior_price |
| 12345 | 20.99 | 2025-01-01 | 19.99 |
Type 4 creates a history table to track an infinite number of historical changes. The current price and the historical prices are kept in the same table. The current price is the price with the most current effective date.
CREATE TABLE product_price_type_4 (product_id INTEGER,price NUMERIC,effective_date date,PRIMARY KEY (product_id, effective_date));
| product_id | price | effective_date |
|---|---|---|
| 12345 | 19.99 | 2025-01-01 |
| 12345 | 20.99 | 2025-02-01 |
Type 5 combines Type 1 and Type 4 – that’s why it is called Type 5. It has both the current data (in our example product_price table) and a history table (in our example product_price_history table)
CREATE TABLE product_price_type_5 (product_id INTEGER,price NUMERIC,PRIMARY KEY (product_id));CREATE TABLE product_price_history_type_5 (product_id INTEGER,price NUMERIC,effective_date date,PRIMARY KEY (product_id, effective_date));
| product_id | price |
| 12345 | 20.99 |
| product_id | price | effective_date |
| 12345 | 19.99 | 2025-01-01 |
| 12345 | 20.99 | 2025-02-01 |
Type 6 combines Type 1, 2, and 3 into a single history table that has start and end dates for the change and a flag to indicate which record represents the current value.
CREATE TABLE product_price_type_6 (product_id INTEGER,price NUMERIC,start_date date,end_date date,current BOOLEAN,/*SQL standard does not allow NULL values in columns that participate in a primary key */UNIQUE (product_id,start_date, end_date));
| product_id | price | start_date | end_date | current |
| 12345 | 19.99 | 2025-01-01 | 2025-01-31 | false |
| 12345 | 20.99 | 2025-02-01 | NULL | true |
A discussion of pros and cons of the different forms of SCDs
This discussion is based on my experience and personal preferences.
| SCD Type | Pro | Con |
| Type 1 | Simple | No historical data |
| Type 2 | Simple | Lookups can become expensive if the dimension sees a lot of changes. Start date and end date consistency can be challenging to manage (see Postgres section below for remedies) |
| Type 3 | Simple | Not an effective way to track historical data |
| Type 4 | Relatively simple | Lookups can become expensive if the dimension sees a lot of changes. It can be challenging to answer questions like “What was the price on Feb 2 2025?” |
| Type 5 | Addresses some of the problems of Type 4. Very fast access to current value. | Inserts and updates cause transactions in two tables. |
| Type 6 | Fast lookups for current data. | Start date and end date consistency can be challenging (see Postgres section below for remedies). The table can become large if data changes frequently. |
In transactional environments I have a general concern with all the approaches that track history through new, additional records, such as Type 5. When the SCD data is more complex, such as a hierarchical bill of materials, then the transaction that refers to the record needs to be updated when the current record moves into the history table, or it needs to copy all the data – which violates the third normal form.
I personally like SCD Type 6 as it keeps all data in one table and it allows to represent future changes. This is especially useful in eCommerce systems, for example, where price changes can be staged and will become effective on a certain date.
I tend to create a surrogate primary key to refer to the rows in the table using a UUID. That way, the fact can easily refer to the original row without the complication of a foreign key that points to a compound primary key. The surrogate key also allows to create start and end dates that are NULL to indicate that a value had no start or end date. N.B.: SQL does not allow columns with NULL values to be part of a primary key.
Postgres provides a set of superb tools to create and work with SCD Type 6 representations, which I will discuss in the next section.
Using Postgres to manage SCD Type 6
Type 6 SCDs are powerful tools, but they present a few management challenges:
- Managing the boundaries between ranges
- Find out what the value was on a given day
- Avoiding overlaps between ranges
- Identifying gaps in ranges
Whereas the original definitions of SCD Type 6 were considering separate start and end date columns as shown earlier, I prefer to use Postgres daterange datatype as it comes with many powerful operators and provides simple answers to (1) and (2).
The btree_gist extension makes it easy to create constraints that make sure that the ranges are distinct and free of overlap, addressing (3).
Postgres window functions help us identify gaps in the date ranges (4)
CREATE EXTENSION IF NOT EXISTS btree_gist;CREATE TABLE product_price_scd (id UUID PRIMARY KEY,product_id INTEGER NOT NULL,price NUMERIC DEFAULT 0 NOT NULL,validity DATERANGE,current BOOLEAN,EXCLUDE USING GIST (product_id WITH =, validity WITH &&));Sample data set (omitting id for sake of readability):SELECT product_id, price, validity, currentFROM product_price_scdORDER BY product_id, lower(validity)ASC NULLS FIRST;product_id | price | validity | current------------+-------+-------------------------+---------12345 | 19.99 | (,2025-02-01) | f12345 | 12.99 | [2025-02-01,2025-03-01) | f12345 | 20.99 | [2025-03-01,) | t12346 | 1.00 | [2025-01-01,2025-02-01) | f12346 | 2.00 | [2025-02-01,2025-03-01) | f12346 | 3.00 | [2025-03-01,2025-04-01) | f12346 | 4.00 | [2025-04-01,2025-05-01) | f12346 | 5.00 | [2025-05-01,2025-06-01) | f12346 | 6.00 | [2025-06-01,2025-07-01) | f12346 | 7.00 | [2025-07-01,2025-07-31) | t12346 | 8.00 | [2025-08-01,2025-08-31) | f12346 | 10.00 | [2025-10-01,2025-10-31) | f
Problem 1: Managing the boundaries between ranges
The Postgres daterange type (and this is true for all range types) provides an easy way to define if the range boundaries are included or excluded in the range.
- Square brackets “[” and “]” indicate that the range boundary is included
- Round brackets “(” and “)” indicate that the range boundary is excluded
This makes it very easy and unambiguous to state that a price is effective starting on June 1 up until the last day prior to July 1: [2025-06-01,2025-07-01). Using separate columns for start and end dates requires that these semantics are encoded elsewhere.
Problem 2: Find out what the value was on a given day
A simple query can be used to find the value at a certain date:
SELECT price FROM product_price_scdWHERE validity @> '2025-06-18'::dateAND product_id = 12345;
Problem 3: Avoiding overlaps between ranges
The btree_gist extension allows us to create the EXCLUDE constraint, which makes sure that the ranges are exclusive and not overlapping. This is what happens when we add a conflicting record (overlapping validity range):
INSERT INTO product_price_scd(id, product_id, price, validity, current)VALUES(gen_random_uuid (), 12346, 100.00, '[2025-07-01,2025-07-02]', false);ERROR: conflicting key value violates exclusion constraint "product_price_scd_product_id_validity_excl"DETAIL: Key (product_id, validity)=(12346, [2025-07-01,2025-07-03)) conflicts with existing key (product_id, validity)=(12346, [2025-07-01,2025-07-31)).
Problem 4: Identifying gaps in ranges
The sample dataset displayed above has two gaps – dates where the price for product 12346 was not defined: no price is defined for July 31 and for the dates between end of August and September 1.
This data quality issue will cause problems during analytics or when the dataset is used to stage prices before they become effective. Fortunately, Postgres Common Table Expressions (CTE) combined with Window Functions help us identify gaps in the ranges.
WITH ordered_ranges AS (SELECTproduct_id,validity,LAG(upper(validity))OVER (ORDER BY lower(validity)) AS previous_range_end,lower(validity) AS current_range_start,upper(validity) AS current_range_end,LEAD(lower(validity)) OVER (ORDER BY lower(validity)) AS next_range_startFROM product_price_scd),gaps AS (SELECTproduct_id,previous_range_end AS gap_start,current_range_start AS gap_end,current_range_start - previous_range_end AS gap_durationFROM ordered_rangesWHERE previous_range_end IS NOT NULLAND current_range_start > previous_range_end)SELECTproduct_id,gap_start,gap_end,gap_durationFROM gapsORDER BY product_id, gap_start;product_id | gap_start | gap_end | gap_duration------------+------------+------------+--------------12346 | 2025-07-31 | 2025-08-01 | 112346 | 2025-08-31 | 2025-10-01 | 31
This query helps us quickly find gaps in the data set and avoid problems in production.
Summary
Postgres provides very powerful tools to manage what I consider to be the most important type of slowly changing dimensions. The daterange data type, the btree_gist extension, CTEs, and the window functions significantly reduce the amount of code that needs to be written to leverage a very powerful data modeling tool.
Use Postgres – get stuff done!
Let us know what you think!