PostgreSQL 11 preview - Surjective indexes - 索引HOT增强(表达式)update评估

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

标签

PostgreSQL , 表达式索引 , 表达式结果变化评估 , projection function , 多值索引 , GIN , 多值元素变化


背景

PostgreSQL 11马上要提交的一个PATCH,可以更亲和的使用HOT,大幅增强表达式索引的更新性能。

背景技术是HOT,当更新某一条记录时,如果被索引的字段值没有变化,同时被更新记录的新版本与老版本在同一个HEAP PAGE中,那么索引不需要增加冗余ENTRY。从而发生HOT更新。

pic

pic

pic

pic

pic

pic

pic

pic

pic

pic

但是,我们想一下,表达式索引和普通字段索引的区别,实际上表达式索引并不一定随字段值的变化而变化。

例1

bookinfo是一个JSONB类型,我们对isbn这个KEY建了索引。

(bookinfo->>'isbn')  

仅仅当isbn的值发生变化时,这个表达式的结果才会变化。而bookinfo的其他部分变化,实际上索引都不需要变化。

例2

又比如这样一个表达式索引,当mod_time的时间变更后,如果新值在同一个小时内,那么这个表达式的结果也没有变化

to_char(mod_time, 'yyyymmddhh24')  

'2018-02-15 10:11:10'  
  
'2018-02-15 10:30:15'  

以往PostgreSQL HOT的判断只针对字段值是否变化,而不判断表达式的结果是否变化。所以表达式索引实际上是有可能出现更高几率的HOT的。

recheck_on_update参数,决定是否在更新后评估表达式的值是否变化

PostgreSQL 11新增了一个索引OPTION:

recheck_on_update   
  
Functional index is based on on projection function:   
function which extract subset of its argument.  
In mathematic such functions are called non-injective.   
  
injective函数随参数变化,返回结果也会变化。  
  
For injective function if any attribute used in the indexed   
expression is changed, then value of index expression is also changed.   
So to check that index is affected by the update,   
it is enough to check the set of changed fields.   
  
non-injective函数,参数值变化,返回结果不一定变化。  
  
recheck_on_update默认为true,并认为表达式为non-injective函数  
  
By default this parameters is assigned true value and function is considered  
as non-injective.  
  
In this case change of any of indexed key doesn't mean that value of the function is changed.   
For example, for the expression (bookinfo->>'isbn') defined  
for column of JSON type is changed only when ISBN is changed, which rarely happen.   
The same is true for most functional indexes.   
  
开启recheck_on_update时,需要用到old tuple和new tuple对表达式索引的表达式结果评估,并判断结果是否不一致。不一致则不发生HOT。  
  
For non-injective functions,   
Postgres compares values of indexed expression for old and updated tuple and updates   
index only when function results are different.   
It allows to eliminate index update and use HOT update.   
But there are extra evaluations of the functions.   
  
So if function is expensive or probability that change of indexed column will not   
effect the function value is small,   
then marking index as recheck_on_update may increase update speed.  

recheck_on_update默认为true,表示UPDATE时,如果表达式涉及字段值发生了变化,那么一定会重新评估表达式的值是否变化,如果表达式值没有变化,会更大程度的使用HOT。减少索引更新。

recheck_on_update设置为false,表示UPDATE时不一定评估表达式的值,取决于表达式的成本,成本很高,则不评估,只要表达式涉及字段变化,就需要更新索引。表达式成本低于阈值则可能评估。(参考IsProjectionFunctionalIndex, (index_expr_cost.startup + index_expr_cost.per_tuple > MAX_HOT_INDEX_EXPR_COST))

