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

2023-11-03 13:20

本文主要是介绍PostgreSQL 10.0 preview 功能增强 - BRIN 索引更新smooth化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

标签

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

这篇关于PostgreSQL 10.0 preview 功能增强 - BRIN 索引更新smooth化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/338458

相关文章

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

MySQL之InnoDB存储引擎中的索引用法及说明

《MySQL之InnoDB存储引擎中的索引用法及说明》:本文主要介绍MySQL之InnoDB存储引擎中的索引用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1、背景2、准备3、正篇【1】存储用户记录的数据页【2】存储目录项记录的数据页【3】聚簇索引【4】二

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

postgresql数据库基本操作及命令详解

《postgresql数据库基本操作及命令详解》本文介绍了PostgreSQL数据库的基础操作,包括连接、创建、查看数据库,表的增删改查、索引管理、备份恢复及退出命令,适用于数据库管理和开发实践,感兴... 目录1. 连接 PostgreSQL 数据库2. 创建数据库3. 查看当前数据库4. 查看所有数据库

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.