当前位置:   article > 正文

PostgreSQL-大版本升级(pg_upgrade方式)_postgresql 升级

postgresql 升级

1.pg_upgrade工具介绍

pg_upgrade 会创建新的系统表,并以重用旧的数据文件的方式进行升级。
pg_upgrade 的参数选项如下: 
 -b bindir,--old-bindir=bindir:旧的 PostgreSQL 可执行文件目录;
 -B bindir,--new-bindir=bindir:新的 PostgreSQL 可执行文件目录;
 -c,--check:只检查升级兼容性,不更改任何数据
 -d configdir,--old-datadir=configdir:旧版本的数据目录
 -D configdir,--new-datadir=configdir:新版本的数据目录
 -j,--jobs=njobs:要同时使用的进程或线程数
 -k,--link:硬链接方式升级
 -o options,--old-options options:直接传送给旧 postgres 命令的选项,多个选
项可以追加在后面
 -O options,--new-options options:直接传送给新 postgres 命令的选项,多个
选项可以追加在后面
 -p port,--old-port=port:旧版本的端口号
 -P port,--new-port=port:新版本的端口号
 -r,--retain:即使在成功完成后也保留 SQL 和日志文件
在升级之前应该运行 pg_upgrade -c 检查新旧版本的兼容性,把每一项不兼容的问题都
解决了才可以顺利升级。使用 pg_upgrade -c 只会检查新旧版本的兼容性,不会运行真
正的升级程序,不会修改数据文件,并且在命令结束时,会输出一份检查结果的报告,还
会对需要手动调整的项做出简要的描述。
pg_upgrade 有普通模式和 link 模式两种升级模式。在普通模式下,会把旧版本的数据拷
贝到新版本中,所以如果使用普通升级模式,要确保有足够的磁盘空间存储新旧两份数据;
link 模式下,只是在新版本的数据目录中建立了旧版本数据文件的硬链接,可以有效减少
磁盘占用的空间。

2.版本信息

升级前    升级后
postgresql14.11postgresql16.2

3.升级准备

安装依赖:

yum install libicu-devel.x86_64 python3 python3-devel

备份数据:

pg_dumpall -p 5432 > /tmp/backup.sql

4.安装新版pg数据库

  1. wget https://ftp.postgresql.org/pub/source/v16.2/postgresql-16.2.tar.bz2
  2. tar -xvf postgresql-16.2.tar.bz2
  3. #编译
  4. cd postgresql-16.2
  5. [root@localhost postgresql-16.2]# mkdir -p /usr/local/pgsql16.2
  6. [root@localhost postgresql-16.2]# make && make install
  7. #生成软连接
  8. [root@localhost local]# unlink pgsql
  9. [root@localhost local]# ln -s /usr/local/pgsql16.2 /usr/local/pgsql
  10. [root@localhost local]# ll
  11. total 0
  12. drwxr-xr-x. 2 root root 6 Apr 11 2018 bin
  13. drwxr-xr-x. 2 root root 6 Apr 11 2018 etc
  14. drwxr-xr-x. 2 root root 6 Apr 11 2018 games
  15. drwxr-xr-x. 2 root root 6 Apr 11 2018 include
  16. drwxr-xr-x. 2 root root 6 Apr 11 2018 lib
  17. drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64
  18. drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec
  19. drwxr-xr-x 6 root root 56 May 2 09:51 pgsq14.11
  20. lrwxrwxrwx 1 root root 20 May 2 10:24 pgsql -> /usr/local/pgsql16.2
  21. drwxr-xr-x 2 root root 6 May 2 09:46 pgsql14.11
  22. drwxr-xr-x 6 root root 56 May 2 10:22 pgsql16.2
  23. drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin
  24. drwxr-xr-x. 5 root root 49 Oct 5 2018 share
  25. drwxr-xr-x. 2 root root 6 Apr 11 2018 src
  26. [root@localhost local]#
  27. #切换postgres用户并建新版pg数据目录
  28. [root@localhost local]# su postgres
  29. [postgres@localhost local]$ mkdir /home/postgres/pgdata16
  30. [postgres@localhost local]$
  31. #初始化新版本pg
  32. [postgres@localhost ~]$ initdb --locale C -D /home/postgres/pgdata16/ -W
  33. Success. You can now start the database server using:
  34. pg_ctl -D /home/postgres/pgdata16/ -l logfile start
  35. #修改配置文件,端口改为5433
  36. [postgres@localhost pgdata16]$ vi postgresql.conf
  37. port = 5433

5.停止旧版本pg数据库服务

  1. [postgres@localhost pgdata16]$ pg_ctl stop -D /home/postgres/pgdata14/ -p 5432
  2. waiting for server to shut down.... done
  3. server stopped

