PostgreSQL数据库备份之pg_dump并行备份

4150阅读 0评论2015-09-16 jackson198574
分类:Mysql/postgreSQL

  

 在数据库导出的时候,可以通过 -j 参数设置并行度,可以在一定程度上提高备份的速度,代价是消耗更多的系统资源。



话不多说,咱们边走边聊,实验走起!~~~

操作过程:


找一个大一点的表,里面有200万条数据:
music=# select count(*) from test;
  count  
---------
 2000005
(1 行记录)

数据库里仅有一张2G左右的表:
music=# select pg_size_pretty(pg_relation_size('test'));
 pg_size_pretty 
----------------
 1968 MB
(1 行记录)


单进程导出:

开始导出music数据库:


-bash-3.2$ date
2015年 07月 30日 星期四 13:39:14 CST
-bash-3.2$ pg_dump music >/tmp/music20150916.dmp
-bash-3.2$ date 
2015年 07月 30日 星期四 13:39:44 CST


在单行导出的时候看到的后台进程:
[root@dbserver music]# ps -ef|grep postgres
postgres  3540     1  0 12:52 ?        00:00:00 /usr/pgsql-9.4/bin/postmaster -D /var/lib/pgsql/9.4/data
postgres  3542  3540  0 12:52 ?        00:00:00 postgres: logger process                                
postgres  3544  3540  0 12:52 ?        00:00:03 postgres: checkpointer process                          
postgres  3545  3540  0 12:52 ?        00:00:00 postgres: writer process                                
postgres  3546  3540  0 12:52 ?        00:00:01 postgres: wal writer process                            
postgres  3547  3540  0 12:52 ?        00:00:00 postgres: autovacuum launcher process                   
postgres  3548  3540  0 12:52 ?        00:00:00 postgres: archiver process   last was 0000000100000001000000B0
postgres  3549  3540  0 12:52 ?        00:00:00 postgres: stats collector process                       
root      5444  5030  0 13:28 pts/0    00:00:00 su - postgres
postgres  5445  5444  0 13:28 pts/0    00:00:00 -bash
postgres  5955  3540  0 13:35 ?        00:00:00 postgres: autovacuum worker process   music             
postgres  6143  5445  1 13:39 pts/0    00:00:00 pg_dump music
postgres  6144  3540 38 13:39 ?        00:00:04 postgres: postgres music [local] COPY                   
root      6151  5515  0 13:39 pts/1    00:00:00 grep postgres



导出的时间是30毫秒。


并行导出:

因为测试系统的CPU是设置的2个单核CPU,因此设置成2度的并行:
-bash-3.2$ date
2015年 07月 30日 星期四 13:41:23 CST
-bash-3.2$ pg_dump music -j 2 -Fd -f music_j2
-bash-3.2$ date
2015年 07月 30日 星期四 13:41:44 CST



导出时间是11毫秒。


在并行导出的时候看到后台的进程:
[root@dbserver music]# ps -ef|grep postgres
postgres  3540     1  0 12:52 ?        00:00:00 /usr/pgsql-9.4/bin/postmaster -D /var/lib/pgsql/9.4/data
postgres  3542  3540  0 12:52 ?        00:00:00 postgres: logger process                                
postgres  3544  3540  0 12:52 ?        00:00:03 postgres: checkpointer process                          
postgres  3545  3540  0 12:52 ?        00:00:00 postgres: writer process                                
postgres  3546  3540  0 12:52 ?        00:00:01 postgres: wal writer process                            
postgres  3547  3540  0 12:52 ?        00:00:00 postgres: autovacuum launcher process                   
postgres  3548  3540  0 12:52 ?        00:00:00 postgres: archiver process   last was 0000000100000001000000B3
postgres  3549  3540  0 12:52 ?        00:00:00 postgres: stats collector process                       
root      5444  5030  0 13:28 pts/0    00:00:00 su - postgres
postgres  5445  5444  0 13:28 pts/0    00:00:00 -bash
postgres  5955  3540  0 13:35 ?        00:00:00 postgres: autovacuum worker process   music             
postgres  6180  5445  0 13:41 pts/0    00:00:00 pg_dump music -j 2 -Fd -f music_j2
postgres  6181  3540  1 13:41 ?        00:00:00 postgres: postgres music [local] idle in transaction    
postgres  6182  6180 52 13:41 pts/0    00:00:06 pg_dump music -j 2 -Fd -f music_j2
postgres  6183  3540 26 13:41 ?        00:00:03 postgres: postgres music [local] COPY                   
postgres  6184  6180  0 13:41 pts/0    00:00:00 pg_dump music -j 2 -Fd -f music_j2
postgres  6185  3540  0 13:41 ?        00:00:00 postgres: postgres music [local] idle in transaction    
root      6190  5515  0 13:41 pts/1    00:00:00 grep postgres


由于数据类型较单一并且数据量较小,且使用的是固态硬盘,因此时间差别不是特别明显,有意使用者可在数据量大和数据类型丰富的环境中验证其导出效果。




上一篇:ACOUG 2015 中国行济南站活动小记
下一篇:PostgreSQL碎片空间回收---vacuum