Postgres- "ERROR: generation expression is not immutable" why is my expression not immutable?

Postgres- "ERROR: generation expression is not immutable" why is my expression not immutable?

我正在尝试为 Postgres 中的文本搜索创建索引,但在创建生成的 tsvector 列时我总是收到错误消息。

ERROR:  generation expression is not immutable
SQL state: 42P17

我有一个文本“标题”列和文本[]“作者”列。我正在尝试将两者结合起来创建一个 tsvector 列

这是出现错误的代码

ALTER TABLE book
    ADD COLUMN tscol tsvector
        GENERATED ALWAYS AS (to_tsvector(title || ' ' || immutable_array_to_string(coalesce(authors, '{}'), ' '))) STORED;

immutable_array_to_string 函数的代码:

CREATE OR REPLACE FUNCTION immutable_array_to_string(text[], text) 
    RETURNS text as $$ SELECT array_to_string(, ); $$ 
LANGUAGE sql IMMUTABLE;

您在调用 to_tsvector 时没有 regconfig,因此使用默认值。在这种情况下,函数只有 stable。如果你想让它不可变,你必须通过 regconfig.

to_tsvector('english',title || ' ' || immutable_array_to_string(coalesce(authors, '{}'), ' ')) 

PS:可以调用\df+ to_tsvector查看不同签名函数的波动率。