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/
責任編輯:Rex_08