Goal: query time intervals and test which tap bookings can coexist.
Connect as the beer_db dataset-owner role to beer_db.
In the connection command, -U beer_db selects the SQL role and -d beer_db selects the database. Expect the psql welcome message and a beer_db=> prompt; run the following SQL there.
Goal: identify the full resource key and its exclusion rule.
Preview five assignments in start-time/id order, including brewery name. Each tap number is local to its brewery: Golden tap 1 and Dark tap 1 are different resources. lower and upper show bounds; [ includes the start and ) excludes the end. Inspect the existing no_double_tap constraint definition rather than adding it again.
no_double_tap combines equal brewery and tap values with overlapping ranges: it blocks that combination, not every shared tap number. pg_get_constraintdef reads the existing rule. SET TIME ZONE controls this session’s timestamp display; it does not move the stored bookings. lower_inc and upper_inc show whether each endpoint is included, using t/f for true/false. ::regclass resolves the table name to its catalog identity. Expect five preview rows and a definition containing equality (=) and overlap (&&).
Goal: test the exact included and excluded boundaries.
Use the earliest real assignment, Golden Brewery tap 1: Jan 1 to Apr 1 2022 UTC. Compare containment at its lower/upper bounds and overlap with the adjacent Apr 1–May 1 interval. Expect true / false / false. The half-open intervals touch without sharing a point.
@> tests whether a range contains a timestamp; && tests whether two ranges overlap. tstzrange constructs an interval with time zone. Expect t, f, f: true at the lower bound, false at the upper, and false for the adjacent interval.
Goal: find the tap bookings active at one moment.
Find all assignments containing June 15 2022 at midnight UTC. Include brewery name and beer name; order by brewery and tap. The unlimited result is two rows at two different breweries. A point test asks whether a moment belongs inside the range.
Cast the UTC literal to timestamptz so @> tests a point rather than another range. Expect two rows; brewery identity is needed to distinguish the two taps.
Goal: find bookings with any time in common with a search window.
Find all assignments sharing any moment with [July 1, September 30) 2022 UTC. Include brewery identity and show the full seven-row result. An assignment can begin before July 1 or end after September 30 and still overlap; it need not fit wholly inside.
&& compares ranges; tstzrange(..., '[)') includes July 1 and excludes September 30 at the specified midnight boundaries. Expect seven rows, not just bookings whose start dates fall inside the window.
Goal: prove the database rejects a double booking of the same resource.
Copy the brewery, beer, and tap from the earliest assignment and attempt Jan 15–Mar 1 2022 UTC. Expect the specific no_double_tap exclusion error. This is one autocommit statement; after its failure psql can still run SELECT. Verify the table still has 300 rows and Golden tap 1 still has one assignment.
The earliest booking already covers Jan 1–Apr 1. The proposed Jan 15–Mar 1 period lies inside it and copies both brewery and tap, so all parts of the exclusion rule conflict. Changing only the beer would not fix it. The error detail should name the two overlapping ranges; COUNT(*) FILTER (WHERE ...) counts only rows for that brewery/tap. Expect assignments = 300 and golden_tap_one = 1: unchanged counts prove the failed insert stored nothing.
Goal: prove adjacent bookings are valid and then undo the test.
BEGIN a transaction. Derive an adjacent interval from the earliest assignment’s upper bound: Apr 1–May 1 UTC. INSERT it for that same brewery/tap and inspect the two periods. This one succeeds because the previous upper endpoint is excluded. ROLLBACK, then verify 300 total rows and one Golden tap 1 booking remain. The baked schedule is preserved.
upper(active_period) supplies the existing end; adding interval '30 days' builds the new end. INSERT ... SELECT copies the resource IDs. Expect INSERT 0 1 and two adjacent periods, then ROLLBACK and restored counts. A successful write inside the transaction is temporary until commit.