GRANT OPTION Lets Grantees Re-Grant
WITH GRANT OPTION lets the grantee re-grant to a third role, and REVOKE with CASCADE tears the whole chain down from the source.
Code
-- WITH GRANT OPTION lets grantees re-grant downstream:转授链从此可能变长
CREATE ROLE report_owner;
CREATE ROLE report_reader;
GRANT SELECT ON sales TO report_owner WITH GRANT OPTION;
-- 以 report_owner 身份把权限转授下去
SET ROLE report_owner;
GRANT SELECT ON sales TO report_reader;
RESET ROLE;
-- information_schema 里能看到下游授权:grantor 是 report_owner 而不是表主
SELECT grantor, grantee, privilege_type
FROM information_schema.role_table_grants
WHERE table_name = 'sales' AND grantee = 'report_reader';
-- 撤销源头授权时 CASCADE 连带撤销 re-grant 出去的下游权限
REVOKE SELECT ON sales FROM report_owner CASCADE;
SELECT count(*) AS remaining_grants
FROM information_schema.role_table_grants
WHERE table_name = 'sales' AND grantee = 'report_reader';What happens step by step
The file runs SELECT / CREATE statements, touching information_schema.role_table_grants and report_owner.
Breaking the statements into their working parts: a WHERE clause narrowing which rows one statement affects; COUNT aggregation.
Applying it
Run the statement against the information_schema.role_table_grants table.
Statements that matter
SELECT grantor, grantee, privilege_type
FROM information_schema.role_table_grants
WHERE table_name = 'sales' AND grantee = 'report_reader';
SELECT count(*) AS remaining_grants
FROM information_schema.role_table_grants
WHERE table_name = 'sales' AND grantee = 'report_reader';
GRANT SELECT ON sales TO report_owner WITH GRANT OPTION;
Interactive Tools for This Topic
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.
FAQ
- What are the relations in play here?
- The references are `information_schema.role_table_grants`, `report_owner`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.