暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL 15:一些新特性

PostgreSQL 15 版本正在开发中,不远的将来就会与大家见面,一起来看看未来的一些新功能吧!

1. 删除public 模式的创建权限

直到今天,使用 PostgreSQL 14,每个人都可以默认写入public模式。使用 PostgreSQL 15,这将受到限制。public模式现在由“pg_database_owner”拥有。让我们做一个简短的测试。

postgres=# create user test;CREATE ROLEpostgres=# \du                                   List of roles Role name |                         Attributes                         | Member of-----------+------------------------------------------------------------+----------- postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {} test      |                                                            | {} postgres=# \c postgres testYou are now connected to database "postgres" as user "test".postgres=> create table t1 (a int);ERROR:  permission denied for schema publicLINE 1: create table t1 (a int);                     ^postgres=
复制

如果您希望能够再次写入public模式,必须再次明确授予权限。

2.扩展pg_basebackup压缩

pg_basebackup 有一些扩展。特别是压缩得到改善。首先,–compress 现在能够接受一种压缩方法和一个(可选的)压缩级别,例如 gzip:9。此外 –compress 接受 client-gzip 和 server-gzip 作为压缩方法来定义压缩备份的位置。另一个优点是压缩现在也可以与 –format=p 一起使用。这使您有机会在服务器端压缩 pg_basebackup 并在客户端自动再次提取它。特别是对于慢速网络连接,这可能是一个好处。让我们看一下新的 –compress 语法。

postgres@pgdebian:/u99/backup2/ [PG15] pg_basebackup --help | grep -A 1 compress  -z, --gzip             compress tar output  -Z, --compress=[{client|server}-]METHOD[:DETAIL]                         compress on client or server as specified  -Z, --compress=none    do not compress tar output
复制

要创建备份,它现在看起来像这样。非常简单易用。

postgres@pgdebian:/u99/backup2/ [PG15] pg_basebackup -h localhost -p 5438 -Ft --compress=client-gzip:9 --pgdata=/u99/backup2/ -Xspostgres@pgdebian:/u99/backup2/ [PG15] lltotal 3136-rw------- 1 postgres postgres  136487 Mar 28 15:49 backup_manifest-rw------- 1 postgres postgres 3050084 Mar 28 15:49 base.tar.gz-rw------- 1 postgres postgres   17649 Mar 28 15:49 pg_wal.tar.gzpostgres@pgdebian:/u99/backup2/ [PG15]
复制

或者使用 lz4(可用于客户端或服务器端压缩):

postgres@pgdebian:/u99/backup2/ [PG15] pg_basebackup -h localhost -p 5438 -Ft --compress=lz4:9 --pgdata=/u99/backup2/ -Xspostgres@pgdebian:/u99/backup2/ [PG15] lltotal 20096-rw------- 1 postgres postgres   136487 Mar 28 15:18 backup_manifest-rw------- 1 postgres postgres  3657232 Mar 28 15:18 base.tar.lz4-rw------- 1 postgres postgres 16779264 Mar 28 15:18 pg_wal.tar
复制

3. 新角色:pg_checkpointer

在 PostgreSQL 14 之前,只允许超级用户执行 CHECKPOINT 命令。从 PostgreSQL 15 开始,有一个名为 pg_checkpointer 的新角色。一旦您将该角色授予用户,它就能够执行 CHECKPOINT 命令。

postgres=# create user checky;CREATE ROLEpostgres=# \c postgres checkyYou are now connected to database "postgres" as user "checky".postgres=> checkpoint;ERROR:  must be superuser or have privileges of pg_checkpointer to do CHECKPOINTpostgres=> \c postgres postgresYou are now connected to database "postgres" as user "postgres".postgres=# grant pg_checkpointer to checky;GRANT ROLEpostgres=# \c postgres checkyYou are now connected to database "postgres" as user "checky".postgres=> checkpoint;CHECKPOINTpostgres=>
复制

4. 合并命令

MERGE 让您有机会在常规表和分区表中执行一个插入/更新/删除行的 SQL 语句。如果将其用作单个 SQL 命令,则会有一些开销,因为您需要大量的 WHEN THEN 表达式。让我们用一个简单的例子来看看这个新特性,两个表相似,但其中一个表还有更多条目。

