Why database security matters in PostgreSQL - Performance Analysis
We want to understand how the effort to keep a database secure changes as the database grows.
How does the work needed to protect data grow when there is more data or more users?
Analyze the time complexity of checking user permissions before allowing data access.
-- Check if user has access to a table
SELECT has_table_privilege('username', 'tablename', 'SELECT');
-- If yes, allow query
SELECT * FROM tablename WHERE condition;
This code checks if a user can read a table before running a query.
Look for repeated checks or scans.
- Primary operation: Checking user privileges for each query.
- How many times: Once per query, but queries can be many as users interact.
As the number of users and queries grows, the number of privilege checks grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 queries | 10 privilege checks |
| 100 queries | 100 privilege checks |
| 1000 queries | 1000 privilege checks |
Pattern observation: The work grows directly with the number of queries.
Time Complexity: O(n)
This means the time to check security grows in a straight line with the number of queries.
[X] Wrong: "Security checks happen only once and do not affect performance."
[OK] Correct: Each query needs a check, so more queries mean more work for security.
Understanding how security checks scale helps you design systems that stay safe and fast as they grow.
"What if we cached user permissions? How would the time complexity change?"