Comment by whalesalad
4 months ago
The valid_range with a gist index is quite fast.
SELECT COUNT(DISTINCT domain)
FROM time_travel
WHERE (CURRENT_DATE - INTERVAL '90 days')::timestamptz <@ valid_range
AND owned;
This is asking, "how many domains did we own 90 days ago"
Instead of finding records where the start is less than, end is greater than, you can just say find me rows that will cover this point in time. The GiST index on valid_range does the heavy lifting.
Lots of handy range-specific query tools available: https://www.postgresql.org/docs/17/functions-range.html
Yep this is it. Since you mostly view the newest stuff you could even have a partial index only over the records that are considered current.
And it’s PG’s range queries that make this shine, as you showed. If you had to simulate the range with two columns all the queries would be a pain.