pg_upgrade
是一个用于将 PostgreSQL 数据库集群二进制升级到新的主要版本的服务器实用程序。
pg_upgrade
添加于PostgreSQL 9.0,最初是基于pg_migrator
的contrib 模块。
源代码
pg_upgrade
源代码位于src/bin/pg_upgrade/。
有关实现的技术说明,请参阅src/bin/pg_upgrade/IMPLEMENTATION。
更改历史
- PostgreSQL 18
- PostgreSQL 17
- PostgreSQL 16
- PostgreSQL 15
- 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_upgrade
的环境变量重命名为以 PG 开头;现在忽略 PGPORT 并将其替换为PGPORTOLD
/PGPORTNEW
(提交a88f4496) - 默认使用端口 50432(提交a88f4496)
- 改进了一般的日志记录(提交717f6d60)
- 仅在使用链接模式时才锁定旧集群(提交2127aac6)
- 添加了用于增量生成更准确统计信息的脚本(提交faec2815)
- 删除了
-g
/-G
/-l
日志记录选项(提交717f6d60) - 添加了
-o
/-O
选项(提交0dc3f57b) - 添加了
-r
/--retain
选项(提交717f6d60) -v
/--verbose
行为改进 (提交 717f6d60)- 支持在没有 postgres 数据库的情况下升级集群 (提交 a50d860a)
- 将特定于
- 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 文档: pg_upgrade
有用链接
- 使用 pg_upgrade 检查 Postgres 升级 - Greg Sabino Mullane / Crunchy Data 于 2024 年 3 月撰写的博客文章
- 使用 Pg_Upgrade 快速升级 - Bruce Momjian(pg_upgrade 的主要作者)的演示文稿 (PDF)
- 使用 pg_upgrade 将 PostgreSQL 9.6 PostGIS 2.4 升级到 PostgreSQL 15 3.3 on Yum - Regina Obe 于 2022 年 10 月撰写的博客文章