#!/bin/bash

port_primary=5431
port_standby=5432
onegb=3000000

echo '##########'
echo '#Clean up#'
echo '##########'

pg_ctl stop -D data_primary -w
pg_ctl stop -D data_standby -w

rm -r data* *log

echo '########'
echo '#Set up#'
echo '########'

initdb -D data_primary -U postgres

cat << EOF >> data_primary/postgresql.conf
wal_level = logical
port = $port_primary
wal_sender_timeout = 0
wal_receiver_timeout = 0
shared_buffers = 40GB
# shared_buffers = 10GB
max_worker_processes = 32
max_parallel_maintenance_workers = 24
max_parallel_workers = 32
#synchronous_commit = off
#checkpoint_timeout = 1d
max_wal_size = 24GB
min_wal_size = 15GB
autovacuum = off
max_logical_replication_workers = 15
max_sync_workers_per_subscription = 15
log_line_prefix = '%n [%p] '
max_wal_senders = 200
max_replication_slots = 200
EOF

# Boot a database instance

pg_ctl -D data_primary start -w -l primary.log

# Define database objects

for i in `seq 1 10`
do
    psql -U postgres -p $port_primary -c "CREATE TABLE t${i} (a int);"
    psql -U postgres -p $port_primary -c "INSERT INTO t${i} VALUES (generate_series(1, $((${onegb}))))"
done

psql -U postgres -p $port_primary -c "CHECKPOINT;"
pg_ctl -D data_primary restart -w -l primary.log
psql -U postgres -p $port_primary -c "CHECKPOINT;"

pg_basebackup -D data_standby -U postgres -p $port_primary -R 

cat << EOF >> data_standby/postgresql.conf
port = $port_standby
EOF


pg_ctl -D data_standby start -w -l standby.log
echo "wait 10s for catching up..."
sleep 10s

#pg_ctl -D data_primary restart -w -l primary.log
pg_ctl -D data_standby stop -w -l standby.log

echo `date +%s%3N`
perf record -a --call-graph dwarf -- pg_subscriber --pgdata data_standby --publisher-conninfo "user=postgres port=$port_primary" --user postgres --port $port_standby --database 'postgres' -r
#perf record -u hayato --call-graph dwarf -- pg_subscriber --pgdata data_standby --publisher-conninfo "user=postgres port=$port_primary" --user postgres --port $port_standby --database 'postgres' -r
# pg_subscriber --pgdata data_standby --publisher-conninfo "user=postgres port=$port_primary" --user postgres --port $port_standby --database 'postgres' -r & perf record --call-graph dwarf -p $!
echo `date +%s%3N`