6.检查新旧版本兼容性

  1. [postgres@localhost pgdata16]$ pg_upgrade -b /usr/local/pgsq14.11/bin -B /usr/local/pgsql16.2/bin -d /home/postgres/pgdata14/ -D /home/postgres/pgdata16/ -k -c
  2. Performing Consistency Checks
  3. -----------------------------
  4. Checking cluster versions ok
  5. old cluster uses data checksums but the new one does not
  6. Failure, exiting
  7. #解决报错,新版本启用checksums
  8. [postgres@localhost pgdata16]$ pg_checksums -D /home/postgres/pgdata16/ -e -P
  9. 22/22 MB (100%) computed
  10. Checksum operation completed
  11. Files scanned: 948
  12. Blocks scanned: 2881
  13. Files written: 780
  14. Blocks written: 2881
  15. pg_checksums: syncing data directory
  16. pg_checksums: updating control file
  17. Checksums enabled in cluster
  18. [postgres@localhost pgdata16]$ pg_checksums -D /home/postgres/pgdata16/
  19. Checksum operation completed
  20. Files scanned: 948
  21. Blocks scanned: 2881
  22. Bad checksums: 0
  23. Data checksum version: 1
  24. [postgres@localhost pgdata16]$
  25. #再次检查兼容性
  26. [postgres@localhost pgdata16]$ pg_upgrade -b /usr/local/pgsq14.11/bin -B /usr/local/pgsql16.2/bin -d /home/postgres/pgdata14/ -D /home/postgres/pgdata16/ -k -c
  27. Performing Consistency Checks
  28. -----------------------------
  29. Checking cluster versions ok
  30. Checking database user is the install user ok
  31. Checking database connection settings ok
  32. Checking for prepared transactions ok
  33. Checking for system-defined composite types in user tables ok
  34. Checking for reg* data types in user tables ok
  35. Checking for contrib/isn with bigint-passing mismatch ok
  36. Checking for incompatible "aclitem" data type in user tables ok
  37. Checking for presence of required libraries ok
  38. Checking database user is the install user ok
  39. Checking for prepared transactions ok
  40. Checking for new cluster tablespace directories ok
  41. *Clusters are compatible*
  42. [postgres@localhost pgdata16]$

7.升级

注:两种方式,普通模式和硬链接模式,可视磁盘空间剩余大小情况而定

  1. #普通模式升级
  2. pg_upgrade -b /usr/local/pgsq14.11/bin -B /usr/local/pgsql16.2/bin -d /home/postgres/pgdata14/ -D /home/postgres/pgdata16/
  1. #link模式/硬链接模式
  2. [postgres@localhost pgdata16]$ pg_upgrade -b /usr/local/pgsq14.11/bin -B /usr/local/pgsql16.2/bin -d /home/postgres/pgdata14/ -D /home/postgres/pgdata16/ -k
  3. Performing Consistency Checks
  4. -----------------------------
  5. Checking cluster versions ok
  6. Checking database user is the install user ok
  7. Checking database connection settings ok
  8. Checking for prepared transactions ok
  9. Checking for system-defined composite types in user tables ok
  10. Checking for reg* data types in user tables ok
  11. Checking for contrib/isn with bigint-passing mismatch ok
  12. Checking for incompatible "aclitem" data type in user tables ok
  13. Creating dump of global objects ok
  14. Creating dump of database schemas
  15. ok
  16. Checking for presence of required libraries ok
  17. Checking database user is the install user ok
  18. Checking for prepared transactions ok
  19. Checking for new cluster tablespace directories ok
  20. If pg_upgrade fails after this point, you must re-initdb the
  21. new cluster before continuing.
  22. Performing Upgrade
  23. ------------------
  24. Setting locale and encoding for new cluster ok
  25. Analyzing all rows in the new cluster ok
  26. Freezing all rows in the new cluster ok
  27. Deleting files from new pg_xact ok
  28. Copying old pg_xact to new server ok
  29. Setting oldest XID for new cluster ok
  30. Setting next transaction ID and epoch for new cluster ok
  31. Deleting files from new pg_multixact/offsets ok
  32. Copying old pg_multixact/offsets to new server ok
  33. Deleting files from new pg_multixact/members ok
  34. Copying old pg_multixact/members to new server ok
  35. Setting next multixact ID and offset for new cluster ok
  36. Resetting WAL archives ok
  37. Setting frozenxid and minmxid counters in new cluster ok
  38. Restoring global objects in the new cluster ok
  39. Restoring database schemas in the new cluster
  40. ok
  41. Adding ".old" suffix to old global/pg_control ok
  42. If you want to start the old cluster, you will need to remove
  43. the ".old" suffix from /home/postgres/pgdata14/global/pg_control.old.
  44. Because "link" mode was used, the old cluster cannot be safely
  45. started once the new cluster has been started.
  46. Linking user relation files
  47. ok
  48. Setting next OID for new cluster ok
  49. Sync data directory to disk ok
  50. Creating script to delete old cluster ok
  51. Checking for extension updates ok
  52. Upgrade Complete
  53. ----------------
  54. Optimizer statistics are not transferred by pg_upgrade.
  55. Once you start the new server, consider running:
  56. /usr/local/pgsql16.2/bin/vacuumdb --all --analyze-in-stages
  57. Running this script will delete the old cluster's data files:
  58. ./delete_old_cluster.sh
  59. [postgres@localhost pgdata16]$

