For my database design, I have to options for storing timestamp ranges and preventing overlaps using a gist exclude constraint:
Option 1
create table example (
id bigserial primary key,
kind_id bigint not null,
start timestamp not null,
stop timestamp,
constraint test_start_stop exclude using gist (kind_id with =, tsrange(start, stop) with &&)
);
Option 2
create table example (
id bigserial primary key,
kind_id bigint not null,
start_stop tsrange not null,
constraint test_start_stop exclude using gist (kind_id with =, start_stop with &&)
);
When looking at the documentation, there does not seem to be an implementation for Postgres's tsrange or tstzrange datatypes for the FromSql and ToSql traits in postgres-rs.
These types are supported in sqlx-rs but I'm reluctant to switch because of the work that goes into refactoring.
Are there any drawbacks that I should be aware of when using the first option ?
Addendum: in this thread Erwin Brandstetter cites a line from the manual "Note that exclusion constraints are not supported as arbiters with ON CONFLICT DO UPDATE.", and he mentions that "the ON CONFLICT clause covers conflicts with EXCLUSION constraints, but only for DO NOTHING".
Ranged types:
empty in which case boundary data is lost, zero length ranges can not be storednull) at both ends, which can require more work in constraints if you want to disallow nulllower_inf() does not test for 'infinity', it tests for null. And lower_inc('[,]'::daterange) returns false<@ will return true even with boundaries will null values-infinity and infinity, just like non-ranged valuesnull values as boundaries, so if you want to disallow null (next to not null on the column) you need extra constraintsRead the documentation entirely beforehand, and then make your choice. It is a matter of taste.