PostgreSQL 15:新特性预告!PostgreSQL 15 版本正在开发中,不远的将来就会与大家见面,所以是时候看看未来的一些新功能吧!1删除public 模式的创建权限直到今天,使用 PostgreSQL 14,每个人都可以默认写入public 模式。使用 PostgreSQL 15,这将不再可能。
public 模式现在由“pg_database_owner”拥有。
让我们做一个简短的测试。postgres=# create user PGer;CREATE ROLEpostgres=# duList of roles Role name |Attributes| Member of-----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} PGer || {} postgres=# c postgres PGerYou are now connected to database "postgres" as user "PGer".postgres=> create table PGCCC ;ERROR: permission denied for schema publicLINE 1: create table PGCCC ;^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=nonedo 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 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.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 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 ROLEpostgres=# c postgres PGerYou are now connected to database "postgres" as user "PGer".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 PGer;GRANT ROLEpostgres=# c postgres PGerYou are now connected to database "postgres" as user "PGer".postgres=> checkpoint;CHECKPOINTpostgres=>
4合并命令MERGE 让您有机会在常规表和分区表等中执行一个插入/更新/删除行的 SQL 语句。如果将其用作单个 SQL 命令,则会有一些开销,因为您需要大量的 WHEN / THEN 表达式。
让我们用一个简单的例子来看看这个新特性,两个表相似,但其中一个表还有更多条目。dvdrental=# select * from PGccc; category_id |name | last_update-------------+-------------+---------------------1 | Action | 2006-02-15 09:46:272 | Animation | 2006-02-15 09:46:273 | Children| 2006-02-15 09:46:274 | Classics| 2006-02-15 09:46:275 | Comedy | 2006-02-15 09:46:276 | documentary | 2006-02-15 09:46:277 | Drama| 2006-02-15 09:46:278 | Family | 2006-02-15 09:46:279 | 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 dvdrental=# select * from PGccc_new; category_id |name | last_update-------------+-------------+----------------------------1 | Action | 2006-02-15 09:46:272 | Animation | 2006-02-15 09:46:273 | Children| 2006-02-15 09:46:274 | Classics| 2006-02-15 09:46:275 | Comedy | 2006-02-15 09:46:276 | documentary | 2006-02-15 09:46:277 | Drama| 2006-02-15 09:46:278 | Family | 2006-02-15 09:46:279 | 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 dvdrental=# MERGE INTO PGccc AS cUSING PGccc_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 ;MERGE 17dvdrental=#
完成 MERGE 命令后,再次选择原始表,查看它是否按计划添加和更新了所有内容:dvdrental=# select * from PGccc order by 1; category_id |name | last_update-------------+-------------+----------------------------1 | Action | 2006-02-15 09:46:272 | Animation | 2006-02-15 09:46:273 | Children| 2006-02-15 09:46:274 | Classics| 2006-02-15 09:46:275 | Comedy | 2006-02-15 09:46:276 | documentary | 2006-02-15 09:46:277 | Drama| 2006-02-15 09:46:278 | Family | 2006-02-15 09:46:279 | 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
Merge 不支持外部表或可更新视图。如果需要的话,也许这会在以后出现。但目前还没有计划。
5结论这些只是 PostgreSQL 版本 15 附带的一些新功能。还有更多新功能要跟进。尤其是在复制方面,还有很多需要检查的地方,而且在安全性的情况下,PostgreSQL 会随着每个新版本的发布而改进。