Tobias Petry: Time Ranges without Overlapping
Storing rows with a start and end date is no rarity. Many applications (hotel bookings, conference room reservations, working time tracking) must store time ranges in a database. However, due to race conditions, ensuring those ranges to not overlap is not easy. But you can ask the database to guarantee this constraint.
许多应用程序需要在数据库中存储不重叠的时间范围,如酒店预订、会议室预订、工作时间跟踪等。为了避免重叠,可以在数据库中设置约束条件。PostgreSQL提供了范围类型和排除约束来实现这一功能。通过在时间范围上添加排除约束,可以确保同一房间的时间范围不重叠。这样可以避免由于竞争条件而导致的错误。虽然仍需进行手动检查,但数据库会防止任何由于竞争条件而导致的错误。