arrays - 在 PostgreSQL 中索引自定义范围类型的数组

标签 arrays database postgresql indexing custom-type

我创建了一个自定义范围类型:

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对于范围数组。 (GiSTSP-GiST 也没有运算符类)。

您应该尝试重新构建您的数据,例如。成一对多关系,其中每个 vendors 行可以在(比方说)vendor_ranges 中有多个条目,您可以在其中使用简单的范围(而不是数组)其中)。在这种情况下,您可以为您的范围建立索引。

关于arrays - 在 PostgreSQL 中索引自定义范围类型的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465187/

相关文章:

php - 在数组内部创建 php json 对象数组

javascript - 如何在 JavaScript 的数组列表中找到最大的数组值

javascript - 根据字符串对对象数组进行排序

mysql - 两个 MySQL 表的复杂连接

C# switch语句查询MYSQL数据库

c - 数组中有符号和无符号字符的索引有什么区别?

sql - 修改数据库时的特殊情况

ruby-on-rails - 使用 rails 的 heroku pg 错误,说 "text"不允许使用类型修饰符

postgresql - 在 Postgres 中按天分组

postgresql - Stream、Option 上的模式匹配和创建元组