PostgreSQL 函数式索引使用注意 - 暨非immutable函数不适合索引的原因

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , 表达式 , 函数稳定性 , immutable


背景

PostgreSQL支持表达式索引,但是表达式必须是immutable的,也即是当输入参数不变时,结果是永恒不变的。

因为当表达式涉及的变量不变时,索引本身不会变化。

给个例子,如果我们有一张表存储了商品价格,另一张表存储了商品折扣 ,如果我们想通过折扣后的价格范围搜索符合价格区间的商品ID,可以使用索引吗?

表达式索引,可以。但是前提是:输入一个商品ID时,商品原价永恒不变。

否则原价发生变化就可能出现索引内容与实际不一致的问题。

例子

create extension btree_gist;  

商品表

create table t_item (id int8 primary key, price jsonb);  

折扣表

create table t_item_discount (id int8, ts daterange, country text, discount float4);  

获取商品折后价格的函数

create or replace function get_price(int8,text,float4) returns float8 as $$  
  select (price->>$2)::float8*$3 from t_item where id=$1;  
$$ language sql strict immutable;  

函数索引,immutable函数

create index idx_t_item_discount_1 on t_item_discount using gist (ts, country, get_price(id,country,discount));  

写入商品

insert into t_item values (1, jsonb '{"global":200, "china":150}');  

写入折扣

insert into t_item_discount values (1, daterange('2018-01-01', '2018-01-10'), 'global', 0.4);  

强制索引扫描

set enable_bitmapscan=off;  
set enable_seqscan=off;  
  
postgres=# explain select ctid,get_price(id,country,discount),* from t_item_discount where ts @> '2018-01-01'::date and get_price(id,country,discount)<300 and country='china';  
                                                                QUERY PLAN                                                                   
-------------------------------------------------------------------------------------------------------------------------------------------  
 Index Scan using idx_t_item_discount_1 on t_item_discount  (cost=0.12..8.40 rows=1 width=90)  
   Index Cond: ((ts @> '2018-01-01'::date) AND (country = 'china'::text) AND (get_price(id, country, discount) < '300'::double precision))  
(2 rows)  
  
postgres=# explain select ctid,get_price(id,country,discount),* from t_item_discount where ts @> '2018-01-01'::date and country='china' and get_price(id,country,discount)<300;  
                                                                QUERY PLAN                                                                   
-------------------------------------------------------------------------------------------------------------------------------------------  
 Index Scan using idx_t_item_discount_1 on t_item_discount  (cost=0.12..8.40 rows=1 width=90)  
   Index Cond: ((ts @> '2018-01-01'::date) AND (country = 'china'::text) AND (get_price(id, country, discount) < '300'::double precision))  
(2 rows)  
  
  
  
postgres=# select ctid,get_price(id,country,discount),* from t_item_discount where ts @> '2018-01-01'::date and country='global' and get_price(id,country,discount)<300;  
 ctid  |    get_price     | id |           ts            | country | discount   
-------+------------------+----+-------------------------+---------+----------  
 (0,1) | 80.0000011920929 |  1 | [2018-01-01,2018-01-10) | global  |      0.4  
(1 row)  

但是如果原价变化,索引并不会更新

postgres=# update t_item set price = jsonb '{"global":2000, "china":1500}' where id=1;  
UPDATE 1  

下面的结果显然是错误的

postgres=# select ctid,get_price(id,country,discount),* from t_item_discount where ts @> '2018-01-01'::date and country='global' and get_price(id,country,discount)<300;  
 ctid  |    get_price     | id |           ts            | country | discount   
-------+------------------+----+-------------------------+---------+----------  
 (0,1) | 800.000011920929 |  1 | [2018-01-01,2018-01-10) | global  |      0.4  
(1 row)  
postgres=# update t_item_discount set discount = discount where id=1;  
UPDATE 1  
postgres=# select ctid,get_price(id,country,discount),* from t_item_discount where ts @> '2018-01-01'::date and country='global' and get_price(id,country,discount)<300;  
 ctid  |    get_price     | id |           ts            | country | discount   