typedef enum IndexAttrBitmapKind  
{  
	INDEX_ATTR_BITMAP_HOT,  
	INDEX_ATTR_BITMAP_PROJ,  
	INDEX_ATTR_BITMAP_KEY,  
	INDEX_ATTR_BITMAP_PRIMARY_KEY,  
	INDEX_ATTR_BITMAP_IDENTITY_KEY  
  
  
+#define MAX_HOT_INDEX_EXPR_COST 1000  
+  
+/*  
+ * Check if functional index is projection: index expression returns some subset of  
+ * its argument values. During hot update check projection indexes are handled in special way:  
+ * instead of checking if any of attributes used in indexed expression was updated,  
+ * we should calculate and compare values of index expression for old and new tuple values.  
+ *  
+ * Decision made by this function is based on two sources:  
+ * 1. Calculated cost of index expression: if it is higher than some threshold (1000) then  
+ *    extra comparison of index expression values is expected to be too expensive.  
+ * 2. "projection" index option explicitly set by user. This setting overrides 1) and 2)  
+ */  
+static bool IsProjectionFunctionalIndex(Relation index, IndexInfo* ii)  
+{  
+	bool is_projection = false;  
+  
+	if (ii->ii_Expressions)  
+	{  
+		HeapTuple       tuple;  
+		Datum           reloptions;  
+		bool            isnull;  
+		QualCost index_expr_cost;  
+  
+		is_projection = true; /* by default functional index is considered as non-injective */  
+  
+		cost_qual_eval(&index_expr_cost, ii->ii_Expressions, NULL);  
+		/*  
+		 * If index expression is too expensive, then disable projection optimization, because  
+		 * extra evaluation of index expression is expected to be more expensive than index update.  
+		 * Current implementation of projection optimization has to calculate index expression twice  
+		 * in case of hit (value of index expression is not changed) and three times if values are different.  
+		 */  
+		if (index_expr_cost.startup + index_expr_cost.per_tuple > MAX_HOT_INDEX_EXPR_COST)  
+		{  
+			is_projection = false;  
+		}  
+  
+		tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(RelationGetRelid(index)));  
+		if (!HeapTupleIsValid(tuple))  
+			elog(ERROR, "cache lookup failed for relation %u", RelationGetRelid(index));  
+  
+		reloptions = SysCacheGetAttr(RELOID, tuple,  
+									 Anum_pg_class_reloptions, &isnull);  
+		if (!isnull)  
+		{  
+			GenericIndexOpts *idxopts = (GenericIndexOpts *)index_generic_reloptions(reloptions, false);  
+			if (idxopts != NULL)  
+			{  
+				is_projection = idxopts->recheck_on_update;  
+				pfree(idxopts);  
+			}  
+		}  
+		ReleaseSysCache(tuple);  
+	}  
+	return is_projection;  
+}  

使用举例

1、设置recheck_on_update=false,则表达式索引无法使用HOT。

create table keyvalue(id integer primary key, info jsonb);  
  
create index nameindex on keyvalue((info->>'name')) with (recheck_on_update=false);  
  
insert into keyvalue values (1, '{"name": "john", "data": "some data"}');  
  
update keyvalue set info='{"name": "john", "data": "some other data"}' where id=1;  
  
select pg_stat_get_xact_tuples_hot_updated('keyvalue'::regclass);  
  
 pg_stat_get_xact_tuples_hot_updated   
-------------------------------------  
                                   0  
(1 row)  

2、设置recheck_on_update=true,则表达式索引可以使用HOT。并且强制要求每次UPDATE后,如果表达式涉及的字段发生变化,那么评估表达式的值。

drop table keyvalue;  
  
create table keyvalue(id integer primary key, info jsonb);  
  
create index nameindex on keyvalue((info->>'name')) with (recheck_on_update=true);  
  
insert into keyvalue values (1, '{"name": "john", "data": "some data"}');  
  
-- 表达式结果不变  
update keyvalue set info='{"name": "john", "data": "some other data"}' where id=1;  
  
select pg_stat_get_xact_tuples_hot_updated('keyvalue'::regclass);  
  
 pg_stat_get_xact_tuples_hot_updated   
-------------------------------------  
                                   1  
(1 row)  
  
-- 表达式结果变化  
update keyvalue set info='{"name": "smith", "data": "some other data"}' where id=1;  
  
select pg_stat_get_xact_tuples_hot_updated('keyvalue'::regclass);  
 pg_stat_get_xact_tuples_hot_updated   
-------------------------------------  
                                   1  
(1 row)  
  
-- 表达式结果不变  
update keyvalue set info='{"name": "smith", "data": "some more data"}' where id=1;  
  
select pg_stat_get_xact_tuples_hot_updated('keyvalue'::regclass);  
 pg_stat_get_xact_tuples_hot_updated   
-------------------------------------  
                                   2  
(1 row)  

3、不设置recheck_on_update,则表达式索引可以使用HOT。并且是否评估表达式的值,还取决于表达式的成本与阈值,成本很高,则不评估,只要表达式涉及字段变化,就需要更新索引。表达式成本低于阈值则可能评估。

drop table keyvalue;  
  
create table keyvalue(id integer primary key, info jsonb);  
  
