Column-level permissions in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When checking column-level permissions in a database, we want to know how the time to verify access grows as the number of columns or users increases.
We ask: How does permission checking scale when more columns or users are involved?
Analyze the time complexity of this permission check query.
-- Check if user has access to a specific column
SELECT has_column_privilege('username', 'table_name', 'column_name', 'SELECT');
-- Or check permissions for multiple columns
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'table_name'
AND has_column_privilege('username', 'table_name', column_name, 'SELECT');
This code checks if a user can select specific columns in a table, either one column or multiple columns.
Look for repeated checks or loops.
- Primary operation: Checking permission for each column.
- How many times: Once per column when checking multiple columns.
As the number of columns grows, the permission check runs once per column.
| Input Size (n columns) | 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 columns.
Time Complexity: O(n)
This means the time to check permissions grows linearly with the number of columns checked.
[X] Wrong: "Checking permissions for multiple columns is done all at once, so time stays the same no matter how many columns there are."
[OK] Correct: Each column's permission is checked separately, so more columns mean more checks and more time.
Understanding how permission checks scale helps you design efficient security in databases and shows you can think about performance in real systems.
"What if we cached permission results for columns? How would that change the time complexity when checking permissions repeatedly?"
Practice
Solution
Step 1: Understand the concept of column-level permissions
Column-level permissions allow control over which columns a user can see or modify in a table.Step 2: Compare with other access types
Other options refer to broader or unrelated access controls, not specific columns.Final Answer:
Access to specific columns in a table -> Option DQuick Check:
Column-level permission = Access to specific columns [OK]
- Confusing column-level with table-level permissions
- Thinking it controls schema or server access
- Assuming it controls row-level access
email of table users to user alice?Solution
Step 1: Recall PostgreSQL syntax for column-level GRANT
The correct syntax is GRANT SELECT (email) ON users TO alice; (parentheses after privilege).Step 2: Match options with syntax
GRANT SELECT(email) ON users TO alice; matches the correct syntax exactly, others misplace keywords or parentheses.Final Answer:
GRANT SELECT(email) ON users TO alice; -> Option AQuick Check:
GRANT SELECT(column) ON table TO user [OK]
- Placing column name after TO user
- Putting column inside ON table()
- Misordering keywords in the statement
employees(id, name, salary), if user bob has SELECT permission only on id and name, what will be the result of SELECT * FROM employees; executed by bob?Solution
Step 1: Understand column-level permission effect on SELECT *
User can see columns they have permission for; restricted columns appear as NULL.Step 2: Apply to given columns
Bob has permission onidandname, sosalaryshows as NULL.Final Answer:
Only id and name columns with data, salary as NULL -> Option AQuick Check:
Restricted columns show NULL, not error [OK]
- Expecting a permission denied error
- Assuming all columns show data
- Thinking restricted columns are hidden completely
GRANT SELECT ON employees(name, salary) TO carol; but get a syntax error. What is the likely cause?Solution
Step 1: Check syntax for GRANT with multiple columns
Correct syntax is GRANT SELECT (name, salary) ON employees TO carol;Step 2: Identify error in command
Command incorrectly places column list after table name; column list must follow SELECT before ON.Final Answer:
Column list in parentheses must precede the table name -> Option CQuick Check:
GRANT SELECT (col1, col2) ON table TO user [OK]
- Placing column list after table name
- Forgetting parentheses around columns
- Assuming multiple columns need separate GRANTs
dave to update only the phone column in the contacts table but not others. Which sequence of commands correctly achieves this?Solution
Step 1: Remove any existing full UPDATE permission
First revoke any broad UPDATE permission to avoid conflicts.Step 2: Grant UPDATE permission only on the phone column
Then grant UPDATE on the specific column to limit access.Final Answer:
REVOKE UPDATE ON contacts FROM dave; GRANT UPDATE(phone) ON contacts TO dave; -> Option BQuick Check:
Revoke broad then grant column-level UPDATE [OK]
- Granting column-level without revoking broad permission
- Revoking column-level instead of broad permission
- Assuming single GRANT is enough if broad permission exists
