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

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

标签

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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
22天前
|
关系型数据库 Serverless 分布式数据库
【公测】PolarDB PostgreSQL版Serverless功能免费使用​!
【公测】PolarDB PostgreSQL版Serverless功能免费使用​,公测于2024年3月28日开始,持续三个月,公测期间可以免费使用!
|
3月前
|
存储 关系型数据库 MySQL
PolarDB优势功能
PolarDB优势功能
|
6月前
|
存储 关系型数据库 数据库
深入了解 PostgreSQL:功能、特性和部署
PostgreSQL,通常简称为Postgres,是一款强大且开源的关系型数据库管理系统(RDBMS),它在数据存储和处理方面提供了广泛的功能和灵活性。本文将详细介绍 PostgreSQL 的功能、特性以及如何部署和使用它。
226 1
深入了解 PostgreSQL:功能、特性和部署
|
7月前
|
SQL 关系型数据库 测试技术
PolarDB的Online DDL功能验证实验
本场景带您体验如何在PolarDB-X中进行Online DDL。
954 0
|
1月前
|
关系型数据库 Serverless 分布式数据库
PolarDB PostgreSQL版Serverless功能上线公测啦,公测期间免费使用!
Serverless数据库能够使得数据库集群资源随客户业务负载动态弹性扩缩,将客户从复杂的业务资源评估和运维工作中解放出来。PolarDB PostgreSQL版 Serverless提供了CPU、内存、存储、网络资源的实时弹性能力,构建计算与存储分离架构下的 PolarDB PostgreSQL版产品新形态。
|
2月前
|
SQL 关系型数据库 分布式数据库
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
43 7
|
2月前
|
关系型数据库 Linux Shell
Centos系统上安装PostgreSQL和常用PostgreSQL功能
Centos系统上安装PostgreSQL和常用PostgreSQL功能
|
3月前
|
关系型数据库 MySQL 分布式数据库
PolarDB MySQL企业版与标准版功能对比:如何选择适合您的版本?
随着数字化时代的到来,企业对于数据处理的需求越来越高,而数据库作为数据处理的核心,其性能和成本成为了企业关注的焦点。阿里云全新推出的PolarDB MySQL企业版和标准版,以全新的架构和优化,为企业提供了高性能、低成本的数据库解决方案。但在功能上,这两个版本有很多差异,我们该如何选择呢?
57 2
|
3月前
|
SQL 关系型数据库 分布式数据库
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能【1月更文挑战第13天】【1月更文挑战第65篇】
30 2
|
3月前
|
关系型数据库 MySQL 分布式数据库
PolarDB MySQL版:强大功能,超高性能,满足企业级需求
PolarDB MySQL版:强大功能,超高性能,满足企业级需求 在当今信息化社会,企业对于数据处理的需求日益增长,如何选择一款高性能、高可靠性且成本合理的数据库成为了一大挑战。阿里巴巴的PolarDB MySQL版应运而生,它不仅兼容MySQL,还具备传统数据库所不具备的优势,为企业提供了更高效、更可靠的数据处理方案。
107 3

相关产品

  • 云原生数据库 PolarDB