psql元命令\du和\dg都可以列出角色或用户,请问这两个命令是否等价?
A.等价
B.不等价
C.不好说
D.不确定
参考答案:A
解析:
要确认某个封装命令是否等价,只需要看后台调用的SQL即可。
一、开启SQL跟踪功能
1.1 修改参数,开启sql跟踪
修改前
#log_statement = 'none' # none, ddl, mod, all
修改后
log_statement = 'all' # none, ddl, mod, all
1.2 重载使参数生效
[postgres@ora19c02 data]$ pg_ctl reload -D $PGDATA
二、测试功能
2.1 du命令
[postgres@ora19c02 data]$ psql -d testdb
psql (15.4)
Type "help" for help.
testdb=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
test | | {}
查看日志输出
2023-10-23 09:42:57.547 CST [56704] LOG: statement: SELECT r.rolname, r.rolsuper, r.rolinherit,
r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
r.rolconnlimit, r.rolvaliduntil,
ARRAY(SELECT b.rolname
FROM pg_catalog.pg_auth_members m
JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
WHERE m.member = r.oid) as memberof
, r.rolreplication
, r.rolbypassrls
FROM pg_catalog.pg_roles r
WHERE r.rolname !~ '^pg_'
ORDER BY 1; 作者:云贝教育 https://www.bilibili.com/read/cv27545596/ 出处:bilibili
2.2 du命令
testdb=# \dg
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
test | | {}
查看日志输出
2023-10-23 09:43:30.543 CST [56704] LOG: statement: SELECT r.rolname, r.rolsuper, r.rolinherit,
r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
r.rolconnlimit, r.rolvaliduntil,
ARRAY(SELECT b.rolname
FROM pg_catalog.pg_auth_members m
JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
WHERE m.member = r.oid) as memberof
, r.rolreplication
, r.rolbypassrls
FROM pg_catalog.pg_roles r
WHERE r.rolname !~ '^pg_'
ORDER BY 1;
三、结论
由上述实验可以得出结论,两者调用的是同一个SQL,是等价的。
标签:PostgreSQL,postgres,roles,云贝,catalog,rolname,PGCA,du,pg From: https://blog.51cto.com/u_16241820/8504425