create index nameindex on keyvalue((info->>'name'));  
  
insert into keyvalue values (1, '{"name": "john", "data": "some data"}');  
  
-- 表达式结果不变  
update keyvalue set info='{"name": "john", "data": "some other data"}' where id=1;  
  
select pg_stat_get_xact_tuples_hot_updated('keyvalue'::regclass);  
 pg_stat_get_xact_tuples_hot_updated   
-------------------------------------  
                                   1  
(1 row)  
  
-- 表达式结果变化  
update keyvalue set info='{"name": "smith", "data": "some other data"}' where id=1;  
  
select pg_stat_get_xact_tuples_hot_updated('keyvalue'::regclass);  
 pg_stat_get_xact_tuples_hot_updated   
-------------------------------------  
                                   1  
(1 row)  
  
-- 表达式结果不变  
update keyvalue set info='{"name": "smith", "data": "some more data"}' where id=1;  
  
select pg_stat_get_xact_tuples_hot_updated('keyvalue'::regclass);  
 pg_stat_get_xact_tuples_hot_updated   
-------------------------------------  
                                   2  
(1 row)  
  
drop table keyvalue;  

多值类型索引

以此类推,多值类型的索引,实际上元素可能有变化,也可能没有变化,也可以借鉴同样的思路,提高多值类型的HOT概率。

array  
  
hstore  
  
json  
  
jsonb  

索引是否需要变化,取决于元素是否变化,NEW TUPLE是否还在同一个HEAP PAGE。

小结

PostgreSQL 11 Surjective indexes功能,新增recheck_on_update索引选项,使得表达式索引也能用上HOT特性(降低索引冗余概率)。对于non-injective函数、表达式索引,参数值变化,返回结果不一定变化。特别适用。

参考

https://www.postgresql.org/message-id/attachment/57692/projection-7.patch

https://commitfest.postgresql.org/17/1152/

src/backend/access/heap/README.HOT

《Use pageinspect EXTENSION view PostgreSQL Page's raw infomation》

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
9天前
|
关系型数据库 MySQL 分布式数据库
PolarDB产品使用问题之mysql迁移后查询不走索引了,该如何解决
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
10天前
|
运维 关系型数据库 分布式数据库
PolarDB产品使用问题之列存索引的原理是什么
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
25天前
|
存储 算法 数据处理
惊人!PolarDB-X 存储引擎核心技术的索引回表优化如此神奇!
【6月更文挑战第11天】PolarDB-X存储引擎以其索引回表优化技术引领数据库发展,提升数据检索速度,优化磁盘I/O,确保系统在高并发场景下的稳定与快速响应。通过示例代码展示了在查询操作中如何利用该技术高效获取结果。索引回表优化具备出色性能、高度可扩展性和适应性,为应对大数据量和复杂业务提供保障,助力企业与开发者实现更高效的数据处理。
|
9天前
|
SQL 关系型数据库 分布式数据库
PolarDB产品使用问题之如何查看SQL语句使用的是行索引还是列索引
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
|
2月前
|
关系型数据库 数据库 索引
|
2月前
|
监控 关系型数据库 数据库
关系型数据库考虑索引的选择性
【5月更文挑战第20天】
36 4
|
2月前
|
SQL Oracle 关系型数据库
关系型数据库中对索引的数目
【5月更文挑战第19天】
53 4
|
2月前
|
监控 关系型数据库 数据库
|
29天前
|
关系型数据库 分布式数据库 数据库
PolarDB产品使用合集之表列存索引要怎么添加
PolarDB是阿里云推出的一种云原生数据库服务,专为云设计,提供兼容MySQL、PostgreSQL的高性能、低成本、弹性可扩展的数据库解决方案,可以有效地管理和优化PolarDB实例,确保数据库服务的稳定、高效运行。以下是使用PolarDB产品的一些建议和最佳实践合集。
|
29天前
|
关系型数据库 分布式数据库 数据库
PolarDB产品使用合集之优化器对索引的阈值一般是多少
PolarDB是阿里云推出的一种云原生数据库服务,专为云设计,提供兼容MySQL、PostgreSQL的高性能、低成本、弹性可扩展的数据库解决方案,可以有效地管理和优化PolarDB实例,确保数据库服务的稳定、高效运行。以下是使用PolarDB产品的一些建议和最佳实践合集。

相关产品

  • 云原生数据库 PolarDB