- 创建新版本使用的目录
mkdir -p /opt/pgdata11.3/pg_root/
mkdir -p /opt/pgsql11.3
- 使用pg_config命令9.6.1查看老版本的编译信息,11.3版本编译信息需要一致,root用户下执行编译安装
安装依赖包
yum -y install coreutils glib2 lrzsz mpstat dstat sysstat e4fsprogs xfsprogs ntp readline-devel zlib-devel openssl-devel pam-devel libxml2-devel libxslt-devel python-devel tcl-devel gcc make smartmontools flex bison perl-devel perl-Ext
Utils* openldap-devel jadetex openjade bzip2
./configure --prefix=/opt/pgsql11.3 --with-pgport=1921 --with-segsize=8 --with-wal-blocksize=64 --with-perl --with-python --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety
gmake world && gmake install-world
chown -R postgres.postgres /opt/pgdata11.3/pg_root/
chown -R postgres.postgres /opt/pgsql11.3
-
切换到postgres用户初始化新版本数据库
/opt/pgsql11.3/bin/initdb -D /opt/pgdata11.3/pg_root/
-
停止老版本9.6.1数据库
pg_ctl stop
-
9.6.1版本的–with-wal-segsize=64是在编译的时候指定的,11.3版本在初始化后使用pg_resetwal修改wal-segsize
/opt/pgsql11.3/bin/pg_resetwal --wal-segsize=64 /opt/pgdata11.3/pg_root/
-
使用pg_upgrade升级
/opt/pgsql11.3/bin/pg_upgrade -U postgres --link -b /opt/pgsql9.6.1/bin/ -B /opt/pgsql11.3/bin/ -d /opt/pgdata9.6/pg_root -D /opt/pgdata11.3/pg_root/
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* data types in user tables ok
Checking for contrib/isn with bigint-passing mismatch ok
Checking for invalid "unknown" user columns 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
If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.
Performing Upgrade
------------------
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_clog to new server 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
Adding ".old" suffix to old global/pg_control ok
If you want to start the old cluster, you will need to remove
the ".old" suffix from /opt/pgdata9.6/pg_root/global/pg_control.old.
Because "link" mode was used, the old cluster cannot be safely
started once the new cluster has been started.
Linking user relation files
ok
Setting next OID for new cluster ok
Sync data directory to disk ok
Creating script to analyze new cluster ok
Creating script to delete old cluster ok
Checking for hash indexes warning
Your installation contains hash indexes. These indexes have different
internal formats between your old and new clusters, so they must be
reindexed with the REINDEX command. The file
reindex_hash.sql
when executed by psql by the database superuser will recreate all invalid
indexes; until then, none of these indexes will be used.
Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
./analyze_new_cluster.sh
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh
重建hash索引
hive=> \c hive hive
You are now connected to database "hive" as user "hive".
hive=> \i reindex_hash.sql
You are now connected to database "hive" as user "hive".
REINDEX
以上提示需要重建hash索引,完成后,需要重新分析数据库生成新的统计信息,并删除老版本的数据,需执行以下命令
./ analyze_new_cluster.sh
./delete_old_cluster.sh
升级到这里完成,步骤比较简单
- 当然升级过程中并不会那么顺利,以下是一些常见报错
报错1:
erforming Consistency Checks
-----------------------------
Checking cluster versions ok
connection to database failed: FATAL: must be superuser to connect in binary upgrade mode
could not connect to source postmaster started with the command:
"/opt/pgsql9.6.1/bin/pg_ctl" -w -l "pg_upgrade_server.log" -D "/opt/pgdata9.6/pg_root" -o "-p 50432 -b -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directories='/home/postgres'" start
解决方法:
因为我机器的环境变量用户是hank,需要超级用户加入-U参数指定为postgres
/opt/pgsql11.3/bin/pg_upgrade -U postgres --link -b /opt/pgsql9.6.1/bin/ -B /opt/pgsql11.3/bin/ -d /opt/pgdata9.6/pg_root -D /opt/pgdata11.3/pg_root/
报错2
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* data types in user tables fatal
Your installation contains one of the reg* data types in user tables.
These data types reference system OIDs that are not preserved by
pg_upgrade, so this cluster cannot currently be upgraded. You can
remove the problem tables and restart the upgrade. A list of the problem
columns is in the file:
tables_using_reg.txt
cat tables_using_reg.txt
Database: hank
public.pathman_config_params.init_callback
Database: postgres
public.pathman_config_params.init_callback
删除相关表或者插件,这里是pg_pathman的插件,于是我进入到相关用户删除插件,升级过程中遇到插件问题一定要注意,可能有的插件版本在老的可用,在新版本不可用,需要重新创建插件,请注意。
drop extension pg_pathman;
报错3
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* data types in user tables ok
Checking for contrib/isn with bigint-passing mismatch ok
Checking for invalid "unknown" user columns ok
Creating dump of global objects ok
Creating dump of database schemas
ok
*failure*
Consult the last few lines of "pg_upgrade_server.log" for
the probable cause of the failure.
connection to database failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/home/postgres/.s.PGSQL.50432"?
could not connect to target postmaster started with the command:
"/opt/pgsql11.3/bin/pg_ctl" -w -l "pg_upgrade_server.log" -D "/opt/pgdata11.3/pg_root/" -o "-p 50432 -b -c synchronous_commit=off -c fsync=off -c full_page_writes=off -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directories='/home/postgres'" start
Failure, exiting
cat loadable_libraries.txt
could not load library "codec.so": ERROR: could not access file "codec.so": No such file or directory
could not load library "codecwithkey.so": ERROR: could not access file "codecwithkey.so": No such file or directory
could not load library "$libdir/mongo_fdw": ERROR: could not access file "$libdir/mongo_fdw": No such file or directory
这里的报错是因为之前我使用了c编写了加密解密函数,这里有两种解决办法,要么在老的库删除掉相关函数,要么在新的库重新编译c函数。
另外常见的比如升级过程中出现错误,需要重新初始化数据库,还需要把新建的表空间目录页删掉,否则重新执行升级时,会报错。
当然这里我只是列举了几个错误,大家可能在升级过程中遇到其他错误,可以根据提示以及日志做出相关应对措施。
比如类似的日志如下:
pg_upgrade_server.log
pg_upgrade_dump_*****.log
pg_upgrade_utility.log
…