From c709d2b75e65742754b24acbdfe7face24bb77ec Mon Sep 17 00:00:00 2001 From: Justin Clift Date: Mon, 30 Sep 2024 21:48:01 +1000 Subject: [PATCH] Initial code (untested) for automatically reindexing the databases --- docker-entrypoint.sh | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 01aa5a8..8e53339 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -540,7 +540,7 @@ _main() { echo "Running pg_upgrade command, from $(pwd)" echo "---------------------------------------" bin_path=$(get_bin_path) - ${bin_path}/pg_upgrade --username="${POSTGRES_USER}" --link -d "${OLD}" -D "${NEW}" -b "${OLDPATH}/bin" -B "${bin_path}" --socketdir="/var/run/postgresql" + "${bin_path}/pg_upgrade" --username="${POSTGRES_USER}" --link -d "${OLD}" -D "${NEW}" -b "${OLDPATH}/bin" -B "${bin_path}" --socketdir="/var/run/postgresql" echo "--------------------------------------" echo "Running pg_upgrade command is complete" echo "--------------------------------------" @@ -572,6 +572,33 @@ _main() { echo "Removing left over database files is complete" echo "---------------------------------------------" + # Automatically reindex the databases (if requested) + if [ "${PGAUTO_REINDEX}" = "yes" ]; then + echo "------------------------" + echo "Reindexing the databases" + echo "------------------------" + + # Get the list of databases in the database cluster + DB_LIST=$(echo 'SELECT datname FROM pg_catalog.pg_database WHERE datistemplate IS FALSE' | postgres --single -D /var/lib/postgresql/data 2>bar | grep datname | grep -v backend | cut -d '"' -f 2) + + # For each database, reindex it + for DATABASE in ${DB_LIST}; do + echo "-------------------------------" + echo "Starting reindex of ${DATABASE}" + echo "-------------------------------" + + echo 'REINDEX DATABASE' | postgres --single -D /var/lib/postgresql/data "${DATABASE}" + + echo "-------------------------------" + echo "Finished reindex of ${DATABASE}" + echo "-------------------------------" + done + + echo "-------------------------------" + echo "End of reindexing the databases" + echo "-------------------------------" + fi + echo "**********************************************************" echo "Automatic upgrade process finished with no errors reported" echo "**********************************************************"