#!/bin/bash

##################
### Definition ###
##################

## prefix
PUB_PREFIX="/home/hayato/pub_mypostgres/bin"

PUB_NUM=14

## scale factor
SCALE=60

## pgbench init command
INIT_COMMAND="pgbench -i -U postgres postgres -s $SCALE"

SOURCE=$1

################
### clean up ###
################

pg_ctl stop -D data_pub -w
pg_ctl stop -D data_sub -w
rm -rf data* *log

#######################
### setup publisher ###
#######################

initdb -D data_pub -U postgres
cat << EOF >> data_pub/postgresql.conf
port=5431
autovacuum = false
shared_buffers = '30GB'
max_wal_size = 20GB
min_wal_size = 10GB
wal_level = logical
max_replication_slots = 500
max_logical_replication_workers = 100
max_active_replication_origins = 500
max_wal_senders=500
max_worker_processes=1000 
max_replication_slots=500
EOF

pg_ctl -D data_pub start -w -l pub.log
${PUB_PREFIX}/$INIT_COMMAND -p 5431

for i in `seq 0 ${PUB_NUM}`
do
    psql -U postgres -p 5431 -c "CREATE PUBLICATION pub_${i} FOR TABLE pgbench_pub_accounts WHERE (aid % 15 = ${i}), TABLE pgbench_pub_branches WHERE (bid % 15 = ${i}), TABLE pgbench_pub_tellers WHERE (tid % 15 = ${i}), TABLE pgbench_pub_history;"
done

#######################
### setup sublisher ###
#######################

pg_basebackup -p 5431 -D data_sub -U postgres
# initdb -D data_sub -U postgres

cat << EOF >> data_sub/postgresql.conf
port=5432
autovacuum = false
shared_buffers = '30GB'
max_wal_size = 20GB
min_wal_size = 10GB
track_commit_timestamp = on
# log_min_messages = DEBUG1
max_replication_slots = 500
max_logical_replication_workers = 100
max_active_replication_origins = 500
max_wal_senders=500
max_worker_processes=1000 
max_replication_slots=500
EOF

pg_ctl -D data_sub start -w -l sub.log
$INIT_COMMAND -p 5432

#(
#    echo "CREATE TABLE pgbench_pub_history (tid int,bid int,aid bigint,delta int,mtime timestamp,filler char(22));"
#    echo "CREATE TABLE pgbench_pub_tellers (tid int not null primary key,bid int,tbalance int,filler char(84));"
#    echo "CREATE TABLE pgbench_pub_accounts (aid bigint not null primary key,bid int,abalance int,filler char(84));"
#    echo "CREATE TABLE pgbench_pub_branches (bid int not null primary key,bbalance int,filler char(88));"
#) | psql -p 5432 -U postgres

if [ $SOURCE = "head" ]
then
    for i in `seq 0 ${PUB_NUM}`
    do
	    psql -U postgres -p 5432 -c "CREATE SUBSCRIPTION sub_${i} CONNECTION 'port=5431 user=postgres' PUBLICATION pub_${i} WITH (copy_data=off);"
    done
else
    for i in `seq 0 ${PUB_NUM}`
    do
        psql -U postgres -p 5432 -c "CREATE SUBSCRIPTION sub_${i} CONNECTION 'port=5431 user=postgres' PUBLICATION pub_${i} WITH (retain_conflict_info = on, copy_data=off);"
    done
fi

sleep 5s
