#!/bin/bash

pub_port=7654
sub_port=7655

initdb -D data_pub -U postgres
initdb -D data_sub -U postgres

cat << EOF >> data_pub/postgresql.conf
wal_level = logical
port = $pub_port
autovacuum = off
EOF

cat << EOF >> data_sub/postgresql.conf
wal_level = logical
port = $sub_port
autovacuum = off
EOF

pg_ctl -D data_pub start -l pub.log
pg_ctl -D data_sub start -l sub.log

psql -d postgres -Upostgres -p $pub_port -c "create table tbl (a int primary key, b text);"
psql -d postgres -Upostgres -p $pub_port -c "create table tbl2 (a int primary key, b text);"

psql -d postgres -Upostgres -p $sub_port -c "create table tbl (a int primary key, b text);"

psql -d postgres -Upostgres -p $pub_port -c "create publication pub for table tbl;"

psql -d postgres -Upostgres -p $sub_port -c "create subscription sub connection 'dbname=postgres port=$pub_port user=postgres ' publication pub;"

sleep 5
date
psql -d postgres -Upostgres -p $pub_port -c "insert into tbl2 SELECT i, 'abcdefg' FROM generate_series(1, 200000000) s(i);"
date
