暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL 14 Bottom-up index tuple deletion 大大减少btree索引膨胀

作者:金涛

准备PG12和PG14两个环境,主要参数保持一致,并执行如下SQL初始化测试表:

CREATE TABLE testtab (   id        bigint      CONSTRAINT testtab_pkey PRIMARY KEY,   unchanged integer,   changed   integer);INSERT INTO testtab   SELECT i, i, 0   FROM generate_series(1, 10000) AS i;CREATE INDEX testtab_unchanged_idx ON testtab (unchanged);CREATE INDEX testtab_changed_idx ON testtab (changed);vacuum ANALYZE testtab;

PG12查看表索引大小:

postgres=# \dt+ testtab                     List of relations Schema |  Name   | Type  |  Owner   |  Size  | Description --------+---------+-------+----------+--------+------------- public | testtab | table | postgres | 576 kB | (1 row)postgres=# \di+ testtab*                                 List of relations Schema |         Name          | Type  |  Owner   |  Table  |  Size  | Description --------+-----------------------+-------+----------+---------+--------+------------- public | testtab_changed_idx   | index | postgres | testtab | 288 kB |  public | testtab_pkey          | index | postgres | testtab | 288 kB |  public | testtab_unchanged_idx | index | postgres | testtab | 288 kB | (3 rows)

PG14查看表索引大小:

postgres=# \di+ testtab*                                                List of relations Schema |         Name          | Type  |  Owner   |  Table  | Persistence | Access method |  Size  | Description --------+-----------------------+-------+----------+---------+-------------+---------------+--------+------------- public | testtab_changed_idx   | index | postgres | testtab | permanent   | btree         | 128 kB |  public | testtab_pkey          | index | postgres | testtab | permanent   | btree         | 288 kB |  public | testtab_unchanged_idx | index | postgres | testtab | permanent   | btree         | 288 kB | (3 rows)

新建pgbench.sql文件并写入如下SQL:

\set id random_gaussian(1, 10000, 10)UPDATE testtab SET changed = changed + 1 WHERE id = :id;UPDATE testtab SET changed = changed + 1 WHERE id = :id;UPDATE testtab SET changed = changed + 1 WHERE id = :id;UPDATE testtab SET changed = changed + 1 WHERE id = :id;UPDATE testtab SET changed = changed + 1 WHERE id = :id;UPDATE testtab SET changed = changed + 1 WHERE id = :id;UPDATE testtab SET changed = changed + 1 WHERE id = :id;UPDATE testtab SET changed = changed + 1 WHERE id = :id;UPDATE testtab SET changed = changed + 1 WHERE id = :id;UPDATE testtab SET changed = changed + 1 WHERE id = :id;

分别在PG12和PG14运行如下pgbench基准测试:

time pgbench -M prepared -n -c 6 -f pgbench.sql -t 10000  postgres

查看索引及表大小:

PG12

postgres=# \di+ testtab*                                  List of relations Schema |         Name          | Type  |  Owner   |  Table  |  Size   | Description --------+-----------------------+-------+----------+---------+---------+------------- public | testtab_changed_idx   | index | postgres | testtab | 14 MB   |  public | testtab_pkey          | index | postgres | testtab | 1472 kB |  public | testtab_unchanged_idx | index | postgres | testtab | 14 MB   | (3 rows)

PG14

postgres=# \di+ testtab*                                                 List of relations Schema |         Name          | Type  |  Owner   |  Table  | Persistence | Access method |  Size   | Description --------+-----------------------+-------+----------+---------+-------------+---------------+---------+------------- public | testtab_changed_idx   | index | postgres | testtab | permanent   | btree         | 5248 kB |  public | testtab_pkey          | index | postgres | testtab | permanent   | btree         | 288 kB  |  public | testtab_unchanged_idx | index | postgres | testtab | permanent   | btree         | 512 kB  | (3 rows)


可以看到明显PG14中的索引比PG12大小小的多得多。升级!升级!升级!



预告 | 2021 PG亚洲大会12月与您相约
PG ACE计划的正式发布
三期PostgreSQL国际线上沙龙活动的举办
六期PostgreSQL国内线上沙龙活动的举办

中国PostgreSQL分会与腾讯云战略合作协议签订

中国PostgreSQL分会与美创科技战略合作协议签订
中国PostgreSQL分会与中软国际战略合作协议签订
中国PostgreSQL分会“走进”北京大学
中国PostgreSQL分会“走进”深圳大学
PGFans社区核心用户点亮计划

PostgreSQL 14.0 正式发布

深度报告:开源协议那些事儿

从“非主流”到“潮流”,开源早已值得拥有

Oracle中国正在进行新一轮裁员,传 N+6 补偿

PostgreSQL与MySQL版权比较

新闻|Babelfish使PostgreSQL直接兼容SQL Server应用程序

四年三冠,PostgreSQL再度荣获“年度数据库”

中国PostgreSQL分会入选工信部重点领域人才能力评价机构


更多新闻资讯行业动态技术热点,请关注中国PostgreSQL分会官方网站

https://www.postgresqlchina.com

中国PostgreSQL分会生态产品

https://www.pgfans.cn

中国PostgreSQL分会资源下载站

https://www.postgreshub.cn


点赞在看分享收藏

文章转载自开源软件联盟PostgreSQL分会,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论