skip to content

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.

booking =
bookings.sql
  1. 1INSERT INTO bookings VALUES (101, 'edsger', '[2026-03-04,2026-03-06)');
ERROR: conflicting key value violates exclusion constraint "bookings_pkey"SQLSTATE 23P01

edsger wants room 101 from march 4 to 6. that overlaps both stays, and the key refuses it.

real output, postgres 18.6 (PGlite 0.5.12) · temporal
room 101 has ada until march 5 and grace from march 5. pick a booking to try. an overlapping stay in the same room is refused; the same dates in another room, or a stay that starts the day the last one ends, go in. empty ranges are refused outright. all real, on the same postgres 18.6 that runs this page.

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:

sqlempty db · ext=btree_gist
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';
output, from postgres 18.6
#1 · CREATE TABLE
#2 · SELECT 1
connamenamecontypecharconperiodbooldeftext
bookings_pkeyptPRIMARY KEY (room, during WITHOUT OVERLAPS)
(1 row)
#3 · SELECT 1
amnamename
gist
(1 row)

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):

sqlempty db
CREATE TABLE bookings (room int, during daterange, PRIMARY KEY (room, during WITHOUT OVERLAPS));
output, from postgres 18.6
ERROR 42704: data type integer has no default operator class for access method "gist"
HINT: You must specify an operator class for the index or define a default operator class for the data type.

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:

sqlempty db · ext=btree_gist
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;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · CREATE TABLE
#3 · INSERT 0 2
#4 · INSERT 0 1
#5 · SELECT 3
whotextwhattextlowerdateupperdate
dept 7research2026-01-012026-04-01
adain dept 72026-02-012026-05-01
dept 7research and data2026-04-012026-07-01
(3 rows)
the PERIOD check, drawn from the block above. department 7 exists from january to july in two versions. ada's assignment is covered by the two together, so it's accepted. grace's, from june to august, runs past the last version: refused, in the next block.
sqlempty db · ext=btree_gist
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';
output, from postgres 18.6
#1 · CREATE TABLE
#2 · CREATE TABLE
#3 · INSERT 0 2
#4 · ERROR 23503
ERROR 23503: insert or update on table "assignments" violates foreign key constraint "assignments_dept_id_valid_fkey"
DETAIL: Key (dept_id, valid)=(7, [2026-06-01,2026-08-01)) is not present in table "departments".
#5 · INSERT 0 1
#6 · ERROR 23503
ERROR 23503: update or delete on table "departments" violates foreign key constraint "assignments_dept_id_valid_fkey" on table "assignments"
DETAIL: Key (id, valid)=(7, [2026-01-01,2026-04-01)) is still referenced from table "assignments".

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.

predict the output
sql
-- 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
temporal support in postgres 18.6: tested on this page, plus what the 18 manual leaves out. live runs; dead is refused or missing.

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:
sqlempty db · ext=btree_gist
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);
output, from postgres 18.6
#1 · CREATE TABLE
#2 · ERROR 0A000
ERROR 0A000: unsupported ON DELETE action for foreign key constraint using PERIOD
  • 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.