#!/bin/bash

INSTALL=.
PGDATA=./pgdata

rm -fr $PGDATA

$INSTALL/bin/initdb -D $PGDATA
cat << EOF >> $PGDATA/postgresql.conf
  max_prepared_transactions = 2
EOF
$INSTALL/bin/pg_ctl -D $PGDATA -w start
$INSTALL/bin/psql postgres << EOF
  UPDATE pg_database SET datallowconn = true;
  CREATE TABLE foo AS SELECT 42 AS id;
  BEGIN;
  SELECT * FROM foo FOR SHARE;
  PREPARE TRANSACTION 'tx1';
EOF
( for ((i = 0; i < 32765; i++)) ; do echo "SELECT * FROM foo FOR SHARE;" ; done ) | $INSTALL/bin/psql postgres > /dev/null
$INSTALL/bin/psql postgres -c "BEGIN; SELECT * FROM foo FOR SHARE; PREPARE TRANSACTION 'tx2';"
$INSTALL/bin/psql postgres -c "SELECT * FROM foo FOR SHARE"
$INSTALL/bin/psql postgres -c "COMMIT PREPARED 'tx1'"
$INSTALL/bin/psql postgres -c "COMMIT PREPARED 'tx2'"

$INSTALL/bin/vacuumdb --freeze --all
$INSTALL/bin/psql postgres -c 'CHECKPOINT'

$INSTALL/bin/pg_ctl -D $PGDATA -w stop
