#!/bin/bash

set -e

export PGDATABASE=postgres

psql -c "drop table if exists brintest;"

psql -c "create unlogged table brintest (id bigserial, t text)"
psql -c "alter table brintest alter column t set storage plain"
psql -c "create index on brintest (id);"

psql -c "create index brintest_idx1 on brintest using brin (t) with (pages_per_range=1);"
psql -c "create index brintest_idx2 on brintest using brin (t) with (pages_per_range=1);"
psql -c "create index brintest_idx3 on brintest using brin (t) with (pages_per_range=1);"
psql -c "create index brintest_idx4 on brintest using brin (t) with (pages_per_range=1);"

cat >/tmp/brintest-update.sql <<EOF
\set id random(1, 1000000)
insert into brintest (t) values (repeat('x', 1000));
UPDATE brintest set t = nextval('brintest_id_seq') || repeat('x', 1000) where id = :id;
EOF

cat >/tmp/brintest-vacuum.sql <<EOF
vacuum brintest;
EOF

PGOPTIONS="-csynchronous_commit=off" pgbench -n -c50 -j4 -t1000 -P1 postgres -f /tmp/brintest-update.sql@10 -f /tmp/brintest-vacuum.sql@1
