虽然PostgreSQL 15.1 是在几周前发布的,但PostgreSQL 16 已经在积极开发中。两天前提交了两个有趣的功能,它们在vacuum和analyze方面为您提供了更大的灵活性。在我们深入研究之前,让我们先看看它目前在 PostgreSQL 15 及之前的版本中是如何工作的。
首先,我们需要用户,其中一个用户有一个简单的表:
postgres=# select version();
version
----------------------------------------------------------------------------------------------------------------------
PostgreSQL 15.0 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
(1 row)
postgres=# create user u1 with login password 'u1';
CREATE ROLE
postgres=# create user u2 with login password 'u2';
CREATE ROLE
postgres=# grant all on schema public to u1;
GRANT
postgres=# \c postgres u1
You are now connected to database "postgres" as user "u1".
postgres=> create table t1 ( a int );
CREATE TABLE
postgres=> 当谈到 vacuum 并分析谁被允许对 u1 拥有的 t1 表执行这些操作时?当然楼主自己:
postgres=> select current_user;
current_user
--------------
u1
(1 row)
postgres=> analyze t1;
ANALYZE
postgres=> vacuum t1;
VACUUM
postgres=> 唯一允许这样做的其他用户是超级用户,但不是 u2 或任何其他非超级用户的用户:
postgres=> \c postgres u2
You are now connected to database "postgres" as user "u2".
postgres=> analyze t1;
WARNING: skipping "t1" --- only table or database owner can analyze it
ANALYZE
postgres=> vacuum t1;
WARNING: skipping "t1" --- only table or database owner can vacuum it
VACUUM
postgres=> \c postgres postgres
You are now connected to database "postgres" as user "postgres".
postgres=# analyze t1;
ANALYZE
postgres=# vacuum t1;
VACUUM这就是您当前拥有的所有选项。PostgreSQL 16 将为您提供更大的灵活性,因此让我们在 PostgreSQL 的当前开发版本中进行相同的设置:
postgres=# select version();
version
------------------------------------------------------------------------------------------------------
PostgreSQL 16devel on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
(1 row)
postgres=# create user u1 with login password 'u1';
CREATE ROLE
postgres=# create user u2 with login password 'u2';
CREATE ROLE
postgres=# grant all on schema public to u1;
GRANT
postgres=# \c postgres u1
You are now connected to database "postgres" as user "u1".
postgres=> create table t1 ( a int );
CREATE TABLE
postgres=> 默认情况下,该行为与 PostgreSQL 15 中的行为完全相同。只允许表的所有者和超级用户清理和分析 t1 表。但是从 PostgreSQL 16 开始,您可以做的是将这些操作授予另一个用户:
postgres=> grant analyze on t1 to u2;
GRANT
postgres=> grant vacuum on t1 to u2;
GRANT
postgres=> \c postgres u2
You are now connected to database "postgres" as user "u2".
postgres=> analyze t1;
ANALYZE
postgres=> vacuum t1;
VACUUM
postgres=> 当然,这不会授予查看数据的权利,也不会授予任何其他权限:
postgres=> select * from t1;
ERROR: permission denied for table t1
postgres=> 除此之外,PostgreSQL 16 还将附带两个额外的预定义角色:
postgres=> select * from pg_roles where rolname in ('pg_vacuum_all_tables','pg_analyze_all_tables');;
rolname | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcanlogin | rolreplication | rolconnlimit | rolpassword | rolvaliduntil | rolbypassrls | rolconfig | oid
-----------------------+----------+------------+---------------+-------------+-------------+----------------+--------------+-------------+---------------+--------------+-----------+------
pg_vacuum_all_tables | f | t | f | f | f | f | -1 | ******** | | f | | 4549
pg_analyze_all_tables | f | t | f | f | f | f | -1 | ******** | | f | | 4550
(2 rows)这些角色在授予某人时,也会授予 vacuum 和 analyze 操作的权限。
原文标题:PostgreSQL 16: More flexibility for vacuum and analyze
原文作者:Daniel Westermann
原文链接:https://www.dbi-services.com/blog/postgresql-16-more-flexibility-for-vacuum-and-analyze/
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




