问题
From PostgreSQL document
Exclusion constraints ensure that if any two rows are compared on the specified columns or expressions using the specified operators, at least one of these operator comparisons will return false or null. The syntax is:
CREATE TABLE circles ( c circle, EXCLUDE USING gist (c WITH &&) );
I was wondering what EXCLUDE USING gist (c WITH &&)
means? In particular, gist()
, c WITH &&
and EXCLUDE USING
.
Can it be rewritten in terms of check
? Thanks.
回答1:
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".
来源:https://stackoverflow.com/questions/51247063/what-does-exclusion-constraint-exclude-using-gist-c-with-mean