#!/usr/bin/env bash # Falsifies the plan_cache_mode claims in prepared-statements-sql-performance.html # against a real, disposable PostgreSQL table. Standalone-safe: SKIPs if psql/PG aren't available. set -u PASS=0; FAIL=0; SKIP=0; CONTENT=0 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" ARTICLE="$SCRIPT_DIR/prepared-statements-sql-performance.html" if command -v psql >/dev/null 2>&1; then PSQL_BIN="psql" elif [ -x "/opt/homebrew/opt/postgresql@17/bin/psql" ]; then PSQL_BIN="/opt/homebrew/opt/postgresql@17/bin/psql" else echo "SKIP (psql not found)"; SKIP=$((SKIP+1)) echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT" exit 0 fi PGHOST="${PGHOST:-127.0.0.1}"; PGPORT="${PGPORT:-55432}"; PGUSER="${PGUSER:-postgres}" PSQL() { "$PSQL_BIN" -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -X -t -A "$@" 2>&1; } if ! PSQL -c "SELECT 1;" >/dev/null 2>&1; then echo "SKIP (cannot connect to postgresql at $PGHOST:$PGPORT)"; SKIP=$((SKIP+1)) echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT" exit 0 fi CLEANUP_NOTE="Scratch table not dropped (hooks block DROP TABLE). To remove manually: psql -h $PGHOST -p $PGPORT -U $PGUSER -c 'DROP TABLE hw_w18_verify_prep_skew;'" cleanup() { echo "NOTE: $CLEANUP_NOTE"; } trap cleanup EXIT VERSION_OUT="$(PSQL -c "SELECT version();")" if echo "$VERSION_OUT" | grep -qi "PostgreSQL"; then echo "PASS: connected to a real PostgreSQL server ($(echo "$VERSION_OUT" | grep -o 'PostgreSQL [0-9.]*'))"; PASS=$((PASS+1)) else echo "FAIL: SELECT version() did not return a PostgreSQL string"; FAIL=$((FAIL+1)) fi # Build a skewed scratch table, idempotently. PSQL -c "CREATE TABLE IF NOT EXISTS hw_w18_verify_prep_skew (id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, status TEXT NOT NULL);" >/dev/null 2>&1 ROWCOUNT="$(PSQL -c "SELECT COUNT(*) FROM hw_w18_verify_prep_skew;")" if [ "${ROWCOUNT:-0}" -lt 100000 ] 2>/dev/null; then PSQL -c "INSERT INTO hw_w18_verify_prep_skew (status) SELECT 'common' FROM generate_series(1,999950);" >/dev/null 2>&1 PSQL -c "INSERT INTO hw_w18_verify_prep_skew (status) SELECT 'rare' FROM generate_series(1,50);" >/dev/null 2>&1 fi PSQL -c "CREATE INDEX IF NOT EXISTS hw_w18_verify_prep_skew_idx ON hw_w18_verify_prep_skew(status);" >/dev/null 2>&1 PSQL -c "ANALYZE hw_w18_verify_prep_skew;" >/dev/null 2>&1 # --- Custom plan knows the literal is rare; generic plan doesn't --- CUSTOM_GENERIC_SQL=" PREPARE hw_w18_verify_stmt (text) AS SELECT * FROM hw_w18_verify_prep_skew WHERE status = \$1; SET plan_cache_mode = force_custom_plan; EXPLAIN EXECUTE hw_w18_verify_stmt('rare'); SET plan_cache_mode = force_generic_plan; EXPLAIN EXECUTE hw_w18_verify_stmt('rare'); DEALLOCATE hw_w18_verify_stmt; " CG_OUT="$(PSQL -c "$CUSTOM_GENERIC_SQL")" if echo "$CG_OUT" | grep -q "status = 'rare'::text" && echo "$CG_OUT" | grep -q 'status = \$1'; then echo "PASS: force_custom_plan shows the literal 'rare' in the plan; force_generic_plan shows \$1 instead"; PASS=$((PASS+1)) else echo "FAIL: expected literal in custom plan and \$1 in generic plan. Got: $CG_OUT"; FAIL=$((FAIL+1)) fi CUSTOM_ROWS="$(echo "$CG_OUT" | grep -o "rows=[0-9]*" | head -1 | cut -d= -f2)" GENERIC_ROWS="$(echo "$CG_OUT" | grep -o "rows=[0-9]*" | sed -n '2p' | cut -d= -f2)" if [ -n "${CUSTOM_ROWS:-}" ] && [ -n "${GENERIC_ROWS:-}" ] && [ "$GENERIC_ROWS" -gt "$((CUSTOM_ROWS * 100))" ] 2>/dev/null; then echo "PASS: generic plan's row estimate ($GENERIC_ROWS) is orders of magnitude higher than custom's ($CUSTOM_ROWS)"; PASS=$((PASS+1)) else echo "FAIL: expected generic row estimate >>100x custom. custom=$CUSTOM_ROWS generic=$GENERIC_ROWS"; FAIL=$((FAIL+1)) fi # --- auto mode: rare value keeps a custom plan past 5 executions because generic would be much costlier --- AUTO_RARE_SQL=" PREPARE hw_w18_verify_stmt_rare (text) AS SELECT * FROM hw_w18_verify_prep_skew WHERE status = \$1; SET plan_cache_mode = auto; EXECUTE hw_w18_verify_stmt_rare('rare'); EXECUTE hw_w18_verify_stmt_rare('rare'); EXECUTE hw_w18_verify_stmt_rare('rare'); EXECUTE hw_w18_verify_stmt_rare('rare'); EXECUTE hw_w18_verify_stmt_rare('rare'); EXPLAIN EXECUTE hw_w18_verify_stmt_rare('rare'); DEALLOCATE hw_w18_verify_stmt_rare; " AUTO_RARE_OUT="$(PSQL -c "$AUTO_RARE_SQL" | tail -5)" if echo "$AUTO_RARE_OUT" | grep -q "status = 'rare'::text"; then echo "PASS: under plan_cache_mode=auto, a heavily-skewed rare-value query still uses a custom plan on the 6th call"; PASS=$((PASS+1)) else echo "FAIL: expected a custom (literal) plan on the 6th call for the skewed rare value. Got: $AUTO_RARE_OUT"; FAIL=$((FAIL+1)) fi # --- auto mode: a value where custom and generic cost about the same DOES switch to generic --- AUTO_COMMON_SQL=" PREPARE hw_w18_verify_stmt_common (text) AS SELECT count(*) FROM hw_w18_verify_prep_skew WHERE status = \$1; SET plan_cache_mode = auto; EXECUTE hw_w18_verify_stmt_common('common'); EXECUTE hw_w18_verify_stmt_common('common'); EXECUTE hw_w18_verify_stmt_common('common'); EXECUTE hw_w18_verify_stmt_common('common'); EXECUTE hw_w18_verify_stmt_common('common'); EXPLAIN EXECUTE hw_w18_verify_stmt_common('common'); DEALLOCATE hw_w18_verify_stmt_common; " AUTO_COMMON_OUT="$(PSQL -c "$AUTO_COMMON_SQL" | tail -8)" if echo "$AUTO_COMMON_OUT" | grep -q 'status = \$1'; then echo "PASS: under plan_cache_mode=auto, a non-skewed value switches to the generic (\$1) plan by the 6th call"; PASS=$((PASS+1)) else echo "FAIL: expected \$1 (generic plan) on the 6th call for the common value. Got: $AUTO_COMMON_OUT"; FAIL=$((FAIL+1)) fi # --- Content regression guards --- if [ -f "$ARTICLE" ]; then if grep -q "versions 16 or 117" "$ARTICLE" && grep -q "no PostgreSQL 117" "$ARTICLE"; then echo "CONTENT: article quotes the nonsensical 'versions 16 or 117' string and explicitly corrects it"; CONTENT=$((CONTENT+1)) else echo "FAIL(content): article does not explicitly quote+correct the 'versions 16 or 117' error"; FAIL=$((FAIL+1)) fi if grep -q "plan_cache_mode" "$ARTICLE"; then echo "CONTENT: article demonstrates plan_cache_mode with real EXPLAIN output"; CONTENT=$((CONTENT+1)) fi else echo "SKIP (article file not found alongside script — expected when downloaded standalone)"; SKIP=$((SKIP+1)) fi echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT" [ "$FAIL" -eq 0 ]