--- /dev/null
+#! /bin/bash
+
+# Written by Peter Palfrader
+# Based on code by Tollef Fog Heen <tfheen@err.no> based on code by Peter Palfrader
+#
+# Restart all postgresql clusters when they do not have connections to them.
+
+set -e
+set -u
+
+# chk_net <port>
+# returns true (0) if there are connections to that port.
+chk_net() {
+ local port="$1"; shift
+ local con="$(ss -H -nt "sport = :$port" | wc -l)"
+ if [ "$con" -ge 1 ]; then
+ return 0
+ else
+ return 1
+ fi
+}
+
+get_pidfile_value() {
+ local version="$1"; shift
+ local name="$1"; shift
+
+ local fn="/run/postgresql/${version}-${name}.pid"
+ if [ -e "$fn" ] ; then
+ cat "$fn"
+ else
+ echo "No pidfile at $fn" >&2
+ exit 1
+ fi
+}
+
+handle_cluster() {
+ local version="$1"; shift
+ local name="$1"; shift
+ local port="$1"; shift
+
+ pid="$(get_pidfile_value "$version" "$name")"
+ while chk_net "$port" && [ "$pid" = "$(get_pidfile_value "$version" "$name")" ]; do
+ sleep 10
+ done
+ systemctl restart "postgresql@${version}-${name}.service"
+}
+
+pg_lsclusters -h | while read version name port status; do
+ echo "$status" | grep -q --word-regexp online || continue
+ handle_cluster $version $name $port &
+done
+
+wait
ensure => $ensure,
source => 'puppet:///modules/postgres/plugin.conf',
}
+ file { '/usr/local/sbin/dsa-restart-all-idle-postgres':
+ ensure => $ensure,
+ source => 'puppet:///modules/postgres/dsa-restart-idle-postgres',
+ mode => '0555',
+ }
}