首页 > 数据库 > 数据库开发技术 > 来,体验下PostgreSQL16的新功能(一)

这里接着前一篇:PostgreSQL 16新特性来了….. 总结一下。让我们体验一下有哪些改进。
CentOS7.9, 下载源码并编译:(wget, aria2之类的都可以)
[05:05:23-postgres@centos1:/iihero/source]$ aria2c https://ftp.postgresql.org/pub/source/v16beta1/postgresql-16beta1.tar.gz
05/31 05:05:27 [NOTICE] Downloading 1 item(s)
[#6b1d93 23MiB/30MiB(78%) CN:1 DL:7.7MiB]
05/31 05:05:32 [NOTICE] Download complete: /iihero/source/postgresql-16beta1.tar.gz
Download Results:
gid |stat|avg speed |path/URI
======+====+===========+=======================================================
6b1d93|OK | 8.0MiB/s|/iihero/source/postgresql-16beta1.tar.gz
Status Legend:
(OK):download completed.
--编译前准备好下述包:
yum install -y readline readline-devel flex bison openssl openssl-devel git
yum install -y gcc gcc-c++ epel-release llvm5.0 llvm5.0-devel clang libicu-devel perl-ExtUtils-Embed zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel openldap-devel systemd-devel tcl-devel python-devel python3-devel lz4 lz4-devel uuid libuuid-devel
[05:07:34-postgres@centos1:/iihero/source/postgresql-16beta1]$
./configure LLVM_CONFIG=/usr/lib64/llvm5.0/bin/llvm-config CLANG=/opt/rh/llvm-toolset-7/root/usr/bin/clang --with-openssl --prefix=/usr/pgsql-16build --with-pgport=6666 --with-blocksize=8 --with-wal-blocksize=8 --with-segsize=1 --with-icu --with-uuid=e2fs --with-lz4 --with-llvm --with-python --with-libxml --with-libxslt --with-systemd -with-extra-version=" [By iihero]"
make -j 4 world-bin && sudo make install-world-bin
环境变量准备:
vim pg16env.sh
export PGBIGVERSION=16
export PGROOT=/usr/pgsql-"$PGBIGVERSION"build
export PGHOME=/var/lib/pgsql
export PGPORT=5432
export PGDATA=$PGHOME/$PGBIGVERSION/data
export PATH=$PGROOT/bin:$PATH
export LD_LIBRARY_PATH=$PGROOT/lib:$LD_LIBRARY_PATH
初始化库并启动:
source pg16env.sh
[05:28:12-postgres@centos1:/var/lib/pgsql]$ initdb -D 16/data --locale=C -E UTF8
[05:30:21-postgres@centos1:/var/lib/pgsql]$ pg_ctl start
waiting for server to start....2023-05-31 05:30:48.708 CST [72394] LOG: starting PostgreSQL 16beta1 [By iihero] on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2023-05-31 05:30:48.709 CST [72394] LOG: listening on IPv6 address "::1", port 5432
2023-05-31 05:30:48.709 CST [72394] LOG: listening on IPv4 address "127.0.0.1", port 5432
2023-05-31 05:30:48.712 CST [72394] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2023-05-31 05:30:48.718 CST [72397] LOG: database system was shut down at 2023-05-31 05:29:56 CST
2023-05-31 05:30:48.723 CST [72394] LOG: database system is ready to accept connections
done
server started
[05:30:48-postgres@centos1:/var/lib/pgsql]$ psql
psql (16beta1 [By iihero])
Type "help" for help.
我们也能看到上边明显的版本标识:(16beta1 [By iihero])
pg16下边针对不同进制的字段值,是如下效果:
postgres=# select 0x0001, 0xff, 123_4;
?column? | ?column? | ?column?
----------+----------+----------
1 | 255 | 1234
(1 row)
-- "_"作千分位隔分位
postgres=# select 12_345;
?column?
----------
12345
(1 row)
如果是在旧版本14里头,则是这样:
postgres=# select 0x0001, 0xff, 123_4;
x0001 | xff | _4
-------+-----+-----
0 | 0 | 123
(1 row)
上边的字段值12_345,主要是为了区分千分位之类,增强可读性。这好像有点借了RUST中语法的味道。很好。
postgres=# select $1 || $2 \bind 'abc' 'bcd' \g
?column?
----------
abcbcd
(1 row)
postgres=# select $1::int + $2::int \bind 123 '456' \g
?column?
----------
579
(1 row)
前边指定变量名,后边通过\bind指定相应的实际值,然后执行。
Sets query parameters for the next query execution, with the specified parameters passed for any parameter placeholders
再来一个例子:
postgres=# insert into t values($1, $2) \bind 1 'abc' \g
INSERT 0 1
postgres=# \bind 2 'ttt' \g
INSERT 0 1
postgres=# \bind 3 'ttt123' \g
INSERT 0 1
postgres=# select * from t;
id | col2
----+--------
1 | abc
2 | ttt
3 | ttt123
(3 rows)
后边可以动态的不断地\bind新值来执行插入操作。
1、可以使用模糊匹配
vi pg_hba.conf
host all /test* 127.0.0.1/32 scram-sha-256
host all all 127.0.0.1/32 trust
我们来验证一下,建两个用户:test1, test2
postgres=# create user test1 with encrypted password 'test123';
CREATE ROLE
postgres=# create user test2 with encrypted password 'test123';
CREATE ROLE
postgres=# select pg_reload_conf();
2023-05-31 05:49:15.058 CST [72394] LOG: received SIGHUP, reloading configuration files
pg_reload_conf
----------------
t
(1 row)
尝试连接:
[05:59:03-postgres@centos1:/var/lib/pgsql/16/data]$ psql -h 127.0.0.1 -U test1 postgres -W
Password:
2023-05-31 05:59:06.319 CST [72803] FATAL: password authentication failed for user "test1"
2023-05-31 05:59:06.319 CST [72803] DETAIL: Connection matched /var/lib/pgsql/16/data/pg_hba.conf line 119: "host all /test* 127.0.0.1/32 scram-sha-256"
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: password authentication failed for user "test1"
注意,test*的用户名配置项是以"/"开头的表达式。这是它的用法里头规定的。加载判断时,仍然是前边的项优先的原则。
# USER can be "all", a user name, a group name prefixed with "+", a
# regular expression (if it starts with a slash (/)) or a comma-separated
# list thereof. In both the DATABASE and USER fields you can also write
# a file name prefixed with "@" to include names from a separate file.
另一种使用方式,是可以把user或database的值指定成 @文件的方式 ,在另一个文件里头定义用户名或database名。相当于一个间接引用。
2、可以包含其它文件进行综合,类似于postgresql.conf
如果我们把上边的那行配置值放入一个新文件,并在pg_hba.conf中最开始的位置加入一行:include。
vi hba2.conf
host all /test* 127.0.0.1/32 scram-sha-256
vi pg_hba.conf
include "hba2.conf"
local all all trust
# IPv4 local connections:
# host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
..........
这样会达到一样的效果。
这个感觉对监控PG数据库的实时IO还是非常有用的,至少可以用于发现系统性能在I/O上的瓶颈和问题所在。
还有一些内容, 后边待续……
PostgreSQL 16 Beta Release Notes: https://www.postgresql.org/docs/16/release-16.html
PostgreSQL 16 Open Issues: https://wiki.postgresql.org/wiki/PostgreSQL_16_Open_Items

原文作者:数据库杂记;如有侵权,请联系管理员删除。