Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm not entirely sure what the valid_range is doing. Besides updating it, do you use this index for anything else? I agree the performance doesn't seem like it would be great.

I do something like 4000 inserts a second, but maybe only a few queries a minute, so I use an "invalidated_by" column which (eventually) points to the newer record, and I update it on query instead of insert (when the multiple nulls are discovered and relevant)





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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: