首页 > 技术文章 > PostgreSQL 15:新特性预告!

pgccc 2022-04-26 11:26 原文

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

 

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

 

直到今天,使用 PostgreSQL 14,每个人都可以默认写入public 模式。使用 PostgreSQL 15,这将不再可能。

public 模式现在由“pg_database_owner”拥有。让我们做一个简短的测试。

postgres=# create user PGer;
CREATE ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 PGer      |                                                            | {}
 
postgres=# \c postgres PGer
You are now connected to database "postgres" as user "PGer".
postgres=> create table PGCCC (a int);
ERROR:  permission denied for schema public
LINE 1: create table PGCCC (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 25 13:40 backup_manifest
-rw------- 1 postgres postgres 3050084 Mar 25 13:40 base.tar.gz
-rw------- 1 postgres postgres   17649 Mar 25 13:40 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 25 13:18 backup_manifest
-rw------- 1 postgres postgres  3657232 Mar 25 13:18 base.tar.lz4
-rw------- 1 postgres postgres 16779264 Mar 25 13:18 pg_wal.tar

 

 

 

3.新角色:pg_checkpointer

 

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

postgres=# create user PGer;
CREATE ROLE
postgres=# \c postgres PGer
You are now connected to database "postgres" as user "PGer".
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 PGer;
GRANT ROLE
postgres=# \c postgres PGer
You are now connected to database "postgres" as user "PGer".
postgres=> checkpoint;
CHECKPOINT
postgres=>

 

 

4.合并命令

 

MERGE 让您有机会在常规表和分区表等中执行一个插入/更新/删除行的 SQL 语句。如果将其用作单个 SQL 命令,则会有一些开销,因为您需要大量的 WHEN / THEN 表达式。

让我们用一个简单的例子来看看这个新特性,两个表相似,但其中一个表还有更多条目。

dvdrental=# select * from PGccc;
 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 PGccc_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 PGccc AS c
USING PGccc_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 PGccc 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 不支持外部表或可更新视图。如果需要的话,也许这会在以后出现。但目前还没有计划。

 

 

5.结论

 

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

 

原文链接:

https://blog.dbi-services.com/postgresql-15-some-new-features/

 

 

PG考试相关详情:http://www.pgccc.com.cn/

 

推荐阅读