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

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介: PostgreSQL数据库备份之pg_dump并行备份。
 在数据库导出的时候,可以通过 -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

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

相关文章
|
2天前
|
NoSQL 关系型数据库 MySQL
微服务架构下的数据库选择:MySQL、PostgreSQL 还是 NoSQL?
在微服务架构中,数据库的选择至关重要。不同类型的数据库适用于不同的需求和场景。在本文章中,我们将深入探讨传统的关系型数据库(如 MySQL 和 PostgreSQL)与现代 NoSQL 数据库的优劣势,并分析在微服务架构下的最佳实践。
|
6天前
|
关系型数据库 MySQL 数据库
Navicat备份数据库
涵盖`Navicat`数据库备份、数据安全及备份策略等主题。文库采用精美主题,提升阅读体验。
11 1
Navicat备份数据库
|
16天前
|
关系型数据库 分布式数据库 数据库
开源云原生数据库PolarDB PostgreSQL 15兼容版本正式发布
PolarDB进行了深度的内核优化,从而实现以更低的成本提供商业数据库的性能。
|
10天前
|
SQL 数据库 数据安全/隐私保护
如何手动备份数据库?
如何手动备份数据库?
34 1
|
27天前
|
SQL 数据库
Microsoft SQL Server 2014如何来备份数据库
Microsoft SQL Server 2014如何来备份数据库
32 3
|
11天前
|
关系型数据库 数据库 网络虚拟化
Docker环境下重启PostgreSQL数据库服务的全面指南与代码示例
由于时间和空间限制,我将在后续的回答中分别涉及到“Python中采用lasso、SCAD、LARS技术分析棒球运动员薪资的案例集锦”以及“Docker环境下重启PostgreSQL数据库服务的全面指南与代码示例”。如果你有任何一个问题的优先顺序或需要立即回答的,请告知。
21 0
|
19天前
|
SQL 关系型数据库 MySQL
SQL Server、MySQL、PostgreSQL:主流数据库SQL语法异同比较——深入探讨数据类型、分页查询、表创建与数据插入、函数和索引等关键语法差异,为跨数据库开发提供实用指导
【8月更文挑战第31天】SQL Server、MySQL和PostgreSQL是当今最流行的关系型数据库管理系统,均使用SQL作为查询语言,但在语法和功能实现上存在差异。本文将比较它们在数据类型、分页查询、创建和插入数据以及函数和索引等方面的异同,帮助开发者更好地理解和使用这些数据库。尽管它们共用SQL语言,但每个系统都有独特的语法规则,了解这些差异有助于提升开发效率和项目成功率。
88 0
|
27天前
|
安全 关系型数据库 数据库
跟我来学如何保护PostgreSQL数据库
跟我来学如何保护PostgreSQL数据库
22 0
|
27天前
|
SQL 关系型数据库 数据库
手把手教你管理PostgreSQL数据库及其对象
手把手教你管理PostgreSQL数据库及其对象
21 0
|
27天前
|
关系型数据库 MySQL Shell
分享一篇mysql数据库备份脚本
分享一篇mysql数据库备份脚本
21 0