#!/bin/bash # Reproduces every factual claim in # https://www.heatware.net/postgresql/jsonb-vs-json-postgresql/ # # Creates a throwaway PostgreSQL cluster in a temp directory on port 55447, # runs each claim, prints PASS/FAIL, then stops the cluster and deletes it. # It does not touch any existing PostgreSQL installation or data. # # Timing note: the article's 2.702 ms vs 44.260 ms figures are machine-specific # and are NOT asserted here; this script asserts the plan SHAPES (index scan vs # seq scan), which is the claim that generalizes. # # Requires: initdb, pg_ctl, psql on PATH (PostgreSQL 12+ for jsonb_path_query). # Usage: bash verify-jsonb-vs-json-postgresql.sh set -u PORT=55447 PGDATA_TMP="$(mktemp -d -t hwjsonb)" trap 'pg_ctl -D "$PGDATA_TMP" stop -m immediate >/dev/null 2>&1; rm -rf "$PGDATA_TMP"' EXIT PASSES=0 FAILS=0 SKIPS=0 q() { psql -h 127.0.0.1 -p $PORT -U postgres -X -At "$@" 2>&1; } hdr(){ printf '\n\033[1m%s\033[0m\n' "$*"; } # check check() { local claim="$1" want="$2" got="$3" if [ "$want" = "$got" ]; then PASSES=$((PASSES+1)); printf ' \033[32mPASS\033[0m %s\n' "$claim" else FAILS=$((FAILS+1)); printf ' \033[31mFAIL\033[0m %s\n' "$claim" printf ' expected: %s\n actual: %s\n' "$want" "$got" fi } skip() { SKIPS=$((SKIPS+1)); printf ' \033[33mSKIP\033[0m %s\n' "$*"; } command -v initdb >/dev/null || { echo "initdb not on PATH"; exit 1; } hdr "Environment" initdb --version initdb -D "$PGDATA_TMP" -U postgres --auth=trust -E UTF8 --locale=C >/dev/null 2>&1 \ || { echo "initdb failed"; exit 1; } pg_ctl -D "$PGDATA_TMP" -o "-p $PORT" -l "$PGDATA_TMP/server.log" start >/dev/null 2>&1 for _ in $(seq 1 30); do q -c "SELECT 1" >/dev/null 2>&1 && break; sleep 1; done q -c "SELECT version();" hdr "Fixture" q -c "CREATE TABLE events (id serial PRIMARY KEY, doc_json json, doc_jsonb jsonb); INSERT INTO events (doc_json, doc_jsonb) VALUES ('{\"z\": 1, \"a\": 2, \"z\": 3}', '{\"z\": 1, \"a\": 2, \"z\": 3}');" >/dev/null echo " events: 1 row with duplicate key + irregular whitespace" q -c "CREATE TABLE docs (id int PRIMARY KEY, body_json json, body_jsonb jsonb); INSERT INTO docs SELECT g, row_to_json(t)::json, row_to_json(t)::jsonb FROM generate_series(1, 200000) AS g, LATERAL (SELECT g AS id, 'user_' || g AS name, (ARRAY['red','green','blue','amber'])[1 + g % 4] AS tag, g % 97 AS score) t; CREATE INDEX docs_jsonb_gin ON docs USING GIN (body_jsonb); ANALYZE docs;" >/dev/null echo " docs: $(q -c 'SELECT count(*) FROM docs;') rows, GIN index on body_jsonb" hdr "Input preservation" check "json stores input verbatim (dup key, spacing, key order)" \ '{"z": 1, "a": 2, "z": 3}' \ "$(q -c "SELECT doc_json::text FROM events;")" check "jsonb sorts keys, strips whitespace, keeps last duplicate" \ '{"a": 2, "z": 3}' \ "$(q -c "SELECT doc_jsonb::text FROM events;")" check "extraction resolves duplicates to the last value on BOTH types" \ "3|3" \ "$(q -c "SELECT (doc_json->>'z')||'|'||(doc_jsonb->>'z') FROM events;")" hdr "Operator support" check "json has no @> operator" \ 'ERROR: operator does not exist: json @> unknown' \ "$(q -c "SELECT doc_json @> '{\"a\": 2}' FROM events;" | head -1)" check "json has no equality operator" \ 'ERROR: operator does not exist: json = json' \ "$(q -c "SELECT doc_json = doc_json FROM events;" | head -1)" check "jsonb @> containment works" \ "t" "$(q -c "SELECT doc_jsonb @> '{\"a\": 2}' FROM events;")" check "jsonb ? key-existence works" \ "t" "$(q -c "SELECT doc_jsonb ? 'a' FROM events;")" check "jsonb_path_query \$.* ? (@ > 1) returns 2 and 3" \ "2,3" \ "$(q -c "SELECT string_agg(v::text, ',') FROM ( SELECT jsonb_path_query(doc_jsonb, '\$.* ? (@ > 1)') FROM events ) s(v);")" hdr "Indexing" check "GIN index on json is rejected (no default operator class)" \ 'ERROR: data type json has no default operator class for access method "gin"' \ "$(q -c "CREATE INDEX docs_json_gin ON docs USING GIN (body_json);" | head -1)" JSONB_PLAN="$(q -c "EXPLAIN SELECT count(*) FROM docs WHERE body_jsonb @> '{\"tag\": \"blue\", \"score\": 42}';")" case "$JSONB_PLAN" in *"Bitmap Index Scan on docs_jsonb_gin"*) got_jsonb="index" ;; *) got_jsonb="no-index: $JSONB_PLAN" ;; esac check "jsonb containment uses the GIN index (Bitmap Index Scan)" "index" "$got_jsonb" JSON_PLAN="$(q -c "EXPLAIN SELECT count(*) FROM docs WHERE body_json::jsonb @> '{\"tag\": \"blue\", \"score\": 42}';")" case "$JSON_PLAN" in *"Seq Scan on docs"*) got_json="seqscan" ;; *) got_json="no-seqscan: $JSON_PLAN" ;; esac check "json containment (via cast) falls back to a Seq Scan" "seqscan" "$got_json" check "both plans find the same 516 matching rows" \ "516|516" \ "$(q -c "SELECT (SELECT count(*) FROM docs WHERE body_jsonb @> '{\"tag\": \"blue\", \"score\": 42}') ||'|'|| (SELECT count(*) FROM docs WHERE body_json::jsonb @> '{\"tag\": \"blue\", \"score\": 42}');")" skip "absolute timings (2.702 ms vs 44.260 ms in the article) are machine-specific — plan shape asserted instead" hdr "Storage" check "compact payload: json 29 bytes, jsonb 44 bytes" \ "29|44" \ "$(q -c "SELECT pg_column_size('{\"tag\":\"blue\",\"score\":42}'::json) ||'|'||pg_column_size('{\"tag\":\"blue\",\"score\":42}'::jsonb);")" check "pretty-printed payload: json (42) still smaller than jsonb (44)" \ "42|44" \ "$(q -c "SELECT pg_column_size(jsonb_pretty('{\"tag\":\"blue\",\"score\":42}'::jsonb)::json) ||'|'||pg_column_size(jsonb_pretty('{\"tag\":\"blue\",\"score\":42}'::jsonb)::jsonb);")" check "200k-row table: json total 11 MB, jsonb total 17 MB" \ "11 MB|17 MB" \ "$(q -c "SELECT pg_size_pretty(sum(pg_column_size(body_json))) ||'|'||pg_size_pretty(sum(pg_column_size(body_jsonb))) FROM docs;")" hdr "Result" printf ' %d passed, %d failed, %d skipped\n' "$PASSES" "$FAILS" "$SKIPS" echo " cluster $PGDATA_TMP will be stopped and deleted on exit" [ "$FAILS" -eq 0 ] || exit 1