PostgreSQL 项目
PostgreSQL 全球开发小组宣布,PostgreSQL 17 的第一个测试版现已可供下载。此版本包含 PostgreSQL 17 正式发布时将提供的所有功能的预览,不过测试期间版本的某些细节可能会发生变化。您可以在发行说明中找到有关所有 PostgreSQL 17 功能和更改的信息:https://www.postgresql.org/docs/17/release-17.html
本着开源 PostgreSQL 社区的精神,我们强烈建议您在您的系统上测试 PostgreSQL 17 的新功能,以帮助我们消除可能存在的错误或其他问题。虽然我们不建议您在生产环境中运行 PostgreSQL 17 Beta 1,但我们鼓励您找到针对此测试版运行典型应用程序工作负载的方法。您的测试和反馈将帮助社区确保 PostgreSQL 17 版本符合我们的标准,即提供世界上最先进的开源关系数据库的稳定、可靠版本。请阅读有关我们的测试流程以及您如何做出贡献的更多信息:https://www.postgresql.org/developer/beta/
PostgreSQL 17 功能亮点
1.查询和操作性能改进
PostgreSQL 17 在最新版本的基础上进行了改进,并继续提高整个系统的性能。Vacuum 是负责回收存储的 PostgreSQL 进程,它拥有一个新的内部数据结构,可将内存使用量减少 20 倍,同时缩短了完成工作所需的总体时间。此外,Vacuum 进程可使用的内存不再有 1GB 的限制(由 Maintenance_work_mem 控制),因此您可以选择将更多资源用于 Vacuum 进程。
此版本引入了流 I/O 接口,并且可以在执行顺序扫描和运行 ANALYZE 时显示性能改进。PostgreSQL 17 还包括可以控制事务、子事务和多事务缓冲区的可扩展性的配置参数。
PostgreSQL 17 现在可以使用规划器统计信息和公用表表达式 (WITH 查询) 的排序顺序来进一步优化这些查询并帮助它们更快地执行。此外,此版本显著缩短了使用带有 B 树索引的 IN 子句的查询的执行时间。从此版本开始,PostgreSQL 可以从具有 NOT NULL 约束的列上执行中删除冗余的 IS NOT NULL 语句,并且不再需要对包含 IS NOT NULL 列上的 IS NULL 子句的查询进行处理。现在从 PostgreSQL 17 开始,您可以为 BRIN 索引使用并行索引构建。具有高度并发更改的工作负载可以从 PostgreSQL 17 中受益,因为它改进了预写日志 (WAL) 锁的管理方式,一些测试显示性能提高了 2 倍。最后,PostgreSQL 17 添加了更多显式 SIMD 指令,包括对 bit_count 函数的 AVX-512 支持。
2.分区和分布式工作负载增强
PostgreSQL 17 为分区管理带来了更大的灵活性,增加了拆分和合并分区的功能,并增加了对分区表的标识列和排除约束的支持。此外,PostgreSQL 外部数据包装器 (postgres_fdw) 可以从带有 EXISTS 和 IN 子查询的查询中获得性能优势,因为现在可以将这些查询推送到远程服务器。
PostgreSQL 17 为逻辑复制添加了新功能,使其在高可用性工作负载和升级中更易于使用。从 PostgreSQL 17 升级到较新版本开始,您在使用时不再需要删除逻辑复制槽pg_upgrade,从而避免在升级后重新同步数据。此外,此版本还引入了逻辑复制的故障转移控制,为在高可用性环境中管理 PostgreSQL 数据库提供了更多控制。PostgreSQL 17 还允许逻辑复制订阅者使用hash索引进行查找,并引入了pg_createsubscriber使用物理复制在副本上添加逻辑复制的命令行工具。
3.开发人员体验
PostgreSQL 17 继续以 SQL/JSON 标准为基础,增加了对JSON_TABLE可以将 JSON 转换为标准 PostgreSQL 表的功能以及 SQL/JSON 构造函数(JSON、JSON_SCALAR、JSON_SERIALIZE)和查询函数(JSON_EXISTS、JSON_QUERY、JSON_VALUE)的支持。值得注意的是,这些功能最初计划用于 PostgreSQL 15 版本,但由于设计考虑,在测试期间被恢复,这就是我们要求您在测试期间帮助我们测试功能的原因之一!此外,PostgreSQL 17 在其jsonpath实现中添加了更多功能,以及将 JSON 值转换为不同数据类型的能力。
该MERGE命令现在支持RETURNING子句,让您可以进一步处理已修改的行。您还可以MERGE使用新函数查看命令的哪一部分修改了行merge_action。PostgreSQL 17 还允许您使用该MERGE命令更新视图,并添加了一个WHEN NOT MATCHED BY SOURCE子句,以便在源行不满足条件时提供行为。
COPY用于高效地批量加载和导出 PostgreSQL 数据,现在使用 PostgreSQL 17,导出大行时性能可提高 2 倍。此外,COPY当源编码与目标编码匹配时,性能有所提高,并且有一个新选项,ON_ERROR即使插入行时出现错误,也可以继续复制。PostgreSQL 17 还为异步和更安全的查询取消例程提供了更好的支持,驱动程序可以使用 libpq API 采用这些例程。
PostgreSQL 17 包含一个内置排序规则提供程序,它提供与排序规则类似的排序语义C,只是使用UTF-8编码而不是SQL_ASCII。此新排序规则保证是不可变的,从而确保无论 PostgreSQL 安装在什么系统上运行,排序的返回值都不会改变。
4.安全功能
PostgreSQL 17 添加了一个新的连接参数 sslnegotiation,它允许 PostgreSQL 在使用 ALPN 时执行直接 TLS 握手,从而消除了网络往返。PostgreSQL 在 ALPN 目录中注册为 postgresql。
此版本引入了在身份验证期间执行的事件触发器,并在 libpq 中包含了一个新的 API,可PQchangePassword在客户端自动对密码进行哈希处理,以防止意外在服务器中以纯文本形式登录。
PostgreSQL 17 添加了一个名为 pg_maintain 的新预定义角色,该角色允许用户在所有关系上执行 VACUUM、ANALYZE、CLUSTER、REFRESH MATERIALIZED VIEW、REINDEX 和 LOCK TABLE。此版本还确保 search_path 对于 VACUUM、ANALYZE、CLUSTER、REFRESH MATERIALIZED VIEW 和 INDEX 等维护操作是安全的。最后,用户现在可以使用 ALTER SYSTEM 来设置无法识别的自定义服务器变量的值。
5.备份和导出管理
PostgreSQL 17 引入了使用 执行增量备份的功能pg_basebackup,并添加了一个新的实用程序,称为pg_combinebackup,用作备份恢复过程的一部分。此版本为 添加了一个新的标志,称为pg_dump,—filter可让您指定一个文件,其中包含有关从转储中包含/排除哪些对象的说明。
6.监控
该EXPLAIN命令提供有关查询计划和执行的信息,并添加了两个新选项:SERIALIZE,显示将数据转换为网络传输所需的时间,以及MEMORY,报告优化器内存使用情况。此外,EXPLAIN现在可以显示 I/O 块读取和写入所花费的时间。
CALLPostgreSQL 17 规范化了中的参数pg_stat_statements,减少了频繁调用的存储过程的条目数。此外,VACUUM进度报告现在显示清理索引的进度。PostgreSQL 17 还引入了一个新视图,pg_wait_events它提供有关等待事件的描述,并且可以与 结合使用pg_stat_activity以更深入地了解活动会话等待的原因。此外,pg_stat_bgwriter视图中的某些信息现在被拆分到新pg_stat_checkpointer视图中。
7.附加功能
PostgreSQL 17 中添加了许多其他新功能和改进。其中许多功能可能对您的用例也有帮助。请参阅 发行说明以获取新功能和更改功能的完整列表:
https://www.postgresql.org/docs/17/release-17.html
8.测试错误和兼容性
每个 PostgreSQL 版本的稳定性很大程度上取决于您(社区)使用工作负载和测试工具测试即将发布的版本,以便在 PostgreSQL 17 正式发布之前发现错误和回归问题。由于这是 Beta 版,因此仍有可能对数据库行为、功能细节和 API 进行微小更改。您的反馈和测试将有助于确定新功能的最终调整,因此请在不久的将来进行测试。用户测试的质量有助于确定我们何时可以发布最终版本。
PostgreSQL wiki 中公开提供了 未解决问题列表。您可以使用 PostgreSQL 网站上的此表单报告错误:
https://www.postgresql.org/account/submitbug/
1.安装前准备
1.1、软件、硬件说明
| 项目 | 版本 | 环境要求 |
|---|---|---|
| 操作系统 | CentOS 7.9 | 内存:2G CPU:2核 |
| 数据库 | PostgreSQL 17.0 beat1 | PostgreSQL 17.0 beat1 |
1.2、检查操作系统版本
[root@PostgreSQL_Beta vnote-continuous-build]# hostnamectlStatic hostname: PostgreSQL_BetaIcon name: computer-vmChassis: vmMachine ID: f909e2afff734de89f78798b68126c97Boot ID: 5c802c6e3e554a9b89a58bb0ca1b6fe1Virtualization: vmwareOperating System: CentOS Linux 7 (Core)CPE OS Name: cpe:/o:centos:centos:7Kernel: Linux 3.10.0-1160.71.1.el7.x86_64Architecture: x86-64[root@PostgreSQL_Beta vnote-continuous-build]#
2、环境初始化
2.1、安装PostgreSQL必备插件
yum -y install readline readline-devel zlib zlib-devel libicu libicu-devel
2.2、安装PostgreSQL必备插件
[root@PostgreSQL_Beta ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
2..3、内核参数调整
[root@PostgreSQL_Beta ~]# cat>>/etc/sysctl.conf<<EOF> fs.file-max = 76724200> kernel.sem = 10000 10240000 10000 1024> kernel.shmmni = 4096> kernel.shmall = 253702> kernel.shmmax = 1039163392> net.ipv4.ip_local_port_range = 9000 65500> net.core.rmem_default = 262144> net.core.wmem_default = 262144> net.core.rmem_max = 4194304> net.core.wmem_max = 1048576> fs.aio-max-nr = 40960000> vm.dirty_ratio=20> vm.dirty_background_ratio=3> vm.dirty_writeback_centisecs=100> vm.dirty_expire_centisecs=500> vm.swappiness=10> vm.min_free_kbytes=524288> vm.swappiness=0> vm.overcommit_memory=2> vm.overcommit_ratio=75> net.ipv4.ip_local_port_range = 10000 65535> EOF
2.4、用户 limits 设置
说明: 把下面的行增加到/etc/security/limits.conf 文件中
[root@PostgreSQL_Beta ~]# cat>>/etc/security/limits.conf<<EOF> postgres soft nofile 1048576> postgres hard nofile 1048576> postgres soft nproc 131072> postgres hard nproc 131072> postgres soft stack 10240> postgres hard stack 32768> postgres soft core 6291456> postgres hard core 6291456> EOF
2.5检查是否修改正确
[root@PostgreSQL_Beta ~]$ cat /etc/security/limits.conf# /etc/security/limits.conf##This file sets the resource limits for the users logged in via PAM.#It does not affect resource limits of the system services.##Also note that configuration files in /etc/security/limits.d directory,#which are read in alphabetical order, override the settings in this#file in case the domain is the same or more specific.#That means for example that setting a limit for wildcard domain here#can be overriden with a wildcard setting in a config file in the#subdirectory, but a user specific setting here can be overriden only#with a user specific setting in the subdirectory.##Each line describes a limit for a user in the form:##<domain> <type> <item> <value>##Where:#<domain> can be:# - a user name# - a group name, with @group syntax# - the wildcard *, for default entry# - the wildcard %, can be also used with %group syntax,# for maxlogin limit##<type> can have the two values:# - "soft" for enforcing the soft limits# - "hard" for enforcing hard limits##<item> can be one of the following:# - core - limits the core file size (KB)# - data - max data size (KB)# - fsize - maximum filesize (KB)# - memlock - max locked-in-memory address space (KB)# - nofile - max number of open file descriptors# - rss - max resident set size (KB)# - stack - max stack size (KB)# - cpu - max CPU time (MIN)# - nproc - max number of processes# - as - address space limit (KB)# - maxlogins - max number of logins for this user# - maxsyslogins - max number of logins on the system# - priority - the priority to run user process with# - locks - max number of file locks the user can hold# - sigpending - max number of pending signals# - msgqueue - max memory used by POSIX message queues (bytes)# - nice - max nice priority allowed to raise to values: [-20, 19]# - rtprio - max realtime priority##<domain> <type> <item> <value>##* soft core 0#* hard rss 10000#@student hard nproc 20#@faculty soft nproc 20#@faculty hard nproc 50#ftp hard nproc 0#@student - maxlogins 4# End of filepostgres soft nofile 1048576postgres hard nofile 1048576postgres soft nproc 131072postgres hard nproc 131072postgres soft stack 10240postgres hard stack 32768postgres soft core 6291456postgres hard core 6291456
3.PostgreSQL17.0beta1部署
3.1、创建用户和组
[root@PostgreSQL_Beta ~]# groupadd postgres -g 3000[root@PostgreSQL_Beta ~]# useradd postgres -g 3000 -u 3000[root@PostgreSQL_Beta ~]# echo "postgres"|passwd --stdin postgres更改用户 postgres 的密码 。passwd:所有的身份验证令牌已经成功更新。
3.2、创建安装目录
目录规划:
| 序号 | 目录名称 | 目录 |
|---|---|---|
| 1 | postgres安装目录 | /postgres/server |
| 2 | 软件安装目录 | /install |
| 3 | 数据目录 | /postgres/data |
| 4 | WAL 目录 | /postgres/wal |
| 5 | 归档目录 | /postgres/archive |
创建目录:
[root@PostgreSQL_Beta ~]# mkdir /install[root@PostgreSQL_Beta ~]# chmod -R 777 /install[root@PostgreSQL_Beta ~]# mkdir /postgres[root@PostgreSQL_Beta ~]# mkdir -p /postgres/server[root@PostgreSQL_Beta ~]# mkdir -p /postgres/data[root@PostgreSQL_Beta ~]# mkdir -p /postgres/wal[root@PostgreSQL_Beta ~]# mkdir -p /postgres/archive[root@PostgreSQL_Beta ~]# chown -R postgres:postgres /postgres /install[root@PostgreSQL_Beta ~]# chmod 0775 /postgres[root@PostgreSQL_Beta ~]# chmod 0700 /postgres/data[root@PostgreSQL_Beta ~]#
3.3、使用安装用户postgres,切换到install安装目录下,并解压安装包
[root@PostgreSQL_Beta ~]# su postgres[postgres@PostgreSQL_Beta root]$ cd /install[postgres@PostgreSQL_Beta install]$ lspostgresql-17beta1.tar.gz[postgres@PostgreSQL_Beta install]$ tar -xvf postgresql-17beta1.tar.gzpostgresql-17beta1/postgresql-17beta1/.cirrus.starpostgresql-17beta1/.cirrus.tasks.yml..................................................................................postgresql-17beta1/src/tutorial/complex.sourcepostgresql-17beta1/src/tutorial/funcs.cpostgresql-17beta1/src/tutorial/funcs.sourcepostgresql-17beta1/src/tutorial/syscat.source
3.4、切换到postgresql-17beta1安装包目录,并执行安装
说明:安装在/postgres/server目录,port=5432
[postgres@PostgreSQL_Beta install]$ lspostgresql-17beta1 postgresql-17beta1.tar.gz[postgres@PostgreSQL_Beta install]$cd postgresql-17beta1[postgres@PostgreSQL_Beta postgresql-17beta1]$ ./configure --prefix=/postgres/server --with-pgport=5432checking build system type... x86_64-pc-linux-gnuchecking host system type... x86_64-pc-linux-gnuchecking which template to use... linuxchecking whether NLS is wanted... nochecking for default port number... 5432checking for block size... 8kBchecking for segment size... 1GBchecking for WAL block size... 8kB...................................................................config.status: creating src/Makefile.globalconfig.status: creating src/include/pg_config.hconfig.status: creating src/include/pg_config_ext.hconfig.status: creating src/interfaces/ecpg/include/ecpg_config.hconfig.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.sconfig.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.cconfig.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.cconfig.status: linking src/include/port/linux.h to src/include/pg_config_os.hconfig.status: linking src/makefiles/Makefile.linux to src/Makefile.port
3.5、使用 make 编译
[postgres@PostgreSQL_Beta postgresql-17beta1]$ makemake -C ./src/backend generated-headersmake[1]: 进入目录“/install/postgresql-17beta1/src/backend”make -C ../include/catalog generated-headersmake[2]: 进入目录“/install/postgresql-17beta1/src/include/catalog”make[2]: 对“generated-headers”无需做任何事。make[2]: 离开目录“/install/postgresql-17beta1/src/include/catalog”make -C nodes generated-header-symlinks........................................................................................................make[2]: 离开目录“/install/postgresql-17beta1/src/test/isolation”make -C test/perl allmake[2]: 进入目录“/install/postgresql-17beta1/src/test/perl”make[2]: 对“all”无需做任何事。make[2]: 离开目录“/install/postgresql-17beta1/src/test/perl”make[1]: 离开目录“/install/postgresql-17beta1/src”make -C config allmake[1]: 进入目录“/install/postgresql-17beta1/config”make[1]: 对“all”无需做任何事。make[1]: 离开目录“/install/postgresql-17beta1/config”
3.6、使用 make install安装
[postgres@PostgreSQL_Beta postgresql-17beta1]$ make installmake -C ./src/backend generated-headersmake[1]: 进入目录“/install/postgresql-17beta1/src/backend”make -C ../include/catalog generated-headersmake[2]: 进入目录“/install/postgresql-17beta1/src/include/catalog”make[2]: 对“generated-headers”无需做任何事。make[2]: 离开目录“/install/postgresql-17beta1/src/include/catalog”make -C nodes generated-header-symlinksmake[2]: 进入目录“/install/postgresql-17beta1/src/backend/nodes”................................................................................................................make -C config installmake[1]: 进入目录“/install/postgresql-17beta1/config”/usr/bin/mkdir -p '/postgres/server/lib/pgxs/config'/usr/bin/install -c -m 755 ./install-sh '/postgres/server/lib/pgxs/config/install-sh'/usr/bin/install -c -m 755 ./missing '/postgres/server/lib/pgxs/config/missing'make[1]: 离开目录“/install/postgresql-17beta1/config”[postgres@PostgreSQL_Beta postgresql-17beta1]$[postgres@PostgreSQL_Beta postgresql-17beta1]$
3.7、查看版本是否正确
[postgres@PostgreSQL_Beta postgresql-17beta1]$ /postgres/server/bin/postgres --versionpostgres (PostgreSQL) 17beta1[postgres@PostgreSQL_Beta postgresql-17beta1]$
3.8、初始化数据目录
[postgres@PostgreSQL_Beta postgresql-17beta1]$ /postgres/server/bin/initdb -D/postgres/data -X/postgres/wal -EUTF8 -Upostgres -WThe files belonging to this database system will be owned by user "postgres".This user must also own the server process.The database cluster will be initialized with locale "zh_CN.UTF-8".initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"The default text search configuration will be set to "simple".Data page checksums are disabled.Enter new superuser password:设置密码Enter it again:设置密码fixing permissions on existing directory /postgres/data ... okfixing permissions on existing directory /postgres/wal ... okcreating subdirectories ... okselecting dynamic shared memory implementation ... posixselecting default "max_connections" ... 100selecting default "shared_buffers" ... 128MBselecting default time zone ... Asia/Shanghaicreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connectionsinitdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.Success. You can now start the database server using:/postgres/server/bin/pg_ctl -D /postgres/data -l logfile start[postgres@PostgreSQL_Beta postgresql-17beta1]$
3.9、设置环境变量
[postgres@PostgreSQL_Beta postgresql-17beta1]$ vi /home/postgres/.bashrc
环境变量设置内容如下:
# .bashrc# Source global definitionsif [ -f /etc/bashrc ]; then. /etc/bashrcfi# Uncomment the following line if you don't like systemctl's auto-paging feature:# export SYSTEMD_PAGER=# User specific aliases and functionsexport PGPORT=5432export PGUSER=postgresexport PGHOME=/postgres/serverexport PGDATA=/postgres/dataexport PATH=$PGHOME/bin:$PATH~"~/.bashrc" 16L, 363C
3.10、使环境变量生效
[postgres@PostgreSQL_Beta postgresql-17beta1]$ source /home/postgres/.bashrc[postgres@PostgreSQL_Beta postgresql-17beta1]$
3.11、开机自启动服务
说明:PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下,这个路径是安装包解压完的路径。
1.切换到root账户,并指定目录/install/postgresql-17beta1/contrib/start-scripts
[root@PostgreSQL_Beta ~]# cd /install/postgresql-17beta1/contrib/start-scripts[root@PostgreSQL_Beta start-scripts]#
2.修改linux文件属性,添加X属性
[root@PostgreSQL_Beta start-scripts]# chmod a+x linux
3.复制linux文件到/etc/init.d目录下,更名为PostgreSQL
[root@PostgreSQL_Beta start-scripts]# cp linux /etc/init.d/PostgreSQL
4.修改/etc/init.d/PostgreSQL文件
[root@PostgreSQL_Beta start-scripts]# vi /etc/init.d/PostgreSQL
5.修改/etc/init.d/PostgreSQL文件中的prefix、PGDATA
说明:prefix设置为PostgreSQL的安装路径:prefix=/postgres/server;
PGDATA设置为PostgreSQL的数据目录路径:PGDATA=”/postgres/data”
# Installation prefixprefix=/postgres/server# Data directoryPGDATA="/postgres/data"# Who to run postgres as, usually "postgres". (NOT "root")PGUSER=postgres
6.执行service PostgreSQL start,就可以启动PostgreSQL服务
[root@PostgreSQL_Beta start-scripts]# service PostgreSQL startStarting PostgreSQL: ok
7、设置PostgreSQL服务开机自启动
[root@PostgreSQL_Beta ~]# chkconfig --add PostgreSQL
8、重启PostgreSQL服务器
[root@PostgreSQL_Beta ~]# reboot
9、检查PostgreSQL服务,此时PostgreSQL服务已自启动
[postgres@PostgreSQL_Beta /]$ pg_ctl statuspg_ctl: server is running (PID: 917)/postgres/server/bin/postgres "-D" "/postgres/data"[postgres@PostgreSQL_Beta /]$
3.11、登录pgsql,测试登录
[postgres@PostgreSQL_Beta /]$ psqlpsql (17beta1)Type "help" for help.postgres=#
3.12、导入数据测试性能
postgres=# select total_exec_time,(total_exec_time/calls) as average from pg_stat_statements;total_exec_time | as average--------------------+---------------------12.39657 | 6.1982850.8457290000000001 | 0.2819096666666666758.91526 | 58.91526(3 rows)postgres=#




