无法在 Postgres (9.3) 索引中使用 concat 函数
Can't use concat function in Postgres (9.3) index
我正在为多个文本列创建索引(在 Postgres 9.3 中),我想使用 concat
,例如:
CREATE INDEX
ON my_table
USING gin (to_tsvector('english', concat(title, ' ', description)))
但是,当我尝试这样做时,出现以下错误:
ERROR: functions in index expression must be marked IMMUTABLE
使用 ||
运算符的普通连接工作正常。但是,我更喜欢使用 concat
,因为 description
可能是 NULL
,并且 ||
运算符似乎将任何与 NULL
的连接转换为 NULL
.
如果我理解正确,这意味着 concat
没有标记为不可变,我不明白这一点。
当然,我可以只coalesce
所有可为空的列,但感觉不够优雅。最重要的是,我很好奇为什么我不能在索引中使用 concat
?
Tom Lane 在 this post 中解释了 CONCAT
不是 IMMUTABLE
的原因:
concat() invokes datatype output functions, which are not necessarily
immutable. An easy example is that timestamptz_out's results depend
on the TimeZone setting.
即这是因为它将接受 non-text 输入,在转换为文本时可能会根据会话设置而改变。
您可能必须为此推出自己的功能。
我正在为多个文本列创建索引(在 Postgres 9.3 中),我想使用 concat
,例如:
CREATE INDEX
ON my_table
USING gin (to_tsvector('english', concat(title, ' ', description)))
但是,当我尝试这样做时,出现以下错误:
ERROR: functions in index expression must be marked IMMUTABLE
使用 ||
运算符的普通连接工作正常。但是,我更喜欢使用 concat
,因为 description
可能是 NULL
,并且 ||
运算符似乎将任何与 NULL
的连接转换为 NULL
.
如果我理解正确,这意味着 concat
没有标记为不可变,我不明白这一点。
当然,我可以只coalesce
所有可为空的列,但感觉不够优雅。最重要的是,我很好奇为什么我不能在索引中使用 concat
?
Tom Lane 在 this post 中解释了 CONCAT
不是 IMMUTABLE
的原因:
concat() invokes datatype output functions, which are not necessarily immutable. An easy example is that timestamptz_out's results depend on the TimeZone setting.
即这是因为它将接受 non-text 输入,在转换为文本时可能会根据会话设置而改变。
您可能必须为此推出自己的功能。