PostgreSQL 9.6 并行计算 在 xfs, ext4 下的表现对比

2 minute read

背景

ext4在红帽5,6的版本中作为主流的文件系统,有非常多的用户群体,也能满足大多数的应用场景需求。

XFS经过几年的发展,从红帽7开始,XFS会成为默认的文件系统。

同时XFS在高并发的场景相比EXT4有较明显的优势,前面我对单机多实例也做过测试,使用EXT4和CGROUP容易遇到IO HANG的问题,而XFS几乎没有这样的问题。

本文将对EXT4和XFS进行测试比较,两者在PostgreSQL采用并行计算是在并行度上的性能差异。

文件系统格式化与mount选项

文件系统的格式化参数以及挂载参数如下
1. XFS

mkfs.xfs -f -b size=4096 -l logdev=/dev/dfc1,size=2047868928,sunit=16 -d agsize=536862720 /dev/dfc2

/dev/dfc2 /u03 xfs defaults,allocsize=16M,inode64,nobarrier,nolargeio,logbsize=262144,noatime,nodiratime,swalloc,logdev=/dev/dfc1 0 0

2. EXT4

mkfs.ext4 /dev/dfc1

e2label /dev/dfc1 u03

LABEL=u03               /u03            ext4            defaults,noatime,nodiratime,nodelalloc,barrier=0,data=writeback 0 0

测试数据

数据量16亿,共90GB。

创建一张测试表,包含一个比特位字段,后面用于测试。

postgres=# create unlogged table t_bit2 (id bit(200)) with (autovacuum_enabled=off, parallel_workers=128);
CREATE TABLE

并行插入16亿记录

for ((i=1;i<=40;i++)) ; do psql -c "insert into t_bit2 select B'10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010' from generate_series(1,40000000);" & done

单表16亿,90GB

postgres=# \dt+
                     List of relations
 Schema |  Name  | Type  |  Owner   |  Size  | Description 
--------+--------+-------+----------+--------+-------------
 public | t_bit2 | table | postgres |  90 GB | 

聚合测试

对比在聚合测试时,XFS与EXT4的拐点。

数据全内存缓存,排除IO瓶颈。

1. xfs
测试发现直到22个并发,性能才开始出现拐点,并且在64个并发时回落没有EXT4那么明显。

postgres=# set max_parallel_workers_per_gather =64 ;  
postgres=# select count(*) from t_bit2 ;
   count    
------------
 1600000000
(1 row)
Time: 18310.130 ms

postgres=# set max_parallel_workers_per_gather =32;
postgres=# select count(*) from t_bit2 ;
   count    
------------
 1600000000
(1 row)
Time: 21144.919 ms

postgres=# set max_parallel_workers_per_gather =17;
postgres=# select count(*) from t_bit2 ;
   count    
------------
 1600000000
(1 row)
Time: 8905.510 ms

postgres=# set max_parallel_workers_per_gather =21;
postgres=# select count(*) from t_bit2;
   count    
------------
 1600000000
(1 row)
Time: 7583.344 ms

2. ext4
测试发现直到17个并发,性能开始出现拐点。

postgres=# set max_parallel_workers_per_gather =64;
postgres=# select count(*) from t_bit2 ;
   count    
------------
 1600000000
(1 row)
Time: 32580.853 ms

postgres=# set max_parallel_workers_per_gather =32;
postgres=# select count(*) from t_bit2 ;
   count    
------------
 1600000000
(1 row)
Time: 30209.980 ms

postgres=# set max_parallel_workers_per_gather =17;
postgres=# select count(*) from t_bit2 ;
   count    
------------
 1600000000
(1 row)
Time: 9313.369 ms

从测试结果来看,XFS要优于EXT4,主要体现在可以做到更高的并发,以及更好的性能。

测试环境是centos 6,如果是7的话,XFS表现可能还会更好。

分析

XFS为什么在高并发时更好?

1. 组策略,对并行的写I/O有较大帮助(如可以在多个组里面并行分配block和inode),格式化时的agcount选项。

所以, XFS对于高并发的写入优势会更加明显,例如单机多实例,或者Greenplum,都是典型的应用场景。

2. 分配组
XFS文件系统内部被分为多个“分配组”,它们是文件系统中的等长线性存储区。

每个分配组各自管理自己的inode和剩余空间。文件和文件夹可以跨越分配组。

这一机制为XFS提供了可伸缩性和并行特性 —— 多个线程和进程可以同时在同一个文件系统上执行I/O操作。

这种由分配组带来的内部分区机制在一个文件系统跨越多个物理设备时特别有用,使得优化对下级存储部件的吞吐量利用率成为可能。

参考

1. http://baike.baidu.com/view/1222157.htm

2. man xfs

agcount=value

This is used to specify the number of allocation groups. 
The data section of the filesystem is divided into allocation groups to improve the performance of XFS. More allocation groups imply that more  parallelism  can  be  achieved  when  allocating blocks and inodes. 
The minimum allocation group size is 16 MiB; 
the maximum size is just under 1 TiB.  
The data section of the filesystem is divided into value allocation groups (default value is scaled automatically based on the underlying device size).

agsize=value

This is an alternative to using the agcount suboption. 
The value is the desired size of the allocation group expressed in bytes (usually using the m or g suffixes).  
This value must be a  multiple  of  the filesystem  block  size,  and  must  be  at least 16MiB, and no more than 1TiB, and may be automatically adjusted to properly align with the stripe geometry.  
The agcount and agsize suboptions are mutually exclusive.  

Flag Counter

digoal’s 大量PostgreSQL文章入口