我创建了一个自定义范围类型:
CREATE OR REPLACE FUNCTION smallint_subtype_diff(x smallint, y smallint) RETURNS float8 AS
'SELECT (x - y)::float8' LANGUAGE sql STRICT IMMUTABLE;
CREATE TYPE smallintrange AS RANGE (
subtype = smallint,
subtype_opclass = int2_ops,
subtype_diff = smallint_subtype_diff -- required by GiST
);
它工作正常,我可以使用 GiST 对其进行索引。
但是,我现在希望索引这种类型的数组:smallintrange[]
无论我尝试 GiST 还是 GIN,我都会得到:
CREATE INDEX ix_Vendors_OpenTimes ON public.Vendors USING gin (OpenTimes);
ERROR: data type smallintrange[] 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.
如何让它发挥作用?
最佳答案
当您尝试对任何内置范围类型 f.ex 建立索引时,会引发相同的错误消息。 int4range
。这是因为没有built-in GIN operator class对于范围数组。 (GiST 或 SP-GiST 也没有运算符类)。
您应该尝试重新构建您的数据,例如。成一对多关系,其中每个 vendors
行可以在(比方说)vendor_ranges
中有多个条目,您可以在其中使用简单的范围(而不是数组)其中)。在这种情况下,您可以为您的范围建立索引。
关于arrays - 在 PostgreSQL 中索引自定义范围类型的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465187/