Skip to content

Instantly share code, notes, and snippets.

@deric
Last active May 7, 2025 13:50
Show Gist options
  • Save deric/f10845cf4f9a3dfad9b9ea39461c8a3e to your computer and use it in GitHub Desktop.
Save deric/f10845cf4f9a3dfad9b9ea39461c8a3e to your computer and use it in GitHub Desktop.
PostgreSQL 14 -> 15 upgrade check script
#!/bin/bash
function -h {
cat <<USAGE
USAGE: Check postgresql database for pg_pltemplate modifcations. Sanity checks before upgrading 14 -> 15
-p / --port PostgreSQL port
-v / --verbose debugging output
./${0##*/}
USAGE
}; function --help { -h ;}
function msg { out "$*" >&1 ;}
function out { printf '%s\n' "$*" ;}
function err { local x=$? ; msg "$*" ;}
function main {
local verbose=false
local port=5432
while [[ $# -gt 0 ]]
do
case "$1" in # Munging globals, beware
-p|--port) port="$2"; shift 2 ;;
-v|--verbose) verbose=true; shift 1 ;;
*) err 'Argument error. Please see help: -h' ;;
esac
done
local error_found=false
if [[ $verbose == true ]]; then
set -ex
fi
for db in $(psql -tc "SELECT datname FROM pg_database;")
do
if [[ "${db}" != "template0" ]]; then
msg "Checking database ${db}"
local dump=$(pg_dump --port ${port} --schema-only --quote-all-identifiers ${db} | grep pg_start_backup)
if [ ! -z "$dump" ]; then
msg "Found incompatible statement: ${dump}"
err "ERROR: ${db} contains pg_start_backup reference. pg_upgrade will fail"
msg "HINT: Revert REVOKE given grants:"
local revoke=$(echo "${dump}" | awk '$1 ~ /^[A-Z]+/ {gsub("GRANT","REVOKE"); gsub("TO","FROM"); print}')
echo $revoke
error_found=true
fi
local dump=$(pg_dump --port ${port} --schema-only --quote-all-identifiers ${db} | grep pg_stop_backup)
if [ ! -z "$dump" ]; then
msg "Found incompatible statement: ${dump}"
err "ERROR: ${db} contains pg_start_backup reference. pg_upgrade will fail"
msg "HINT: Revert REVOKE given grants:"
local revoke=$(echo "${dump}" | awk '$1 ~ /^[A-Z]+/ {gsub("GRANT","REVOKE"); gsub("TO","FROM"); print}')
msg ${revoke}
error_found=true
fi
fi
done
if [ ${error_found} = "true" ]; then
msg "CRITICAL: upgrade to postgresql 15 will fail"
exit 1
else
msg "OK: no pg_pltemplate modifications were found"
exit 0
fi
}
if [[ ${1:-} ]] && declare -F | cut -d' ' -f3 | fgrep -qx -- "${1:-}"
then
case "$1" in
-h|--help) : ;;
*) ;;
esac
"$@"
else
main "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment