PostgreSQL 10.0 preview 功能增强 - BRIN 索引更新smooth化

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

标签

PostgreSQL , 10.0 , BRIN , de-summarization , summarization , 平滑更新BRIN , 平滑失效BRIN


背景

我们将数据存入PostgreSQL时,如果创建的是堆表,那么数据是往数据文件的末尾不断追加存储的。

为了提高数据的检索速度,可以对响应的字段创建索引,在PostgreSQL中,已有8种索引类型,分别是B-Tree,hash, gin, gist, sp-gist, brin, bloom, rum。分别对应不同的应用场景。

如果数据值与物理存储线性相关性比较好,可以使用块级索引(BRIN),以块(或连续的若干块)为最小单位,索引中存储的是这些最小单位的统计信息(最大值,最小值,记录条数,SUM,NULL值条数等)。

为了提高插入速度,HEAP末端的块,可能并不会实时的更新到BRIN索引中,10.0以前,vacuum或者autovacuum表时,会生成未生成统计信息的HEAP block range,如果插入很快,比如在一个vacuum周期内,插入了100个数据块的内容,那么vacuum需要扫描这100个数据块并生成BRIN索引对应的信息。

pic

10.0 写时触发更新brin

为了防止插入过快,导致vacuum一次要统计过多的数据块,或者导致未进入BRIN索引的堆表末端数据块过多,10.0的改进如下,在插入时,如果插入到下一个block range的块时,自动触发vacuum去统计前一个block range的BRIN统计信息(如果还没有被统计的话)。

这样就可以解决前面提到的问题,因为insert的时候,只要到达block range边界,就会自动触发vacuum去统计它。

开启"insert的时候,只要到达block range边界,就会自动触发vacuum去统计它"的方法,创建索引时,指定autosummarize参数。

https://www.postgresql.org/docs/devel/static/sql-createindex.html

BRIN indexes accept different parameters:  
  
pages_per_range  
Defines the number of table blocks that make up one block range for each entry of a BRIN index (see Section 65.1 for more details). The default is 128.  
  
autosummarize  
Defines whether a summarization run is invoked for the previous page range whenever an insertion is detected on the next one.  
  
create index idx on table using brin (id) with (autosummarize=on);  

pic

同时,10.0还新增了用户UDF接口,用户可以自己调用函数,去统计(更新)BRIN索引,指定表,以及堆表对应的block_id即可。

+-- Test brin_summarize_range  
+CREATE TABLE brin_summarize (  
+    value int  
+) WITH (fillfactor=10, autovacuum_enabled=false);  
+CREATE INDEX brin_summarize_idx ON brin_summarize USING brin (value) WITH (pages_per_range=2);  
+-- Fill a few pages  
+DO $$  
+DECLARE curtid tid;  
+BEGIN  
+  LOOP  
+    INSERT INTO brin_summarize VALUES (1) RETURNING ctid INTO curtid;  
+    EXIT WHEN curtid > tid '(2, 0)';  
+  END LOOP;  
+END;  
+$$;  
+-- summarize one range  
+SELECT brin_summarize_range('brin_summarize_idx', 0);  
+ brin_summarize_range   
+----------------------  
+                    1  
+(1 row)  
+  
+-- nothing: already summarized  
+SELECT brin_summarize_range('brin_summarize_idx', 1);  
+ brin_summarize_range   
+----------------------  
+                    0  
+(1 row)  
+  
+-- summarize one range  
+SELECT brin_summarize_range('brin_summarize_idx', 2);  
+ brin_summarize_range   
+----------------------  
+                    1  
+(1 row)  
+  
+-- nothing: page doesn't exist in table  
+SELECT brin_summarize_range('brin_summarize_idx', 4294967295);  
+ brin_summarize_range   
+----------------------  
+                    0  
+(1 row)  
+  
+-- invalid block number values  
+SELECT brin_summarize_range('brin_summarize_idx', -1);  
+ERROR:  block number out of range: -1  
+SELECT brin_summarize_range('brin_summarize_idx', 4294967296);  
+ERROR:  block number out of range: 4294967296  

patch信息如下

BRIN auto-summarization  
  
Previously, only VACUUM would cause a page range to get initially  
summarized by BRIN indexes, which for some use cases takes too much time  
since the inserts occur.  To avoid the delay, have brininsert request a  
summarization run for the previous range as soon as the first tuple is  
inserted into the first page of the next range.  Autovacuum is in charge  
of processing these requests, after doing all the regular vacuuming/  
analyzing work on tables.  
  
This doesn't impose any new tasks on autovacuum, because autovacuum was  
already in charge of doing summarizations.  The only actual effect is to  
change the timing, i.e. that it occurs earlier.  For this reason, we  
don't go any great lengths to record these requests very robustly; if  
they are lost because of a server crash or restart, they will happen at  
a later time anyway.  
  
Most of the new code here is in autovacuum, which can now be told about  
"work items" to process.  This can be used for other things such as GIN  
pending list cleaning, perhaps visibility map bit setting, both of which  
are currently invoked during vacuum, but do not really depend on vacuum  
taking place.  
  
The requests are at the page range level, a granularity for which we did  
not have SQL-level access; we only had index-level summarization  
requests via brin_summarize_new_values().  It seems reasonable to add  
SQL-level access to range-level summarization too, so add a function  
brin_summarize_range() to do that.  
  
Authors: Álvaro Herrera, based on sketch from Simon Riggs.  
Reviewed-by: Thomas Munro.  
Discussion: https://postgr.es/m/20170301045823.vneqdqkmsd4as4ds@alvherre.pgsql  

10.0 brin索引条目失效接口

brin记录的是block range的最大值,最小值,随着对应block range内数据的更新,删除,BRIN IDX中对应的block range统计信息可能会越来越宽泛(不准确),因此为了提高BRIN的精度,10.0新增了一个失效接口,可以将对应block range的brin index内的条目失效,然后你可以等VACUUM再次统计它,或者调用前面提到的UDF接口来统计它。

pic

失效heap block range对应brin idx条目的方法

+SELECT brin_desummarize_range('brinidx', 0);  
+ brin_desummarize_range   
+------------------------  
+   
+(1 row)  
+  
  
+-- Tests for brin_desummarize_range  
+SELECT brin_desummarize_range('brinidx', -1); -- error, invalid range  
+ERROR:  block number out of range: -1  
+SELECT brin_desummarize_range('brinidx', 0);  
+ brin_desummarize_range   
+------------------------  
+   
+(1 row)  
+  
+SELECT brin_desummarize_range('brinidx', 0);  
+ brin_desummarize_range   
+------------------------  
+   
+(1 row)  
+  
+SELECT brin_desummarize_range('brinidx', 100000000);  
+ brin_desummarize_range   
+------------------------  
+   
+(1 row)  
+  

patch信息如下

BRIN de-summarization  
  
When the BRIN summary tuple for a page range becomes too "wide" for the  
values actually stored in the table (because the tuples that were  
present originally are no longer present due to updates or deletes), it  
can be useful to remove the outdated summary tuple, so that a future  
summarization can install a tighter summary.  
  
This commit introduces a SQL-callable interface to do so.  
  
Author: Álvaro Herrera  
Reviewed-by: Eiji Seki  
Discussion: https://postgr.es/m/20170228045643.n2ri74ara4fhhfxf@alvherre.pgsql  

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

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

参考

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=7526e10224f0792201e99631567bbe44492bbde4

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=c655899ba9ae2a0d24e99c797167c33e0cfa0820

