#!/usr/bin/env bash # Falsify the claims in partitioning-vs-sharding.html: that PostgreSQL partitioning is native, # that pruning works for a literal WHERE clause, and that a plain EXPLAIN lists every partition for a subquery # value (it is known only at execution time, where PostgreSQL can still prune) and for the join. # Standalone: builds its own scratch database+tables if a server is reachable; SKIPs otherwise. # Never touches the article's own scratch table (hw_w17_sales) - uses uniquely-named objects here. set -u PASS=0 FAIL=0 SKIP=0 CONTENT=0 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ARTICLE="$SCRIPT_DIR/partitioning-vs-sharding.html" pass() { echo "PASS: $1"; PASS=$((PASS+1)); } fail() { echo "FAIL: $1"; FAIL=$((FAIL+1)); } skip() { echo "SKIP: $1"; SKIP=$((SKIP+1)); } content_pass() { echo "CONTENT: $1"; CONTENT=$((CONTENT+1)); } # Try common connection targets: the documented scratch server first, then a bare local default. PSQL="" for CAND in \ "psql -h 127.0.0.1 -p 55432 -U postgres" \ "psql -h 127.0.0.1 -U postgres" \ "psql"; do if command -v psql >/dev/null 2>&1 && $CAND -c "SELECT 1;" >/dev/null 2>&1; then PSQL="$CAND" break fi done TABLE="hw_verify_w17_sales_$$" LOOKUP="hw_verify_w17_lookup_$$" cleanup() { [ -n "$PSQL" ] && $PSQL -c "DROP TABLE IF EXISTS ${TABLE};" >/dev/null 2>&1 } trap cleanup EXIT if [ -z "$PSQL" ]; then skip "no reachable PostgreSQL server found (tried 127.0.0.1:55432 and defaults) - live checks skipped" else SETUP_OK=1 $PSQL -v ON_ERROR_STOP=1 </tmp/hw_verify_w17_setup.log 2>&1 || SETUP_OK=0 CREATE TABLE ${TABLE} ( id BIGINT GENERATED ALWAYS AS IDENTITY, d DATE NOT NULL, PRIMARY KEY (id, d) ) PARTITION BY RANGE (d); CREATE TABLE ${TABLE}_p2024 PARTITION OF ${TABLE} FOR VALUES FROM ('2024-01-01') TO ('2025-01-01'); CREATE TABLE ${TABLE}_p2025 PARTITION OF ${TABLE} FOR VALUES FROM ('2025-01-01') TO ('2026-01-01'); INSERT INTO ${TABLE} (d) SELECT g::date FROM generate_series('2024-01-01'::date, '2025-12-31'::date, '1 day') g; ANALYZE ${TABLE}; SQL if [ "$SETUP_OK" = "1" ]; then pass "created a real range-partitioned table with 2 partitions and 730 rows" # Claim: a literal-date WHERE clause prunes to exactly one partition. PLAN1=$($PSQL -t -A -c "EXPLAIN (COSTS OFF) SELECT * FROM ${TABLE} WHERE d = '2025-06-15';" 2>/dev/null) N1=$(echo "$PLAN1" | grep -c "Seq Scan on ${TABLE}_p") if [ "$N1" = "1" ]; then pass "literal-date WHERE clause prunes to exactly 1 partition (as claimed)" else fail "expected exactly 1 partition scanned for a literal date, got $N1 -- plan: $PLAN1" fi # Claim: plan-time pruning cannot use a scalar subquery's value - a plain EXPLAIN lists all partitions. PLAN2=$($PSQL -t -A -c "EXPLAIN (COSTS OFF) SELECT * FROM ${TABLE} WHERE d = (SELECT '2025-06-15'::date);" 2>/dev/null) N2=$(echo "$PLAN2" | grep -c "Seq Scan on ${TABLE}_p") if [ "$N2" = "2" ]; then pass "scalar-subquery WHERE clause: plain EXPLAIN lists both partitions (no plan-time pruning; execution-time pruning can still skip one)" else fail "expected plain EXPLAIN to list both partitions for the subquery case, got $N2 -- plan: $PLAN2" fi # Claim: for a join against another table's column, a plain EXPLAIN also lists every partition (the page: a hash # join like the article's scans every partition; a parameterized nested loop can still prune at execution time). # (TEMP tables are session-scoped, so create/populate/query in one psql invocation.) PLAN3=$($PSQL -t -A </dev/null CREATE TEMP TABLE ${LOOKUP} (d text); INSERT INTO ${LOOKUP} VALUES ('2025-06-15'); EXPLAIN (COSTS OFF) SELECT s.* FROM ${TABLE} s JOIN ${LOOKUP} l ON s.d = l.d::date; SQL ) N3=$(echo "$PLAN3" | grep -c "Seq Scan on ${TABLE}_p") if [ "$N3" = "2" ]; then pass "join against another table's column: plain EXPLAIN lists both partitions (as claimed)" else fail "expected both partitions scanned for the join case, got $N3 -- plan: $PLAN3" fi else fail "could not create the scratch partitioned table (see /tmp/hw_verify_w17_setup.log)" fi # Claim: core PostgreSQL has postgres_fdw available but Citus is not installed - i.e. no built-in sharding extension. FDW=$($PSQL -t -A -c "SELECT count(*) FROM pg_available_extensions WHERE name='postgres_fdw';" 2>/dev/null) CITUS=$($PSQL -t -A -c "SELECT count(*) FROM pg_available_extensions WHERE name='citus';" 2>/dev/null) if [ "$FDW" = "1" ]; then pass "postgres_fdw is available in core PostgreSQL (pg_available_extensions)" else fail "postgres_fdw not found in pg_available_extensions - contradicts the article's core claim" fi if [ "$CITUS" = "0" ]; then pass "citus is NOT installed on this server, consistent with the article's claim that sharding is not built in" else echo "NOTE: citus IS available on this server (count=$CITUS) - the article's 'not installed here' framing may not hold on this host." fi fi # --- content regression guards --- if [ -f "$ARTICLE" ]; then if grep -qi "no built-in sharding" "$ARTICLE"; then content_pass "article states core PostgreSQL has no built-in sharding" else fail "article dropped the core no-built-in-sharding claim" fi if grep -qi "faq" "$ARTICLE" | grep -qvE "^$"; then : # no-op guard placeholder fi if grep -qiE "]*>[^<]*FAQ" "$ARTICLE"; then fail "article still contains an FAQ heading block, which the brief bans" else content_pass "no FAQ heading block present" fi else skip "partitioning-vs-sharding.html not found next to this script (downloaded standalone) - content checks skipped" fi echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT" [ "$FAIL" -eq 0 ]