range type. For example:
<programlisting>
-ALTER TABLE reservation ADD EXCLUDE USING gist (during WITH &&);
+CREATE TABLE reservation (
+ during tsrange,
+ EXCLUDE USING gist (during WITH &&)
+);
</programlisting>
That constraint will prevent any overlapping values from existing
<programlisting>
INSERT INTO reservation VALUES
- (1108, '[2010-01-01 11:30, 2010-01-01 13:00)');
+ ('[2010-01-01 11:30, 2010-01-01 15:00)');
INSERT 0 1
INSERT INTO reservation VALUES
- (1108, '[2010-01-01 14:45, 2010-01-01 15:45)');
+ ('[2010-01-01 14:45, 2010-01-01 15:45)');
ERROR: conflicting key value violates exclusion constraint "reservation_during_excl"
-DETAIL: Key (during)=([ 2010-01-01 14:45:00, 2010-01-01 15:45:00 )) conflicts
-with existing key (during)=([ 2010-01-01 14:30:00, 2010-01-01 15:30:00 )).
+DETAIL: Key (during)=(["2010-01-01 14:45:00","2010-01-01 15:45:00")) conflicts
+with existing key (during)=(["2010-01-01 11:30:00","2010-01-01 15:00:00")).
</programlisting>
</para>
are equal:
<programlisting>
+CREATE EXTENSION btree_gist;
CREATE TABLE room_reservation (
room text,
during tsrange,
INSERT INTO room_reservation VALUES
('123A', '[2010-01-01 14:30, 2010-01-01 15:30)');
ERROR: conflicting key value violates exclusion constraint "room_reservation_room_during_excl"
-DETAIL: Key (room, during)=(123A, [ 2010-01-01 14:30:00, 2010-01-01 15:30:00 )) conflicts with
-existing key (room, during)=(123A, [ 2010-01-01 14:00:00, 2010-01-01 15:00:00 )).
+DETAIL: Key (room, during)=(123A, ["2010-01-01 14:30:00","2010-01-01 15:30:00")) conflicts
+with existing key (room, during)=(123A, ["2010-01-01 14:00:00","2010-01-01 15:00:00")).
INSERT INTO room_reservation VALUES
('123B', '[2010-01-01 14:30, 2010-01-01 15:30)');