Postgres constraint for unique datetime range
问题 My table has two columns: startsAt endsAt Both hold date and time. I want to make following constraint: IF both columns are NOT NULL then range between startsAt and endsAt must not overlap with other ranges (from other rows). 回答1: You can keep your separate timestamp columns and still use an exclusion constraint on an expression: CREATE TABLE tbl ( tbl_id serial PRIMARY KEY , starts_at timestamp , ends_at timestamp , EXCLUDE USING gist (tsrange(starts_at, ends_at) WITH &&) -- no overlapping )