-------+------------------+----+-------------------------+---------+----------  
 (0,2) | 800.000011920929 |  1 | [2018-01-01,2018-01-10) | global  |      0.4  
(1 row)  

只有当表达式字段内容发生变化时,相应的表达式才会变化

postgres=# update t_item_discount set discount=discount+0.0000001 where id=1;  
UPDATE 1  
  
postgres=# select ctid,get_price(id,country,discount),* from t_item_discount where ts @> '2018-01-01'::date and country='global' and get_price(id,country,discount)<300;  
 ctid | get_price | id | ts | country | discount   
------+-----------+----+----+---------+----------  
(0 rows)  
  
postgres=# select float4send(discount),* from t_item_discount ;  
 float4send | id |           ts            | country | discount   
------------+----+-------------------------+---------+----------  
 \x3eccccd0 |  1 | [2018-01-01,2018-01-10) | global  |      0.4  
(1 row)  

参考

《PostgreSQL Oracle 兼容性之 - PL/SQL DETERMINISTIC 与PG函数稳定性(immutable, stable, volatile)》

《PostgreSQL 函数稳定性与constraint_excluded分区表逻辑推理过滤的CASE》

《函数稳定性讲解 - retalk PostgreSQL function's [ volatile|stable|immutable ]》

《函数稳定性讲解 - 函数索引思考, pay attention to function index used in PostgreSQL》

《函数稳定性讲解 - Thinking PostgreSQL Function's Volatility Categories》

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
29天前
|
关系型数据库 分布式数据库 数据库
PolarDB常见问题之加了索引但是查询没有使用如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
2月前
|
SQL 存储 缓存
PostgreSQL函数管理接口
学习PostgreSQL服务端开发必须要对函数管理接口有比较深入的了解
142 0
|
1月前
|
关系型数据库 PostgreSQL
postgresql日程排程函数的编写实例
postgresql日程排程函数的编写实例
|
2月前
|
SQL 关系型数据库 分布式数据库
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
42 7
|
2月前
|
SQL 算法 关系型数据库
PolarDB-X的XPlan索引选择
对于数据库来说,正确的选择索引是基本的要求,选错索引轻则导致查询缓慢,重则导致数据库整体不可用。PolarDB-X存在多种不同的索引,局部索引、全局索引、列存索引、归档表索引。本文主要介绍一种CN上的局部索引算法:XPlan索引选择。
125754 13
PolarDB-X的XPlan索引选择
|
3月前
|
关系型数据库 定位技术 索引
在关系型数据库中,常见的索引种类包括哪些
在关系型数据库中,常见的索引种类包括哪些
486 0
|
4月前
|
SQL 关系型数据库 C语言
PostgreSQL【应用 03】Docker部署的PostgreSQL扩展SQL之C语言函数(编写、编译、载入)计算向量余弦距离实例分享
PostgreSQL【应用 03】Docker部署的PostgreSQL扩展SQL之C语言函数(编写、编译、载入)计算向量余弦距离实例分享
45 0
|
4月前
|
SQL 关系型数据库 数据库
PostgreSQL【应用 02】扩展SQL之C语言函数(编写、编译、载入)实例分享
PostgreSQL【应用 02】扩展SQL之C语言函数(编写、编译、载入)实例分享
47 0
|
4月前
|
SQL 关系型数据库 PostgreSQL
PostgreSQL【SQL 01】根据条件更新字段值或追加信息STRPOS(string, substring)函数使用及LIKE函数对比
PostgreSQL【SQL 01】根据条件更新字段值或追加信息STRPOS(string, substring)函数使用及LIKE函数对比
54 0
|
4月前
|
SQL 关系型数据库 编译器
PostgreSQL SQL扩展 ---- C语言函数(二)
可以用C(或者与C兼容,比如C++)语言编写用户自定义函数(User-defined functions)。这些函数被编译到动态可加载目标文件(也称为共享库)中并被守护进程加载到服务中。“C语言函数”与“内部函数”的区别就在于动态加载这个特性,二者的实际编码约定本质上是相同的(因此,标准的内部函数库为用户自定义C语言函数提供了丰富的示例代码)
65 0

相关产品

  • 云原生数据库 PolarDB