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

标签

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

时间: 2024-11-08 22:30:36

PostgreSQL 10.0 preview 功能增强 - 分区表(hash,range,list)的相关文章

PostgreSQL 10.0 preview 功能增强 - 分区表(list default)

标签 PostgreSQL , 10.0 , 分区表 , 默认分区 背景 PostgreSQL 10.0将支持range,list分区表,同时hash分区处于POC阶段(同时还有一些需要改进的地方,例如优化器部分). 如果你使用的是10.0以前的版本,可以使用pg_pathman插件实现分区,pg_pathman已经非常的完美. PostgreSQL支持伪表作为分区,例如外部表,物化视图.伪表作为分区有很多可以适合的使用场景,例如将外部表作为分区,则可以实现sharding场景. 分区表用法 h

PostgreSQL 10.0 preview 功能增强 - 增加access method CHECK接口amcheck

标签 PostgreSQL , 10.0 , amcheck , 逻辑一致性检测 , 物理存储检测 背景 一些高端存储.包括ZFS文件系统,在使用了RAID后,有块检测和异常块的修复功能. 对于数据库来说,数据的可靠性是非常重要的指标,例如: 1. 写进入是什么,读出来就应该是什么. 2. 当操作系统的collate发生变化时,索引的顺序可能与实际的collate顺序不匹配.造成不稳定现象. 3. 数据块partial write,可能导致数据损坏. 4. 内存页异常,使用到某些异常页时,可能带

PostgreSQL 10.0 preview 功能增强 - 后台运行(pg_background)

标签 PostgreSQL , 10.0 , 后台运行 , pg_background_launch , pg_background_result , pg_background_detach , pg_background 背景 当用户在管理数据库时,如果要在交互式界面跑一些QUERY,但是不知道QUERY要运行多久,担心网络问题或者其他问题导致终端断开,QUERY执行情况不明的话.就需要后台运行这个功能了. 后台运行在LINUX中也很常见,比如 nohup ls -la / >/tmp/re

PostgreSQL 10.0 preview 功能增强 - 逻辑订阅端 控制参数解说

标签 PostgreSQL , 10.0 , 逻辑订阅 背景 PostgreSQL 逻辑订阅相关文章请参考 <PostgreSQL 10.0 preview 变化 - 逻辑复制pg_hba.conf变化,不再使用replication条目> <PostgreSQL 10.0 preview 功能增强 - 备库支持逻辑订阅,订阅支持主备漂移了> <PostgreSQL 10.0 preview 功能增强 - 逻辑复制支持并行COPY初始化数据> <PostgreSQ

PostgreSQL 10.0 preview 功能增强 - OLAP增强 向量聚集索引(列存储扩展)

标签 PostgreSQL , 10.0 , Vertical Clustered Index (columnar store extension) , 列存储 , 向量聚集索引 背景 未来数据库OLTP+OLAP逐渐模糊化,需求逐渐融合是一个大的趋势,如果你的数据库只支持OLTP的场景,未来可能会成为业务的绊脚石. 在这方面PostgreSQL每年发布的新版本,都给用户很大的惊喜,OLTP已经具备非常强大的竞争力(性能.功能.稳定性.成熟度.案例.跨行业应用等),而OLAP方面,新增的feat

PostgreSQL 10.0 preview 功能增强 - hash index 支持wal(灾难恢复)

标签 PostgreSQL , 10.0 , hash index , wal , 灾难恢复 背景 PostgreSQL 10.0 将支持hash index WAL. 因此建hash index再也不怕数据库crash或者备库hash index不可用了. $SUBJECT will make hash indexes reliable and usable on standby. AFAIU, currently hash indexes are not recommended to be

PostgreSQL 10.0 preview 性能增强 - 分区表性能增强(plan阶段加速)

标签 PostgreSQL , 10.0 , 分区表 , 子表 , 元信息搜索性能增强 背景 PostgreSQL 10.0 增强了分区表的子表搜索性能,对于涉及分区表包含子表特别多的QUERY,可以提升性能. 性能分析 get_tabstat_entry, find_all_inheritors成为主要瓶颈. Hello. I decided to figure out whether current implementation of declarative partitioning has

PostgreSQL 10.0 preview 功能增强 - 触发器函数内置中间表

标签 PostgreSQL , 10.0 , 触发器 , 中间表 , OLD , NEW 背景 在触发器中,如果要提取触发该事件的记录,使用OLD和NEW关键字. OLD.* , NEW.* 提取 对于for statement after触发器,触发的记录数可能是很多的,PostgreSQL 10.0增加了一个功能,中间表. 在触发器函数中,可以使用这个中间表,中间表的数据就是触发器涉及的数据,中级镖的功能支持after触发器(因为after后才有全部的记录呀). 语法 [ REFERENCI

PostgreSQL 10.0 preview 功能增强 - WAL一致性校验

标签 PostgreSQL , 10.0 , WAL , wal_consistency_checking 背景 10.0 新增了一个DEBUG参数,用于检测recovery过程中,由于wal replay BUG或者备库的物理数据块异常导致的wal replay回放出来的块不正确的问题. 当产生脏页时,在wal记录中,可能有两种信息: 1. 只记录了数据变更的部分. 2. FULL PAGE,记录了整个数据块.(发生时机:当开启了full page write参数,checkpoint后第一次