#!/bin/bash

INSTALL=/pgsql/install/master/
PGDATA=~/junk/pgdata

rm -fr $PGDATA

$INSTALL/bin/initdb -D $PGDATA
cat << EOF >> $PGDATA/postgresql.conf
  max_prepared_transactions = 1
  lc_messages = C
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<131071;i++)) ; do echo "SELECT * FROM foo FOR SHARE;" ; done ) | $INSTALL/bin/psql postgres > /dev/null
$INSTALL/bin/psql postgres << EOF
  COMMIT PREPARED 'tx1';
EOF
$INSTALL/bin/vacuumdb --freeze --all

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

echo "second round"

$INSTALL/bin/pg_ctl -D $PGDATA -w start
$INSTALL/bin/psql postgres << EOF
  BEGIN;
  SELECT * FROM foo FOR SHARE;
  PREPARE TRANSACTION 'tx1';
EOF

( for ((i=0;i<131072;i++)) ; do echo "SELECT * FROM foo FOR SHARE;" ; done ) | $INSTALL/bin/psql postgres > /dev/null
$INSTALL/bin/psql postgres << EOF
  COMMIT PREPARED 'tx1';
EOF

$INSTALL/bin/vacuumdb --freeze --all

$INSTALL/bin/pg_ctl -D $PGDATA -w stop
ls $PGDATA/pg_multixact/offsets
$INSTALL/bin/pg_ctl -D $PGDATA -w start
$INSTALL/bin/pg_ctl -D $PGDATA -w stop
ls $PGDATA/pg_multixact/offsets