https://www.postgresql.org/docs/devel/static/sql-createindex.html

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
3月前
|
存储 SQL 关系型数据库
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
|
3月前
|
存储 关系型数据库 MySQL
MySQL数据库索引的数据结构?
MySQL中默认使用B+tree索引,它是一种多路平衡搜索树,具有树高较低、检索速度快的特点。所有数据存储在叶子节点,非叶子节点仅作索引,且叶子节点形成双向链表,便于区间查询。
102 4
|
5月前
|
存储 关系型数据库 MySQL
阿里面试:MySQL 一个表最多 加几个索引? 6个?64个?还是多少?
阿里面试:MySQL 一个表最多 加几个索引? 6个?64个?还是多少?
阿里面试:MySQL 一个表最多 加几个索引? 6个?64个?还是多少?
|
4月前
|
存储 监控 关系型数据库
B-tree不是万能药:PostgreSQL索引失效的7种高频场景与破解方案
在PostgreSQL优化实践中,B-tree索引虽承担了80%以上的查询加速任务,但因多种原因可能导致索引失效,引发性能骤降。本文深入剖析7种高频失效场景,包括隐式类型转换、函数包裹列、前导通配符等,并通过实战案例揭示问题本质,提供生产验证的解决方案。同时,总结索引使用决策矩阵与关键原则,助你让索引真正发挥作用。
257 0
|
7月前
|
关系型数据库 MySQL 数据库
Mysql的索引
MYSQL索引主要有 : 单列索引 , 组合索引和空间索引 , 用的比较多的就是单列索引和组合索引 , 空间索引我这边没有用到过 单列索引 : 在MYSQL数据库表的某一列上面创建的索引叫单列索引 , 单列索引又分为 ● 普通索引:MySQL中基本索引类型,没有什么限制,允许在定义索引的列中插入重复值和空值,纯粹为了查询数据更快一点。 ● 唯一索引:索引列中的值必须是唯一的,但是允许为空值 ● 主键索引:是一种特殊的唯一索引,不允许有空值 ● 全文索引: 只有在MyISAM引擎、InnoDB(5.6以后)上才能使⽤用,而且只能在CHAR,VARCHAR,TEXT类型字段上使⽤用全⽂文索引。
|
3月前
|
存储 SQL 关系型数据库
MySQL 核心知识与索引优化全解析
本文系统梳理了 MySQL 的核心知识与索引优化策略。在基础概念部分,阐述了 char 与 varchar 在存储方式和性能上的差异,以及事务的 ACID 特性、并发事务问题及对应的隔离级别(MySQL 默认 REPEATABLE READ)。 索引基础部分,详解了 InnoDB 默认的 B+tree 索引结构(多路平衡树、叶子节点存数据、双向链表支持区间查询),区分了聚簇索引(数据与索引共存,唯一)和二级索引(数据与索引分离,多个),解释了回表查询的概念及优化方法,并分析了 B+tree 作为索引结构的优势(树高低、效率稳、支持区间查询)。 索引优化部分,列出了索引创建的六大原则
|
4月前
|
存储 关系型数据库 MySQL
MySQL覆盖索引解释
总之,覆盖索引就像是图书馆中那些使得搜索变得极为迅速和简单的工具,一旦正确使用,就会让你的数据库查询飞快而轻便。让数据检索就像是读者在图书目录中以最快速度找到所需信息一样简便。这样的效率和速度,让覆盖索引成为数据库优化师傅们手中的尚方宝剑,既能够提升性能,又能够保持系统的整洁高效。
125 9
|
5月前
|
机器学习/深度学习 关系型数据库 MySQL
对比MySQL全文索引与常规索引的互异性
现在,你或许明白了这两种索引的差异,但任何技术决策都不应仅仅基于理论之上。你可以创建你的数据库实验环境,尝试不同类型的索引,看看它们如何影响性能,感受它们真实的力量。只有这样,你才能熟悉它们,掌握什么时候使用全文索引,什么时候使用常规索引,以适应复杂多变的业务需求。
113 12
|
6月前
|
SQL 存储 关系型数据库
MySQL选错索引了怎么办?
本文探讨了MySQL中因索引选择不当导致查询性能下降的问题。通过创建包含10万行数据的表并插入数据,分析了一条简单SQL语句在不同场景下的执行情况。实验表明,当数据频繁更新时,MySQL可能因统计信息不准确而选错索引,导致全表扫描。文章深入解析了优化器判断扫描行数的机制,指出基数统计误差是主要原因,并提供了通过`analyze table`重新统计索引信息的解决方法。
139 3

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版
  • 推荐镜像

    更多