天天看点

PostgreSQL 大版本升级方法之一 - 不落地并行导出导入

标签

PostgreSQL , 大版本升级 , rds_dbsync , pg_dump , pg_restore

https://github.com/digoal/blog/blob/master/201809/20180911_01.md#%E8%83%8C%E6%99%AF 背景

尽量快的大版本升级的方法。

https://github.com/digoal/blog/blob/master/201809/20180911_01.md#%E4%B8%8094%E4%BB%A5%E4%B8%8B%E7%89%88%E6%9C%AC%E4%BD%BF%E7%94%A8pg_dump%E5%B9%B6%E8%A1%8C%E5%AF%BC%E5%87%BApg_restore%E5%B9%B6%E8%A1%8C%E5%AF%BC%E5%85%A5%E8%BF%81%E7%A7%BB 一、9.4以下版本,使用pg_dump并行导出,pg_restore并行导入,迁移

(导出使用源版本pg_dump,导入使用目标版本pg_restore。如果是ppas请使用enterprisedb对应版本。)

1、(源库)全局元数据(用户、表空间)导出

需要superuser权限(如果你没有这个权限,跳过此步,但是务必在执行下一步时,人为在目标实例中创建所有与对象权限相关的用户)。

pg_dumpall -g -h IP地址 -p 端口 -U 用户 -W -l 数据库名  
           

2、(目标库)全局元数据导入

导入以上元数据,在目标库执行即可(通常包括创建用户,修改用户密码,创建表空间等。)  
           

执行第2步的目的是保证导入时,执行grant, alter set owner等操作时,目标用户已存在,否则缺失用户会导致pg_restore报错。

3、(目标库)建库

postgres=# create database newdb;  
CREATE DATABASE  
           

4、(目标库)插件

安装postgresql软件时,打包源库已使用的的插件。  
  
略  
           

5、(源库)导出

mkdir /data01/pg/backup  
pg_dump -j 32 -f /data01/pg/backup -F d -h IP地址 -p 端口 -U 用户 -W newdb   
           

6、(目标实例)关闭autovacuum,加速导入(可选)

使用超级用户执行SQL  
  
alter system set autovacuum=off;  
select pg_reload_conf();  
           

7、(目标库)导入

pg_restore -d newdb -F d -j 32 -h IP地址 -p 端口 -U 用户 /data01/pg/backup  
           

8、(目标实例)开启autovacuum(如果执行了6)

使用超级用户执行SQL  
  
alter system set autovacuum=on;  
select pg_reload_conf();  
           

9、(目标实例)收集统计信息(如果执行了6)

使用超级用户执行SQL,收集统计信息  
\c newdb 超级用户  
analyze;  
           

使用以上方法,60GB的数据库迁移,耗时约10分钟。

https://github.com/digoal/blog/blob/master/201809/20180911_01.md#%E5%A4%9A%E6%9C%BA%E7%8E%A9%E6%B3%95 多机玩法

https://momjian.us/main/blogs/pgblog/2018.html#September_12_2018
Where this gets interesting is with multiple hosts. You can:

$ # dump a remote database to your local machine
$ pg_dump -h remotedb.mydomain.com -f /home/postgres/dump.sql test
 
$ # dump a local database and write to a remote machine
$ pg_dump -h remotedb.mydomain.com test | ssh [email protected] 'cat > dump.sql'
 
$ # dump a remote database and write to the same remote machine
$ pg_dump -h remotedb.mydomain.com test | ssh [email protected] 'cat > dump.sql'
 
$ # or a different remote machine
$ pg_dump -h remotedb1.mydomain.com test | ssh [email protected] 'cat > dump.sql'
 




You also have similar restore options. I will use psql below but pg_restore works the same:

$ # dump a remote database and restore to your local machine
$ pg_dump -h remotedb.mydomain.com test1 | psql test2
 
$ # dump a local database and restore to a remote machine
$ pg_dump -h remotedb.mydomain.com test | ssh [email protected] 'psql test'
 
$ # dump a remote database and restore to the same remote machine
$ pg_dump -h remotedb.mydomain.com test1 | ssh [email protected] 'psql test2'
 
$ # or a different remote machine
$ pg_dump -h remotedb1.mydomain.com test | ssh [email protected] 'psql test'
           

https://github.com/digoal/blog/blob/master/201809/20180911_01.md#%E4%BA%8C94%E5%8F%8A%E4%BB%A5%E4%B8%8A%E7%89%88%E6%9C%AC%E4%BD%BF%E7%94%A8rds_dbsync%E4%B8%8D%E8%90%BD%E5%9C%B0%E5%B9%B6%E8%A1%8C%E8%BF%81%E7%A7%BB 二、9.4及以上版本,使用rds_dbsync不落地并行迁移

https://github.com/digoal/blog/blob/master/201809/20180911_01.md#%E4%B8%8994%E5%8F%8A%E4%BB%A5%E4%B8%8A%E7%89%88%E6%9C%AC%E4%BD%BF%E7%94%A8pg_logical%E5%A2%9E%E9%87%8F%E8%BF%81%E7%A7%BB 三、9.4及以上版本,使用pg_logical,增量迁移

https://github.com/digoal/blog/blob/master/201809/20180911_01.md#%E5%9B%9B10%E5%8F%8A%E4%BB%A5%E4%B8%8A%E7%89%88%E6%9C%AC%E4%BD%BF%E7%94%A8%E9%80%BB%E8%BE%91%E8%AE%A2%E9%98%85%E5%A2%9E%E9%87%8F%E8%BF%81%E7%A7%BB 四、10及以上版本,使用逻辑订阅,增量迁移

https://github.com/digoal/blog/blob/master/201809/20180911_01.md#%E4%BA%94%E4%BB%BB%E4%BD%95%E7%89%88%E6%9C%AC%E9%83%BD%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8pg_upgrade%E5%8D%87%E7%BA%A7 五、任何版本,都可以使用pg_upgrade升级

详见

《PostgreSQL 快速大版本升级多种方案 - 含重点介绍fast & safe upgrade to PostgreSQL 9.4 use pg_upgrade & zfs》 《PostgreSQL pg_upgrade 9.3 improve and example》