在创建函数索引时遇到报错,报错信息即为标题,下面是详细信息。

1 表定义

1
2
3
4
5
6
7
skytf=> \d test_39;  
Table "skytf.test_39"
Column | Type | Modifiers
-------------+--------------------------+-----------
skyid | integer |
create_time | timestamp with time zone |
name | character varying(32) |

现在需要在字段 “skyid”, “create_time” 上创建联合索引。

2 创建索引

1
2
skytf=> create index CONCURRENTLY idx_test_skyid_ctime on test_39 using btree (skyid, to_char(create_time, 'YYYY-MM-DD') );  
ERROR: functions in index expression must be marked IMMUTABLE

创建函数索引报错,”functions in index expression must be marked IMMUTABLE” ,意思为建立函数索引时 函数必须标识为 “IMMUTABLE”。

3 查看 to_char 函数

1
2
3
4
5
6
7
8
9
10
11
12
13
skytf=> \df to_char();  
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+---------+------------------+-----------------------------------+--------
pg_catalog | to_char | text | bigint, text | normal
pg_catalog | to_char | text | double precision, text | normal
pg_catalog | to_char | text | integer, text | normal
pg_catalog | to_char | text | interval, text | normal
pg_catalog | to_char | text | numeric, text | normal
pg_catalog | to_char | text | real, text | normal
pg_catalog | to_char | text | timestamp with time zone, text | normal
pg_catalog | to_char | text | timestamp without time zone, text | normal
(8 rows)

4 以 postgres 超级用户连接,修改 to_char 函数属性

1
2
skytf=# alter function to_char(timestamp with time zone, text) IMMUTABLE;  
ALTER FUNCTION

备注:由于表 test_39 上的列 create_time 类型为 “timestamp with time zone” , 所以修改函数时应该修改函数 to_char(timestamp with time zone, text),为了安全己见,不要直接修改 to_char 函数,建议新建一个 IMMUTABLE 属性的 to_char_immutable 函数。

5 验证是否生效

1
2
3
4
5
6
skytf=> \ef to_char(timestamp with time zone, text)
CREATE OR REPLACE FUNCTION pg_catalog.to_char(timestamp with time zone, text)
RETURNS text
LANGUAGE internal
IMMUTABLE STRICT
AS $function$timestamptz_to_char$function$

从“IMMUTABLE STRICT” 中可以看出,函数已经修改成 “IMMUTABLE”属性。

6 以 skytf 连接, 再次创建索引

1
2
skytf=> create index CONCURRENTLY idx_test_skyid_ctime on test_39 using btree (skyid, to_char(create_time, 'YYYY-MM-DD') );  
CREATE INDEX

备注:在修改函数 to_char(timestamp with time zone, text) 属性后,创建索引就成功了。

1
2
3
4
5
6
7
8
9
skytf=> \d test_39  
Table "skytf.test_39"
Column | Type | Modifiers
-------------+--------------------------+-----------
skyid | integer |
create_time | timestamp with time zone |
name | character varying(32) |
Indexes:
"idx_test_skyid_ctime" btree (skyid, to_char(create_time, 'YYYY-MM-DD'::text))

7 手册上关于 “IMMUTABLE” 属性解释

IMMUTABLE indicates that the function cannot modify the database and always returns the same result when given the same argument values; that is, it does not do database lookups or otherwise use information not directly present in its argument list. If this option is given, any call of the function with all-constant arguments can be immediately replaced with the function value.

8 总结
函数的默认属性为 “VOLATILE”, 即可变的,在创建函数索引时,需要将引用函数的属性改为”IMMUTABLE”, 即稳定的,函数索引才能创建成功。也就是说,只有属性为稳定的函数,才能用来创建函数索引。

