加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

pg_upgrade 版本升级

发布时间:2020-12-13 17:06:11 所属栏目:百科 来源:网络整理
导读:--对于小版本的升级,内部存储格式是兼容的,大版本不一定兼容--小版本可以直接替换二进制文件,大版本需要导出与导入升级Minor releases never change the internal storage format and are always compatible with earlier and later minor releases of th
--对于小版本的升级,内部存储格式是兼容的,大版本不一定兼容
--小版本可以直接替换二进制文件,大版本需要导出与导入升级
Minor releases never change the internal storage format and are always compatible with earlier and later minor releases of the same major version number,e.g.,8.4.2 is compatible with 8.4,8.4.1 and 8.4.6. 
To update between compatible versions,you simply replace the executables while the server is down and restart the server. The data directory remains unchanged — minor upgrades are that simple.
For major releases of PostgreSQL,the internal data storage format is subject to change,thus complicating upgrades. The traditional method for moving data to a new major version is to dump and reload the
database,though this can be slow. A faster method is pg_upgrade. Replication methods are also available,as discussed below.

--在使用pg_dumpall进行长级时,注意要保持数据的完整性
If making a backup,make sure that your database is not being updated. This does not affect the integrity of the backup,but the changed data would of course not be included. 
If necessary,edit the permissions in the file pg_hba.conf (or equivalent) to disallow access from everyone except you



--pg_upgrade用于大版本的升级,但对于小版本的升级不起作用
pg_upgrade allows data stored in PostgreSQL data files to be upgraded to a later PostgreSQL major version without the data dump/reload typically required for major version
upgrades,e.g. from 8.4.7 to the current major release of PostgreSQL. It is not required for minor version upgrades,e.g. from 9.0.1 to 9.0.4.
-- pg_upgrade 用于升级系统表,但要求内部存储格式是不变的
Major PostgreSQL releases regularly add new features that often change the layout of the system tables,but the internal data storage format rarely changes. pg_upgrade uses this fact to perform rapid upgrades
by creating new system tables and simply reusing the old user data files. If a future major release ever
changes the data storage format in a way that makes the old data format unreadable,pg_upgrade will not
be usable for such upgrades. (The community will attempt to avoid such situations.)

