Quettabyte
Gist | Mastodon | Threads
WITHOUT OVERLAPS Temporal Contraints Postgres 19
SQL
CREATE EXTENSION btree_gist;

CREATE TABLE price_book(
  PRIMARY KEY (
    product_code
    , price_validity 
        WITHOUT OVERLAPS
    ),

  product_code integer
  , price float
  , price_validity daterange 
      NOT NULL

);

INSERT INTO price_book
VALUES
(10, 1, daterange('2026-01-01', '2026-03-31'))
, (10, 1, daterange('2026-03-01', '2026-04-13'))
; -- failed due to overlapping time periods

/*
ERROR:  conflicting key value violates exclusion constraint "price_book_pkey"
DETAIL:  Key (product_code, price_validity)=(10, [2026-03-01,2026-04-13)) conflicts with existing key (product_code, price_validity)=(10, [2026-01-01,2026-03-31)).
*/
Created on Fri, 28 Aug 2026 09:00:31 +0000 | URL: https://quettabyte.net/gist.php?id=5HkFD

Code screenshot

WITHOUT OVERLAPS Temporal Contraints Postgres 19 - Pinterest Image

« Create Another Gist