简体   繁体   English

在已转换为数组的文本列上创建postgresql索引

[英]Create postgresql index on text column casted to array

I have a postgresql table that has a column with data type = 'text' in which I need to create an index which involves this column being type casted to integer[]. 我有一个postgresql表,其中有一个数据类型为'text'的列,我需要在其中创建一个索引,该索引涉及此类的类型被转换为integer []。 However, whenever I try to do so, I get the following error: 但是,每当我尝试这样做时,我都会收到以下错误:

ERROR: functions in index expression must be marked IMMUTABLE 错误:索引表达式中的函数必须标记为IMMUTABLE

Here is the code: 这是代码:

create table test (a integer[], b text);

insert into test values ('{10,20,30}','{40,50,60}');

CREATE INDEX index_test on test USING GIN ((   b::integer[]      ));

Note that one potential workaround is to create a function that is marked as IMMUTABLE that takes in a column value and performs the type casting within the function, but the problem (aside from adding overhead) is that I have many different 'target' array data types (EG: text[], int2[], int4[], etc...), and it would not be possible to create a separate function for each potential target array data type. 请注意,一个可能的解决方法是创建一个标记为IMMUTABLE的函数,该函数接收列值并在函数内执行类型转换,但问题(除了增加开销)是我有许多不同的“目标”数组数据类型(EG:text [],int2 [],int4 []等...),并且不可能为每个潜在的目标数组数据类型创建单独的函数。

Answered in this thread on the PostgreSQL mailing lists. PostgreSQL邮件列表的这个帖子中回答。 Click on "Follow-ups" or "next by thread" in the links after the post to follow the (short) thread on the topic. 点击帖子后链接中的“后续”或“下一个帖子”,跟随主题的(简短)主题。

There's no recipe given there, but Tom's just talking about defining an explicit cast from text[] to integer[] . 那里没有给出配方,但是汤姆只是在谈论从text[]integer[]定义一个显式的强制转换。 If time permits I'll flesh this answer out with an example. 如果时间允许,我会用一个例子来解释这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM