PostgreSQL中基于GIST排他约束实现时间戳范围防重叠:两种方案的差异及方案1的潜在弊端咨询
PostgreSQL中基于GIST排他约束实现时间戳范围防重叠:两种方案的差异及方案1的潜在弊端咨询
我目前在设计PostgreSQL数据库时,需要实现「同一kind_id下的时间戳范围不能重叠」的约束,现在有两种可行的方案,想请教大家用第一种方案的话有没有什么需要注意的弊端。
方案1:拆分独立的start/stop字段
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 &&) );
方案2:直接使用tsrange类型字段
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 &&) );
之所以纠结是因为,我查了postgres-rs的文档,发现它并没有为PostgreSQL的tsrange或tstzrange类型实现FromSql和ToSql这两个 trait,虽然sqlx-rs支持这些类型,但重构现有代码的成本太高,所以倾向于用方案1,但不确定有没有隐藏的问题。
另外补充一点我已经了解到的信息:在PostgreSQL手册里提到「排他约束不能作为ON CONFLICT DO UPDATE的仲裁约束」,不过ON CONFLICT DO NOTHING是可以处理排他约束冲突的,这点我已经清楚了。
备注:内容来源于stack exchange,提问作者Code4R7
相关产品推荐
相关产品推荐

