如何处理具有排除约束的 table 上的更新?
how to handle updates on a table with exclusion constraint?
我有一个 table 跟踪具有如下排除限制的酒店预订。
目前,我允许房客更新他们的预订。因此,如果 guest_id 1 将他的预订从 2010-01-03、2010-01-03 的 3 天预订更改为一天,如果 i 运行 the本次更新声明:
update reservation set from_ts = '2021-01-03 00:00:00', to_ts='2021-01-10 23:59:00', during = '[2021-01-03 00:00:00, 2021-01-10 23:59:00]' where id = 1
那么您如何允许此更新?是否必须保持预订 ID 不变,并删除其他的?
** 注意:我实际上每行存储一天,因为我还有其他属性可以每天跟踪 **
Table: reservation
id | room | from_ts | to_ts | guest_id
----+------+---------------------+---------------------+------------
1 | 101 | 2010-01-01 00:00:00 | 2010-01-01 23:59:00 | 1
2 | 101 | 2010-01-02 00:00:00 | 2010-01-02 23:59:00 | 1
3 | 101 | 2010-01-03 00:00:00 | 2010-01-03 23:59:00 | 1
CREATE TABLE reservation (
id int,
guest_id int,
room int,
from_ts timestamp without time zone,
to_ts timestamp without time zone,
during tsrange,
EXCLUDE USING GIST (room WITH =, during WITH &&)
);
-- bootstrap to test the problem
INSERT INTO reservation ( id, guest_id, room, from_ts, to_ts, during ) VALUES ( 1, 1, 101, '2021-01-01 00:00:00', '2021-01-01 23:59:00', '[2021-01-01 00:00:00, 2021-01-01 23:59:00]');
INSERT INTO reservation ( id, guest_id, room, from_ts, to_ts, during ) VALUES ( 2, 1, 101, '2021-01-02 00:00:00', '2021-01-02 23:59:00', '[2021-01-02 00:00:00, 2021-01-02 23:59:00]' );
INSERT INTO reservation ( id, guest_id, room, from_ts, to_ts, during ) VALUES ( 3, 1, 101, '2021-01-03 00:00:00', '2021-01-03 23:59:00', '[2021-01-03 00:00:00, 2021-01-03 23:59:00]' );
-- update statement will fail after you run the insert statements
update reservation set from_ts = '2021-01-03 00:00:00', to_ts='2021-01-10 23:59:00', during = '[2021-01-03 00:00:00, 2021-01-10 23:59:00]' where id = 1
您可以使用不同的排除约束而不是您创建的排除约束来解决该问题:
ALTER TABLE reservation ADD EXCLUDE USING gist (
room WITH =,
guest_id WITH <>,
tsrange(from_ts, to_ts, '[]') WITH &&
);
这将排除房间相等的两个条目和时间戳范围重叠和预订是针对不同客人的。
请注意,我使用的是表达式而不是 during
。在数据库设计中避免冗余是个好主意。您当然也可以保留 duration
并取消 from_ts
和 to_ts
.
还要注意 to_ts
有多尴尬,以 23:59:00 结尾。您可以改为选择在上端开放的区间:
SELECT tsrange('2021-01-02 00:00:00', '2021-01-03 00:00:00');
tsrange
═══════════════════════════════════════════════
["2021-01-02 00:00:00","2021-01-03 00:00:00")
(1 row)
这不会与以 2021-01-03 00:00:00 开头的范围重叠。
我有一个 table 跟踪具有如下排除限制的酒店预订。
目前,我允许房客更新他们的预订。因此,如果 guest_id 1 将他的预订从 2010-01-03、2010-01-03 的 3 天预订更改为一天,如果 i 运行 the本次更新声明:
update reservation set from_ts = '2021-01-03 00:00:00', to_ts='2021-01-10 23:59:00', during = '[2021-01-03 00:00:00, 2021-01-10 23:59:00]' where id = 1
那么您如何允许此更新?是否必须保持预订 ID 不变,并删除其他的?
** 注意:我实际上每行存储一天,因为我还有其他属性可以每天跟踪 **
Table: reservation
id | room | from_ts | to_ts | guest_id
----+------+---------------------+---------------------+------------
1 | 101 | 2010-01-01 00:00:00 | 2010-01-01 23:59:00 | 1
2 | 101 | 2010-01-02 00:00:00 | 2010-01-02 23:59:00 | 1
3 | 101 | 2010-01-03 00:00:00 | 2010-01-03 23:59:00 | 1
CREATE TABLE reservation (
id int,
guest_id int,
room int,
from_ts timestamp without time zone,
to_ts timestamp without time zone,
during tsrange,
EXCLUDE USING GIST (room WITH =, during WITH &&)
);
-- bootstrap to test the problem
INSERT INTO reservation ( id, guest_id, room, from_ts, to_ts, during ) VALUES ( 1, 1, 101, '2021-01-01 00:00:00', '2021-01-01 23:59:00', '[2021-01-01 00:00:00, 2021-01-01 23:59:00]');
INSERT INTO reservation ( id, guest_id, room, from_ts, to_ts, during ) VALUES ( 2, 1, 101, '2021-01-02 00:00:00', '2021-01-02 23:59:00', '[2021-01-02 00:00:00, 2021-01-02 23:59:00]' );
INSERT INTO reservation ( id, guest_id, room, from_ts, to_ts, during ) VALUES ( 3, 1, 101, '2021-01-03 00:00:00', '2021-01-03 23:59:00', '[2021-01-03 00:00:00, 2021-01-03 23:59:00]' );
-- update statement will fail after you run the insert statements
update reservation set from_ts = '2021-01-03 00:00:00', to_ts='2021-01-10 23:59:00', during = '[2021-01-03 00:00:00, 2021-01-10 23:59:00]' where id = 1
您可以使用不同的排除约束而不是您创建的排除约束来解决该问题:
ALTER TABLE reservation ADD EXCLUDE USING gist (
room WITH =,
guest_id WITH <>,
tsrange(from_ts, to_ts, '[]') WITH &&
);
这将排除房间相等的两个条目和时间戳范围重叠和预订是针对不同客人的。
请注意,我使用的是表达式而不是 during
。在数据库设计中避免冗余是个好主意。您当然也可以保留 duration
并取消 from_ts
和 to_ts
.
还要注意 to_ts
有多尴尬,以 23:59:00 结尾。您可以改为选择在上端开放的区间:
SELECT tsrange('2021-01-02 00:00:00', '2021-01-03 00:00:00');
tsrange
═══════════════════════════════════════════════
["2021-01-02 00:00:00","2021-01-03 00:00:00")
(1 row)
这不会与以 2021-01-03 00:00:00 开头的范围重叠。