#!/bin/sh # # src/test/performance/cfs/pgbench_writeheavy.sh # # Phase 13 / A13.1 -- heavy write (hot UPDATE) harness for CFS. # # Unlike pgbench_readwrite.sh, this one is designed to EXPOSE the CPU cost of # CFS compression by forcing pages to be evicted from the buffer cache while # the workload is running. It does so by: # # * starting a throwaway cluster with a SMALL shared_buffers (so the table is # several times larger than the cache), AND # * running a random-UPDATE workload against that oversubscribed table. # # Each evicted dirty page must be compressed (lz4/zstd) and appended to the # .cfp, leaving the previous compressed record as dead space. The report # therefore also prints dead_pages / dead_bytes -- a non-zero value is the # proof that the CFS write path actually ran (on a cache-resident table it # stays at zero and the test shows no CFS overhead at all). # # Usage: # PG_BINDIR=/path/to/pg/bin ./pgbench_writeheavy.sh # # Environment: # PG_BINDIR directory containing initdb/pg_ctl/pgbench/psql (required) # SCALE rows loaded into each table (default 50000 -> ~20 MB @ 400 chars) # PAYLOAD_LEN payload length in chars (default 400) # CLIENTS space-separated client counts (default "1 8 32") # DURATION per-run duration in seconds (default 20) # BUF_KB shared_buffers for the throwaway cluster, in MB (default 16) # # The harness always starts its own throwaway cluster under # $TMPDIR/cfsevic-PID (default /tmp) with the small buffers; it never touches # a running server, because oversubscription is the whole point. set -eu HERE=$(cd "$(dirname "$0")" && pwd) : "${PG_BINDIR:?PG_BINDIR must point at the postgres bin directory}" : "${SCALE:=20000}" : "${PAYLOAD_LEN:=400}" : "${CLIENTS:=1 8}" : "${DURATION:=20}" : "${BUF_KB:=16}" : "${LICENSE_FILE:=/tmp/license.xml}" PSQL="$PG_BINDIR/psql" PGBENCH="$PG_BINDIR/pgbench" TABLES="evic_plain evic_lz4_1 evic_zstd_1 evic_zstd_3" PGDATA="$(mktemp -d "${TMPDIR:-/tmp}/cfsevic.XXXXXX")" PGPORT=55413 cleanup() { "$PG_BINDIR/pg_ctl" -D "$PGDATA" -m fast stop >/dev/null 2>&1 || true rm -rf "$PGDATA" } trap cleanup EXIT INT TERM printf '=== CFS write-heavy harness ===\n' printf ' cluster: throwaway, shared_buffers=%sMB\n' "$BUF_KB" printf ' scale: %s rows, payload %s chars (~%s MB raw per table)\n' \ "$SCALE" "$PAYLOAD_LEN" "$(awk -v s=$SCALE -v p=$PAYLOAD_LEN 'BEGIN{printf "%.0f", s*p/1048576}')" printf ' (table must be several x larger than shared_buffers for eviction)\n\n' # Bring up a throwaway cluster with SMALL buffers. "$PG_BINDIR/initdb" -D "$PGDATA" --license "$LICENSE_FILE" --auth trust --no-locale -E UTF8 >/dev/null "$PG_BINDIR/pg_ctl" -D "$PGDATA" -l "$PGDATA/evic.log" \ -o "-p $PGPORT -c shared_buffers=${BUF_KB}MB -c checkpoint_timeout=600s -c max_wal_size=4GB -c max_connections=200" \ start >/dev/null 2>&1 PSQLARGS="-h 127.0.0.1 -p $PGPORT -d postgres -v ON_ERROR_STOP=1" PGBARGS="-h 127.0.0.1 -p $PGPORT -d postgres" # Schema: identical data, only the storage policy differs. "$PSQL" $PSQLARGS < plain TPS at that count. for c in $CLIENTS; do eval "plain_at_$c="; done for tbl in $TABLES; do for c in $CLIENTS; do # Run pgbench; capture exit code so a client abort (e.g. a CFS # bug surfacing under the hot-UPDATE workload) is reported as a # row instead of aborting the whole matrix. set +e out=$("$PGBENCH" $PGBARGS -n -c "$c" -j "$c" -T "$DURATION" \ -f "$HERE/pgbench_hotupdate.sql" \ -D "scale=$SCALE" -D "payload_len=$PAYLOAD_LEN" -D "table=$tbl" 2>&1) rc=$? set -e if [ "$rc" -ne 0 ]; then printf '%-14s %-8s %12s\n' "$tbl" "$c" "FAILED ($rc)" printf ' %s\n' "$(printf '%s\n' "$out" | grep -iE 'error|aborted' | head -3 | tr '\n' '|')" continue fi tps=$(printf '%s\n' "$out" | awk '/^tps = / {print $3}') lat=$(printf '%s\n' "$out" | awk '/latency average = / {print $4}') # Record plain baseline for this client count, then compare CFS to it. eval "p=\$plain_at_$c" if [ "$tbl" = "evic_plain" ]; then eval "plain_at_$c=$tps" ovh="-" elif [ -n "$p" ]; then ovh=$(awk -v t="$tps" -v pp="$p" \ 'BEGIN { if (pp==""||pp==0||t=="") print "-"; else printf "%+.0f", (t/pp-1)*100 }') else ovh="-" fi printf '%-14s %-8s %12s %12s %14s\n' "$tbl" "$c" "${tps:-0}" "${lat:-0}" "$ovh" done done # dead_bytes after a CHECKPOINT (exact at a checkpoint): non-zero proves the # write path left superseded records behind; write_count proves the writes. "$PSQL" $PSQLARGS -c "CHECKPOINT;" >/dev/null printf '\n--- CFS accounting after workload (dead_bytes > 0 = superseded records exist) ---\n' "$PSQL" $PSQLARGS -c " SELECT s.relname, s.dead_pages, pg_size_pretty(s.dead_bytes) AS dead_bytes, pg_size_pretty(s.physical_size) AS physical, round(s.compression_ratio::numeric,2) AS ratio, s.write_count FROM pg_stat_cfs s WHERE s.relname LIKE 'evic_%' AND s.relname NOT LIKE '%_pkey' ORDER BY s.relname;" printf '\n--- physical on-disk size (incl. sidecars) ---\n' "$PSQL" $PSQLARGS -c " SELECT c.relname, pg_size_pretty(pg_relation_size(c.oid)) AS main_fork, pg_size_pretty(pg_total_relation_size(c.oid)) AS total_incl_sidecars FROM pg_class c WHERE c.relname LIKE 'evic_%' AND c.relkind='r' ORDER BY c.relname;"