The idea · Lab 3.3.5ARRAY columns store a genuine list of values per row, queryable with operators purpose-built for set-like questions: @> asks whether one array contains another as a subset, && asks whether two arrays share at least one element. Both can be backed by a GIN index, the identical indexing mechanism already proven for full-text search and JSONB earlier in this certificate, now indexing array membership instead.
Range types solve a different, equally common problem: representing an interval — a booking's start and end, a validity period — as one genuine value rather than two separate columns compared awkwardly against each other. tstzrange stores exactly that. The real payoff arrives with EXCLUDE USING GiST: a constraint that can express "no two rows may exist where this column is equal AND that range column overlaps," enforced by the database itself on every insert and update, with no application-level check-then-insert race condition possible — two nearly-simultaneous booking attempts cannot both slip through, because the database checks and rejects the conflict as the same atomic operation as the insert.
Array Operators: Contains and Overlap
SELECT id FROM sql_articles WHERE tags @> ARRAY['postgres']; -- id 1, 3
SELECT id FROM sql_articles WHERE tags && ARRAY['mysql','performance']; -- id 2, 3
@> requires every element on the right to be present in the row's array; && only requires at least one shared element between the two arrays — genuinely different questions, both real set operations.
GIN, Genuinely Used for Array Membership at Scale
CREATE INDEX ON sql_articles_big USING GIN(tags);
EXPLAIN SELECT id FROM sql_articles_big WHERE tags @> ARRAY['postgres'];
Bitmap Heap Scan on sql_articles_big (cost=90.42..746.15 rows=12458 width=4)
Recheck Cond: (tags @> '{postgres}'::text[])
-> Bitmap Index Scan on sql_articles_big_tags_idx
The identical GIN mechanism proven for full-text search and JSONB earlier in this certificate, now backing array containment.
A Range Type and an Exclusion Constraint
CREATE TABLE room_bookings (id serial PRIMARY KEY, room_id int, booked_during tstzrange);
CREATE EXTENSION IF NOT EXISTS btree_gist;
ALTER TABLE room_bookings ADD CONSTRAINT no_overlap
EXCLUDE USING GiST (room_id WITH =, booked_during WITH &&);
booked_during is one column, one real interval value — not a separate start and end column compared manually. btree_gist is required specifically because room_id is a plain integer, and GiST needs an operator class for the equality check on it, which the base install does not provide for simple types by default.
The Constraint, Genuinely Rejecting a Real Conflict
INSERT INTO room_bookings (room_id, booked_during) VALUES (1, tstzrange('2025-06-01 09:00', '2025-06-01 10:00'));
-- INSERT 0 1
INSERT INTO room_bookings (room_id, booked_during) VALUES (1, tstzrange('2025-06-01 09:30', '2025-06-01 10:30'));
-- ERROR: conflicting key value violates exclusion constraint "no_overlap"
-- DETAIL: Key (room_id, booked_during)=(1, [...)) conflicts with existing key (room_id, booked_during)=(1, [...)).
INSERT INTO room_bookings (room_id, booked_during) VALUES (2, tstzrange('2025-06-01 09:30', '2025-06-01 10:30'));
-- INSERT 0 1
The overlapping 9:30–10:30 booking for room 1 is genuinely rejected — a real constraint violation, not an application-level check that could theoretically be raced. The identical overlapping time for room 2 succeeds without issue, since the constraint only applies within the same room_id.
Key concepts
EXCLUDE USING GiST
A table constraint that generalizes UNIQUE beyond plain equality: EXCLUDE USING GiST (col1 WITH =, col2 WITH &&) rejects any new or updated row where col1 equals an existing row's value AND col2 overlaps that same existing row's range — enforced atomically by the database on every insert and update, with no possible race condition between a check and the write it is supposed to guard.
tstzrange
A range type representing a span of timestamp-with-timezone values as a single value, rather than two separate start/end columns. Range types support meaningful operators directly — && for overlap, @> for containment of a point or another range — which is exactly what an exclusion constraint on booking conflicts, or any interval-overlap query, needs.
What to remember
- @> tests whether one array fully contains another; && tests only whether the two arrays share at least one element — genuinely different questions
- GIN indexes array containment with the identical mechanism already proven for full-text search and JSONB — one indexing structure, three different use cases
- A range type like tstzrange represents an interval as one real value with meaningful operators (&&, @>), rather than two separate columns compared manually
- EXCLUDE USING GiST generalizes UNIQUE to non-equality conditions like range overlap, enforced atomically by the database on every write
- An exclusion constraint eliminates an entire class of check-then-act race condition that application-level conflict checking cannot fully close, since the check and the write are the same atomic operation