Table-level permissions in PostgreSQL - Time & Space Complexity
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?"