#!/usr/bin/env bash # ATTACH PARTITION on two builds (master and patched), alternating rounds. # # MASTERBIN=... PATCHEDBIN=... WORKDIR=... attach-bench.sh # # case "curve": for N in $NS (default 25 50 100 200 400 800 1600): parent with # N NOT NULL int columns, child created with LIKE, one ATTACH # PARTITION in a new session, time reported by psql's \timing. # One fresh cluster per round and build, started with # synchronous_commit=off so that the time is the work of the # command and not the WAL flush at commit; one unmeasured # ATTACH of a one-column table first, so that no N pays for # the first ATTACH in the cluster. The build that goes first # alternates between rounds. # case "checks": parent with one CHECK constraint, child with that CHECK plus # 1600 more ("checks"), or with only that one ("checks0"); # 200 ATTACH/DETACH cycles in one DO block, user-space # instructions of the whole server (perf stat). set -uo pipefail C=$1 N=$2 : "${MASTERBIN:?} ${PATCHEDBIN:?} ${WORKDIR:?}" W=$WORKDIR/attach-$C; mkdir -p "$W"; cd "$W" || exit 1 PORT=54841 sql() { "$B/psql" -X -q -h "$W" -p $PORT -U postgres -d postgres -v ON_ERROR_STOP=1 "$@"; } fresh() { rm -rf data; "$B/initdb" -D data -U postgres -A trust >/dev/null || exit 1 } setup_checks() { # $1 = number of extra CHECK constraints on the child sql < 0)) PARTITION BY RANGE (a); SELECT format('CREATE TABLE s (a int, b int CONSTRAINT k0 CHECK (b > 0)%s)', coalesce(string_agg(format(', CONSTRAINT k%s CHECK (b > -%s)', i, i), ''), '')) FROM generate_series(1, $1) i \gexec SQL } NS=${NS:-25 50 100 200 400 800 1600} for r in $(seq 1 "$N"); do order="master patched"; [ "$C" = curve ] && [ $((r % 2)) = 0 ] && order="patched master" for b in $order; do [ $b = master ] && B=$MASTERBIN || B=$PATCHEDBIN if [ "$C" = curve ]; then fresh "$B/pg_ctl" -D data -o "-k $W -p $PORT -c synchronous_commit=off" -l server.log -w start >/dev/null sql -c 'CREATE TABLE w (c1 int NOT NULL) PARTITION BY RANGE (c1)' -c 'CREATE TABLE ws (LIKE w)' \ -c 'ALTER TABLE w ATTACH PARTITION ws FOR VALUES FROM (0) TO (10)' for n in $NS; do ms=$(sql </dev/null else for k in 1600 0; do fresh "$B/pg_ctl" -D data -o "-k $W -p $PORT" -l server.log -w start >/dev/null setup_checks $k "$B/pg_ctl" -D data -m fast -w stop >/dev/null perf stat -x, -e instructions:u -o perf.txt -- "$B/postgres" -D data -k "$W" -p $PORT \ > server.log 2>&1 & until "$B/pg_isready" -q -h "$W" -p $PORT; do sleep 0.2; done n=$(sql -At <<'SQL' | tail -1 DO $$ BEGIN FOR i IN 1..200 LOOP ALTER TABLE p ATTACH PARTITION s FOR VALUES FROM (0) TO (10); ALTER TABLE p DETACH PARTITION s; END LOOP; END $$; SELECT count(*) FROM pg_constraint WHERE conrelid = 's'::regclass; SQL ) "$B/pg_ctl" -D data -m fast -w stop >/dev/null; wait printf '%s\tround=%d\t%s\tchild_checks=%s\tinstr_M=%.1f\n' "checks$([ $k = 0 ] && echo 0)" \ "$r" $b "$n" "$(awk -F, '/instructions/{print $1/1e6}' perf.txt)" done fi done done