#!/bin/bash

PGINSTALL=~/install/postgres
PGDATA=$PGINSTALL/pg_data

echo "time segments usage_fraction usage_kb oldest_mxid next_mxid next_offset cat_a_relminmxid cat_b_relminmxid cat_c_relminmxid cat_d_relminmxid"
while true ; do
  date="` date +%H:%M:%S `"
  segments="` ls $PGDATA/pg_multixact/members | wc -l | sed 's/^ *//' `"
  usage_fraction="` echo \"scale=4; $segments/82040\" | bc `"
  usage_kb="` du -sk $PGDATA/pg_multixact | cut -f1 `"
  oldest_mxid="` $PGINSTALL/bin/pg_controldata -D ~/install/postgres/pg_data | grep 'oldestMultiXid' | cut -d' ' -f6 `"
  next_mxid="` $PGINSTALL/bin/pg_controldata -D ~/install/postgres/pg_data | grep 'NextMultiXactId' | cut -d' ' -f5 `"
  next_offset="` $PGINSTALL/bin/pg_controldata -D ~/install/postgres/pg_data | grep 'NextMultiOffset' | cut -d' ' -f5 `"
  cat_a_relminmxid="` $PGINSTALL/bin/psql postgres -tAc \"select relminmxid from pg_class where relname = 'cat_a'\" `"
  cat_b_relminmxid="` $PGINSTALL/bin/psql postgres -tAc \"select relminmxid from pg_class where relname = 'cat_b'\" `"
  cat_c_relminmxid="` $PGINSTALL/bin/psql postgres -tAc \"select relminmxid from pg_class where relname = 'cat_c'\" `"
  cat_d_relminmxid="` $PGINSTALL/bin/psql postgres -tAc \"select relminmxid from pg_class where relname = 'cat_d'\" `"
  echo "$date $segments $usage_fraction $usage_kb $oldest_mxid $next_mxid $next_offset $cat_a_relminmxid $cat_b_relminmxid $cat_c_relminmxid $cat_d_relminmxid"
  sleep 60
done
