用户在使用中,可能会用到基于函数的索引,但是函数是非 immutable 类型的,导致函数索引无法创建。如:

test=# create index ind_t1 on t1(to_char(create_date,'yyyy-mm'));
ERROR: functions in index expression must be marked IMMUTABLE

这里我们先看下函数的类型:

test=# \df+ to_char
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_char | text | bigint, text | func | stable | safe | system | invoker | | internal | int8_to_char | format int8 to text
pg_catalog | to_char | text | double precision, text | func | stable | safe | system | invoker | | internal | float8_to_char | format float8 to text
pg_catalog | to_char | text | integer, text | func | stable | safe | system | invoker | | internal | int4_to_char | format int4 to text
pg_catalog | to_char | text | interval, text | func | stable | safe | system | invoker | | internal | interval_to_char | format interval to text
pg_catalog | to_char | text | numeric, text | func | stable | safe | system | invoker | | internal | numeric_to_char | format numeric to text
pg_catalog | to_char | text | real, text | func | stable | safe | system | invoker | | internal | float4_to_char | format float4 to text
pg_catalog | to_char | text | timestamp without time zone, text | func | stable | safe | system | invoker | | internal | timestamp_to_char | format timestamp to text
pg_catalog | to_char | text | timestamp with time zone, text | func | stable | safe | system | invoker | | internal | timestamptz_to_char | format timestamp with time zone to text
pg_catalog | to_char | text | tinyint | func | immutable | safe | system | invoker | | sql | select cast($1 as text) | convert tinyint to text
pg_catalog | to_char | text | tinyint, text | func | stable | safe | system | invoker | | c | tinyint_to_char | format tinyint to text
sys | to_char | text | bigint | func | immutable | safe | system | invoker | | sql | select to_char($1::text); |
sys | to_char | text | boolean | func | stable | unsafe | system | invoker | | sql | select cast($1 as text); +|
| | | | | | | | | | | |
sys | to_char | text | boolean, text | func | stable | unsafe | system | invoker | | sql | select text($1); +|
| | | | | | | | | | | |
sys | to_char | text | integer | func | immutable | safe | system | invoker | | sql | select to_char($1::text); |
sys | to_char | text | smallint | func | immutable | safe | system | invoker | | sql | select to_char($1::text); |
sys | to_char | text | text | func | immutable | safe | system | invoker | | plpgsql | +|
| | | | | | | | | | | begin +|
| | | | | | | | | | | return $1; +|
| | | | | | | | | | | end; |
sys | to_char | text | text, text | func | immutable | safe | system | invoker | | sql | select to_char($1::numeric,$2); |
sys | to_char | text | timestamp without time zone | func | immutable | safe | system | invoker | | c | ora_to_char_timestamp | Convert timestamp to string
(18 rows)

我们在看下对象的数据类型:

test=# \d t1
Table "public.t1"
Column | Type | Collation | Nullable | Default
-------------+---------+-----------+----------+---------
id | integer | | |
create_date | date | | |

可以看到对象的 类型是date,再根据date类型寻找具体的to_char 函数,并修改为immutable

test=# alter function to_char(timestamp without time zone, text) immutable;
ALTER FUNCTION

再创建索引:

test=# create index ind_t1 on t1(to_char(create_date,'yyyy-mm'));
CREATE INDEX

  

最新文章

  1. python计算器
  2. SharePoint 2013 对二进制大型对象(BLOB)进行爬网
  3. bzoj 1305 dance跳舞
  4. 图片懒加载插件lazyload使用方法
  5. hdu1166敌兵布阵_线段树单点更新
  6. (easy)LeetCode 242.Valid Anagram
  7. Hbse的读写过程
  8. HTML5 文件域+FileReader 读取文件(一)
  9. mysql导入excel数据
  10. RPC 实现
  11. Maven命令行创建web项目,并部署到jobss当中(解决No plugin found for prefix 'jboss-as' in the current project and in the plugin groups [org.apache.maven.plugins,问题)
  12. BZOJ3224/洛谷P3391 - 普通平衡树(Splay)
  13. no system images installed for this target这个问题如何解决?
  14. [Swift]LeetCode961. 重复 N 次的元素 | N-Repeated Element in Size 2N Array
  15. js中提示框闪退问题
  16. Windows安装diango框架<一>
  17. PHP json_encode函数中需要注意的地方
  18. 46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)
  19. 【转】Linux 如何通过命令仅获取IP地址
  20. vsphere中虚机的cpu热插拔和内存热添加

热门文章

  1. C#判断数组或集合中是否含有属性值为value的对象
  2. Android (微信扫码登录) 获取微信二维码+扫码登录
  3. CODING DevOps 助力中化信息打造新一代研效平台,驱动“线上中化”新未来
  4. Pyinstaller打包pikepdf失败的问题排查
  5. 零基础学Java(3)运算符
  6. NuGetTools:批量上传、删除和显示NuGet包
  7. 挑战30天写操作系统-day1-从计算机结构到汇编程序入门
  8. angular 变化检测和ngZone
  9. websocket心跳实现
  10. odoo 14 Debug 调试