2015-09-28 19:45:35 +02:00
|
|
|
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
set -x
|
|
|
|
|
2015-11-10 07:16:41 +01:00
|
|
|
vergte() {
|
|
|
|
[ "$1" = "`echo -e "$1\n$2" | sort -V | tail -n1`" ]
|
|
|
|
}
|
|
|
|
|
2015-09-28 19:45:35 +02:00
|
|
|
cd /
|
|
|
|
if [ "$EUID" -eq 0 ]; then
|
|
|
|
version=$(su postgres -c 'psql -A -t -d postgres -c "show server_version"')
|
|
|
|
else
|
|
|
|
version=$(psql -A -t -d postgres -c "show server_version")
|
|
|
|
fi
|
|
|
|
|
|
|
|
major=$(echo $version | cut -d. -f1,2)
|
|
|
|
username=$1
|
2015-11-10 07:16:41 +01:00
|
|
|
|
2015-09-28 19:45:35 +02:00
|
|
|
shift
|
|
|
|
tables=$(echo "'$@'" | sed "s/ /','/g")
|
2015-11-10 07:16:41 +01:00
|
|
|
if vergte "$major" "9.2"; then
|
2015-09-28 19:45:35 +02:00
|
|
|
pidname="pid"
|
|
|
|
else
|
|
|
|
pidname="procpid"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$EUID" -eq 0 ]; then
|
|
|
|
su postgres -c psql postgres postgres <<EOF
|
|
|
|
SELECT pg_terminate_backend($pidname) FROM pg_stat_activity WHERE datname IN ($tables);
|
|
|
|
EOF
|
|
|
|
else
|
|
|
|
psql -h localhost postgres "$username" <<EOF
|
|
|
|
SELECT pg_terminate_backend($pidname) FROM pg_stat_activity WHERE datname IN ($tables);
|
|
|
|
EOF
|
|
|
|
fi
|