#!/bin/sh

set -e

BLOCKDEV=/dev/sda5
MOUNTPOINT=/mnt/pgtest
export PGDATA=$MOUNTPOINT/data

for wal_sync_method in open_datasync fdatasync; do
for wal_buffers in 32kB 64kB 128kB 256kB 512kB 1MB 2MB 4MB 8MB 16MB 32MB 64MB 128MB; do
  mkfs.ext4 $BLOCKDEV
  mount $BLOCKDEV $MOUNTPOINT
  chown postgres: $MOUNTPOINT

  OPTS="-c wal_buffers=$wal_buffers -c wal_sync_method=$wal_sync_method"

  hdparm -W /dev/sda |tee -a pgtest.log
  su postgres -c initdb
  su postgres -c "postgres -p 5000 $OPTS" &

  # make sure that write I/O settles down before starting tests
  sleep 1; sync
  sleep 1; sync
  sleep 1

  psql -U postgres -p 5000 <<EOF |tee -a pgtest.log
  begin;
  \x
  show wal_sync_method;
  show wal_buffers;
  -- create table foo(id integer primary key);
  create table foo(id integer);
  \timing
  insert into foo (id) select generate_series(1, 2000000);
  commit;
EOF
  kill -QUIT `cat $PGDATA/postmaster.pid`
  sleep 1

  umount $MOUNTPOINT
done
done
