【问题标题】:Postgres- "ERROR: generation expression is not immutable" why is my expression not immutable?Postgres-“错误:生成表达式不是不可变的”为什么我的表达式不是不可变的?
【发布时间】:2021-08-10 18:23:40
【问题描述】:

我正在尝试为 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($1, $2); $$ 
LANGUAGE sql IMMUTABLE;

【问题讨论】:

    标签: postgresql full-text-search immutability


    【解决方案1】:

    您调用to_tsvector 时没有regconfig,因此使用默认值。在这种情况下,函数只有stable。如果你希望它是不可变的,你必须传递regconfig

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

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

    【讨论】:

    • 谢谢!!!我现在看到了我的错误。也感谢 \df+ to_tsvector 提示 :)))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 2011-06-16
    • 1970-01-01
    相关资源
    最近更新 更多