排除约束 `EXCLUDE USING gist (c WITH &&)` 是什么意思?

What does exclusion constraint `EXCLUDE USING gist (c WITH &&)` mean?

来自 PostgreSQL 文档

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 &&)
);

我想知道 EXCLUDE USING gist (c WITH &&) 是什么意思?特别是 gist()c WITH &&EXCLUDE USING

可以用check改写吗?谢谢。

CHECK 约束基于 table 的单行计算表达式,而 EXCLUDE 约束计算 table 中两行的比较。把它想象成一个广义的 UNIQUE 约束:而不是 "no two rows can be equal",你可以说 "no two rows overlap",甚至 "no two rows can be different".

为了在不检查所有可能的值组合的情况下实现这一点,它需要一个适当的索引结构,以允许它在您插入或更新行时找到可能的违规行为。这就是声明的 gist 部分所指的: a particular type of index 可用于加速除相等以外的操作。

声明的其余部分是约束本身:c 是被测试的列,&& 是运算符,对于任何一对行都不能 return 为真。在这种情况下,&& 是 "overlaps" 运算符 as listed on the geometric operators manual page.

所以放在一起,约束 EXCLUDE USING gist (c WITH &&) 转换为 "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"。