GRANT and REVOKE permissions in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we give or take away permissions in a database, it takes some time to process. We want to understand how this time changes when we have more users or more permissions.
How does the time to grant or revoke permissions grow as the number of users or permissions increases?
Analyze the time complexity of the following code snippet.
GRANT SELECT, INSERT ON employees TO user1;
REVOKE INSERT ON employees FROM user1;
GRANT UPDATE ON employees TO user2;
REVOKE ALL ON employees FROM user3;
-- Grant or revoke permissions for multiple users
GRANT SELECT ON employees TO user1, user2, user3;
This code gives or removes permissions on the "employees" table for different users.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying permission changes for each user and each permission.
- How many times: Once per user-permission pair in the command.
When you add more users or more permissions in one command, the work grows because the database must update each user's permissions separately.
| Input Size (n users x m permissions) | Approx. Operations |
|---|---|
| 10 users x 2 permissions | 20 permission changes |
| 100 users x 3 permissions | 300 permission changes |
| 1000 users x 5 permissions | 5000 permission changes |
Pattern observation: The total work grows roughly by multiplying the number of users by the number of permissions.
Time Complexity: O(n * m)
This means the time to grant or revoke permissions grows in proportion to the number of users times the number of permissions involved.
[X] Wrong: "Granting permissions to many users at once takes the same time as granting to one user."
[OK] Correct: The database must update each user's permissions separately, so more users mean more work and more time.
Understanding how permission changes scale helps you design systems that manage access efficiently and avoid slowdowns when many users are involved.
"What if we grant permissions to a role instead of individual users? How would the time complexity change?"
Practice
GRANT command do in PostgreSQL?Solution
Step 1: Understand the purpose of GRANT
The GRANT command is used to give permissions like SELECT, INSERT, or UPDATE to users or roles.Step 2: Compare with other options
Deleting users, creating databases, or backing up are done by other commands, not GRANT.Final Answer:
It gives specific permissions to a user or role. -> Option AQuick Check:
GRANT = give permissions [OK]
- Confusing GRANT with user creation
- Thinking GRANT deletes data
- Mixing GRANT with backup commands
employees to user john?Solution
Step 1: Recall correct GRANT syntax
The correct syntax is: GRANT permission ON object TO user;Step 2: Match syntax with options
GRANT SELECT ON employees TO john; matches the correct order: GRANT SELECT ON employees TO john;Final Answer:
GRANT SELECT ON employees TO john; -> Option DQuick Check:
GRANT + permission + ON + object + TO + user [OK]
- Mixing order of keywords
- Placing user before permission
- Omitting ON keyword
GRANT SELECT ON customers TO alice;REVOKE SELECT ON customers FROM alice;What permissions does user
alice have on table customers after these commands?Solution
Step 1: Analyze the GRANT command
Alice is given SELECT permission on customers table.Step 2: Analyze the REVOKE command
The SELECT permission is then revoked from Alice, removing her ability to SELECT.Final Answer:
Alice cannot SELECT from customers. -> Option CQuick Check:
REVOKE removes permission given by GRANT [OK]
- Assuming REVOKE adds permissions
- Confusing SELECT with INSERT
- Ignoring the order of commands
REVOKE ALL PRIVILEGES employees FROM bob;Solution
Step 1: Check correct REVOKE syntax
The correct syntax is: REVOKE privileges ON object FROM user;Step 2: Compare with given command
The command uses: REVOKE ALL PRIVILEGES employees FROM bob; missing ON keyword and wrong order.Final Answer:
The order of keywords is incorrect. -> Option AQuick Check:
REVOKE + privileges + ON + object + FROM + user [OK]
- Omitting ON keyword
- Swapping user and object positions
- Using ALL PRIVILEGES incorrectly
carol to insert and update data on table orders, but not delete. Which commands correctly grant these permissions?Solution
Step 1: Identify required permissions
Carol needs INSERT and UPDATE permissions only, no DELETE.Step 2: Match commands with required permissions
GRANT INSERT, UPDATE ON orders TO carol; grants INSERT and UPDATE correctly. Options B and D grant DELETE, which is not wanted. GRANT ALL PRIVILEGES ON orders TO carol; grants all permissions, including DELETE.Final Answer:
GRANT INSERT, UPDATE ON orders TO carol; -> Option BQuick Check:
Grant only needed permissions, avoid ALL PRIVILEGES if not required [OK]
- Granting DELETE when not needed
- Using ALL PRIVILEGES carelessly
- Confusing SELECT with UPDATE
