lightdb中的函数可变性属性及ERROR: functions in index expression must be marked IMMUTABLE解决
在postgresql中,函数有个不稳定性分类属性,它会影响优化器评估函数的可优化级别、同时也会影响postgresql中并行执行的可行性。
函数有三种类型:VOLATILE、
STABLE以及
IMMUTABLE。VOLATILE是函数默认类别,也就是优化器假设函数会修改数据库,不会做任何特定的优化。STABLE可用于基于函数的索引扫描。IMMUTABLE通常用于静态常量的优化。对于一些短小逻辑的函数,典型的是数据字典翻译,但是写在SQL中会导致很复杂的逻辑,应该在创建函数时声明为STABLE。一般来说IMMUTABLE不是特别必要,更像是resultset_cache的实现。如下:
lightdb@postgres=# CREATE INDEX idx1_lem_db_log ON lem_db_log USING gin(to_tsquery(concat('field1='|| field1,' field2=' || field2,' field3=' || field3,' field4=' || field4,' field5='||field5))); ERROR: functions in index expression must be marked IMMUTABLE 可见在函数索引中,表达式必须为不可变的。
要修改系统函数的函数属性,可通过alter table函数签名进行修改,如下:
lightdb@postgres=# \df+ concat() List of functions Schema | Name | Result data type | Argument data types | Type | Volatility | Parallel | Owner | Security | Access privileges | Language | Source code | Description ------------+--------+------------------+---------------------+------+------------+----------+---------+----------+-------------------+----------+-------------+-------------------- pg_catalog | concat | text | VARIADIC "any" | func | stable | safe | lightdb | invoker | | internal | text_concat | concatenate values (1 row) lightdb@postgres=# alter function pg_catalog.concat(variadic "any") immutable; -- 任何长度、任意类型的参数 ALTER FUNCTION
lightdb@postgres=# CREATE INDEX idx1_lem_db_log ON lem_db_log USING gin(to_tsquery(concat('field1='|| field1,' field2=' || field2,' field3=' || field3,' field4=' || field4,' field5='||field5))); ERROR: functions in index expression must be marked IMMUTABLE lightdb@postgres=# \df+ to_tsvector() List of functions Schema | Name | Result data type | Argument data types | Type | Volatility | Parallel | Owner | Security | Access privileges | Language | Source code | Description ------------+-------------+------------------+---------------------+------+------------+----------+---------+----------+-------------------+----------+-------------------------------+-------- ---------------------------------------- pg_catalog | to_tsvector | tsvector | json | func | stable | safe | lightdb | invoker | | internal | json_string_to_tsvector | transfo rm string values from json to tsvector pg_catalog | to_tsvector | tsvector | jsonb | func | stable | safe | lightdb | invoker | | internal | jsonb_string_to_tsvector | transfo rm string values from jsonb to tsvector pg_catalog | to_tsvector | tsvector | regconfig, json | func | immutable | safe | lightdb | invoker | | internal | json_string_to_tsvector_byid | transfo rm string values from json to tsvector pg_catalog | to_tsvector | tsvector | regconfig, jsonb | func | immutable | safe | lightdb | invoker | | internal | jsonb_string_to_tsvector_byid | transfo rm string values from jsonb to tsvector pg_catalog | to_tsvector | tsvector | regconfig, text | func | immutable | safe | lightdb | invoker | | internal | to_tsvector_byid | transfo rm to tsvector pg_catalog | to_tsvector | tsvector | text | func | stable | safe | lightdb | invoker | | internal | to_tsvector | transfo rm to tsvector (6 rows) 多个重载的时候,要找相同的那个重载实现。
修改之后,函数索引即可创建,如下:
lightdb@postgres=# CREATE INDEX idx1_lem_db_log ON lem_db_log USING gin(to_tsvector('simple',concat('field1='|| field1,' field2=' || field2,' field3=' || field3,' field4=' || field4,' field5='||field5))); CREATE INDEX
CREATE FUNCTION dup(in int, out f1 int, out f2 text) AS $$ SELECT $1, CAST($1 AS text) || ' is text' $$ LANGUAGE sql IMMUTABLE ; explain analyze SELECT dup(42) from (select 1 from pg_catalog.generate_series(1, 1000000));
除了IMMUTABLE,还有一个可选的STRICT修饰符。
CREATE [ OR REPLACE ] FUNCTION name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] ) [ RETURNS rettype | RETURNS TABLE ( column_name column_type [, ...] ) ] { LANGUAGE lang_name | TRANSFORM { FOR TYPE type_name } [, ... ] | WINDOW | IMMUTABLE | STABLE | VOLATILE | [ NOT ] LEAKPROOF | CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT | [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER | PARALLEL { UNSAFE | RESTRICTED | SAFE } | COST execution_cost | ROWS result_rows | SUPPORT support_function | SET configuration_parameter { TO value | = value | FROM CURRENT } | AS 'definition' | AS 'obj_file', 'link_symbol' } ...
声明为STRICT或RETURNS NULL ON NULL INPUT表示如果任意入参是NULL,则总是返回NULL。否则NULL需要由函数编写者处理。
http://www.light-pg.com/docs/lightdb/13.3-22.2/xfunc-volatility.html
http://www.light-pg.com/docs/lightdb/13.3-22.2/sql-createfunction.html
http://www.light-pg.com/docs/lightdb/13.3-22.2/xfunc-pl.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2016-07-09 j2ee分布式缓存同步实现方案dlcache