PostgreSQL 10.0 preview 性能增强 - 间接索引(secondary index)

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

标签

PostgreSQL , 10.0 , 间接索引 , 第二索引


背景

我们知道,PostgreSQL的MVCC是多版本来实现的,当更新数据时,产生新的版本。

那么如果新版本不在同一个数据块的时候,索引也要随之变化,当新版本在同一个堆表的块里面时,则发生HOT UPDATE,不需要变更没有发生值改变的索引。

但是HOT总不能覆盖100%的更新,所以还是有索引更新的可能存在。

为了解决这个问题,PostgreSQL 10.0引入了第二索引(间接索引)的概念,即在PK或者UK之上,构建其他索引。

那么只要UK或者PK的值不变,其他索引都不需要变更。

间接索引的好处,不言而喻,可以减少表的DML带来的索引变更。

间接索引的缺陷,通过间接索引查询堆表数据时,需要扫描两个索引(间接索引,以及第一索引),还需要扫描堆表定位数据。

详情

I propose we introduce the concept of "indirect indexes".  I have a toy  
implementation and before I go further with it, I'd like this assembly's  
input on the general direction.  

Indirect indexes are similar to regular indexes, except that instead of  
carrying a heap TID as payload, they carry the value of the table's  
primary key.  Because this is laid out on top of existing index support  
code, values indexed by the PK can only be six bytes long (the length of  
ItemPointerData); in other words, 281,474,976,710,656 rows are  
supported, which should be sufficient for most use cases.[1]  

A new flag is added to the index AM routine, indicating whether it can  
support indirect indexes.  Initially, only the b-tree AM would support  
indirect indexes, but I plan to develop support for GIN indirect soon  
afterwards, which seems most valuable.  

To create an indirect index, the command  
    CREATE INDIRECT INDEX  
is used.  Currently this always uses the defined primary key index[2].  

Implementation-wise, to find a value using an indirect index that index  
is scanned first; this produces a PK value, which can be used to scan  
the primary key index, the result of which is returned.  

There are two big advantages to indirect indexes, both of which are  
related to UPDATE's "write amplification":  

1. UPDATE is faster.  Indirect indexes on column that are not modified  
   by the update do not need to be updated.  
2. HOT can be used more frequently.  Columns indexed only by indirect  
   indexes do not need to be considered for whether an update needs to  
   be non-HOT, so this further limits "write amplification".  

The biggest downside is that in order to find out a heap tuple using the  
index we need to descend two indexes (the indirect and the PK) instead  
of one, so it's slower.  For many use cases the tradeoff is justified.  

I measured the benefits with the current prototype implementation.  In  
two separate schemas, I created a pgbench_accounts table, with 12  
"filler" columns, and indexed them all; one schema used regular indexes,  
the other used indirect indexes.  Filled them both to the equivalent of  
scale 50, which results in a table of some 2171 MB; the 12 indexes are  
282 MB each, and the PK index is 107 MB).  I then ran a pgbench with a  
custom script that update a random one of those columns and leave the  
others alone on both schemas (not simultaneously).  I ran 100k updates  
for each case, 5 times:  

  method  │   TPS: min / avg (stddev) / max   │        Duration: min / avg / max        │ avg_wal   
──────────┼───────────────────────────────────┼─────────────────────────────────────────┼─────────  
 direct   │  601.2 / 1029.9 ( 371.9) / 1520.9 │ 00:01:05.76 / 00:01:48.58 / 00:02:46.39 │ 4841 MB  
 indirect │ 2165.1 / 3081.6 ( 574.8) / 3616.4 │ 00:00:27.66 / 00:00:33.56 / 00:00:46.2  │ 1194 MB  
(2 rows)  

This is a pretty small test (not long enough for autovacuum to trigger  
decently) but I think this should be compelling enough to present the  
case.  

Please discuss.  

Implementation notes:  

Executor-wise, we could have a distinct IndirectIndexScan node, or we  
could just hide the second index scan inside a regular IndexScan.  I  
think from a cleanliness POV there is no reason to have a separate node;  
efficiency wise I think a separate node leads to less branches in the  
code.  (In my toy patch I actually have the second indexscan hidden  
inside a separate "ibtree" AM; not what I really propose for commit.)  
Additionally, executor will have to keep track of the values in the PK  
index so that they can be passed down on insertion to each indirect  
index.  

Planner-wise, I don't think we need to introduce a distinct indirect  
index Path.  We can just let the cost estimator attach the true cost of  
the two scans to a regular index scan path, and the correct executor  
node is produced later if that index is chosen.  

In relcache we'll need an additional bitmapset of columns indexed by  
indirect indexes.  This is used so that heap_update can return an output  
bitmapset of such columns that were not modified (this is computed by  
HeapSatisfiesHOTandKeyUpdate).  The executor uses this to know when to  
skip updating each indirect index.  

