-
2024-12-10 PostgreSQL
information_schema.table_privilegesとinformation_schema.role_table_grantsの違い
information_schema の table_privileges ビューと role_table_grants ビュー
ロールに付与されている権限、information_schema の table_privileges ビューまたは role_table_grants ビューから確認できます。
この table_privileges と role_table_grants の違いについて、PostgreSQL 文書に説明がありましたので引用します。
【 PostgreSQL 16.4文書 - 37.37. role_table_grants - 】
https://www.postgresql.jp/document/16/html/infoschema-role-table-grants.html
このビューとtable_privilegesとの間の実質的な違いは、このビューでは現在のユーザがPUBLICに与えられた権限によりアクセスできるようになったテーブルを省略していることだけです。
検証 SQL を実行して挙動を確認
実際に検証用 SQL を実行し、どのような違いがあるのかを確認します。
検証に利用した環境は下記となります。
- RockeyLinux 9.4
- PostgreSQL 16.4
-- table_privileges を確認
postgres=# SELECT grantor, grantee, COUNT(1) FROM information_schema.table_privileges GROUP BY grantor, grantee;
grantor | grantee | count
-----------+-------------------+-------
postgres | PUBLIC | 190
postgres | pg_read_all_stats | 2
postgres | postgres | 1876
test_role | test_role | 7
(4 rows)
-- role_table_grants を確認
postgres=# SELECT grantor, grantee, COUNT(1) FROM information_schema.role_table_grants GROUP BY grantor, grantee;
grantor | grantee | count
-----------+-------------------+-------
postgres | PUBLIC | 190
postgres | pg_read_all_stats | 2
postgres | postgres | 1876
test_role | test_role | 7
(4 rows)
上記の通り、table_privileges と role_table_grants の内容は同じとなっていました。これはスーパユーザでビューを参照してしまっているためです。
今度は一般ユーザ(非スーパーユーザ)でログインし、同じ SQL を実行します。
-- 一般ユーザで table_privileges を確認
postgres=> SELECT grantor, grantee, COUNT(1) FROM information_schema.table_privileges GROUP BY grantor, grantee;
grantor | grantee | count
----------+---------+-------
postgres | PUBLIC | 190
(1 row)
postgres=> SELECT grantor, grantee, COUNT(1) FROM information_schema.role_table_grants GROUP BY grantor, grantee;
grantor | grantee | count
---------+---------+-------
(0 rows)
今度は table_privileges と role_table_grants の結果に違いがありました。具体的には grantor が postgres で grantee が PUBLIC のレコードが出力されているかどうか、という違いになります。table_privileges では該当のレコードが出力され、role_table_grants には出力されませんでした。
現在のユーザが PUBLIC ロールによって与えられた権限に該当するのが、「grantor が postgres で grantee が PUBLIC」のレコードとなりますので、PostgreSQL 文書の説明にもある通り role_table_grants では該当レコードが出力されないということを確認できました。
個人的には「grantor が PUBLIC で grantee が現在のロール」のレコードが存在するのかと想定していたのですが、そうではありませんでした。PUBLIC ロールに対して権限付与されたもの = 現在のロールに対して権限付与されたもの、という扱いのようです(grantee の PUBLIC は、現在のロールに置き換えてみると、理解できました)。
まとめ
簡単ではありますが、information_schema.table_privileges ビューとinformation_schema.role_table_grants ビューの違いについて確認した結果を記載いたしました。
権限付与の状況を確認する際にどちらのビューを利用しても、大きく違いはないと思いますので、運用しやすい方のビューを参照するのが推奨となります。