标签

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


背景

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

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

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

例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》

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

  1. PostgreSQL 11 preview - bloom filter 误报率评估测试及如何降低误报 - 暨bloom filter应用于HEAP与INDEX的一致性检测...

    标签 PostgreSQL , bloom filter , 误报率 , amcheck 背景 bloom filter是一个空间压缩,概率数据结构,可以存储超容量的数据集,用于判断某个元素是否存在于 ...

  2. PostgreSQL 11 新特性之 PL/pgSQL 增强

    文章目录 PostgreSQL 11 增加了一个新的编程对象,存储过程(PROCEDURE).它与存储函数类似,但是没有返回值.存储过程还支持事务,参考文章"PostgreSQL 11 新特 ...

  3. PostgreSQL 11 preview - Allow on-line enabling and disabling of data checksums

    标签 PostgreSQL , checksum , online modify , pg_verify_checksums , pg_enable_data_checksums , pg_disab ...

  4. PostgreSQL 11 100亿 tpcb 性能测试 on ECS

    标签 PostgreSQL , tpcb , pgbench , 100亿 背景 PostgreSQL 11 发布在即,以下是tpcc与tpch测试的结果: <PostgreSQL 11 tpc ...

  5. PostgreSQL 当有多个索引可选时,优化器如何选择

    标签 PostgreSQL , 索引 , 复合索引 , 选择 , 成本 , 优化器 背景 当一个表有很多索引时,并且一个QUERY可以使用到其中的多个索引时,数据库会如何做出选择?最终选择哪个,或者哪 ...

  6. PostgreSQL 收缩膨胀表或索引 - pg_squeeze or pg_repack

    PostgreSQL 收缩膨胀表或索引 - pg_squeeze or pg_repack 作者 digoal 日期 2016-10-30 标签 PostgreSQL , pg_repack , pg ...

  7. Visual Studio 2019 v16.10 和 v16.11 Preview 1 现已推出!

    Visual Studio 2019 v16.10有什么新功能? 我们很高兴地宣布Visual Studio 2019 v16.10 GA 和 v16.11 preview 1发布.此版本使我们的主题 ...

  8. PostgreSQL 11 1000亿 tpcb、1000W tpcc 性能测试 - on 阿里云ECS + ESSD (含quorum based 0丢失多副本配置与性能测试)...

    标签 PostgreSQL , pgbench , tpcb , tpcc , tpch 背景 https://help.aliyun.com/knowledge_detail/64950.html ...

  9. PostgreSQL 11 新特性之哈希分区

    文章目录 PostgreSQL 10 引入了声明式分区(declarative partitioning)特性,但是实现的功能有限.PostgreSQL 11 为此带来了许多关于分区的增强功能.首先就 ...

最新文章

  1. “AI+”农业向农民致敬-丰收节交易会:谋定工业反哺农业
  2. iphone 如何实现全屏截图
  3. keepalive之LVS-DR架构
  4. [壹刊]Azure AD(四)知识补充-服务主体
  5. spark 查看yarn日志_spark周边项目之Livy
  6. jQuery学习(十一)— 常用的删除方法
  7. 计算机信息的应用安全中心在哪,腾讯游戏安全中心
  8. Mysql 根类别和子类别,如何在MySQL中设计类别和子类别?
  9. 基于URL的轻量级恶意页面检测分类器
  10. 基于MATLAB的人脸识别
  11. 一步一步超级详细的zabbix安装教程
  12. Python3批量发送邮件,支持smpt的ssl验证,支持163和outlook邮箱,可发送html格式和附件,使用asyncio和线程池实现并发并行
  13. 干法:经营者应该怎样工作
  14. odi oracle to mysql_【ODI】| 数据ETL:从零开始使用Oracle ODI完成数据集成(三)
  15. hotmail 获取邮箱授权码
  16. 百家企业短信网关(背景及核心代码)-1-开源项目短信接口征集
  17. 阿里云MaxCompute 2019-7月刊
  18. icon图标 地址栏 收藏夹显示 代码
  19. 深度学习之选择GPU或CPU方法
  20. referrer 值设置不对,导致 引入百度站长 hm.js 读取只有175B且内容长度为0的原因

热门文章

  1. 种群遗传学的多态性衡量参数
  2. 解决office软件无法打印预览 / 添加打印机失败问题
  3. 全新安装Windows10系统(PE下)
  4. Minitab中的一些最常用的功能和任务。
  5. Ipconfig ping tracert netstat arp的使用
  6. 怎么在一台电脑上同时打开运行两个微信号
  7. IOS 应用运行环境理解
  8. 【数据结构】AVL树(高度平衡的二叉搜索树)
  9. 嵌入式软件工程师笔试面试指南-网络编程
  10. 关于服务器登录用户信息查看的若干命令