PostgreSQL 15:一些新特性

2022/04/25 15:53
阅读数 9K


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

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


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

postgres=# create user test;
CREATE ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 test      |                                                            | {}
 
postgres=# \c postgres test
You are now connected to database "postgres" as user "test".
postgres=> create table t1 (a int);
ERROR:  permission denied for schema public
LINE 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/ -Xs

postgres@pgdebian:/u99/backup2/ [PG15] ll
total 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.gz
postgres@pgdebian:/u99/backup2/ [PG15]


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


postgres@pgdebian:/u99/backup2/ [PG15] pg_basebackup -h localhost -p 5438 -Ft --compress=lz4:9 --pgdata=/u99/backup2/ -Xs
postgres@pgdebian:/u99/backup2/ [PG15] ll
total 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 ROLE
postgres=# \c postgres checky
You are now connected to database "postgres" as user "checky".
postgres=> checkpoint;
ERROR:  must be superuser or have privileges of pg_checkpointer to do CHECKPOINT
postgres=> \c postgres postgres
You are now connected to database "postgres" as user "postgres".
postgres=# grant pg_checkpointer to checky;
GRANT ROLE
postgres=# \c postgres checky
You are now connected to database "postgres" as user "checky".
postgres=> checkpoint;
CHECKPOINT
postgres=>


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 c
USING category_new AS n
ON c.category_id = n.category_id
WHEN MATCHED AND c.name = n.name  THEN
  DO NOTHING
WHEN MATCHED AND c.name  n.name THEN
  UPDATE SET name=n.name
WHEN NOT MATCHED THEN
  INSERT VALUES (n.category_id, n.name, n.last_update)
;
MERGE 17
dvdrental=


完成 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 也会随着每个新版本的发布而改进。



点击此处阅读原文

本文分享自微信公众号 - 开源软件联盟PostgreSQL分会(kaiyuanlianmeng)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

展开阅读全文
加载中

作者的其它热门文章

打赏
0
1 收藏
分享
打赏
1 评论
1 收藏
0
分享
返回顶部
顶部