pkill -9 postgres
rm -rf data
rm -rf standby
rm standby.log
rm logfile

./initdb -D data -c "wal_level=logical" -c "max_prepared_transactions = 10" -c "listen_addresses=*" -c "max_slot_wal_keep_size=300"

cp pg_hba_primary.conf data/pg_hba.conf

./pg_ctl -D data -l logfile -c start

./psql -d postgres -c "CREATE ROLE replication WITH REPLICATION PASSWORD 'password' LOGIN;"
./psql -d postgres -c "SELECT * FROM pg_create_physical_replication_slot('replica_1', true);"

./pg_ctl -D data -l logfile -c restart

./psql -d postgres -c "create database db1;"
./psql -d postgres -c "create database db2;"
./psql -d db1 -c "CREATE TABLE public.t1 (id int, val text);"
./psql -d db1 -c "CREATE TABLE public.t2 (id int, val text);"
./psql -d db2 -c "CREATE TABLE public.t3 (id int, val text, extra int);"
./psql -d db1 -c "CREATE PUBLICATION pub1 FOR table public.t1;"
./psql -d db1 -c "CREATE PUBLICATION pub2 FOR table public.t2;"
./psql -d db1 -c "CREATE PUBLICATION pub3 FOR table public.t1;"
./psql -d db1 -c "CREATE PUBLICATION pub4 FOR table public.t2;"

./pg_basebackup -h 127.0.0.1 -D /home/shubham/Project/Git/postgres/inst/bin/standby -P -U replication -R

cp standby.signal standby/

echo "wal_level=logical
\n listen_addresses='*'
\n Port=9999
\n max_logical_replication_workers=5
\n max_sync_workers_per_subscription=1" >> standby/postgresql.conf

sleep 3

./pg_ctl -D standby -l standby.log -o "-p 9999" -c start

sleep 2

./psql -d postgres -c "ALTER SYSTEM SET wal_level = 'logical';"
./psql -d postgres -c "ALTER SYSTEM SET listen_addresses = '*';"
./psql -d postgres -c "ALTER SYSTEM SET port = 9999;"
./psql -d postgres -c "ALTER SYSTEM SET max_logical_replication_workers = 5;"
./psql -d postgres -c "ALTER SYSTEM SET max_sync_workers_per_subscription = 1;"

./pg_ctl -D standby -l standby.log -c stop

sleep 1

./pg_createsubscriber -D standby/ -P "host=localhost port=5432 dbname=postgres" -d db2 --publication pub2 --clean=publications --dry-run --verbose

#./pg_createsubscriber -D standby/ -P "host=localhost port=5432 dbname=postgres" -d db2 --publication pub2 --dry-run --verbose

#./pg_createsubscriber -D standby/ -P "host=localhost port=5432 dbname=postgres" -d db1 --publication pub1 --clean=publications --dry-run --verbose

#./pg_createsubscriber -D standby/ -P "host=localhost port=5432 dbname=postgres" -d db1 --publication pub1 --dry-run --verbose

#./pg_createsubscriber -D standby/ -P "host=localhost port=5432 dbname=postgres" --clean=publications --dry-run --verbose

#./pg_createsubscriber -D standby/ -P "host=localhost port=5432 dbname=postgres" --dry-run --verbose
