2016-01-12 13:08:43 +01:00
|
|
|
#!/usr/bin/env bash
|
2015-09-28 19:45:35 +02:00
|
|
|
set -e
|
|
|
|
|
2015-11-10 07:16:41 +01:00
|
|
|
vergte() {
|
2018-08-03 02:14:48 +02:00
|
|
|
[ "$1" = "$(echo -e "$1\\n$2" | sort -V | tail -n1)" ]
|
2015-11-10 07:16:41 +01:00
|
|
|
}
|
|
|
|
|
2016-01-12 13:08:43 +01:00
|
|
|
DEFAULT_USER="postgres"
|
|
|
|
if [ "$(uname)" = "OpenBSD" ]; then
|
|
|
|
DEFAULT_USER="_postgresql"
|
|
|
|
fi
|
|
|
|
|
2015-09-28 19:45:35 +02:00
|
|
|
cd /
|
|
|
|
if [ "$EUID" -eq 0 ]; then
|
2016-01-12 13:08:43 +01:00
|
|
|
version=$(sudo -u "$DEFAULT_USER" sh -c 'psql -A -t -d postgres -c "show server_version"')
|
2015-09-28 19:45:35 +02:00
|
|
|
else
|
|
|
|
version=$(psql -A -t -d postgres -c "show server_version")
|
|
|
|
fi
|
|
|
|
|
2018-08-03 02:14:48 +02:00
|
|
|
major=$(echo "$version" | cut -d. -f1,2)
|
2015-09-28 19:45:35 +02:00
|
|
|
username=$1
|
2015-11-10 07:16:41 +01:00
|
|
|
|
2015-09-28 19:45:35 +02:00
|
|
|
shift
|
2018-08-03 02:14:48 +02:00
|
|
|
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
|
2016-02-18 02:48:20 +01:00
|
|
|
sudo -u "$DEFAULT_USER" sh -c "psql postgres '$DEFAULT_USER'" <<EOF
|
2015-09-28 19:45:35 +02:00
|
|
|
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
|