PostgreSQL升级之pg_upgrade升级
PostgreSQL中的升级,如果针对小版本的升级,比如9.6.1升级到9.6.2(当前的最新版本),只需要用9.6.2版本的软件替换9.6.1版本的软件即可,不需要做额外的操作,因为整个大版本是相互兼容的,内部存储形式也是兼容的。但如果涉及到跨大版本升级比如9.4.11升级到9.6.2,这种直接替换软件就不行了,因为跨版本的内部存储形式发生了变化。
官方给了三种升级的方式来解决跨版本升级:
pg_dumpall
pg_upgrade
通过复制
pg_dumpall是一种把数据从旧版本逻辑导出,再导入新版本的方法,就是一个导出导入的过程。
通过复制的方式是创建一个高版本的从库,等数据同步完后主变备,备变主,达到升级的目的。
再一种是通过pg_upgrade命令的升级方式,它是一种快速升级的方法,通过创建新的系统表并使用旧的用户表的方式进行升级。它又分为两种方式:原地升级和非原地升级,原地升级需要指定--link参数。
下面介绍一下使用pg_upgrade做升级的大体步骤:
示例是从9.4.11升级到9.6.2。
1、安装新版本软件
新版本的软件需要保证与旧版本的软件在配置上兼容,pg_upgrade会在升级前检查pg_controldata,确保所有的设置是兼容的。
2、用新版本初始化一个新的数据库
[postgres@rhel7~]$/opt/pgsql-9.6.2/bin/initdb-D/pgdata-new/Thefilesbelongingtothisdatabasesystemwillbeownedbyuser"postgres".Thisusermustalsoowntheserverprocess.Thedatabaseclusterwillbeinitializedwithlocale"en_US.UTF-8".Thedefaultdatabaseencodinghasaccordinglybeensetto"UTF8".Thedefaulttextsearchconfigurationwillbesetto"english".Datapagechecksumsaredisabled.fixingpermissionsonexistingdirectory/pgdata-new...okcreatingsubdirectories...okselectingdefaultmax_connections...100selectingdefaultshared_buffers...128MBselectingdynamicsharedmemoryimplementation...posixcreatingconfigurationfiles...okrunningbootstrapscript...okperformingpost-bootstrapinitialization...oksyncingdatatodisk...okWARNING:enabling"trust"authenticationforlocalconnectionsYoucanchangethisbyeditingpg_hba.conforusingtheoption-A,or--auth-localand--auth-host,thenexttimeyouruninitdb.Success.Youcannowstartthedatabaseserverusing:/opt/pgsql-9.6.2/bin/pg_ctl-D/pgdata-new/-llogfilestart
3、设置pg_hba.conf,保证pg_upgrade通过连接新旧两个库
4、停止旧库
#创建测试表[postgres@rhel7~]$psqlpsql(9.4.11)Type"help"forhelp.^postgres=#createtablezx(idint);CREATETABLEpostgres=#\dListofrelationsSchema|Name|Type|Owner--------+------+-------+----------public|zx|table|postgres(1row)postgres=#insertintozxvalues(1);INSERT01postgres=#select*fromzx;id----1(1row)#停止旧库[postgres@rhel7~]$/opt/pgsql-9.4/bin/pg_ctlstop-D/usr/local/pgsql/data/waitingforservertoshutdown....doneserverstopped
5、使用pg_upgrade执行升级
[postgres@rhel7~]$/opt/pgsql-9.6.2/bin/pg_upgrade-d/usr/local/pgsql/data/-D/pgdata-new/-b/opt/pgsql-9.4/bin/-B/opt/pgsql-9.6.2/bin/PerformingConsistencyChecks-----------------------------CheckingclusterversionsokCheckingdatabaseuseristheinstalluserokCheckingdatabaseconnectionsettingsokCheckingforpreparedtransactionsokCheckingforreg*systemOIDuserdatatypesokCheckingforcontrib/isnwithbigint-passingmismatchokCheckingforrolesstartingwith'pg_'okCreatingdumpofglobalobjectsokCreatingdumpofdatabaseschemasokCheckingforpresenceofrequiredlibrariesokCheckingdatabaseuseristheinstalluserokCheckingforpreparedtransactionsokIfpg_upgradefailsafterthispoint,youmustre-initdbthenewclusterbeforecontinuing.PerformingUpgrade------------------AnalyzingallrowsinthenewclusterokFreezingallrowsonthenewclusterokDeletingfilesfromnewpg_clogokCopyingoldpg_clogtonewserverokSettingnexttransactionIDandepochfornewclusterokDeletingfilesfromnewpg_multixact/offsetsokCopyingoldpg_multixact/offsetstonewserverokDeletingfilesfromnewpg_multixact/membersokCopyingoldpg_multixact/memberstonewserverokSettingnextmultixactIDandoffsetfornewclusterokResettingWALarchivesokSettingfrozenxidandminmxidcountersinnewclusterokRestoringglobalobjectsinthenewclusterokRestoringdatabaseschemasinthenewclusterokCopyinguserrelationfilesokSettingnextOIDfornewclusterokSyncdatadirectorytodiskokCreatingscripttoanalyzenewclusterokCreatingscripttodeleteoldclusterokUpgradeComplete----------------Optimizerstatisticsarenottransferredbypg_upgradeso,onceyoustartthenewserver,considerrunning:./analyze_new_cluster.shRunningthisscriptwilldeletetheoldcluster'sdatafiles:./delete_old_cluster.sh
介绍下使用的参数-b指定旧版本软件的bin目录-B指定新版本软件的bin目录,-d指定旧版本对应的数据目录,-D指定新版本对应的数据目录。
6、启动新版本数据库并做检查
[postgres@rhel7~]$/opt/pgsql-9.6.2/bin/pg_ctlstart-D/pgdata-new/-llogfileserverstarting[postgres@rhel7~]$psqlpsql(9.6.2)Type"help"forhelp.postgres=#\dListofrelationsSchema|Name|Type|Owner--------+------+-------+----------public|zx|table|postgres(1row)postgres=#select*fromzx;id----1(1row)
7、恢复配置文件如pg_hba.conf、postgresql.conf等
8、收集统计信息
由于升级过程中不会把统计信息传到新库系统表中,需要重新收集统计信息。pg_upgrade的最给出了收集统计信息的脚本:
[postgres@rhel7~]$./analyze_new_cluster.shThisscriptwillgenerateminimaloptimizerstatisticsrapidlysoyoursystemisusable,andthengatherstatisticstwicemorewithincreasingaccuracy.Whenitisdone,yoursystemwillhavethedefaultlevelofoptimizerstatistics.IfyouhaveusedALTERTABLEtomodifythestatisticstargetforanytables,youmightwanttoremovethemandrestorethemafterrunningthisscriptbecausetheywilldelayfaststatisticsgeneration.Ifyouwouldlikedefaultstatisticsasquicklyaspossible,cancelthisscriptandrun:"/opt/pgsql-9.6.2/bin/vacuumdb"--all--analyze-onlyvacuumdb:processingdatabase"postgres":Generatingminimaloptimizerstatistics(1target)vacuumdb:processingdatabase"template1":Generatingminimaloptimizerstatistics(1target)vacuumdb:processingdatabase"postgres":Generatingmediumoptimizerstatistics(10targets)vacuumdb:processingdatabase"template1":Generatingmediumoptimizerstatistics(10targets)vacuumdb:processingdatabase"postgres":Generatingdefault(full)optimizerstatisticsvacuumdb:processingdatabase"template1":Generatingdefault(full)optimizerstatisticsDone
9、升级成功后删除旧版本软件和数据。
官方文档:https://www.postgresql.org/docs/9.6/static/pgupgrade.html
https://www.postgresql.org/docs/9.6/static/upgrading.html
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。