Boolean type behavior in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to work with Boolean values changes as we use more data.
Specifically, how does checking or using Boolean values in queries grow when the data grows?
Analyze the time complexity of the following code snippet.
SELECT id, is_active
FROM users
WHERE is_active = TRUE;
This query selects user IDs where the Boolean column is_active is true.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in the
userstable to check theis_activevalue. - How many times: Once for every row in the table.
As the number of users grows, the database checks more rows one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks of is_active |
| 100 | 100 checks of is_active |
| 1000 | 1000 checks of is_active |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the table gets bigger.
[X] Wrong: "Checking a Boolean column is instant no matter how big the table is."
[OK] Correct: Even though Booleans are simple, the database still looks at each row, so more rows mean more work.
Understanding how simple Boolean checks scale helps you explain query performance clearly and confidently.
"What if the is_active column had an index? How would the time complexity change?"