time ranges and temporal keys
exclusion constraints for no double booking, and WITHOUT OVERLAPS on primary and foreign keys.
- address
- everything.design.temporal_keys
- part
- part 7, schema design
- chapter
- chapter 10 of 13
- reading time
- 7 min
- version
- pg 18+
a temporal key is a key that's unique per moment: room 101 can appear many times, as long as its date ranges never overlap. postgres 18 pg 18+ writes it as PRIMARY KEY (room, during WITHOUT OVERLAPS), enforced like an exclusion constraint by a GiST index.
and a temporal foreign key, FOREIGN KEY (dept_id, PERIOD valid) REFERENCES departments (id, PERIOD valid), checks that the parent existed for the child's whole range, even when that takes several parent versions together.
the picture
you know the double-booked meeting room? two rows, same room, overlapping times, and every one of them "unique" in the eyes of a normal key, because their times aren't equal. equality is the wrong question. overlap is the right one.
think of a hotel's booking ledger. the rule isn't "no two lines may mention room 101"; it's "no two lines may hold room 101 on the same night". and a reservation for a conference room should only be accepted for days the room actually exists, which might span a renovation that renamed it.
- 1INSERT INTO bookings VALUES (101, 'edsger', '[2026-03-04,2026-03-06)');
edsger wants room 101 from march 4 to 6. that overlaps both stays, and the key refuses it.
the overlap error is SQLSTATE 23P01, an exclusion violation, not 23505. that's the tell for how it works.
WITHOUT OVERLAPS
the docs describe it plainly: PRIMARY KEY (room, during WITHOUT OVERLAPS) behaves like EXCLUDE USING gist (room WITH =, during WITH &&), plus NOT NULL, plus being the primary key that foreign keys can point at. the last column must be a range or multirange, and needs at least one ordinary column before it:
CREATE TABLE bookings (room int, during daterange, PRIMARY KEY (room, during WITHOUT OVERLAPS));
SELECT conname, contype, conperiod, pg_get_constraintdef(oid) AS def
FROM pg_constraint WHERE conrelid = 'bookings'::regclass AND contype = 'p';
SELECT am.amname FROM pg_class AS c JOIN pg_am AS am ON am.oid = c.relam WHERE c.relname = 'bookings_pkey';| connamename | contypechar | conperiodbool | deftext |
|---|---|---|---|
| bookings_pkey | p | t | PRIMARY KEY (room, during WITHOUT OVERLAPS) |
| amnamename |
|---|
| gist |
a primary key backed by a gist index, with conperiod true. that GiST index is also why the btree_gist extension is needed: GiST has no = for a plain integer until btree_gist adds it. without it, the same table fails (this block runs without the extension):
CREATE TABLE bookings (room int, during daterange, PRIMARY KEY (room, during WITHOUT OVERLAPS));so every other block on this page starts with ext=btree_gist, which creates the extension first. (with only range columns, say PRIMARY KEY (seats int4range, during WITHOUT OVERLAPS), no extension is needed, but real keys are rarely ranges.)
compared with writing the exclusion constraint yourself (range types shows that one), WITHOUT OVERLAPS adds three things: it is the primary key (or a UNIQUE constraint), so it can be referenced; it implies NOT NULL; and it refuses empty ranges, which an && exclusion lets through because an empty range overlaps nothing.
PERIOD foreign keys
a temporal foreign key says: for every moment of the child's range, some parent row with the same key was valid. the parent may have several versions covering it between them:
CREATE TABLE departments (
id int NOT NULL,
name text NOT NULL,
valid daterange NOT NULL,
PRIMARY KEY (id, valid WITHOUT OVERLAPS)
);
CREATE TABLE assignments (
employee text NOT NULL,
dept_id int NOT NULL,
valid daterange NOT NULL,
PRIMARY KEY (employee, valid WITHOUT OVERLAPS),
FOREIGN KEY (dept_id, PERIOD valid) REFERENCES departments (id, PERIOD valid)
);
-- the department was renamed on april 1: two versions, back to back.
INSERT INTO departments VALUES (7, 'research', '[2026-01-01,2026-04-01)'), (7, 'research and data', '[2026-04-01,2026-07-01)');
-- ada's assignment spans the rename. no single version covers it; both together do.
INSERT INTO assignments VALUES ('ada', 7, '[2026-02-01,2026-05-01)');
SELECT 'dept 7' AS who, name AS what, lower(valid), upper(valid) FROM departments
UNION ALL
SELECT employee, 'in dept ' || dept_id, lower(valid), upper(valid) FROM assignments
ORDER BY 3, 1;| whotext | whattext | lowerdate | upperdate |
|---|---|---|---|
| dept 7 | research | 2026-01-01 | 2026-04-01 |
| ada | in dept 7 | 2026-02-01 | 2026-05-01 |
| dept 7 | research and data | 2026-04-01 | 2026-07-01 |
CREATE TABLE departments (id int, name text, valid daterange, PRIMARY KEY (id, valid WITHOUT OVERLAPS));
CREATE TABLE assignments (employee text, dept_id int, valid daterange,
FOREIGN KEY (dept_id, PERIOD valid) REFERENCES departments (id, PERIOD valid));
INSERT INTO departments VALUES (7, 'research', '[2026-01-01,2026-04-01)'), (7, 'research and data', '[2026-04-01,2026-07-01)');
INSERT INTO assignments VALUES ('grace', 7, '[2026-06-01,2026-08-01)');
INSERT INTO assignments VALUES ('ada', 7, '[2026-02-01,2026-05-01)');
DELETE FROM departments WHERE name = 'research';grace is refused, and so is the last statement: the parent side is checked too. deleting the january-to-april version would leave part of ada's assignment uncovered.
-- departments: (7, '[2026-01-01,2026-04-01)'), (7, '[2026-04-01,2026-07-01)')
INSERT INTO assignments VALUES ('ada', 7, '[2026-03-15,2026-04-15)');what 18 doesn't do yet
- ├── PRIMARY KEY / UNIQUE (..., r WITHOUT OVERLAPS)GiST, 23P01 on overlap
- ├── covered by the parent rows combined
- │ ├── ON DELETE / ON UPDATE NO ACTION
- │ └── CASCADE, SET NULL, SET DEFAULT, RESTRICT0A000 at CREATE
- ├── UPDATE / DELETE ... FOR PORTION OFnot in 18
- ├── FOR SYSTEM_TIME AS OFnot in postgres
- └── (r WITHOUT OVERLAPS) aloneneeds at least two columns
checked on postgres 18.6:
- referential actions: a PERIOD foreign key only supports NO ACTION. CASCADE, SET NULL, SET DEFAULT, even RESTRICT, are refused when you create it:
CREATE TABLE departments (id int, valid daterange, PRIMARY KEY (id, valid WITHOUT OVERLAPS));
CREATE TABLE assignments (dept_id int, valid daterange,
FOREIGN KEY (dept_id, PERIOD valid) REFERENCES departments (id, PERIOD valid) ON DELETE CASCADE);- UPDATE ... FOR PORTION OF: the standard's way to change "the price from march 10 to march 20" and have the database split rows around it isn't in 18. you still split versions by hand (or in a trigger), like the history table in history, audit and soft delete.
- system-versioned tables (
FOR SYSTEM_TIME AS OF): not in postgres at all. these keys are about application time, the period a fact is true in the world, which you set yourself. - a key with only the range:
PRIMARY KEY (valid WITHOUT OVERLAPS)is refused ("needs at least two columns"); for a single global timeline, use an exclusion constraint.
keylines
- a temporal key is unique per moment: same key, non-overlapping ranges.
- PRIMARY KEY (k, r WITHOUT OVERLAPS) (18+) is an exclusion constraint you can reference, NOT NULL and no empty ranges.
- PERIOD foreign keys check the child's range is covered by the parent's versions combined.
- 18 supports only NO ACTION for PERIOD keys, and no FOR PORTION OF.
- use half-open ranges so back-to-back periods meet without overlapping.