]> git.nbdom.net Git - nb.git/commitdiff
pg_count
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 10 Dec 2014 22:45:21 +0000 (22:45 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 10 Dec 2014 22:45:21 +0000 (22:45 +0000)
bin/pg_count [new file with mode: 0755]

diff --git a/bin/pg_count b/bin/pg_count
new file mode 100755 (executable)
index 0000000..890c7c7
--- /dev/null
@@ -0,0 +1,25 @@
+#!/bin/bash
+# NB affiche les tables avec leur nombre d'enregistrement d'une base de donne
+# postgres
+dbs="$@"
+[ -z "$dbs" ] && dbs=`psql -l | perl -ane 'print $F[0]."\n" if $i++>2 and /^\s*\w/ and !/^\s*template\d/'`
+for db in $dbs; do
+
+       [ "$db" == "" ] && db=`whoami`
+
+       echo "* $db"
+
+       tables=`psql $db -Atc '\dt' | grep -v '^No relations found' | cut -d\| -f2`
+       length=`echo "$tables" | perl -pe 's/^(.*)$/length($1)/e' | sort -n -r | head -1`
+  (
+       for tb in $tables; do
+
+               echo "SELECT '$tb',count(*) FROM $tb;"
+
+       done
+  ) | psql -At $db | awk -F'|' '{printf "%-'$length's %9d\n",$1,$2}'
+
+  echo
+       shift
+
+done