Goal: inspect, query, index, and update JSONB while distinguishing SQL types.
Connect as the tv_db dataset-owner role to tv_db.
In the connection command, -U tv_db selects the SQL role and -d tv_db selects the database. Expect the psql welcome message and a tv_db=> prompt; run the following SQL there.
Goal: inspect the actual payload shape before writing path queries.
Preview five imports ordered by external_id, then count the full relation. Five displayed rows are not the table size: all 500 imports have payloads with source_rank and verified. Nested scores appear only in the separate demo used later.
raw_payload stores the JSON object; braces show objects and quoted names show keys. Expect five preview rows and a full count of 500. Do not infer a nested score from the ordinary score column.
Goal: distinguish JSON extraction from text extraction and explicit conversion.
Read source_rank from EXT-000004 with both arrow operators. The displayed value is 4 in both columns; use pg_typeof to see jsonb versus text, then cast the text to integer. Printed digits alone do not tell you the SQL type.
-> returns a JSONB value and ->> returns text; ::integer casts that text to a number for numeric operations. pg_typeof reports the SQL type. Expect 4 with types jsonb/text, then the integer value 4.
Goal: follow a nested path and recognize a missing key.
Construct a nested meta.score with jsonb_build_object. Read it with #> and #>>, show their types, and check a missing path with IS NULL. The baked imports have no meta.score; this small literal deliberately supplies it.
jsonb_build_object constructs a value for this statement, not a stored table row. '{meta,score}' describes two successive keys; #> keeps JSONB and #>> returns text. WITH payload AS (...) names the constructed object for this query. Expect score 8.5 with those two types and the missing-path test t (true).
Goal: filter JSON objects by containment.
Use @> to preview five verified imports in external_id order. Then remove the display limit and count all matches: 167 of 500. The boolean true is a JSON boolean, not the string "true".
@> asks whether the left JSONB contains the right fragment. The right-hand literal is interpreted as JSONB for this operator. Expect five preview rows with verified: true and a full count of 167.
Goal: turn a JSON field into a typed grouping key.
Extract verified as text, cast it to boolean, then group and average score. Expect 333 false rows (5.41 average) and 167 true rows (5.01 average). This is a grouping calculation, not evidence that verification raises scores.
The cast makes JSON text true/false usable as an SQL boolean; t/f are psql’s boolean display. GROUP BY 1 and ORDER BY 1 refer to the first selected expression, the cast boolean, rather than a column named 1. Expect two summaries rather than 500 detail rows.
Goal: show that the containment operator can use a GIN index.
Create a GIN index on raw_payload and ANALYZE the table. In a transaction, SET LOCAL enable_seqscan = off and EXPLAIN a selective containment query, then ROLLBACK to restore the planner setting. This demonstrates index support under a controlled setting; it is not a natural-plan or speed benchmark. Look for jsonb_ratings_payload_gin and an Index Cond with @>.
GIN indexes components of JSONB; ANALYZE refreshes planner statistics. EXPLAIN displays the plan instead of returning matched rows. SET LOCAL lasts only for this transaction and discourages sequential scans; ROLLBACK clears that setting, but does not remove the index created before BEGIN.
Goal: distinguish a function’s computed result from a saved update.
Use jsonb_ratings_demo, a separate two-row fixture. First SELECT jsonb_set for row 1: the preview changes source_rank from 4 to 0 without saving it. Re-read rank 4, then UPDATE row 1 and verify rank 0 while meta.score stays 8.5 and row 2 stays unchanged. The 500 baked imports remain intact.
jsonb_set takes the original object, a key path, and a replacement JSONB value. Selecting its result does not write anything; UPDATE ... WHERE id = 1 stores it for that one demo row. Expect UPDATE 1; row 1 now has rank 0 and nested score 8.5, while row 2 keeps its original verified/rank payload.