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 integer primary key, counter integer, filler text default repeat('x', 1000)) with (fillfactor=10)"
psql -p 54321 postgres -c "insert into t values (generate_series(1,100000), 0)"
psql -p 54321 postgres -c "create publication pub1 for table t"
psql -p 54322 postgres -c "create table t(pk integer primary key, counter integer, filler text default repeat('x', 1000)) with (fillfactor=10)"
psql -p 54322 postgres -c "create subscription sub1 connection 'port=54321 dbname=postgres' publication pub1"
for ((count=0; count < 100000; ))
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 update.sql -p 54321 -d postgres
psql -p 54321 postgres -c "insert into t values (100001, 0)"
pg_ctl -D pgsql.replica -l logfile.replica start
while ((count < 100001))
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