Range types (int4range, daterange) in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with range types like int4range or daterange in PostgreSQL, it's important to understand how the time to process queries grows as the data size increases.
We want to know how the number of operations changes when we search or filter using these range types.
Analyze the time complexity of this query using a range type filter.
SELECT *
FROM events
WHERE event_date && daterange('2024-01-01', '2024-12-31', '[]');
This query selects all rows where the event_date range overlaps with the year 2024.
Look for repeated checks or scans in the query.
- Primary operation: Checking each row's event_date range for overlap.
- How many times: Once per row in the events table.
As the number of rows grows, the database must check more ranges for overlap.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 overlap checks |
| 100 | 100 overlap checks |
| 1000 | 1000 overlap checks |
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 linearly with the number of rows in the table.
[X] Wrong: "Using range types automatically makes queries run in constant time."
[OK] Correct: The database still needs to check each row unless an index is used, so the time grows with the number of rows.
Understanding how range queries scale helps you explain query performance clearly and shows you know how databases handle special data types.
What if we added a GiST index on the event_date column? How would the time complexity change?
Practice
int4range data type store?Solution
Step 1: Understand the name
The prefixint4rangeint4means 4-byte integer in PostgreSQL.Step 2: Identify the stored data type
int4rangestores a range (interval) of 4-byte integers.Final Answer:
A range of 4-byte integers -> Option BQuick Check:
int4range= integer range [OK]
- Confusing int4range with floating point ranges
- Thinking it stores dates instead of integers
- Assuming it stores text ranges
period of type daterange?Solution
Step 1: Recall correct type declaration
In PostgreSQL, range types likedaterangeare used directly as column types.Step 2: Check each option
CREATE TABLE events (period daterange); usesperiod daterangewhich is correct syntax. CREATE TABLE events (period range(daterange)); wrongly usesrange(daterange). CREATE TABLE events (period date range); splits the type incorrectly. CREATE TABLE events (period daterange[]); declares an array, not a single range.Final Answer:
CREATE TABLE events (period daterange); -> Option CQuick Check:
Use type name directly for range columns [OK]
- Adding extra parentheses around range type
- Splitting type name into two words
- Using array syntax when not needed
bookings with a daterange column stay, what does this query return?SELECT * FROM bookings WHERE stay && daterange('2024-06-01', '2024-06-10');Solution
Step 1: Understand the operator
The&&for ranges&&operator checks if two ranges overlap at all.Step 2: Analyze the query condition
The query selects rows where thestayrange overlaps with the range from June 1 to June 10, 2024.Final Answer:
Rows where stay overlaps with June 1 to June 10, 2024 -> Option DQuick Check:
Range overlap operator && means any overlap [OK]
- Thinking && means fully inside
- Confusing overlap with exact start or end match
- Assuming it filters only before or after dates
SELECT * FROM bookings WHERE stay <@ daterange('2024-06-01', '2024-06-30');But it returns no rows even though some bookings are inside June. What is the likely problem?
Solution
Step 1: Understand
<@operator meaning<@means the left range is fully contained inside the right range.Step 2: Check daterange bounds behavior
By default,daterangeupper bound is exclusive, so '2024-06-30' is not included in the range. This excludes bookings on June 30.Final Answer:
The daterange upper bound is exclusive by default, so '2024-06-30' is not included -> Option AQuick Check:
Upper bound exclusive means end date not included [OK]
- Confusing <@ with overlap operator
- Assuming inclusive upper bound by default
- Ignoring data type mismatch
intervals with column int_range (type int4range) that do NOT overlap with [10, 20). Which query correctly finds these non-overlapping ranges?Solution
Step 1: Understand the overlap operator
&&&&returns true if ranges overlap.Step 2: Find non-overlapping ranges
To find ranges that do NOT overlap, negate the overlap condition withNOT (int_range && int4range(10, 20)).Final Answer:
SELECT * FROM intervals WHERE NOT (int_range && int4range(10, 20)); -> Option AQuick Check:
Negate overlap to find non-overlapping ranges [OK]
- Using && without NOT to find non-overlapping
- Confusing <@ and @> operators
- Using containment instead of overlap
