top | item 45208686

(no title)

akorotkov | 5 months ago

Exclusion constraints should work well for this case. On any transaction isolation level.

https://www.postgresql.org/docs/current/ddl-constraints.html...

discuss

order

tux3|5 months ago

That's indeed the first thing you would want to try. In this case these time ranges can repeat according to a formula, like a cron job for example.

An exclusion constraint needs concrete values to compare, but here we can't pre-compute and index every future value (there are infinitely many)

We solve a diophantine equation for this check (if there is a solution to Ax - By = 0, then formulas A and B can conflict at some point)

btown|5 months ago

This seems like a fascinating problem domain! I'm incredibly curious what product you're working on - if you're taking this much attention to detail on recurring events, it seems like it's a step beyond most scheduling systems I've seen.

Heck, https://www.sciencedirect.com/science/article/pii/S147466701... and https://www.amazon.com/Declarative-Models-Concurrent-Cyclic-... seem to indicate this is still an area of active research. And, to your point, almost certainly too complex to try to encode into a Postgres index - that would be a paper-worthy project unto itself!

(I've had to implement the naive approach here, which nested-loops over dates and rules and finds ones that might conflict. Definitely not meant to scale, and would be a nightmare if not at a date-level granularity!)

No pressure to share, but would love to learn more!