PG如何实现跨大版本升级

数据库进行升级,是一个再正常不过的功能,比如功能的需要,遇到BUG,安全漏洞等等,具体升级包含子版本升级,主版本升级。如果用过ORACLE的朋友,一定知道,在ORACLE中,如果要实现大版本的升级,比如从11g,升级到19c,一般使用的dbua来完成,并且标准的升级,一定会有数据库停机时间,小版本的升级,如19.3,升级到19.24 ,使用OPATCH 工具来完成,一样需要停数据库。

那对于PG,一样也存在升级,如果是小版本升级,比如12.0 升级到12.19 ,直接使用二进制文件替换即可,如果是大版本升级,一般使用 pg_upgrade来完成,当然,如果时间允许,也可以使用pg_dumpall也行。只是时间要长一些。

关于PG_UPGRADE的使用,可以参见官方文档:PostgreSQL: Documentation: 16: pg_upgrade

部分截图如下:

PG_UPGRADE 的用法:

pg_upgrade -b oldbindir [-B newbindir ] -d oldconfigdir -D newconfigdir [option...]

目前pg_upgrade 支持从 9.2.x 到目前最新的版本,甚至BETA版本。

下面我们以一个升级案例来说明:

目录

1.升级环境

2.源码安装新版本16.3

3.关闭源和目标数据库

4.执行升级检查

5.正式升级

6.修改环境变量

7.修改初始参数

8.启动PG并调整PG_UPGRADE要求的脚本

9.结果验证

10、联系我们


1.升级环境

源版本:pg 12.0

PGHOME=/u01/app/postgres/product/12.0/dbhome_1

PGDATA=/u01/app/postgres/product/pgdata_12

PORT=5432

目标版本:pg 16.3

PGHOME=/u01/app/postgres/product/16.3/dbhome_1

PGDATA=/u01/app/postgres/product/pgdata

PORT=5432

操作系统:OpenEuler 24.03

sztechdb=# select * from version();

version


PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 12.3.1 (openEuler 12.3.1-30.oe2403), 64-bit

(1 row)

2.源码安装新版本16.3

这个步骤,可以参见前面的文档:https://blog.csdn.net/cqsztech/article/details/140748640

如果源端有额外的插件,需要安装

这里安装,只需要到 initdb即可,不需要打开

postgres@openeuler2403 bin]$ ./initdb --pgdata=/u01/app/postgres/pgdata -U postgres

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 "zh_CN.UTF-8".

The default database encoding has accordingly been set to "UTF8".

initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"

The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /u01/app/postgres/pgdata ... ok

creating subdirectories ... ok

selecting dynamic shared memory implementation ... posix

selecting default max_connections ... 100

selecting default shared_buffers ... 128MB

selecting default time zone ... Asia/Shanghai

creating configuration files ... ok

running bootstrap script ... ok

performing post-bootstrap initialization ... ok

syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections

initdb: hint: 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 /u01/app/postgres/pgdata -l logfile start

3.关闭源和目标数据库

把源和目标数据库都关闭。

[postgres@openeuler2403 pgdata_12]$ pg_ctl stop

waiting for server to shut down.... done

server stopped

4.执行升级检查

是否能升级,正式升级之前,可以先检查一下,有点试升级的味道。

注意,执行PG_UPGRADE命令,需要在高版本的环境下运行

先看看pg_upgrade 的用法:

[postgres@openeuler2403 bin]$ pg_upgrade -c --old-datadir=/u01/app/postgres/pgdata_12 \

> --new-datadir=/u01/app/postgres/pgdata \

> --old-bindir=/u01/app/postgres/product/12.0/dbhome_1/bin \

> --new-bindir=/u01/app/postgres/product/16.3/dbhome_1/bin

查看丢失的库是什么

[postgres@openeuler2403 pgdata_12]$ cat /u01/app/postgres/pgdata/pg_upgrade_output.d/20240801T164259.299/loadable_libraries.txt

could not load library "libdir/pgcrypto": ERROR: could not access file "libdir/pgcrypto": ûĿ¼

In database: postgres

--暂时源端去掉这个插件

postgres=# drop extension pgcrypto;

DROP EXTENSION

再次检查,OK。

5.正式升级

[postgres@openeuler2403 bin]$ pg_upgrade --old-datadir=/u01/app/postgres/pgdata_12 \
> --new-datadir=/u01/app/postgres/pgdata \
> --old-bindir=/u01/app/postgres/product/12.0/dbhome_1/bin \
> --new-bindir=/u01/app/postgres/product/16.3/dbhome_1/bin

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 incompatible "aclitem" data type in user tables ok

Checking for user-defined encoding conversions ok

Checking for user-defined postfix operators ok

Checking for incompatible polymorphic functions 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


Setting locale and encoding for new cluster ok

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 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

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

Checking for extension updates notice

Your installation contains extensions that should be updated

with the ALTER EXTENSION command. The file

update_extensions.sql

when executed by psql by the database superuser will update

these extensions.

Upgrade Complete


Optimizer statistics are not transferred by pg_upgrade.

Once you start the new server, consider running:

/u01/app/postgres/product/16.3/dbhome_1/bin/vacuumdb --all --analyze-in-stages

Running this script will delete the old cluster's data files:

./delete_old_cluster.sh

[postgres@openeuler2403 bin]$

6.修改环境变量

[postgres@openeuler2403 bin]$ cat ~/.bash_profile

Source /root/.bashrc if user has one

[ -f ~/.bashrc ] && . ~/.bashrc

export PGPORT=5432

export PGDATA=/u01/app/postgres/pgdata

export PGHOME=/u01/app/postgres/product/16.3/dbhome_1

export LD_LIBRARY_PATH=PGHOME/lib:LD_LIBRARY_PATH

export PATH=PGHOME/bin:PATH:.

export PGHOST=$PGDATA

export PGUSER=postgres

export PGDATABASE=postgres

export PGCLIENTENCODING=GBK

7.修改初始参数

cat >> $PGDATA/postgresql.conf << "EOF"

listen_addresses = '*'

port=5432

unix_socket_directories='/u01/app/postgres/pgdata'

logging_collector = on

log_directory = 'pg_log'

log_filename = 'postgresql-%a.log'

log_truncate_on_rotation = on

max_connections = 500

shared_buffers = 2GB

EOF

cat >> $PGDATA/pg_hba.conf << "EOF"

TYPE DATABASE USER ADDRESS METHOD

host all all 0.0.0.0/0 md5

EOF

8.启动PG并调整PG_UPGRADE要求的脚本

--插件更新

[postgres@openeuler2403 pgdata]$ psql -f /u01/app/postgres/product/16.3/dbhome_1/bin/update_extensions.sql

You are now connected to database "postgres" as user "postgres".

ALTER EXTENSION

ALTER EXTENSION

ALTER EXTENSION

--收集统计信息

[postgres@openeuler2403 pgdata]$ /u01/app/postgres/product/16.3/dbhome_1/bin/vacuumdb --all --analyze-in-stages

vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)

vacuumdb: processing database "sztechdb": Generating minimal optimizer statistics (1 target)

vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)

vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)

vacuumdb: processing database "sztechdb": Generating medium optimizer statistics (10 targets)

vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)

vacuumdb: processing database "postgres": Generating default (full) optimizer statistics

vacuumdb: processing database "sztechdb": Generating default (full) optimizer statistics

vacuumdb: processing database "template1": Generating default (full) optimizer statistics

--删除老版本的文件

[postgres@openeuler2403 pgdata]$ cat /u01/app/postgres/product/16.3/dbhome_1/bin/delete_old_cluster.sh

#!/bin/sh

rm -rf '/u01/app/postgres/pgdata_12'

rm -rf '/data/pgdata/PG_12_201909212'

[postgres@openeuler2403 pgdata]$ /u01/app/postgres/product/16.3/dbhome_1/bin/delete_old_cluster.sh

[postgres@openeuler2403 pgdata]$ ls -ltr /u01/app/postgres/

总计 8

drwxrwxr-x. 4 postgres postgres 30 7月31日 17:53 product

drwxr-xr-x. 2 postgres postgres 4096 7月31日 19:17 archive_log

drwx------. 21 postgres postgres 4096 8月 1日 17:36 pgdata

[postgres@openeuler2403 pgdata]$ ls -ltr /data/pgdata/

总计 8

drwx------ 3 postgres postgres 4096 8月 1日 09:23 PG_16_202307071_bak

drwx------ 3 postgres postgres 4096 8月 1日 17:27 PG_16_202307071

9.结果验证

到此,PG_UPGRADE 升级就完成了。

但感觉PG_UPGRADE还是有一些遗憾,

1.整个升级,都是命令行完成,没有图形界面,不太直观

2.升级过程中,数据文件路径要变化,不能就数据文件原地升级,也许是强迫症吧。

10、联系我们

如果你还想和我交流,可以按如下方式找到我们。


微信群:水煮数据库

主要交流日常运维中用到的数据库相关问题,包含但不限于:ORACLE,PG,MYSQL,SQLSERVER,OB,TIDB,达梦,TDSQL,OPENGAUSS,人大金仓,GBASE等等,

加我微信:zq24803366,备注:水煮数据库, 我拉你入群。


相关推荐
远歌已逝1 小时前
维护在线重做日志(二)
数据库·oracle
qq_433099402 小时前
Ubuntu20.04从零安装IsaacSim/IsaacLab
数据库
Dlwyz2 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
工业甲酰苯胺4 小时前
Redis性能优化的18招
数据库·redis·性能优化
没书读了5 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
i道i6 小时前
MySQL win安装 和 pymysql使用示例
数据库·mysql
小怪兽ysl6 小时前
【PostgreSQL使用pg_filedump工具解析数据文件以恢复数据】
数据库·postgresql
wqq_9922502776 小时前
springboot基于微信小程序的食堂预约点餐系统
数据库·微信小程序·小程序
爱上口袋的天空6 小时前
09 - Clickhouse的SQL操作
数据库·sql·clickhouse
聂 可 以8 小时前
Windows环境安装MongoDB
数据库·mongodb