#!/bin/bash
# Find assertion failure on out-of-space error for standby.
# to be used with git bisect:
# git bisect start $BAD_COMMIT $GOOD_COMMIT
# git bisect run crash_standby.bash

# Failure found down to the merge-base of REL9_2_STABLE and REL9_3_STABLE.

# Compile Postgres
# This depends on your environment...
#cd $HOME/git/postgres
# Removal of all live files..
#git menage > /dev/null 2>&1
#pg_compile -c > /dev/null 2>&1
#make install -j 4 > /dev/null 2>&1

# Three separate partitions used here:
# - primary data at 4GB
# - standby data at 2GB
# - WAL data for each in partition at 4GB, upper-bounded by low value
#   for max_wal_size. No need to care about I/O here.
PRIMARY_DATA=$HOME/data/primary_data
PRIMARY_WAL=/mnt/disk3/ioltas/primary_wal
PRIMARY_PORT=5432
STANDBY_DATA=/mnt/disk2/ioltas/standby_data
STANDBY_WAL=/mnt/disk3/ioltas/standby_wal
STANDBY_PORT=5433

LOG_FILENAME=postgresql.log

# Because this is bad
killall -9 postgres

rm -rf $PRIMARY_DATA $PRIMARY_WAL $STANDBY_DATA $STANDBY_WAL

# Initialize and start primary
initdb -D $PRIMARY_DATA -A trust > /dev/null 2>&1

# max_wal_size should be low enough to not trigger a out-of-space error on
# the partition for WAL data, so the default is fine. And this was introduced
# in 9.5..
cat >> $PRIMARY_DATA/postgresql.conf <<EOF
max_wal_senders = 10
wal_level = hot_standby
hot_standby = on
log_directory = 'log'
logging_collector = on
log_filename = '$LOG_FILENAME'
port = $PRIMARY_PORT
EOF
cat >> $PRIMARY_DATA/pg_hba.conf <<EOF
local   all             all                                     trust
local   replication     all                                     trust
EOF

pg_ctl start -D $PRIMARY_DATA -w  > /dev/null 2>&1
createdb $USER

# Create a standby from it
pg_basebackup -D $STANDBY_DATA > /dev/null 2>&1
cat >> $STANDBY_DATA/postgresql.conf <<EOF
port = $STANDBY_PORT
EOF
cat >> $STANDBY_DATA/recovery.conf <<EOF
primary_conninfo = 'port=$PRIMARY_PORT user=$USER application_name=node_$STANDBY_PORT'
standby_mode = on
EOF
pg_ctl start -D $STANDBY_DATA -w > /dev/null 2>&1

# Fill in nodes with enough data to trigger an out-of-space error.
psql -X -p $PRIMARY_PORT -c "create table aa (a int);"
# 2GB ~=> 70M rows, should be enough to trigger out-of-space error on standby..
psql -X -p $PRIMARY_PORT -c "insert into aa values (generate_series(1,70000000));"

# Look into the standby logs to see if there is an assertion failure.
if grep -q 'FailedAssertion' $STANDBY_DATA/log/$LOG_FILENAME; then
	echo "found assertion failure"
	exit 1
fi

# No assertion failure..
exit 0
