Table-level permissions in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to check table-level permissions changes as the number of tables grows.
How does the system handle permission checks when many tables exist?
Analyze the time complexity of this permission check query.
-- Check if user has SELECT permission on a table
SELECT has_table_privilege('username', 'schema.table_name', 'SELECT');
-- Or list all tables user can SELECT from
SELECT tablename
FROM pg_tables
WHERE has_table_privilege('username', schemaname || '.' || tablename, 'SELECT');
This code checks if a user has SELECT permission on one or many tables.
Look for repeated checks or loops.
- Primary operation: Checking permission for each table using
has_table_privilege. - How many times: Once per table when listing all tables.
As the number of tables grows, the system checks permissions for each table separately.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 permission checks |
| 100 | 100 permission checks |
| 1000 | 1000 permission checks |
Pattern observation: The number of permission checks grows directly with the number of tables.
Time Complexity: O(n)
This means the time to check permissions grows in a straight line as the number of tables increases.
[X] Wrong: "Checking permissions on many tables happens instantly regardless of how many tables exist."
[OK] Correct: Each table requires a separate permission check, so more tables mean more work and more time.
Understanding how permission checks scale helps you design systems that stay fast even as data grows.
"What if we cached permission results for tables? How would the time complexity change when checking permissions repeatedly?"
Practice
GRANT SELECT ON table_name TO user_name; command do in PostgreSQL?Solution
Step 1: Understand the GRANT command
The GRANT command is used to give specific permissions to users on database objects like tables.Step 2: Identify the permission type SELECT
SELECT permission allows reading data from the table but not modifying it.Final Answer:
Allows the user to read data from the specified table. -> Option DQuick Check:
GRANT SELECT = read permission [OK]
- Confusing SELECT with DELETE permission
- Thinking GRANT creates tables
- Mixing GRANT with REVOKE commands
employees from user john?Solution
Step 1: Recall REVOKE syntax
The correct syntax is REVOKE permission ON table FROM user;Step 2: Match syntax with options
REVOKE INSERT ON employees FROM john; matches the correct order: REVOKE INSERT ON employees FROM john;Final Answer:
REVOKE INSERT ON employees FROM john; -> Option CQuick Check:
REVOKE permission ON table FROM user [OK]
- Swapping ON and FROM keywords
- Using TO instead of FROM
- Incorrect order of clauses
GRANT SELECT ON orders TO alice; GRANT INSERT ON orders TO bob; REVOKE SELECT ON orders FROM alice;
Which of the following is true about user permissions on the
orders table?Solution
Step 1: Analyze granted permissions
Alice was granted SELECT (read) permission, Bob was granted INSERT permission.Step 2: Analyze revoked permissions
Alice's SELECT permission was revoked, so she no longer can read data.Final Answer:
Alice cannot read data; Bob can insert data. -> Option AQuick Check:
Revoked SELECT removes read access [OK]
- Assuming revoked permission still applies
- Confusing INSERT with SELECT
- Thinking REVOKE affects other users
GRANT UPDATE ON customers TO ;
What is the error in this command?
Solution
Step 1: Check syntax completeness
The command ends with TO but does not specify a user or role name.Step 2: Validate permission and table name
UPDATE is a valid permission and customers is the table name, so those parts are correct.Final Answer:
Missing user name after TO keyword. -> Option AQuick Check:
GRANT requires user after TO [OK]
- Leaving user name blank after TO
- Confusing permission names
- Omitting table name
carol to read and insert data into the products table but prevent her from deleting or updating any data. Which commands should you use?Solution
Step 1: Grant only SELECT and INSERT permissions
To allow reading and inserting, grant SELECT and INSERT on products to carol.Step 2: Revoke DELETE and UPDATE permissions
To prevent deleting or updating, explicitly revoke DELETE and UPDATE permissions if previously granted.Final Answer:
GRANT SELECT, INSERT ON products TO carol; REVOKE DELETE, UPDATE ON products FROM carol; -> Option BQuick Check:
Grant needed permissions, revoke unwanted ones [OK]
- Granting ALL permissions instead of specific ones
- Not revoking unwanted permissions
- Granting DELETE or UPDATE by mistake
