#!/usr/bin/env bash

set -e

PATH_OLD=$PATH

CLIENTS=128
STEP=4

ROWS=1442000
DATADIR=/mnt/data/pg
RUNS=5

TS=$(date +%Y%m%d-%H%M%S)

killall -9 postgres || true

rm -Rf $DATADIR

export PATH=/home/tomas/builds/master/bin:$PATH_OLD

pg_ctl -D $DATADIR init
pg_checksums --disable $DATADIR


echo 'max_wal_size = 128GB' >> $DATADIR/postgresql.conf
echo 'io_method = io_uring' >> $DATADIR/postgresql.conf
echo 'maintenance_work_mem = 256MB' >> $DATADIR/postgresql.conf
echo 'work_mem = 256MB' >> $DATADIR/postgresql.conf


pg_ctl -D $DATADIR -l pg.log start


rm -Rf scripts

mkdir $TS
mkdir $TS/scripts


createdb test


psql test -c "CREATE TABLE t (id int, val text) PARTITION BY LIST (id)"

SCRIPTS=""

for p in $(seq 1 $CLIENTS); do

	psql test -c "CREATE TABLE t_${p} PARTITION OF t FOR VALUES IN (${p}) WITH (fillfactor=10)"
	psql test -c "INSERT INTO t_${p} SELECT ${p}, md5(i::text) FROM generate_series(1,$ROWS) s(i)"

	echo "SELECT * FROM t_${p} OFFSET 1000000000" >> $TS/scripts/$p.sql

	SCRIPTS="$SCRIPTS -f $TS/scripts/${p}.sql"

done


psql test -c "vacuum freeze"

psql test -c "checkpoint"

psql test -c "\d+"


pg_ctl -D $DATADIR -l pg.log stop
