10月13日发布了PostgreSQL 15!
它包含自版本14以来的许多吸引人的功能,因此值得升级运行早期版本的PostgreSQL的数据库:
改进的排序性能
内存中统计信息收集(删除统计信息收集器进程)
SQL标准的MERGE语法,可以在一条语句中包含INSERT、UPDATE和DELETE操作
逻辑复制发布者选项,用于包括模式中的所有表(包括将来添加的表)、行筛选和列列表以及简化的冲突管理
JSON 格式日志输出(仅针对文件,而不是syslog)
用于 WAL(预写日志)文件的可选 LZ4 和 Zstandard 压缩,对于那些不使用 btrfs 或 zfs 文件系统内置压缩的文件特别有用
新增正则表达式函数regexp_count, regexp_instr, regexp_like, regexp_substr
以及许多其他性能改进和功能增强
感谢PostgreSQL开发人员的持续出色工作!
本文介绍如何升级到 PostgreSQL 15,如果您:
正在运行红帽系列的 Linux 操作系统之一,包括Red Hat Enterprise Linux (RHEL), Rocky Linux, AlmaLinux, Oracle Linux, 或者 CentOS已安装PostgreSQL 14
不使用dnf模块化或库存分布 postgresql RPMs,而是使用postgresql全球开发组(PGDG) postgresql 14 RPMs ,允许多个Postgres主要版本同时共存
我的例子是在 Fedora 36 x86_64操作系统上完成的,其他红帽家族成员的情况也非常相似。
我们在这里做系统管理工作,所以让我们扮演系统管理员,作为root用户工作,而不是重复使用sudo。
$ su -
Password:
[root@yourhost ~]#复制
停止现有的 PostgreSQL 14 数据库服务器(如果它正在运行),并防止它在将来引导时自动启动:
[root@yourhost ~]# systemctl disable --now postgresql-14
复制
确保您的软件包已完全更新:
[root@yourhost ~]# dnf upgrade
Last metadata expiration check: 2:28:40 ago on Fri 14 Oct 2022 09:01:38 AM MDT.
Dependencies resolved. Nothing to do.
Complete!复制
如果有待处理的更新,请应用它们。
如果 Linux 内核、glibc、systemd或其他核心包已更新,请重新启动以加载系统所有最新部分。
我们将在这里使用一组基本的PostgreSQL客户端,服务器,开发包和额外贡献的软件包。
您可能还需要一些其他可用于过程语言的软件包,例如 PL/Perl 和 PL/Python、PostGIS 或其他软件包。请参阅 Yum 存储库索引:
https://download.postgresql.org/pub/repos/yum/15/fedora/fedora-36-x86_64/
现在安装 PostgreSQL 15,由于 PGDG RPM 的设计方式和安装文件的位置,它与 PostgreSQL 14(可能还有其他版本)共存:
[root@yourhost ~]# dnf install postgresql15-server postgresql15-devel postgresql15-contrib
Last metadata expiration check: 2:29:14 ago on Fri 14 Oct 2022 09:01:38 AM MDT.
Dependencies resolved.
==========================================================================
Package Architecture Version Repository Size
==========================================================================
Installing:
postgresql15-contrib x86_64 15.0-1PGDG.f36 pgdg15 708 k
postgresql15-devel x86_64 15.0-1PGDG.f36 pgdg15 5.2 M
postgresql15-server x86_64 15.0-1PGDG.f36 pgdg15 5.9 M
Installing dependencies:
postgresql15 x86_64 15.0-1PGDG.f36 pgdg15 1.5 M
postgresql15-libs x86_64 15.0-1PGDG.f36 pgdg15 290 k
Transaction Summary
==========================================================================
Install 5 Packages
Total download size: 14 M
Installed size: 54 M
Is this ok [y/N]: y
Downloading Packages:
(1/5): postgresql15-contrib-15.0-1PGDG.f36.x86_64.rpm 412 kB/s | 708 kB 00:01
(2/5): postgresql15-libs-15.0-1PGDG.f36.x86_64.rpm 171 kB/s | 290 kB 00:01
(3/5): postgresql15-15.0-1PGDG.f36.x86_64.rpm 435 kB/s | 1.5 MB 00:03
(4/5): postgresql15-devel-15.0-1PGDG.f36.x86_64.rpm 1.0 MB/s | 5.2 MB 00:05
(5/5): postgresql15-server-15.0-1PGDG.f36.x86_64.rpm 1.7 MB/s | 5.9 MB 00:03
-----------------------------------------------------------------------------------
Total 2.0 MB/s | 14 MB 00:06
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : postgresql15-libs-15.0-1PGDG.f36.x86_64 1/5
Running scriptlet: postgresql15-libs-15.0-1PGDG.f36.x86_64 1/5
Installing : postgresql15-15.0-1PGDG.f36.x86_64 2/5
Running scriptlet: postgresql15-15.0-1PGDG.f36.x86_64 2/5
Running scriptlet: postgresql15-server-15.0-1PGDG.f36.x86_64 3/5
Installing : postgresql15-server-15.0-1PGDG.f36.x86_64 3/5
Running scriptlet: postgresql15-server-15.0-1PGDG.f36.x86_64 3/5
Installing : postgresql15-contrib-15.0-1PGDG.f36.x86_64 4/5
Installing : postgresql15-devel-15.0-1PGDG.f36.x86_64 5/5
Running scriptlet: postgresql15-devel-15.0-1PGDG.f36.x86_64 5/5
Verifying : postgresql15-15.0-1PGDG.f36.x86_64 1/5
Verifying : postgresql15-contrib-15.0-1PGDG.f36.x86_64 2/5
Verifying : postgresql15-devel-15.0-1PGDG.f36.x86_64 3/5
Verifying : postgresql15-libs-15.0-1PGDG.f36.x86_64 4/5
Verifying : postgresql15-server-15.0-1PGDG.f36.x86_64 5/5
Installed:
postgresql15-15.0-1PGDG.f36.x86_64 postgresql15-contrib-15.0-1PGDG.f36.x86_64
postgresql15-devel-15.0-1PGDG.f36.x86_64 postgresql15-libs-15.0-1PGDG.f36.x86_64
postgresql15-server-15.0-1PGDG.f36.x86_64
Complete!复制
现在我们已经安装了新的 PostgreSQL 版本,我们可以创建新的数据库集群了。
您需要使用与运行 Postgres 14 时相同的initdb选项。
例如,如果您使用了有用的数据校验和功能,则需要将选项-k传递给initdb,这是通过 RPM 特定于 postgresql-15-setup的脚本间接完成的,如下所示:
[root@yourhost ~]# PGSETUP_INITDB_OPTIONS=-k /usr/pgsql-15/bin/postgresql-15-setup initdb
Initializing database ... OK复制
如果您使用了默认initdb选项,只需省略环境变量:
PGSETUP_INITDB_OPTIONS
[root@yourhost ~]# /usr/pgsql-15/bin/postgresql-15-setup initdb
Initializing database ... OK复制
在这一点上,你可以启动PostgreSQL 15服务器并使用psql连接到它,如果你想从一个空的数据库开始,或者通过通常的方式加载你的数据,例如使用psql或使用pg_dump输出的pg_restore。
但是如果你想转换所有现有的 PostgreSQL 14集群的数据,那么你现在可以运行pg_upgrade。
对于任何重要的系统,在做其他事情之前先备份您的数据,并通读整个 pg_upgrade手册。
它包含几个需要考虑的要点:
使用与旧集群匹配的initdb标志(已经讨论过)。
安装扩展共享目标文件(有些可能包含在贡献包中,而另一些则是单独的,如PostGIS) 。
对于新旧版本的Postgres,在pg_hba.conf中设置认证为peer。这是新集群的默认设置,但是旧的 PostgreSQL 14集群可能需要进行调整,以便pg_update可以访问它。
我们将以postgres操作系统用户的身份运行pg_upgrade,并为postgres 14和15指定标准的PGDG RPM位置:
[root@yourhost ~]# su - postgres
[postgres@yourhost ~]$ /usr/pgsql-15/bin/pg_upgrade -b /usr/pgsql-14/bin -B /usr/pgsql-15/bin -d /var/lib/pgsql/14/data -D /var/lib/pgsql/15/data -j 4
Performing Consistency Checks
-----------------------------
Checking cluster versions ok
old cluster does not use data checksums but the new one does
Failure, exiting复制
哦 ! Pg_upgrade不能将一个没有数据校验和的旧集群转换为一个有数据校验和的新集群,因此在本例中,我们需要删除我们的新集群,并在不使用initdb -k选项校验和的情况下重新创建它。
退出到根shell并重新运行一个合适的initdb命令,如上面所示:
[postgres@yourhost ~]$ exit
logout
[root@yourhost ~]# rm -rf ~postgres/15/data/*
[root@yourhost ~]# /usr/pgsql-15/bin/postgresql-15-setup initdb
Initializing database ... OK复制
现在让我们再次尝试运行:pg_upgrade
[root@yourhost ~]# su - postgres
[postgres@yourhost ~]$ /usr/pgsql-15/bin/pg_upgrade -b /usr/pgsql-14/bin -B /usr/pgsql-15/bin -d /var/lib/pgsql/14/data -D /var/lib/pgsql/15/data -j 4
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 system-defined composite types in user tables ok
Checking for reg* data types in user tables ok
Checking for contrib/isn with bigint-passing mismatch 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
Checking for new cluster tablespace directories 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_xact to new server ok
Setting oldest XID for new cluster 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
Copying user relation files
ok
Setting next OID for new cluster ok
Sync data directory to disk ok
Creating script to delete old cluster ok
Checking for extension updates ok
Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade.
Once you start the new server, consider running:
/usr/pgsql-15/bin/vacuumdb --all --analyze-in-stages
Running this script will delete the old cluster's data files:
./delete_old_cluster.sh复制
很好,通过了!
您可能需要手动迁移postgresql.conf和 pg_hba .conf中的任何配置。
从旧的PostgreSQL 14集群确认到新的 PostgreSQL 15集群,使用diff或类似的工具。
一旦你准备好了,退出回到root 用户,然后启动新PostgreSQL 15数据库,并设置自动启动:
[postgres@yourhost ~]$ exit
logout
[root@yourhost ~]# systemctl enable --now postgresql-15
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql-15.service → /usr/lib/systemd/system/postgresql-15.service.复制
让我们采纳pg_upgrade的建议来分析我们新导入的数据库,这样查询计划器就有了它需要的统计数据来进行明智的计划。
我们不急着让数据库重新上线,所以我们不会费心分阶段进行分析,而是一次性完成所有工作:
[root@yourhost ~]# su - postgres
[postgres@yourhost ~]$ /usr/pgsql-15/bin/vacuumdb -a -Z
vacuumdb: vacuuming database "funtimes"
vacuumdb: vacuuming database "postgres"
vacuumdb: vacuuming database "template1"复制
现在我们可以尝试新的安装,并检查一些巧妙的PostgreSQL 15新特性是否真的可用:
[postgres@yourhost ~]$ psql
psql (15.0)
Type "help" for help.
postgres=# select version();
version
----------------------------------------------------------------------------------------------------------
PostgreSQL 15.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 12.2.1 20220819 (Red Hat 12.2.1-2), 64-bit
(1 row)
postgres=# select regexp_count('the quick brown fox jumped over the lazy', ' ');
regexp_count
--------------
7
(1 row)
postgres=# \h merge
Command: MERGE
Description: conditionally insert, update, or delete rows of a table
Syntax:
[ WITH with_query [, ...] ]
MERGE INTO target_table_name [ [ AS ] target_alias ]
USING data_source ON join_condition
when_clause [...]
where data_source is:
{ source_table_name | ( source_query ) } [ [ AS ] source_alias ]
and when_clause is:
{ WHEN MATCHED [ AND condition ] THEN { merge_update | merge_delete | DO NOTHING } |
WHEN NOT MATCHED [ AND condition ] THEN { merge_insert | DO NOTHING } }
and merge_insert is:
INSERT [( column_name [, ...] )]
[ OVERRIDING { SYSTEM | USER } VALUE ]
{ VALUES ( { expression | DEFAULT } [, ...] ) | DEFAULT VALUES }
and merge_update is:
UPDATE SET { column_name = { expression | DEFAULT } |
( column_name [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]
and merge_delete is:
DELETE复制
URL: https://www.postgresql.org/docs/15/sql-merge.htm
您可能没有这样做的紧迫性,但是稍后在您确信PostgreSQL 15对您来说运行良好之后,您可以删除旧的PostgreSQL 14软件包和数据。
首先,我们将在不同意的情况下运行此命令,以查看包管理器计划执行的操作:
[root@yourhost ~]# rpm -qa postgresql14\* | xargs dnf erase
Dependencies resolved.
======================================================================
Package Architecture Version Repository Size
======================================================================
Removing:
postgresql14 x86_64 14.5-1PGDG.f36 @pgdg14 7.7 M
postgresql14-devel x86_64 14.5-1PGDG.f36 @pgdg14 19 M
postgresql14-libs x86_64 14.5-1PGDG.f36 @pgdg14 935 k
postgresql14-server x86_64 14.5-1PGDG.f36 @pgdg14 24 M
Transaction Summary
======================================================================
Remove 4 Packages
Freed space: 51 M
Is this ok [y/N]: Operation aborted.复制
如果它建议只删除你想要的,而不是任何你想要保留的依赖,你可以通过-y (yes)选项让dnf继续进行:
[root@yourhost ~]# rpm -qa postgresql14\* | xargs dnf erase -y
复制
您可以运行pg_upgrade的脚本,帮您删除旧集群的数据文件:
[root@yourhost ~]# su - postgres
[postgres@yourhost ~]$ ./delete_old_cluster.sh复制
就到这里,尽情使用吧!
参考
PostgreSQL 15发布公告:
https://www.postgresql.org/about/news/postgresql-15-released-2526/
PostgreSQL 15发行说明:
https://www.postgresql.org/docs/release/15.0/
PostgreSQL 红帽家族 Linux 下载:
https://www.postgresql.org/download/linux/redhat/
PostgreSQL全球开发组(PGDG) YUM 存储库:
https://yum.postgresql.org/
点击此处阅读原文
↓↓↓