uuid数组上的PostgreSQL GIN index



我想在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.

如何添加必要的运算符类以使其正常工作?

请注意,对于类型 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;

感谢它为我指明了正确的方向。

相关文档在索引的接口扩展中,特别是那里描述了 GIN 的运算符策略和函数编号。

从PostgreSQL 10开始,不再需要自定义运算符类_uuid_ops,因为现在anyarry上有一个通用的内置opclass array_ops(参见:https://www.postgresql.org/docs/current/static/gin-builtin-opclasses.html)

最新更新