pg_upgrade 版本升级

简介: --对于小版本的升级,内部存储格式是兼容的,大版本不一定兼容--小版本可以直接替换二进制文件,大版本需要导出与导入升级Minor releases never change the in...
--对于小版本的升级,内部存储格式是兼容的,大版本不一定兼容
--小版本可以直接替换二进制文件,大版本需要导出与导入升级
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;

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
6月前
|
存储 缓存 NoSQL
淘宝的购物车为什么永不丢失?分布式会话架构深度解析(附python源码)
淘宝购物车“永不丢失”源于其无状态服务+分布式缓存(Redis/Tair)架构:数据脱离单机内存,统一存储于高可用共享集群;结合user_id/匿名ID贯通多端,并支持登录自动合并。文末附可运行的Python模拟实现。(239字)
|
9月前
|
Web App开发 人工智能
火语言 RPA:模拟真人操作 —— 微博超话签到、发布互动贴与浏览全流程案例
本案例基于火语言RPA,自动化完成微博超话签到、发帖互动及10次滚动浏览,精准模拟真人行为。支持指定Chrome路径与用户配置,智能判断签到弹窗、条件执行操作,并自动关闭浏览器释放资源,有效提升账号活跃度。
670 2
|
监控 Java C#
Spring Event 的介绍
Spring Event 是 Spring 框架中的事件驱动机制,允许组件间进行同步或异步消息传递,无需直接依赖。它包括事件(Event)、事件发布者(Publisher)和事件监听器(Listener),通过 `ApplicationEventPublisher` 广播事件,实现松耦合通信,增强模块化和可维护性。Spring 还提供了多种内置事件,如 `ContextRefreshedEvent` 和 `ContextClosedEvent`,支持同步及异步处理,并具备良好的扩展性。
666 3
|
弹性计算 安全 小程序
编程之美:Python让你领略浪漫星空下的流星雨奇观
这段代码使用 Python 的 `turtle` 库实现了一个流星雨动画。程序通过创建 `Meteor` 类来生成具有随机属性的流星,包括大小、颜色、位置和速度。在无限循环中,流星不断移动并重新绘制,营造出流星雨的效果。环境需求为 Python 3.11.4 和 PyCharm 2023.2.5。
1558 9
|
机器学习/深度学习 自然语言处理
彻底改变语言模型:全新架构TTT超越Transformer,ML模型代替RNN隐藏状态
【7月更文挑战第25天】近年来,NLP领域取得显著进展但也面临挑战,如长上下文建模与计算效率的平衡。为此,研究人员提出Test-Time Training (TTT) 模型架构。TTT由多机构合作开发,旨在解决长上下文建模难题及提高计算效率。通过将隐藏状态视为可学习更新的模型,TTT能随输入增长提升表示能力;采用自监督学习更新规则确保线性计算复杂度的同时保持高性能。实验显示TTT在多种NLP任务中表现优秀,尤其在长上下文处理方面超越Transformer。尽管如此,TTT仍面临训练资源需求高及自监督学习鲁棒性等挑战。[论文](https://arxiv.org/abs/2407.04620)
1082 5
|
JavaScript 前端开发 Go
8 大博客引擎 jekyll/hugo/Hexo/Pelican/Gatsby/VuePress/Nuxt.js/Middleman 对比
探索各类博客引擎:Jekyll、Hugo、Hexo、Pelican、Gatsby、VuePress、Nuxt.js和Middleman的对比,包括语言、模板引擎、速度、社区活跃度等。了解每种引擎的优缺点,助你选择合适的博客构建工具。查看详细文章以获取更多实战和安装指南。
|
人工智能 JSON 机器人
【Chat GPT】用 ChatGPT 运行 Python
【Chat GPT】用 ChatGPT 运行 Python
802 1
|
数据采集 JSON JavaScript
JS逆向-反反爬案例
破解爬虫JS加密反爬, 逆向JS解析
|
机器学习/深度学习 人工智能 自然语言处理
大模型技术的发展与实践
一、大模型的概念 大型语言模型,也称大语言模型、大模型(Large Language Model,LLM;Large Language Models,LLMs) 。 大语言模型是一种深度学习模型,特别是属于自然语言处理(NLP)的领域,一般是指包含数干亿(或更多)参数的语言模型,这些参数是在大量文本数据上训练的,例如模型GPT-3,PaLM,LLaMA等,大语言模型的目的是理解和生成自然语言,通过学习大量的文本数据来预测下一个词或生成与给定文本相关的内容。 参数可以被理解为模型学习任务所需要记住的信息,参数的数量通常与模型的复杂性和学习能力直接相关,更多的参数意味着模型可能具有更强的学习
1604 0