#!/bin/bash

OUTFILE=$1
DURATION=$2

s=`psql test -t -A -c "select extract(epoch from now())"`

while /bin/true; do

	psql test -t -A -c "select wait_event_type, wait_event, count(*), now() from pg_stat_activity group by 1, 2" >> $OUTFILE/wait-events.data 2>&1
	psql test -t -A -c "select locktype, fastpath, count(*), now() from pg_locks group by 1, 2" >> $OUTFILE/locks.data 2>&1
	psql test -t -A -c "select pg_sleep(0.005)"

	x=`psql test -t -A -c "select $s + $DURATION < extract(epoch from now())"`

	if [ "$x" == "t" ]; then
		break
	fi

done
