#!/bin/sh
# settings
export PGDATABASE=bench
BENCHOPTS='-T 10'

set -e

# initialization
psql -1 <<EOF
drop table if exists unindexed;
drop table if exists indexed;
create table unindexed(b bool);
create table indexed(b bool);
create index on indexed(b);
vacuum analyze unindexed;
vacuum analyze indexed;

checkpoint;
EOF
sleep 1

runbench() {
	cat pgbench_script.tmp
	for i in `seq 1 3`; do
		pgbench -n -f pgbench_script.tmp $BENCHOPTS |grep 'excluding connections'
	done
}

cat >pgbench_script.tmp <<EOF
select bool_and(b) from unindexed;
EOF
runbench

cat >pgbench_script.tmp <<EOF
select bool_and(b) from indexed;
EOF
runbench

# cleanup
rm pgbench_script.tmp
