Upgrading PostgreSQL From 9.6 To 11.3

對於跨版本的PostgreSQL升級,官方給出瞭如下三種方法:sql

  • pg_dumpall--把數據從舊版本中導出,而後再導入到新版本,此過程就是一個導入導出的過程。
  • Logical Replication--建立一個高版本的從庫,待數據同步完成後,主備角色互換,以達到升級目的。
  • pg_upgrade--官方推薦的快速升級方法,經過建立新的系統表並使用舊的用戶表的方式進行升級。
    本文經過pg_upgrade工具,快速將PostgreSQL 9.6升級到11.3。其中二進制軟件包都是使用官方YUM源。

    一、安裝PostgreSQL 11.3並初始化

    [root@db04 ~]# 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
    [postgres@db04 ~]$ /usr/pgsql-11/bin/initdb -D /var/lib/pgsql/11/data

    二、複製舊版本的配置文件

    [postgres@db04 ~]$ cp /var/lib/pgsql/9.6/data/postgresql.conf /var/lib/pgsql/11/data
    [postgres@db04 ~]$ cp /var/lib/pgsql/9.6/data/pg_hba.conf /var/lib/pgsql/11/data

    三、中止舊版本服務

    [root@db04 ~]# systemctl stop postgresql-9.6

    四、使用pg_upgrade升級

    pg_upgrade的參數能夠參考官方文檔。數據庫

[postgres@db04 ~]$ /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 ×××tall 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 ×××tall 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,耗時兩分鐘完成升級。api

五、收集統計信息

在升級過程當中不會把統計信息傳到新庫系統表中,因此須要從新收集統計信息。根據升級過程當中產生的腳本,生成新的統計信息。ide

[postgres@db04 ~]$  ./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

六、刪除舊庫以及軟件包

使用升級過程當中產生的刪除腳本清楚舊庫信息並使用yum刪除舊軟件包。工具

[postgres@db04 ~]$ ./delete_old_cluster.sh
[root@db04 ~]# yum -y remove postgresql96*
相關文章
相關標籤/搜索