PostgreSQL 10.0 preview 功能增强 - 分区表(hash,range,list)

本文涉及的产品
PolarDB Agent Express,2核4GB
PolarSearch,搜索节点 4核8GB
RDS AI 助手,专业版
简介:

标签

PostgreSQL , 10.0 , 分区表


背景

PostgreSQL 10.0将支持range,list分区表,同时hash分区处于POC阶段(同时还有一些需要改进的地方,例如优化器部分)。

如果你使用的是10.0以前的版本,可以使用pg_pathman插件实现分区,pg_pathman已经非常的完美。

PostgreSQL支持伪表作为分区,例如外部表,物化视图。伪表作为分区有很多可以适合的使用场景,例如将外部表作为分区,则可以实现sharding场景。

分区表用法

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

《PostgreSQL 10.0 内置分区表》

hash poc 如下

Hi all,  

Now we have a declarative partitioning, but hash partitioning is not  
implemented yet. Attached is a POC patch to add the hash partitioning  
feature. I know we will need more discussions about the syntax and other  
specifications before going ahead the project, but I think this runnable  
code might help to discuss what and how we implement this.  

* Description  

In this patch, the hash partitioning implementation is basically based  
on the list partitioning mechanism. However, partition bounds cannot be  
specified explicitly, but this is used internally as hash partition  
index, which is calculated when a partition is created or attached.  

The tentative syntax to create a partitioned table is as bellow;  

 CREATE TABLE h (i int) PARTITION BY HASH(i) PARTITIONS 3 USING hashint4;  

The number of partitions is specified by PARTITIONS, which is currently  
constant and cannot be changed, but I think this is needed to be changed in  
some manner. A hash function is specified by USING. Maybe, specifying hash  
function may be ommitted, and in this case, a default hash function  
corresponding to key type will be used.  

A partition table can be create as bellow;  

 CREATE TABLE h1 PARTITION OF h;  
 CREATE TABLE h2 PARTITION OF h;  
 CREATE TABLE h3 PARTITION OF h;  

FOR VALUES clause cannot be used, and the partition bound is  
calclulated automatically as partition index of single integer value.  

When trying create partitions more than the number specified  
by PARTITIONS, it gets an error.  

postgres=# create table h4 partition of h;  
ERROR:  cannot create hash partition more than 3 for h  

An inserted record is stored in a partition whose index equals  
abs(hashfunc(key)) % <number_of_partitions>. In the above  
example, this is abs(hashint4(i))%3.  

postgres=# insert into h (select generate_series(0,20));  
INSERT 0 21  

postgres=# select *,tableoid::regclass from h;  
 i  | tableoid   
----+----------  
  0 | h1  
  1 | h1  
  2 | h1  
  4 | h1  
  8 | h1  
 10 | h1  
 11 | h1  
 14 | h1  
 15 | h1  
 17 | h1  
 20 | h1  
  5 | h2  
 12 | h2  
 13 | h2  
 16 | h2  
 19 | h2  
  3 | h3  
  6 | h3  
  7 | h3  
  9 | h3  
 18 | h3  
(21 rows)  

* Todo / discussions  

In this patch, we cannot change the number of partitions specified  
by PARTITIONS. I we can change this, the partitioning rule  
(<partition index> = abs(hashfunc(key)) % <number_of_partitions>)  
is also changed and then we need reallocatiing records between  
partitions.  

In this patch, user can specify a hash function USING. However,  
we migth need default hash functions which are useful and  
proper for hash partitioning.   

Currently, even when we issue SELECT query with a condition,  
postgres looks into all partitions regardless of each partition's  
constraint, because this is complicated such like "abs(hashint4(i))%3 = 0".  

postgres=# explain select * from h where i = 10;  
                        QUERY PLAN                          
----------------------------------------------------------  
 Append  (cost=0.00..125.62 rows=40 width=4)  
   ->  Seq Scan on h  (cost=0.00..0.00 rows=1 width=4)  
         Filter: (i = 10)  
   ->  Seq Scan on h1  (cost=0.00..41.88 rows=13 width=4)  
         Filter: (i = 10)  
   ->  Seq Scan on h2  (cost=0.00..41.88 rows=13 width=4)  
         Filter: (i = 10)  
   ->  Seq Scan on h3  (cost=0.00..41.88 rows=13 width=4)  
         Filter: (i = 10)  
