Whereas a CHECK
constraint evaluates an expression based on a single row of the table, an EXCLUDE
constraint evaluates a comparison of two rows in the table. Think of it like a generalised UNIQUE
constraint: instead of "no two rows can be equal", you can say things like "no two rows overlap", or even "no two rows can be different".
In order to achieve this without checking every possible combination of values, it needs an appropriate index structure which allows it to find possible violations when you insert or update a row. This is what the gist
part of the declaration refers to: a particular type of index which can be used to speed up operations other than equality.
The remainder of the declaration is the constraint itself: c
is the column being tested, and &&
is the operator which must not return true for any pair of rows. In this case, &&
is the "overlaps" operator as listed on the geometric operators manual page.
So put together, the constraint EXCLUDE USING gist (c WITH &&)
translates to "no two values of c
must overlap each other (more precisely, A.c && B.c
must return false or null for all distinct rows A
and B
), and please use a gist
index to monitor this constraint".
<>
to ensure that all rows match. – Torietorii