#!/bin/bash
# set -e
echo 'Clean up'

# Stop server process if exists
pg_ctl stop -D data

# Remove previous database cluster
rm -r data* logfile

# Setup and start 
initdb -D data -U postgres
cat << EOF >> data/postgresql.conf
shared_buffers = 60GB
EOF
pg_ctl -D data start -w -l logfile

# Create table and generate dead tuples
(
    echo -e "create unlogged table test (a int) with (autovacuum_enabled = off);"
    echo -e "insert into test select generate_series(1, 600000000); --- 20GB table"
    echo -e "delete from test where a % 5 = 0;"
) | psql -U postgres

# Restart to clean up shared buffer
pg_ctl -D data restart -w -l logfile

# And do vacuuming
(
    echo -E "\timing on"
    echo -e "vacuum (verbose, parallel 1) test;"
) | psql -U postgres
