DBAtlas · 数据库运维图谱

使用 pg_upgrade --link 进行大版本升级(端到端操作手册)

PostgreSQL升级

命令

# As the postgres user, in a writable directory; example 16 -> 17
export OLD_BIN=/usr/pgsql-16/bin NEW_BIN=/usr/pgsql-17/bin
export OLD_DATA=/var/lib/pgsql/16/data NEW_DATA=/var/lib/pgsql/17/data
# 0) initdb the new cluster with the same encoding/locale and checksum setting as the old one (-k if the old cluster has checksums)
# 1) Compatibility check (can run while the old cluster is up, changes no data):
$NEW_BIN/pg_upgrade --old-bindir=$OLD_BIN --new-bindir=$NEW_BIN --old-datadir=$OLD_DATA --new-datadir=$NEW_DATA --check
# 2) Stop the old cluster and upgrade with hard links:
$OLD_BIN/pg_ctl -D $OLD_DATA stop -m fast
$NEW_BIN/pg_upgrade --old-bindir=$OLD_BIN --new-bindir=$NEW_BIN --old-datadir=$OLD_DATA --new-datadir=$NEW_DATA --link --jobs=4
# 3) Carry postgresql.conf/pg_hba.conf customizations over to the new cluster, start it:
$NEW_BIN/pg_ctl -D $NEW_DATA -l /tmp/pg17_start.log start
# 4) Gather statistics, run generated scripts if any:
$NEW_BIN/vacuumdb --all --analyze-in-stages
# psql -X -f update_extensions.sql   (if pg_upgrade generated it)
# 5) After validation and a fresh backup, delete the old cluster:
./delete_old_cluster.sh

作用

先用 --check 验证兼容性,然后通过将旧集群的数据文件硬链接到新版本而不是复制,在几分钟内完成大版本升级,与数据量无关;之后分阶段收集统计信息并删除旧集群。

适用场景

用于以可接受的短暂中断(数分钟)将大型集群原地迁移到新的大版本。

注意

使用 --link 时,一旦新集群启动,旧集群就无法再安全使用;唯一的回退方式是升级前所做的备份。新旧数据目录必须位于同一文件系统上。在 analyze 完成之前,新集群上的计划可能较差。如果使用 pgBackRest,请运行 stanza-upgrade 并立即进行新的全量备份;旧版本的备份无法还原到新集群中。

在 DBAtlas 中打开 →

相关内容