我想在
uuid[]
上使用 GIN 索引(对 uuid 数组进行有效的成员资格测试)。然而,当我尝试时,PostgreSQL 给了我一个错误:
mydb=> CREATE TABLE foo (val uuid[]);
CREATE TABLE
mydb=> CREATE INDEX foo_idx ON foo USING GIN(val);
ERROR: data type uuid[] has no default operator class for access method "gin"
HINT: You must specify an operator class for the index or define a default operator class for the data type.
如何添加必要的运算符类才能使其正常工作?
请注意,this是类型
citext
的类似问题,但提供的答案不起作用。
注意:这个答案已经过时,因为它现在是标准 PostgreSQL 的一部分,请参阅 tbussmann 的其他答案(你应该投票)。
原答案:
这可以使用以下运算符类来完成:
CREATE OPERATOR CLASS _uuid_ops DEFAULT
FOR TYPE _uuid USING gin AS
OPERATOR 1 &&(anyarray, anyarray),
OPERATOR 2 @>(anyarray, anyarray),
OPERATOR 3 <@(anyarray, anyarray),
OPERATOR 4 =(anyarray, anyarray),
FUNCTION 1 uuid_cmp(uuid, uuid),
FUNCTION 2 ginarrayextract(anyarray, internal, internal),
FUNCTION 3 ginqueryarrayextract(anyarray, internal, smallint, internal, internal, internal, internal),
FUNCTION 4 ginarrayconsistent(internal, smallint, anyarray, integer, internal, internal, internal, internal),
STORAGE uuid;
感谢this为我指明了正确的方向。
相关文档位于索引的接口扩展,特别是其中描述了 GIN 的操作策略和函数编号。
从 PostgreSQL 10 开始,不再需要自定义运算符类
_uuid_ops
,因为现在 array_ops
上有一个通用的内置操作类 anyarry
(请参阅:https://www.postgresql.org/docs/current /gin.html#GIN-BUILTIN-OPCLASSES)