Upgrading PostgreSQL From 9.6 To 11

对于跨版本的PostgreSQL升级,官方给出了以下三种方法:

  • pg_dumpall--把数据从旧版本中导出,然后再导入到新版本,此过程就是一个导入导出的过程。
  • Logical Replication--创建一个高版本的从库,待数据同步完成后,主备角色互换,以达到升级目的。
  • pg_upgrade--官方推荐的快速升级方法,通过创建新的系统表并使用旧的用户表的方式进行升级。
    本文通过pg_upgrade工具,快速将PostgreSQL 9.6升级到11.3。其中二进制软件包都是使用官方YUM源。

    1、安装PostgreSQL 11.3并初始化

    [[email protected] ~]# yum install postgresql11.x86_64 postgresql11-contrib.x86_64 postgresql11-devel.x86_64 postgresql11-docs.x86_64 postgresql11-libs.x86_64 postgresql11-pltcl.x86_64 postgresql11-server.x86_64
    [[email protected] ~]$ /usr/pgsql-11/bin/initdb -D /var/lib/pgsql/11/data

    2、复制旧版本的配置文件

    [[email protected] ~]$ cp /var/lib/pgsql/9.6/data/postgresql.conf /var/lib/pgsql/11/data
    [[email protected] ~]$ cp /var/lib/pgsql/9.6/data/pg_hba.conf /var/lib/pgsql/11/data

    3、停止旧版本服务

    [[email protected] ~]# systemctl stop postgresql-9.6

    4、使用pg_upgrade升级

    pg_upgrade的参数可以参考官方文档。

[[email protected] ~]$ /usr/pgsql-11/bin/pg_upgrade -b /usr/pgsql-9.6/bin -B /usr/pgsql-11/bin -d /var/lib/pgsql/9.6/data -D /var/lib/pgsql/11/data
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for invalid "unknown" user columns                 ok
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows in the new cluster                        ok
Deleting files from new pg_xact                             ok
Copying old pg_clog to new server                           ok
Setting next transaction ID and epoch for new cluster       ok
Deleting files from new pg_multixact/offsets                ok
Copying old pg_multixact/offsets to new server              ok
Deleting files from new pg_multixact/members                ok
Copying old pg_multixact/members to new server              ok
Setting next multixact ID and offset for new cluster        ok
Resetting WAL archives                                      ok
Setting frozenxid and minmxid counters in new cluster       ok
Restoring global objects in the new cluster                 ok
Restoring database schemas in the new cluster
                                                            ok
Copying user relation files
                                                            ok
Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to analyze new cluster                      ok
Creating script to delete old cluster                       ok
Checking for hash indexes                                   ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
    ./analyze_new_cluster.sh

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh

整个数据库大约12G,耗时两分钟完成升级。

5、收集统计信息

在升级过程中不会把统计信息传到新库系统表中,所以需要重新收集统计信息。根据升级过程中产生的脚本,生成新的统计信息。

[[email protected] ~]$  ./analyze_new_cluster.sh
This script will generate minimal optimizer statistics rapidly
so your system is usable, and then gather statistics twice more
with increasing accuracy.  When it is done, your system will
have the default level of optimizer statistics.

If you have used ALTER TABLE to modify the statistics target for
any tables, you might want to remove them and restore them after
running this script because they will delay fast statistics generation.

If you would like default statistics as quickly as possible, cancel
this script and run:
    "/usr/pgsql-11/bin/vacuumdb" --all --analyze-only

vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "rhnschema": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "rhnschema": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "rhnschema": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics

Done

6、删除旧库以及软件包

使用升级过程中产生的删除脚本清楚旧库信息并使用yum删除旧软件包。

[[email protected] ~]$ ./delete_old_cluster.sh
[[email protected] ~]# yum -y remove postgresql96*

猜你喜欢

转载自blog.51cto.com/candon123/2407147
0条评论
添加一条新回复