function initcluster() {
	echo "======== init $1 ========"
	killall postgres >> keybench.log 2>&1
	rm -Rf 'tmp/pgdata' >> keybench.log 2>&1
	pg_ctl -D 'tmp/pgdata' init >> keybench.log 2>&1
	cp postgresql.conf 'tmp/pgdata'
	pg_ctl -D 'tmp/pgdata' -l bench.log start >> keybench.log 2>&1
	sleep 5
	createdb test >> keybench.log 2>&1
}

function init() {
	echo "======== scale $1 ========"
	psql -h localhost test -c "drop table if exists stuff_text" >> keybench.log 2>&1
	psql -h localhost test -c "drop table if exists stuff_numeric" >> keybench.log 2>&1
	psql -h localhost test -c "drop table if exists stuff_float" >> keybench.log 2>&1
	psql -h localhost test -c "drop table if exists stuff_int" >> keybench.log 2>&1
	psql -h localhost test -c "create table stuff_text    as select (random())::text         as randtxt   from generate_series(1,$1)" >> keybench.log 2>&1
	psql -h localhost test -c "create table stuff_numeric as select (random())::numeric      as randnum   from generate_series(1,$1)" >> keybench.log 2>&1
	psql -h localhost test -c "create table stuff_float   as select (random())::float        as randfloat from generate_series(1,$1)" >> keybench.log 2>&1
	psql -h localhost test -c "create table stuff_int     as select (10000000*random())::int as randint   from generate_series(1,$1)" >> keybench.log 2>&1
	psql -h localhost test -c "analyze stuff_text"    >> keybench.log 2>&1
	psql -h localhost test -c "analyze stuff_numeric" >> keybench.log 2>&1
	psql -h localhost test -c "analyze stuff_float"   >> keybench.log 2>&1
	psql -h localhost test -c "analyze stuff_int"     >> keybench.log 2>&1
}

function query() {
	echo "$1"
	for i in `seq 1 10`; do
		r=`time -f '%e' psql -h localhost test -c "$1"`
	done
}

function queries() {
	# text
	query "select percentile_disc(0) within group (order by randtxt) from stuff_text"
	query "select count(distinct randtxt) from stuff_text"
	query "select * from (select * from stuff_text order by randtxt offset 100000000000) foo"

	# numeric
	query "select percentile_disc(0) within group (order by randnum) from stuff_numeric"
	query "select count(distinct randnum) from stuff_numeric"
	query "select * from (select * from stuff_numeric order by randnum offset 100000000000) foo"

	# float
	query "select percentile_disc(0) within group (order by randfloat) from stuff_float"
	query "select count(distinct randfloat) from stuff_float"
	query "select * from (select * from stuff_float order by randfloat offset 100000000000) foo"

	# int
	query "select percentile_disc(0) within group (order by randint) from stuff_int"
	query "select count(distinct randint) from stuff_int"
	query "select * from (select * from stuff_int order by randint offset 100000000000) foo"
}

initcluster

init 1000000
queries

init 2000000
queries

init 3000000
queries

init 4000000
queries

init 5000000
queries
