#!/bin/bash
touch /home/ubuntu/.bash_history; chown ubuntu /home/ubuntu/.bash_history
echo 'tail -f /var/log/cloud-init-output.log' >> /home/ubuntu/.bash_history
for f in /home/ubuntu /root; do
  echo '\timing' >> $f/.psqlrc
  echo "alias l='ls -lrt'" >> $f/.bashrc
  echo 'export PAGER=less' >> $f/.bashrc
  echo "set mouse-=a" >> $f/.vimrc
  echo 'PATH="$HOME/bin:/usr/local/pgsql/bin:$PATH"' >> $f/.profile
  echo 'PGHOST=localhost' >> $f/.profile
done
export DEBIAN_FRONTEND=noninteractive
sudo apt install -y postgresql
sudo -u postgres psql -c 'create user ubuntu' -c' create database ubuntu owner ubuntu'

su - ubuntu <<'EOubuntu'

psql -c'
create table j (id bigserial, j jsonb);
create index on j using gin (j);
alter user ubuntu set synchronous_commit to off;
alter index j_j_idx set (fastupdate = off);
'
for g in `seq 1 100`; do  for f in `seq 1 10`; do psql -c 'truncate j'; pgbench -n -f <(echo 'insert into j (j) select jsonb_object_agg(x::text, left(md5(random()::text),5)) from generate_series(1,10) f(x);') -T 60 -c $f -j $f | fgrep tps; done; done
EOubuntu