注:如果运行 pg_upgrade 失败,必须重新初始化新版本的数据目录,看到 upgrade

complete 说明升级已经顺利完成。

8.更新统计信息

pg_upgrade 会创建新的系统表,并重用旧的数据进行升级,统计信息并不会随升级过程
迁移,所以在启用新版本之前,应该首先重新收集统计信息,避免没有统计信息导致错误
的查询计划。我们可以手动运行 vacuum 命令,如下:
vacuumdb --all --analyze-in-stages -h 127.0.0.1 -p xxx
  1. #启动新版本pg服务
  2. [postgres@localhost pgdata16]$ pg_ctl -D /home/postgres/pgdata16/ -l logfile start
  3. waiting for server to start.... done
  4. server started
  5. #更新统计信息
  6. [postgres@localhost pgdata16]$ vacuumdb --all --analyze-in-stages -h 127.0.0.1 -p 5433
  7. vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
  8. vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
  9. vacuumdb: processing database "testdb": Generating minimal optimizer statistics (1 target)
  10. vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
  11. vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
  12. vacuumdb: processing database "testdb": Generating medium optimizer statistics (10 targets)
  13. vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
  14. vacuumdb: processing database "template1": Generating default (full) optimizer statistics
  15. vacuumdb: processing database "testdb": Generating default (full) optimizer statistics
  16. [postgres@localhost pgdata16]$

9.验证数据

  1. #升级前
  2. [postgres@localhost ~]$ psql
  3. psql (14.11)
  4. Type "help" for help.
  5. postgres=# create database testdb;
  6. CREATE DATABASE
  7. postgres=# \c testdb;
  8. You are now connected to database "testdb" as user "postgres".
  9. testdb=# create table t_trgm (id int,trgm text,no_trgm text);
  10. CREATE TABLE
  11. testdb=# insert into t_trgm select t,md5(t::text),md5(t::text) from generate_series(1,1000000) as t;
  12. INSERT 0 1000000
  13. testdb=# \d
  14. List of relations
  15. Schema | Name | Type | Owner
  16. --------+--------+-------+----------
  17. public | t_trgm | table | postgres
  18. (1 row)
  19. testdb=# select count(*) from t_trgm;
  20. count
  21. ---------
  22. 1000000
  23. (1 row)
  24. testdb=# \d t_trgm
  25. Table "public.t_trgm"
  26. Column | Type | Collation | Nullable | Default
  27. ---------+---------+-----------+----------+---------
  28. id | integer | | |
  29. trgm | text | | |
  30. no_trgm | text | | |
  31. #升级后
  32. [postgres@localhost pgdata16]$ psql -p 5433
  33. psql (16.2)
  34. Type "help" for help.
  35. postgres=# \l
  36. List of databases
  37. Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges
  38. -----------+----------+----------+-----------------+---------+-------+------------+-----------+-----------------------
  39. postgres | postgres | UTF8 | libc | C | C | | |
  40. template0 | postgres | UTF8 | libc | C | C | | | =c/postgres +
  41. | | | | | | | | postgres=CTc/postgres
  42. template1 | postgres | UTF8 | libc | C | C | | | postgres=CTc/postgres+
  43. | | | | | | | | =c/postgres
  44. testdb | postgres | UTF8 | libc | C | C | | |
  45. (4 rows)
  46. postgres=# \c testdb
  47. You are now connected to database "testdb" as user "postgres".
  48. testdb=# \d
  49. List of relations
  50. Schema | Name | Type | Owner
  51. --------+--------+-------+----------
  52. public | t_trgm | table | postgres
  53. (1 row)
  54. testdb=# \d t_trgm;
  55. Table "public.t_trgm"
  56. Column | Type | Collation | Nullable | Default
  57. ---------+---------+-----------+----------+---------
  58. id | integer | | |
  59. trgm | text | | |
  60. no_trgm | text | | |
  61. testdb=# select count(*) from t_trgm;
  62. count
  63. ---------
  64. 1000000
  65. (1 row)
  66. testdb=#

10.处理旧数据更改环境信息

  1. #确认新版本运行正常,酌情移除旧版本的数据目录即可。
  2. --删除旧版本数据
  3. ./delete_old_cluster.sh
  4. --修改环境变量
  5. vi .bashrc
  6. export PGDATA=/home/postgres/pgdata16/
  7. source .bashrc

 11.失败回滚方案

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/960122
推荐阅读
相关标签
  

闽ICP备14008679号