terminate-psql-sessions: Remove postgres <9.2 support.

Those older versions of postgres reached end-of-life upstream, and it
makes this shell script easier to read.
This commit is contained in:
Tim Abbott 2018-08-09 15:24:22 -07:00
parent 4c4b6d105e
commit 460ea76d96
1 changed files with 2 additions and 11 deletions

View File

@ -1,10 +1,6 @@
#!/usr/bin/env bash
set -e
vergte() {
[ "$1" = "$(echo -e "$1\\n$2" | sort -V | tail -n1)" ]
}
DEFAULT_USER="postgres"
if [ "$(uname)" = "OpenBSD" ]; then
DEFAULT_USER="_postgresql"
@ -22,18 +18,13 @@ username=$1
shift
tables=$(echo "'$*'" | sed "s/ /','/g")
if vergte "$major" "9.2"; then
pidname="pid"
else
pidname="procpid"
fi
if [ "$EUID" -eq 0 ]; then
sudo -u "$DEFAULT_USER" sh -c "psql postgres '$DEFAULT_USER'" <<EOF
SELECT pg_terminate_backend($pidname) FROM pg_stat_activity WHERE datname IN ($tables);
SELECT pg_terminate_backend(pid) 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);
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname IN ($tables);
EOF
fi