Vacuuming presents an additional challenge: in order to remove index  
items from an indirect index, it's critical to scan the PK index first  
and collect the PK values that are being removed.  Then scan the  
indirect index and remove any items that match the PK items removed.  
This is a bit problematic because of the additional memory needed to   
store the array of PK values.  I haven't implemented this yet.  


Items I haven't thought about yet:  
* UNIQUE INDIRECT?  I think these should work, but require some  
  tinkering.  
* Deferred unique indexes?  See unique_key_recheck.  
* CREATE INDEX CONCURRENTLY.  


[1]  Eventually we can expand this to allow for "normal" datatypes, say  
bigint, but that's likely to require a much bigger patch in order to  
change IndexTuple to support it.  I would defer that project to a later  
time.  

[2] It is possible to extend the grammar to allow other UNIQUE indexes  
to be used, if they are on top of NOT NULL columns.  This would allow to  
extend existing production databases with a new column.  A proposed  
syntax is   
  CREATE INDIRECT INDEX idx ON tab (a, b, c)  
  REFERENCES some_unique_index  
  [optional WHERE clause] ;  
which Bison accepts.  I propose not to implement this yet.  However this  
is an important item because it allows existing databases to simply add  
an UNIQUE NOT NULL column to their existing big tables to take advantage  
of the feature, without requiring a lengthy dump/reload of tables that  
currently only have larger keys.  

--   
Álvaro Herrera                https://www.2ndQuadrant.com/  
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services  

这个patch的讨论,详见邮件组,本文末尾URL。

PostgreSQL社区的作风非常严谨,一个patch可能在邮件组中讨论几个月甚至几年,根据大家的意见反复的修正,patch合并到master已经非常成熟,所以PostgreSQL的稳定性也是远近闻名的。

参考

https://commitfest.postgresql.org/13/874/

https://www.postgresql.org/message-id/flat/20161018182843.xczrxsa2yd47pnru@alvherre.pgsql#20161018182843.xczrxsa2yd47pnru@alvherre.pgsql

《深入浅出PostgreSQL B-Tree索引结构》

《B-Tree和B+Tree》

《为PostgreSQL讨说法 - 浅析《UBER ENGINEERING SWITCHED FROM POSTGRES TO MYSQL》》

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
29天前
|
关系型数据库 分布式数据库 数据库
PolarDB常见问题之加了索引但是查询没有使用如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
4月前
|
存储 SQL 关系型数据库
PolarDB这个sql行存和列存性能差别好大 ,为什么?
PolarDB这个sql行存和列存性能差别好大 ,为什么?
33 0
|
4月前
|
存储 关系型数据库 数据库
postgresql|数据库|提升查询性能的物化视图解析
postgresql|数据库|提升查询性能的物化视图解析
154 0
|
3月前
|
关系型数据库 MySQL Serverless
阿里云云原生数据库 PolarDB MySQL Serverless:卓越的性能与无与伦比的弹性
阿里云原生数据库 PolarDB MySQL Serverless 拥有卓越性能和无与伦比的弹性。通过实验体验,深入了解其基本管理和配置、智能弹性伸缩特性和全局一致性特性。实验包括主节点和只读节点的弹性压测以及全局一致性测试,旨在亲身体验 PolarDB 的强大性能。通过实验,可以更好地在实际业务场景中应用 PolarDB,并根据需求进行性能优化和调整。
679 2
|
2月前
|
SQL 算法 关系型数据库
PolarDB-X的XPlan索引选择
对于数据库来说,正确的选择索引是基本的要求,选错索引轻则导致查询缓慢,重则导致数据库整体不可用。PolarDB-X存在多种不同的索引,局部索引、全局索引、列存索引、归档表索引。本文主要介绍一种CN上的局部索引算法:XPlan索引选择。
125754 13
PolarDB-X的XPlan索引选择
|
3月前
|
存储 关系型数据库 分布式数据库
阿里云PolarDB解决乐麦多源数据存储性能问题
乐麦通过使用PolarDB数据库,使整个系统之间的数据查询分析更加高效
390 3
|
3月前
|
关系型数据库 定位技术 索引
在关系型数据库中,常见的索引种类包括哪些
在关系型数据库中,常见的索引种类包括哪些
486 0
|
3月前
|
关系型数据库 数据挖掘 分布式数据库
报名预约|体验PolarDB澎湃性能与高性价比在线直播
「飞天技术沙龙数据库技术周」直播聚焦PolarDB产品体验
|
4月前
|
存储 SQL 关系型数据库
PolarDB-x 比mysql查询性能怎么样?速度快吗
PolarDB-x 比mysql查询性能怎么样?速度快吗
155 0
|
9月前
|
SQL Cloud Native 关系型数据库
ADBPG(AnalyticDB for PostgreSQL)是阿里云提供的一种云原生的大数据分析型数据库
ADBPG(AnalyticDB for PostgreSQL)是阿里云提供的一种云原生的大数据分析型数据库
730 1

相关产品

  • 云原生数据库 PolarDB