Exclusion Constraints
Constraints that prevent any two rows from having a combination of values that satisfy a specified operator, enabling overlap prevention beyond simple equality.
EXCLUDE EXCLUDE USING exclusion constraint overlap constraint GiST exclusion
constraint exclusion overlap GiST range PostgreSQL
| Database | Status | Syntax Family | Min Version |
|---|---|---|---|
| PostgreSQL | full | sql-standard | - |
| MySQL | none | - | - |
| MariaDB | none | - | - |
| Oracle | none | - | - |
| SQL Server | none | - | - |
| SQLite | none | - | - |
| Redshift | none | - | - |
| DB2 | none | - | - |
| Snowflake | none | - | - |
| BigQuery | none | - | - |
Compare Databases
Database Details
PostgreSQL
full
-- Prevent double-booking of a room for overlapping time periods
-- Requires: CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE TABLE bookings (
room_id INT,
during TSRANGE,
CONSTRAINT no_overlapping_bookings
EXCLUDE USING gist (
room_id WITH =,
during WITH &&
)
);
-- Partial exclusion constraint
CREATE TABLE appointments (
doctor_id INT,
slot TSRANGE,
cancelled BOOLEAN DEFAULT FALSE,
CONSTRAINT no_double_booking
EXCLUDE USING gist (doctor_id WITH =, slot WITH &&)
WHERE (NOT cancelled)
);- Backed by a GiST index; mixing scalar types with range types needs the btree_gist extension (shipped with PostgreSQL).
- A WHERE clause narrows the constraint to a subset of rows (partial exclusion).
- USING can target SP-GiST or other index types when the operator class supports them.
MySQL
none
- Not supported. Use BEFORE INSERT / BEFORE UPDATE triggers to reject overlapping rows.
Docs: MySQL Constraints
MariaDB
none
- Not supported. Use BEFORE-triggers or application code to prevent overlaps.
Docs: Constraints
Oracle
none
- Not supported. Use BEFORE-triggers, or encode overlap rules in a materialized view with a UNIQUE constraint on the conflict key.
Docs: Constraints
SQL Server
none
- Not supported. Filtered unique indexes cover equality-style exclusion but not range overlap. Use INSTEAD OF triggers or application-level locking for the overlap case.
Redshift
none
- Not supported, and no triggers either. Enforce overlap rules in the application or ETL.
Docs: Defining Constraints
Snowflake
none
- Not supported, and no triggers either. Enforce overlap rules upstream.
Docs: Constraints
BigQuery
none
- Not supported, and no triggers either. Enforce overlap rules upstream.