暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

PostgreSQL主备集群模式下版本升级

IT那活儿 2022-09-07
986
点击上方“IT那活儿”公众号,关注后了解更多内容,不管IT什么活儿,干就完了!!!

master节点数据库版本升级

1. master节点安装新版本数据库并初始化数据目录
initdb -A trust -D /data/pg140 -E utf8 -k -U postgres -W --wal-segsize=10
24
2. 检查当前数据同步状态
user:postgres@db:postgres[[local]:5432]#select * from pg_stat_replication ;
+-[ RECORD 1 ]-----+-------------------------------+
| pid | 15236                         |
|
 usesysid | 16384 |
| usename | replic |
|
 application_name | walreceiver |
| client_addr | 192.168.229.121               |
|
 client_hostname | NULL |
| client_port | 46028                         |
|
 backend_start | 2021-08-11 10:30:16.978079+08 |
| backend_xmin | 499                           |
|
 state | streaming |
| sent_lsn | 3/800000D8 |
|
 write_lsn | 3/800000D8 |
| flush_lsn | 3/800000D8 |
|
 replay_lsn | 3/800000D8 |
| write_lag | NULL |
|
 flush_lag | NULL |
| replay_lag | NULL |
|
 sync_priority | 0 |
| sync_state | async |
|
 reply_time | 2021-08-11 10:31:47.648992+08 |
+------------------+-------------------------------+

3. 停止主备数据库
pg_ctl stop -D $PGDATA
4. master节点进行数据库升级
1)注意:要求新旧版本数据块大小、wal-blocksize大小必须一致。
2)调整pg_hba.conf 认证模式为trust。
3)使用pg_upgrade进行升级检查。
[postgres@PG-1 pg133]$ /opt/pgsql/pg140/bin/pg_upgrade -b /opt/pgsql/pg133/bin/ -B /opt/pgsql/pg140/bin/ -d /data/pg133 -D /data/pg140 -j 8 -p 5432 -P 5433 -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 user-defined encoding conversions ok
Checking for user-defined postfix operators 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*

注意:要所有的检查选项都通过。
4)执行升级
[postgres@PG-1 pg133]$ /opt/pgsql/pg140/bin/pg_upgrade -b /opt/pgsql/pg133/bin/ -B /opt/pgsql/pg140/bin/ -d /data/pg133 -D /data/pg140 -j 8 -p 5432 -P 5433
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
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_xact 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 delete old cluster ok
Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade.
Once you start the new server, consider running:
/opt/pgsql/pg140/bin/vacuumdb --all --analyze-in-stages
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh

注意:所有状态都OK,表示升级完成
5)调整配置参数、启动新版本数据库及统计信息收集

/opt/pgsql/pg140/bin/pg_ctl start -D /data/pg140
/opt/pgsql/pg140/bin/vacuumdb --all --analyze-in-stages

standby节点数据库版本升级

1. standby节点安装新版本数据库。
2. 使用rsync将主节点数据同步到standby节点。
rsync -avz postgres@192.168.229.129:/data/pg140 /data
3. 调整数据库配置参数。
4. 创建standby.signal,权限0600或者cp /data/oldatadir/standby.signal /data/pg140
5. 将连接信息添加到postgresql.auto.conf中或者cp /data/olddatadir/postgresql.auto.conf /data/pg140。

6. 启动standby数据库。

检查主备复制情况

user:postgres@db:postgres[[local]:5432]#select * from pg_stat_replication;
+-[ RECORD 1 ]-----+-------------------------------+
| pid | 15980                         |
|
 usesysid | 16384 |
| usename | replic |
|
 application_name | walreceiver |
| client_addr | 192.168.229.121               |
|
 client_hostname | NULL |
| client_port | 46122                         |
|
 backend_start | 2021-08-11 11:23:38.478461+08 |
| backend_xmin | NULL |
|
 state | streaming |
| sent_lsn | 5/2ABF20                      |
|
 write_lsn | 5/2ABF20 |
| flush_lsn | 5/2ABF20                      |
|
 replay_lsn | 5/2ABF20 |
| write_lag | 00:00:00.000595 |
|
 flush_lag | 00:00:00.000982 |
| replay_lag | 00:00:00.001227               |
|
 sync_priority | 0 |
| sync_state | async |
|
 reply_time | 2021-08-11 11:23:51.450434+08 |
+------------------+-------------------------------+


本文作者:魏 强(上海新炬王翦团队)

本文来源:“IT那活儿”公众号

文章转载自IT那活儿,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论