ERROR: Functions in index expression must be marked IMMUTABLE的更多相关文章

  1. Error: Error setting TTL index on collection : sessions

    Error: Error setting TTL index on collection : sessions 一.步骤一: 这个问题一般是直接升级 mongodb和connect-mongo的版本为 ...

  2. Solve Error Debug Assertion Failed Expression vector iterators incompatible Using PCL in Release Mode of VS2010

    When using PCL 1.4.0 in the release mode building under VS2010, we might sometime get the error &quo ...

  3. 解决报错error the @annotation pointcut expression is only supported at Java 5

    eclipse搭建环境后报错 error the @annotation pointcut expression is only supported at Java 5 错误意思大致是:注释切入点表达 ...

  4. 编译时错误之 error C2338: tuple_element index out of bounds

    part 1 编译器 vs2015 VC++. 完整的错误信息粘贴如下: d:\program files (x86)\microsoft visual studio 14.0\vc\include\ ...

  5. Type Syntax error, insert ")" to complete Expression

      今天倒持了 几个小时!    愣是 没有明确 ,为什么我的JSP的第一行没有代码?  还是报错!   错误是: Description Resource Path Location Type Sy ...

  6. bullet, iOS真机编译错误error: identifier or immediate expression expected解决方法

    刚才发现c3dEngine2(http://git.oschina.net/wantnon2/c3dEngine2 或 https://github.com/wantnon2/c3dEngine2)的 ...

  7. ERROR 1709 (HY000): Index column size too large. The maximum column size is 767 bytes.

    MySQL版本5.6.35 在一个长度为512字符的字段上创建unique key报错 CREATE DATABASE dpcs_metadata DEFAULT CHARACTER SET utf8 ...

  8. C++ error:Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlock)

    Debug Assertion Failed.Expression:_BLOCK_TYPE_IS_VALID(phead->nBlockUse) 关于上面这个错误,我在上一篇文章中的程序遇到过了 ...

  9. error the @annotation pointcut expression is only supported at Java 5 compliance

    今天碰到这个错误,在网上找了下,是因为aspectjweaver.jar用的是1.5.3 本地eclipse的jdk版本为1.7 下载高版本的aspectjweaver.jar会解决此问题 http: ...

  10. digestmd5.c:4037:15: error: #elif with no expression

    执行如下:sed -i.bak 's/#elif WITH_DES/#elif defined(WITH_DES)/' \ plugins/digestmd5.c

随机推荐

  1. 基于EasyCode定制Mybatisplus全自动单表实现:新增/批量新增/修改/批量删除/分页查询/ID查询

    基于EasyCode定制Mybatisplus全自动单表实现CRUD接口 分页查询 ID查询 新增 批量新增 修改 批量删除 注意使用了MybatisPlus的自动填充功能,和insertBatchS ...

  2. Django路由层之路由分发 名称空间 虚拟环境 视图层之三板斧 JsonRsponse对象 request对象获取文件 FBV与CBV CBV源码剖析 模板层

    目录 路由层之路由分发 路由层之名称空间 方式1:名称空间 方式2:别名不冲突即可 虚拟环境 pycharm创建虚拟环境 命令行形式创建虚拟环境 视图层之三板斧 HttpRsponse render ...

  3. ORM增删改查 django请求生命周期图 django路由层及反向解析

    目录 可视化界面之数据增删改查 1.建表 2.数据展示功能 3.数据添加功能 4.数据编辑功能 5.数据删除功能 django请求生命周期流程图 django路由层 1.路由匹配 2.转换器功能 pa ...

  4. 下载kali以及安装

    一.kali下载 下载地址:https://www.kali.org/get-kali/#kali-virtual-machines 二.kali安装 1.打开VMware,打开文件,选择刚刚下载并解 ...

  5. Nmap常用方法

    1.扫描单个目标地址  在Nmap后面直接添加目标地址即可扫描  nmap 目标地址  2.扫描多个目标地址  如果目标不在同一网段,或在同一网段但不连续且数量不多,可以使用该方法进行扫描  nmap ...

  6. adb环境配置及常用命令

    一.adb环境配置 1.下载并安装adb驱动 2.下载adb工具platform-tools.rar,解压放在某个文件夹下 3.右击此电脑->属性->高级系统设置->环境变量-> ...

  7. 侦察工具——Httrack

    前言 web渗透学习笔记,实验环境为Metasploitable靶机上的DVWA.此随笔介绍Web渗透侦察工具Httrack Httrack 简介 Httrack能够克隆拷贝目标网站上的所有可访问.可 ...

  8. 学习.NET MAUI Blazor(二)、MAUI是个啥

    随着.NET 7的发布,MAUI也正式发布了.那么MAUI是个啥?我们先来看看官方解释: .NET 多平台应用 UI (.NET MAUI) 是一个跨平台框架,用于使用 C# 和 XAML 创建本机移 ...

  9. [python] 基于Gradio可视化部署机器学习应用

    Gradio是一个开源的Python库,用于构建机器学习和数据科学演示应用.有了Gradio,你可以围绕你的机器学习模型或数据科学工作流程快速创建一个简单漂亮的用户界面.Gradio适用于以下情况: ...

  10. [OpenCV实战]47 基于OpenCV实现视觉显著性检测

    人类具有一种视觉注意机制,即当面对一个场景时,会选择性地忽略不感兴趣的区域,聚焦于感兴趣的区域.这些感兴趣的区域称为显著性区域.视觉显著性检测(Visual Saliency Detection,VS ...