Goal: query multi-valued columns and distinguish array membership, order, and repetition.
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: interpret array lengths, empty arrays, and missing arrays.
Inspect five beers, including hops and malts, in name/id order. Compare array_length and cardinality. Then test a two-element, empty, and SQL NULL array: the empty array has cardinality 0 but array_length NULL. Both functions yield NULL for a NULL array.
ARRAY[...] constructs an array. array_length(..., 1) asks for the first dimension’s length; cardinality counts all elements. {} is an empty array, whereas SQL NULL means the array is missing. array_to_string joins element text for display without changing the array. ::text[] gives an empty or NULL literal the array type PostgreSQL cannot infer from elements. Expect lengths/counts 2/2, empty_length_is_null = t, empty_count = 0, and null_count_is_null = t.
Goal: verify two ways of asking for one hop.
Preview five beers with Saaz using = ANY. Then compare ANY and singleton containment on the full catalogue: both count 38 beers, and their ID sets have zero differences. Two equal counts alone would not establish identical membership.
'Saaz' = ANY(hops) compares Saaz with each element; hops @> ARRAY['Saaz'] requires the array to contain it. The provided WITH names both ID sets; EXCEPT checks missing IDs in both directions and UNION ALL collects the differences. Expect counts 38/38 and id_differences = 0.
Goal: require Citra and Mosaic together rather than either one.
Containment requires every requested hop. Show the full seven-row result in name/id order; this query is not a preview. Reversed array order still matches. @> is supported by the existing hops GIN index, but this step does not measure speed.
hops @> ARRAY['Citra', 'Mosaic'] requires both values in the same beer’s array. Expect seven beers; reversing the requested array would not change membership.
Goal: expand array elements into rows before aggregating.
Use CROSS JOIN LATERAL unnest(b.hops) AS h(hop). Count element occurrences and distinct beer IDs per hop, ordered by occurrence count then hop name. LIMIT 10 is a preview of varieties. In this seed no beer repeats a hop, so the two counts agree. Check the full expansion: 400 beers produce 1,171 hop rows.
LATERAL lets the right-side function use the current beer’s hops; unnest emits one row per element. AS h(hop) names the result and its element column. A beer with three hops contributes three expanded rows, explaining why 1,171 differs from 400.
Goal: see how a repeated element affects membership and counts.
Insert ARRAY['Citra','Citra','Mosaic'] into the separate array_recipe_demo. Inspect containment versus exact equality, then unnest it. Citra appears twice; containment ignores that multiplicity, while equality considers order and contents. COUNT(*) counts occurrences; COUNT(DISTINCT name) counts recipes. The 400 baked beers remain unchanged.
The lab supplies an empty demo table; the insert adds one recipe. Expect containment true but exact equality with the two-element array false, then Citra twice and Mosaic once. Citra’s occurrence count is 2 but its distinct-recipe count is 1.
Goal: inspect existing index support without changing the catalogue.
Inspect the existing idx_beers_hops definition; do not recreate it. In a transaction discourage sequential scans, EXPLAIN Citra/Mosaic containment, and ROLLBACK. Look for idx_beers_hops. This is a controlled capability check; the normal planner can choose another plan.
The pg_indexes view supplies indexdef, the index definition; GIN indexes array elements. SET LOCAL is transaction-scoped, and EXPLAIN returns the proposed plan. Success is an index condition for the containment operator followed by ROLLBACK, not just finding the index name in its definition.