开发者

Create postgresql index on text column casted to array

开发者 https://www.devze.com 2023-03-30 07:40 出处:网络
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[]. However, whenever I tr开发者_StackOv

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[]. However, whenever I tr开发者_StackOverflow中文版y to do so, I get the following error:

ERROR: functions in index expression must be marked 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.


Answered in this thread on the PostgreSQL mailing lists. 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[]. If time permits I'll flesh this answer out with an example.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号