用户在使用中,可能会用到基于函数的索引,但是函数是非 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

  

函数索引引用的函数必须是immutable类型的更多相关文章

  1. 通过进程检测服务时脚本文件名不要起要检测的服务名字命名 shell程序从上到下执行若定义函数或引用系统函数需先定义 kill -USR2

    通过进程检测服务时脚本文件名不要起要检测的服务名字命名 kill -USR2 `cat /var/run/mysqld.pid`

  2. MySQL5.7 虚拟列实现表达式或函数索引

    MySQL5.7 虚拟列实现表达式或函数索引 http://www.linuxidc.com/Linux/2015-11/125162.htm https://dev.mysql.com/doc/re ...

  3. Python 函数参数引用(传值/传址)/copy/deepcopy

    精简版: 传值:被调函数局部变量改变不会影响主调函数局部变量 传址:被调函数局部变量改变会影响主调函数局部变量 Python参数传递方式:传递对象引用(传值和传址的混合方式),如果是数字,字符串,元组 ...

  4. php中引用&的真正理解-变量引用、函数引用、对象引用

    php的引用(就是在变量或者函数.对象等前面加上&符号) //最重要就是 删除引用的变量 ,只是引用的变量访问不了,但是内容并没有销毁 在PHP 中引用的意思是:不同的名字访问同一个变量内容. ...

  5. Oracle索引梳理系列(六)- Oracle索引种类之函数索引

    版权声明:本文发布于http://www.cnblogs.com/yumiko/,版权由Yumiko_sunny所有,欢迎转载.转载时,请在文章明显位置注明原文链接.若在未经作者同意的情况下,将本文内 ...

  6. MySQL 5.7新特性之Generated Column(函数索引)

    MySQL 5.7引入了Generated Column,这篇文章简单地介绍了Generated Column的使用方法和注意事项,为读者了解MySQL 5.7提供一个快速的.完整的教程.这篇文章围绕 ...

  7. php函数的引用返回

    <?php function &test(){ static $b = 1; $b += 2; return $b; } $a = &test(); $a =8; $c = te ...

  8. PHP引用(&)初探:函数的引用返回

    函数的引用返回 先看代码: <?php function &test() { static $b=0;//申明一个静态变量 $b=$b+1; echo $b; return $b; } ...

  9. c++将引用作为函数的参数---6

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 引用经常被用作函数参数,使得函数中的变量名成为调用程序中的变量别名.这种传递参数 的方法称为按引用传递. ...

随机推荐

  1. Java实现无界面计算器

    ## 要求### 1.四个方法加减乘除### 1.循环加switch### 1.传递2个数源码如下: ``` public class Jisuanqi { public static void ma ...

  2. Spring Boot 整合 minio(一步到位)

    按照这个步骤来,宝贝保你一步到位 一.minio版本安装:这里我安装的新版本 新版本安装 # docker 下载镜像 docker pull minio/minio # 安装镜像 docker run ...

  3. Chrome实现自动化测试:录制回放网页动作

    Chrome 浏览器是真的恐怖,它会把相关的小工具都卷死.从它诞生至今,创造了一个又一个的传奇,现在可以看到基于它的操作系统 chrome os ,还能买到用它做系统的笔记本电脑. 最近,新版本支持录 ...

  4. Vue中关于this指向的问题

    由Vue管理的函数 例如: computed 计算属性 watch 监视属性 filters (Vue3中已弃用且不再支持) 过滤器 .... 上述属性里配置的函数不要采用箭头函数写法,因为箭头函数没 ...

  5. 参数化设计(多次调用同一子模块,critical warning,引脚constraint sources)

    1.设计定义:4个led灯以不同的频率各自闪烁. 2.设计输入:时钟信号,复位信号,led多位输出. 思路:没有要求流水的效果,所以不需要叠加counter达到某一特定值来位移.只需要让每个灯的闪烁周 ...

  6. mybatis-plus详解

    旧的代码生成 记得导包,依赖如下 <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</grou ...

  7. 聊聊 C++ 右值引用 和 移动构造函数

    一: 背景 最近在看 C++ 的右值引用和移动构造函数,感觉这东西一时半会还挺难理解的,可能是没踩过这方面的坑,所以没有那么大的深有体会,不管怎么说,这一篇我试着聊一下. 二: 右值引用 1. 它到底 ...

  8. Collection集合和Collection的常用功能

    boolean add(E e); 向集合里添加元素 boolean remove(E e); 删除集合中的某个元素 void clear(); 清空集合的所有元素 boolean contains( ...

  9. Vue ref属性 && props配置项

    1 // # ref属性: 2 // # 1.用来给元素或者子组件注册引用信息(id的替代者) 3 // # 2.应用在html标签上获取的是真实的DOM元素,应用在组件标签上是组件实例对象(vc) ...

  10. 5.4 NOI模拟

    \(5.4\ NOI\)模拟 \(T1\) 想到分讨,但是暴力输出一下方案之后有很多特别的情况要讨论,就弃了... 假设\(a\)是原序列,\(b\)是我们得到的序列 设\(i\)是最长公共前缀,\( ...