pkill -9 postgres
sleep 1
rm -f logfile.primary logfile.replica
rm -fr pgsql.primary
rm -fr pgsql.replica
initdb -D pgsql.primary
initdb -D pgsql.replica
cp postgresql.conf.primary pgsql.primary/postgresql.conf
cp postgresql.conf.replica pgsql.replica/postgresql.conf
pg_ctl -D pgsql.primary -l logfile.primary start
pg_ctl -D pgsql.replica -l logfile.replica start
psql -p 54321 postgres -c "create table t(pk serial primary key, sk integer, counter integer default 0)"
psql -p 54321 postgres -c "insert into t (sk) select random()*10000000 from generate_series(1,10000000)"
psql -p 54321 postgres -c "create index on t(sk)"
psql -p 54321 postgres -c "create publication pub1 for table t"
psql -p 54322 postgres -c "create table t(pk integer primary key, sk integer, counter integer)"
psql -p 54322 postgres -c "create index on t(sk)"
psql -p 54322 postgres -c "create subscription sub1 connection 'port=54321 dbname=postgres' publication pub1"
for ((count=0; count != 10000000; ))
do
    psql -p 54321 postgres -c "select flush_lsn,reply_time,pg_current_wal_lsn() from pg_stat_replication"
	count=`psql -t -p 54322 postgres -c "select count(*) from t"`
	sleep 10
done
pg_ctl -D pgsql.replica -l logfile.replica stop
pgbench -T 100 -c 10 -M prepared -n -f insert.sql -p 54321 -d postgres
pg_ctl -D pgsql.replica -l logfile.replica start
primary_count=`psql -t -p 54321 postgres -c "select count(*) from t"`
while ((count < primary_count))
do
    psql -p 54321 postgres -c "select flush_lsn,reply_time,pg_current_wal_lsn() from pg_stat_replication"
	count=`psql -t -p 54322 postgres -c "select count(*) from t"`
	sleep 10
done

