pg_upgrade 是一个 服务器实用程序,用于将 PostgreSQL 数据库集群进行二进制升级到新的主版本。
pg_upgrade 在 PostgreSQL 9.0 中被添加,最初是基于 pg_migrator 的一个 contrib 模块。
源代码
pg_upgrade 的源代码位于 src/bin/pg_upgrade/。
请参阅 src/bin/pg_upgrade/IMPLEMENTATION 获取其实现的技术描述。
变更历史
- PostgreSQL 19
- 在可能的情况下,
pg_largeobject_metadata的文件将被传输(提交 3bcfcd81)。
- 在可能的情况下,
- PostgreSQL 18
- PostgreSQL 17
- PostgreSQL 16
- PostgreSQL 15
- 添加了
-N/--no-sync选项(提交 3d5ffccb) - 保留了 filenode 和 tablespace 的 OID(提交 9a974cbc)。
- 保留了
postgres和template0数据库的 OID(提交 aa010514)。 pg_upgrade生成的文件现在将存储在升级集群的数据目录下的pg_upgrade_output.d中(提交 38bfae36 和 a0084963)。- 所有无法连接的数据库现在都会被列出(提交 26ebb0e2)。
- 如果输出不是 tty,则不会发出进度状态(提交 27b02e07)。
- 删除了从 PostgreSQL 9.2 之前的版本升级的支持(提交 38bfae36 和 a0084963)。
- 添加了
- PostgreSQL 13
- 默认的新 bindir 现在与正在执行的
pg_upgrade二进制文件的位置相同,在大多数情况下无需显式指定-B newbindir(提交 959f6d6a)。
- 默认的新 bindir 现在与正在执行的
- PostgreSQL 12
- PostgreSQL 10
- 旧 PostgreSQL 版本的哈希索引将被使失效,并生成一个用于重新创建它们的脚本(提交 a95410e2)。
- PostgreSQL 9.5
- PostgreSQL 9.4
- PostgreSQL 9.3
- PostgreSQL 9.2
- 以 PG 开头的
pg_upgrade特定的环境变量已被重命名;PGPORT 现在被忽略,并由PGPORTOLD/PGPORTNEW替换(提交 a88f4496)。 - 默认使用端口 50432(提交 a88f4496)。
- 一般的日志记录改进(提交 717f6d60)。
- 旧集群仅在使用了链接模式时才会被锁定(提交 2127aac6)。
- 添加了用于增量生成更准确统计信息的脚本(提交 faec2815)。
- 删除了
-g/-G/-l日志选项(提交 717f6d60)。 - 添加了
-o/-O选项(提交 0dc3f57b)。 - 添加了
-r/--retain选项(提交 717f6d60)。 - 改进了
-v/--verbose的行为(提交 717f6d60)。 - 支持升级不包含 postgres 数据库的集群(提交 a50d860a)。
- 以 PG 开头的
- PostgreSQL 9.0
- 作为 contrib 模块添加(提交 c2e9b2f2)。
请注意,pg_upgrade 的早期版本最初是作为 contrib 模块在 PostgreSQL 7.2 中添加(提交 92a8bc96),并在 PostgreSQL 8.1 中删除(提交 d20763db)。
示例
pg_upgrade 操作的样本输出。
$ pg_upgrade \
--old-bindir=/usr/local/pg12/bin \
--old-datadir=/var/lib/pg12/data \
--new-datadir=/var/lib/pg13/data \
--old-port=5432 \
--new-port=5433 \
--jobs 4
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 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:
/usr/local/pg13/bin/vacuumdb --all --analyze-in-stages
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh
参考资料
- PostgreSQL documentation: pg_upgrade
有用链接
- 使用 pg_upgrade 检查 Postgres 升级 - 2024 年 3 月,Greg Sabino Mullane / Crunchy Data 的博客文章
- 使用 Pg_Upgrade 快速升级 - pg_upgrade 的主要作者 Bruce Momjian 的演示文稿(PDF)
- 使用 pg_upgrade 将 PostgreSQL 9.6 PostGIS 2.4 升级到 Yum 上的 PostgreSQL 15 3.3 - 2022 年 10 月,Regina Obe 的博客文章