(9 rows)  

However, if we modify a condition into a same expression  
as the partitions constraint, postgres can exclude unrelated  
table from search targets. So, we might avoid the problem  
by converting the qual properly before calling predicate_refuted_by().  

postgres=# explain select * from h where abs(hashint4(i))%3 = abs(hashint4(10))%3;  
                        QUERY PLAN                          
----------------------------------------------------------  
 Append  (cost=0.00..61.00 rows=14 width=4)  
   ->  Seq Scan on h  (cost=0.00..0.00 rows=1 width=4)  
         Filter: ((abs(hashint4(i)) % 3) = 2)  
   ->  Seq Scan on h3  (cost=0.00..61.00 rows=13 width=4)  
         Filter: ((abs(hashint4(i)) % 3) = 2)  
(5 rows)  

Best regards,  
Yugo Nagata  

--   
Yugo Nagata <nagata(at)sraoss(dot)co(dot)jp>  

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

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

参考

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

https://www.postgresql.org/message-id/flat/20170228233313.fc14d8b6.nagata@sraoss.co.jp#20170228233313.fc14d8b6.nagata@sraoss.co.jp

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
安全
List集合特有功能
List集合特有功能
193 2
使用Vant框架的组件van-pull-refresh搭配van-list和van-card完成上滑加载更多列表数据,下拉刷新当前列表数据(等同于翻页功能)
使用Vant框架的组件van-pull-refresh搭配van-list和van-card完成上滑加载更多列表数据,下拉刷新当前列表数据(等同于翻页功能)
|
Python
Python list列表中每一个数字乘于2的两种多功能函数代码设计
要让Python中list列表中的每一个数字乘于2,可以有两种方法,一是使用列表推导式来完成,另一种是使用map()函数来完成。下面将通过实例代码来之一进行介绍。
341 2
|
SQL 存储 弹性计算
2022云栖精选—云数据库RDS重磅功能发布与最佳实践
彭祥 阿里云数据库事业部资深技术专家 RDS产品部负责人 许鸿斌 阿里云数据库事业部高级产品专家
2022云栖精选—云数据库RDS重磅功能发布与最佳实践
|
存储 监控 Kubernetes
【实操系列】 AnalyticDB PostgreSQL发布实例暂停功能,助力成本优化
本文将对AnalyticDB PostgreSQL产品的暂停功能以及其背后的实现机制和最佳实践做详细介绍。
1341 3
【实操系列】 AnalyticDB PostgreSQL发布实例暂停功能,助力成本优化
|
NoSQL Java Redis
(Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
(Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
1247 1
(Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
|
SQL 弹性计算 关系型数据库
RDS SQL Server 备份功能
RDS SQL Server 备份功能。 您可以通过设置备份策略调整RDS数据备份和日志备份的周期来实现自动备份,也可以手动备份RDS数据。
|
关系型数据库 分布式数据库 PolarDB
《PolarDB for PostgreSQL三节点功能介绍》电子版地址
PolarDB for PostgreSQL三节点功能介绍
1271 0
《PolarDB for PostgreSQL三节点功能介绍》电子版地址
|
弹性计算 资源调度 运维
【实操系列】 AnalyticDB PostgreSQL发布实例计划管理功能,实现资源分时弹性&分时启停
本文将对AnalyticDB PostgreSQL产品的计划任务管理功能以及其背后的实现机制和最佳实践做详细介绍。
【实操系列】 AnalyticDB PostgreSQL发布实例计划管理功能,实现资源分时弹性&分时启停
|
存储 JSON 监控
Redis - Redis List 功能详解与工业应用
Redis 列表 List 是简单的字符串列表,按照插入顺序排序,一个列表最多可以包含 232- 1 个元素 (4294967295, 每个列表超过40亿个元素)。下面介绍下 Redis List 常用功能以及在工业场景下 Redis List 的几种使用场景。......
528 0
Redis - Redis List 功能详解与工业应用

相关产品

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

    更多