#!/bin/bash

# USAGE:
# Set PGXXX env variables to ensure a connection
# No database setup is needed and no relations are affected

rm idle_trans_in notif_in reader_in

mkfifo idle_trans_in
psql <idle_trans_in &>/dev/null &
exec 3>idle_trans_in

echo "LISTEN test; BEGIN; select error;" >&3

mkfifo reader_in
psql <reader_in &>/dev/null &
exec 5>reader_in
echo "LISTEN something;" >&5

mkfifo notif_in
psql <notif_in >/dev/null &
exec 4>notif_in
for i in {0..500}; do
	for n in {0..1000}; do
		echo "NOTIFY something;" >&4
	done
	echo "SELECT 1;" >&5
done

#echo "Waiting for notify's to execute"
#sleep 20
DD=$(psql -tc "SHOW data_directory;")
ls -l $DD/pg_notify

echo -e "\\\\timing\\nLISTEN something;" | psql 

exec 3>&-
exec 4>&-
exec 5>&-
rm idle_trans_in notif_in

