#!/bin/bash
#
# Create a backup and about 5 GB of WAL. The WAL consists of just
# logical messages, which require practically no effort to
# replay. That makes any per-record overhead in reading the WAL and
# calling the redo function stand out.

set -e

rm -rf pgdata pgdata-archive pgdata-backup
mkdir pgdata-archive
initdb -D pgdata

cp -a pgdata pgdata-backup

cat <<EOF >> pgdata/postgresql.conf
archive_mode = on
archive_command = 'test ! -f /home/heikki/pgsql.fsmfork/pgdata-archive/%f && cp %p /home/heikki/pgsql.fsmfork/pgdata-archive/%f'
EOF

pg_ctl -D pgdata start -l log

for (( i=0; i<10; i++ )) ; {
    psql postgres -c "select pg_logical_emit_message(false, '', '', false) from generate_series(1, 10000000) g;" > /dev/null
}

psql postgres -c "select pg_create_restore_point('the-end')" > /dev/null
psql postgres -c "select pg_switch_wal()" > /dev/null

# wait for all the WAL to be archived
sleep 2
pg_ctl -D pgdata stop
