首页  :: 新随笔  :: 管理

PostgreSQL 9.6升级到14.5(pg_upgrade)

Posted on 2022-09-23 17:08  高&玉  阅读(1061)  评论(0编辑  收藏  举报

前言

提示:如果是PostgreSQL复制流,需要先升级primary然后升级standby,否则会报错:

[postgres@host72 postgres]$ /usr/local/postgresql-10.22/bin/pg_upgrade -b /usr/local/postgresql-9.6.0/bin/ -B /usr/local/postgresql-10.22/bin/ -d /data/postgres_9.6/ -D /data/postgres_10.22/ -j 8 -p 5432 -P 5432 -k -c
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok

The source cluster was shut down while in recovery mode.  To upgrade, use "rsync" as documented or shut it down as a primary.
Failure, exiting

 

原版本:PostgreSQL 9.6.0

目标版本:PostgreSQL 14.5

源码安装PG 14.5

安装依赖包

[root]# yum install -y perl-ExtUtils-Embed python-devel bison flex readline-devel zlib-devel gcc gcc-c++ wget

源码安装uuid

[root]# wget ftp://ftp.ossp.org/pkg/lib/uuid/uuid-1.6.2.tar.gz
[root]# tar -zxvf uuid-1.6.2.tar.gz
[root]# cd uuid-1.6.2
[root]# ./configure --with-uuid=ossp
[root]# make && make install

源码安装PG 14.5

[root]# wget --no-check-certificate https://ftp.postgresql.org/pub/source/v14.5/postgresql-14.5.tar.gz
[root]# tar -zxvf postgresql-14.5.tar.gz
[root]# cd postgresql-14.5
[root]# ./configure --prefix=/usr/local/postgresql-14.5 --with-perl --with-python --enable-thread-safety --with-uuid=ossp
[root]# make && make install

安装contrib工具

[root]# cd postgresql-14.5/contrib
[root]# make && make install

 更新安装目录用户及属组

[root]# chown -R postgres:postgres /usr/local/postgresql-14.5

软链接libuuid.so.16

[root]# ln -s /usr/local/lib/libuuid.so.16 /usr/local/postgresql-14.5/lib/

创建PGDATA目录

[root]# mkdir /data/postgres_14.5
[root]# chown postgres:postgres /data/postgres_14.5
[root]# chmod 0700 /data/postgres_14.5

初始化postgres

[postgres]# /usr/local/postgresql-14.5/bin/initdb -D /data/postgres_14.5
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are enabled.

fixing permissions on existing directory /data/postgres_14.5 ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /data/postgres_14.5 -l logfile start

在线修改wal-segsize值为64M(缺省是16M)

[postgres]# pg_ctl start
[postgres]# /usr/local/postgresql-14.5/bin/pg_resetwal --wal-segsize=64 /data/postgres_14.5
Write-ahead log reset

升级primary

安装PostgreSQL 14.5,切记安装目录区分开

PG 9.6.0安装路径:/usr/local/postgresql-9.6.0,然后使用软链接到/usr/local/postgresql

PG 14.5安装路径: /usr/local/postgresql-14.5

 

关闭postgres服务

[postgres]# pg_ctl stop

使用14.5进行升级前检查(-c是check的意思)

[postgres@host71 postgres]# /usr/local/postgresql-14.5/bin/pg_upgrade -b /usr/local/postgresql-9.6.0/bin/ -B /usr/local/postgresql-14.5/bin/ -d /data/postgres_9.6/ -D /data/postgres_14.5/ -j 8 -p 5432 -P 5432 -c -k
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 system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for incompatible polymorphic functions             ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Checking for invalid "unknown" user columns                 ok
Checking for hash indexes                                   ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok

*Clusters are compatible*

上面检查无问题后,下面进行升级(去掉-c参数)

[postgres@host71 postgres]$ /usr/local/postgresql-14.5/bin/pg_upgrade -b /usr/local/postgresql-9.6.0/bin/ -B /usr/local/postgresql-14.5/bin/ -d /data/postgres_9.6/ -D /data/postgres_14.5/ -j 8 -p 5432 -P 5432 -k
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 system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for incompatible polymorphic functions             ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          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
Checking for new cluster tablespace directories             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 oldest XID for new cluster                          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
Adding ".old" suffix to old global/pg_control               ok

If you want to start the old cluster, you will need to remove
the ".old" suffix from /data/postgres_9.6/global/pg_control.old.
Because "link" mode was used, the old cluster cannot be safely
started once the new cluster has been started.

Linking user relation files
                                                            ok
Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to delete old cluster                       ok
Checking for hash indexes                                   ok
Checking for extension updates                              ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade.
Once you start the new server, consider running:
    /usr/local/postgresql-14.5/bin/vacuumdb --all --analyze-in-stages

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

修改PGDATA软链接指向新版本

[root]# unlink /data/postgres
[root]# ln -s /data/postgres_14.5 /data/postgres

修改安装包的软链接

[root]# unlink /usr/local/postgresql
[root]# ln -s /usr/local/postgresql-14.5 /usr/local/postgresql

与版本对比调整相关参数文件、启动14.5版本数据库、统计信息收集

[postgres]# vi $PGDATA/postgresql.conf
[postgres]# vi $PGDATA/pg_hba.conf
[postgres]# pg_ctl start
[postgres]# vacuumdb --all --analyze-in-stages

升级standby

升级standby端,两种方法使用pg_upgrade升级或者直接安装全新版本,最后始终都需要重新构建复制流。

 

关闭postgres服务

[postgres]# pg_ctl stop

PG 14.5升级前检查(-c是check的意思)

[postgres]# /usr/local/postgresql-10.22/bin/pg_upgrade -b /usr/local/postgresql-9.6.0/bin/ -B /usr/local/postgresql-10.22/bin/ -d /data/postgres_9.6/ -D /data/postgres_10.22/ -j 8 -p 5432 -P 5432 -k -c
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 system-defined composite types in user tables  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
Checking for hash indexes                                   ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok

*Clusters are compatible*

升级(去掉-c参数)

[postgres@host72 postgres]# /usr/local/postgresql-10.22/bin/pg_upgrade -b /usr/local/postgresql-9.6.0/bin/ -B /usr/local/postgresql-10.22/bin/ -d /data/postgres_9.6/ -D /data/postgres_10.22/ -j 8 -p 5432 -P 5432 -k
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 system-defined composite types in user tables  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
Checking for new cluster tablespace directories             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 oldest XID for new cluster                          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
Adding ".old" suffix to old global/pg_control               ok

If you want to start the old cluster, you will need to remove
the ".old" suffix from /data/postgres_9.6/global/pg_control.old.
Because "link" mode was used, the old cluster cannot be safely
started once the new cluster has been started.

Linking 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
Checking for extension updates                              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

修改相关软链接

[root]# unlink /data/postgres
[root]# ln -s /data/postgres_14.5 /data/postgres
[root]# unlink /usr/local/postgresql
[root]# ln -s /usr/local/postgresql-14.5 /usr/local/postgresql

然后根据实际情况相关参数。

构建复制流

同步primary端的$PGDATA到standby端$PGDATA(可使用pg_basebackup和rsync进行同步。)

[postgres]# rsync -avz postgres@192.168.1.71:/data/postgres_14.5/ /data/postgres_14.5

standby端创建standby.signal

[postgres]# touch $PGDATA/standby.signal
[postgres]# chmod 0600 $PGDATA/standby.signal

standby端将复制流primary_conninfo配置到postgresql.auto.conf中

[postgres]# vi $PGDATA/postgresql.auto.conf
primary_conninfo = 'host=192.168.1.71 port=5432 user=repl password=postgres'

standby端启动standby端postgres

[postgres]# pg_ctl start

 

primary端查看复制流信息

postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid              | 51595
usesysid         | 16384
usename          | repl
application_name | walreceiver
client_addr      | 192.168.1.72
client_hostname  | host71
client_port      | 35932
backend_start    | 2022-09-23 10:48:04.634275+08
backend_xmin     | 
state            | streaming
sent_location    | 2/1C0000D0
write_location   | 2/1C0000D0
flush_location   | 2/1C0000D0
replay_location  | 2/1C000000
sync_priority    | 0
sync_state       | async

升级期间遇到的问题

升级检查报错

升级前检查

[postgres@host71 postgres]$ /usr/local/postgresql-14.5/bin/pg_upgrade -b /usr/local/postgresql-9.6.0/bin/ -B /usr/local/postgresql-14.5/bin/ -d /data/postgres_9.6/ -D /data/postgres_14.5/ -j 8 -p 5432 -P 5432 -c -k
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 system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for incompatible polymorphic functions             ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Checking for invalid "unknown" user columns                 ok
Checking for hash indexes                                   ok

*failure*
Consult the last few lines of "pg_upgrade_server.log" for
the probable cause of the failure.

could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/data/postgres_9.6/.s.PGSQL.5432"?

could not connect to target postmaster started with the command:
"/usr/local/postgresql-14.5/bin/pg_ctl" -w -l "pg_upgrade_server.log" -D "/data/postgres_14.5" -o "-p 5432 -b -c synchronous_commit=off -c fsync=off -c full_page_writes=off -c vacuum_defer_cleanup_age=0  -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directories='/data/postgres_9.6'" start
Failure, exiting

根据提示查看pg_upgrade_server.log

[postgres]# cat pg_upgrade_server.log
-----------------------------------------------------------------
  pg_upgrade run on Fri Sep 23 15:59:45 2022
-----------------------------------------------------------------

command: "/usr/local/postgresql-9.6.0/bin/pg_ctl" -w -l "pg_upgrade_server.log" -D "/data/postgres_9.6" -o "-p 5432 -b  -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directories='/data/postgres_9.6'" start >> "pg_upgrade_server.log" 2>&1
waiting for server to start....LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_log".
 done
server started


command: "/usr/local/postgresql-9.6.0/bin/pg_ctl" -w -D "/data/postgres_9.6" -o "" -m smart stop >> "pg_upgrade_server.log" 2>&1
waiting for server to shut down.... done
server stopped


command: "/usr/local/postgresql-14.5/bin/pg_ctl" -w -l "pg_upgrade_server.log" -D "/data/postgres_14.5" -o "-p 5432 -b -c synchronous_commit=off -c fsync=off -c full_page_writes=off -c vacuum_defer_cleanup_age=0  -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directories='/data/postgres_9.6'" start >> "pg_upgrade_server.log" 2>&1
waiting for server to start....2022-09-23 15:59:48.021 CST [62615] FATAL:  "min_wal_size" must be at least twice "wal_segment_size"
2022-09-23 15:59:48.021 CST [62615] LOG:  database system is shut down
 stopped waiting
pg_ctl: could not start server
Examine the log output.

从上面的日志输出中截取重要报错部分:

FATAL:  "min_wal_size" must be at least twice "wal_segment_size"

安装PostgreSQL 14.5后,使用了pg_resetwal修改了wal-segsize为=64M,默认min_wal_size值为80M,而根据报错提示min_wal_size=wal-segsize * 2,也就是最少为128。将min_wal_size更改为128后

[postgres]# vi $PGDATA/postgresql.conf
min_wal_size = 128

重新检查升级

[postgres@host71 postgres]# /usr/local/postgresql-14.5/bin/pg_upgrade -b /usr/local/postgresql-9.6.0/bin/ -B /usr/local/postgresql-14.5/bin/ -d /data/postgres_9.6/ -D /data/postgres_14.5/ -j 8 -p 5432 -P 5432 -c -k
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 system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for incompatible polymorphic functions             ok
Checking for tables WITH OIDS                               ok
Checking for invalid "sql_identifier" user columns          ok
Checking for invalid "unknown" user columns                 ok
Checking for hash indexes                                   ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok

*Clusters are compatible*

无异常。

 

参考:http://postgres.cn/docs/14/pgupgrade.html