dvdrental=# select * from category; category_id |    name     |     last_update-------------+-------------+---------------------           1 | Action      | 2006-02-15 09:46:27           2 | Animation   | 2006-02-15 09:46:27           3 | Children    | 2006-02-15 09:46:27           4 | Classics    | 2006-02-15 09:46:27           5 | Comedy      | 2006-02-15 09:46:27           6 | Documentary | 2006-02-15 09:46:27           7 | Drama       | 2006-02-15 09:46:27           8 | Family      | 2006-02-15 09:46:27           9 | Foreign     | 2006-02-15 09:46:27          10 | Games       | 2006-02-15 09:46:27          11 | Horror      | 2006-02-15 09:46:27          12 | Music       | 2006-02-15 09:46:27          13 | New         | 2006-02-15 09:46:27          14 | Sci-Fi      | 2006-02-15 09:46:27          15 | Sports      | 2006-02-15 09:46:27          16 | Travel      | 2006-02-15 09:46:27(16 rows) dvdrental=# select * from category_new; category_id |    name     |        last_update-------------+-------------+----------------------------           1 | Action      | 2006-02-15 09:46:27           2 | Animation   | 2006-02-15 09:46:27           3 | Children    | 2006-02-15 09:46:27           4 | Classics    | 2006-02-15 09:46:27           5 | Comedy      | 2006-02-15 09:46:27           6 | Documentary | 2006-02-15 09:46:27           7 | Drama       | 2006-02-15 09:46:27           8 | Family      | 2006-02-15 09:46:27           9 | Foreign     | 2006-02-15 09:46:27          10 | Games       | 2006-02-15 09:46:27          11 | Horror      | 2006-02-15 09:46:27          12 | Music       | 2006-02-15 09:46:27          13 | Biography   | 2022-04-12 11:53:34.986878          14 | Sci-Fi      | 2006-02-15 09:46:27          15 | Sports      | 2006-02-15 09:46:27          16 | Travel      | 2006-02-15 09:46:27          17 | Dramedy     | 2022-04-12 11:48:49.559058          18 | Love        | 2022-04-12 11:49:32.072536(17 rows) dvdrental=# MERGE INTO category AS cUSING category_new AS nON c.category_id = n.category_idWHEN MATCHED AND c.name = n.name  THEN  DO NOTHINGWHEN MATCHED AND c.name  n.name THEN  UPDATE SET name=n.nameWHEN NOT MATCHED THEN  INSERT VALUES (n.category_id, n.name, n.last_update);MERGE 17dvdrental=
复制

完成 MERGE 命令后,再次选择原始表,查看它是否按计划添加和更新了所有内容:

dvdrental=# select * from category order by 1; category_id |    name     |        last_update-------------+-------------+----------------------------           1 | Action      | 2006-02-15 09:46:27           2 | Animation   | 2006-02-15 09:46:27           3 | Children    | 2006-02-15 09:46:27           4 | Classics    | 2006-02-15 09:46:27           5 | Comedy      | 2006-02-15 09:46:27           6 | Documentary | 2006-02-15 09:46:27           7 | Drama       | 2006-02-15 09:46:27           8 | Family      | 2006-02-15 09:46:27           9 | Foreign     | 2006-02-15 09:46:27          10 | Games       | 2006-02-15 09:46:27          11 | Horror      | 2006-02-15 09:46:27          12 | Music       | 2006-02-15 09:46:27          13 | Biography   | 2022-04-12 13:42:26.187381          14 | Sci-Fi      | 2006-02-15 09:46:27          15 | Sports      | 2006-02-15 09:46:27          16 | Travel      | 2006-02-15 09:46:27          17 | Dramedy     | 2022-04-12 11:48:49.559058          18 | Love        | 2022-04-12 11:49:32.072536(18 rows)
复制


Merge 不支持外部表或可更新视图。如果需要的话,也许这会在以后出现。但目前还没有计划。


结论
这些只是 PostgreSQL 版本 15 附带的一些新功能,还有更多新功能要跟进,尤其是在复制方面,还有很多需要检查的地方;安全性方面,PostgreSQL 也会随着每个新版本的发布而改进。



点击此处阅读原文
最后修改时间:2022-04-26 09:22:10
文章转载自开源软件联盟PostgreSQL分会,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论