-- 在 pg_upgrade 升级时使用link方式可以减少磁盘占用的空间
If you use link mode,the upgrade will be much faster (no file copying) and use less disk space,but you will not be able to access your old cluster once you start the new cluster after the upgrade. 
Link mode also requires that the old and new cluster data directories be in the same file system. (Tablespaces and pg_xlog can be on different file systems.


--pg_upgrade升级前可以使用check校验是否兼容
Once started,pg_upgrade will verify the two clusters are compatible and then do the upgrade. You can use pg_upgrade --check to perform only the checks,even if the old server is still running.
pg_upgrade --check will also outline any manual adjustments you will need to make after the upgrade

-- 升级之后,老版本的cluster处理方式
? If you ran pg_upgrade with --check,no modifications were made to the old cluster and you can re-use it anytime.
? If you ran pg_upgrade with --link,the data files are shared between the old and new cluster. If you started the new cluster,the new server has written to those shared files and it is unsafe to use the old cluster.
? If you ran pg_upgrade without --link or did not start the new server,the old cluster was not modified except that,if linking started,a .old suffix was appended to
$PGDATA/global/pg_control. To reuse the old cluster,possibly remove the .old suffix from
$PGDATA/global/pg_control; you can then restart the old cluster.


-- pg_upgrade 从9.3升级到9.6

--安装9.6版本数据库

--配置环境变量
PATH=$PATH:$HOME/bin
export PATH=/opt/PostgreSQL/9.6/bin:$PATH
export PGDATA=/opt/PostgreSQL/9.6/9601
export PGHOME=/opt/PostgreSQL/9.6
export LD_LIBRARY_PATH=/opt/PostgreSQL/9.6/lib
export PGPORT=9601
export PATH


--初始化新数据库
/opt/PostgreSQL/9.6/bin/initdb -E UTF8 -D /data/pgsql/newdb  --locale=C 

--修改新库的posgresql.conf文件,是其尽量与旧库一样
--启动新数据库,安装必要的插件,比如pg_stat_statements,pg_statsinfo ,然后再关闭新数据库

--停老库
pg_ctl stop -m fast -D /data/pgsql/olddb


--pg_upgrade 前检查
/opt/PostgreSQL/9.6/bin/pg_upgrade -c --link -b /opt/PostgreSQL/9.3/bin -B /opt/PostgreSQL/9.6/bin -d /data/pgsql/olddb -D  /data/pgsql/newdb
--备注: -b,-B 分别表示老版本 PG bin 目录,新版本 PG bin目录, -d,-D 分别表示老版本PG 数据目录,新版本 PG 数据目录, -c 表示仅检查,并不会做任何更改, 根据提示查看文件 loadable_libraries.txt 。

[postgres@beta newdb]$ /opt/PostgreSQL/9.6/bin/pg_upgrade -c --link -b /opt/PostgreSQL/9.3/bin -B /opt/PostgreSQL/9.6/bin -d /data/pgsql/olddb -D  /data/pgsql/newdb
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* system OID user data types                ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for roles starting with 'pg_'                      ok
Checking for invalid "line" user columns                    ok

lc_collate values for database "postgres" do not match:  old "en_US.UTF-8",new "C"
Failure,exiting
--由以上可知,新老数据库的符集不对应
/opt/PostgreSQL/9.6/bin/initdb -E UTF8 -D /data/pgsql/newdb  --locale=en_US.UTF-8

--没有安装pg_statsinfo,安装之
Could not load library "$libdir/pg_statsinfo"
-- 在使用9.6版本过程中,安装pg_statsinfo但一直报错,估计pg_statsinfo还不支持9.6版本,此时可以先不安装此插件
/opt/PostgreSQL/9.6/lib/postgresql/pg_statsinfo.so: undefined symbol: SnapshotNowData
--启动原有的老库,删除stasinfo,再把老库关闭
postgres=# drop schema statsinfo cascade;



--pg_upgrade 升级
/opt/PostgreSQL/9.6/bin/pg_upgrade --link -b /opt/PostgreSQL/9.3/bin -B /opt/PostgreSQL/9.6/bin -d /data/pgsql/olddb -D  /data/pgsql/newdb
--备注:这里使用了 --link 模式,升级完成后提示运行分析脚本   analyze_new_cluster.sh
--link 模式,新版本软件共享老版本软件数据目录,用新版本软件启动数据目录后,再次用老版本软件启动目录会有问题。默认表空间其文件创建的连接为硬连接

--由于使用的硬连接,故删除老的数据文件并不影响新的数据文件的使用
postgres@localhost-> du -sh *
170M    olddb
3.0M    newdb
postgres@localhost-> rm -rf olddb
--删除后,文件统计信息正确
postgres@localhost-> du -sh *
170M    newdb


--用新版本软件起新库
/opt/PostgreSQL/9.6/bin/pg_ctl start -D /data/pgsql/newdb


--升级后的操作
--运行分析脚本,使用pg_upgrade升级的话,统计信息不会迁移过来. 所以需要手工统计一下
./analyze_new_cluster.sh 
备注: 这个脚本其实就一条 vacuumdb 命令,收集新库统计信息。



说明 : 如果数据库里有手工添加的表空间,那么实际上升级完成后,newdb 里只有一个到olddb 的表空间的符号链接,
这个应该要手工把这个表空间目录下的文件copy到newdb 中,并修改pg_tblspc 里对应的符号链接,默认表空间其文件创建的连接为硬连接
[root@beta pg_tblspc]# ls -alh
lrwxrwxrwx  1 postgres postgres   15 May 20 17:55 16400 -> /data/pgsql/tbl



--删除老版本软件
./delete_old_cluster.sh 

备注:新库一切正常后,删除老版本软件。

--修改 postgresql.conf,pg_hba.conf 等配置文件
   根据生产需要,调整这两个文件的配置,在生产环境升级时,为了减少停机维护时间,可以事先写好这两个文件。
   

   
    create tablespace tbs location '/data/pgsql/tbl';
    create table t1 tablespace tbs as select * from postgres_log;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读