-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpgautoupgrade-postupgrade.sh
executable file
·86 lines (70 loc) · 2.78 KB
/
pgautoupgrade-postupgrade.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env bash
set -e
if [ $# -ne 3 ]; then
echo "Required number of arguments not passed to post upgrade script. 3 expected, $# received"
exit 1
fi
PGDATA=$1
POSTGRES_DB=$2
PGAUTO_ONESHOT=$3
# Wait for PostgreSQL to start and become available
COUNT=0
RUNNING=1
while [ $RUNNING -ne 0 ] && [ $COUNT -le 20 ]; do
# Check if PostgreSQL is running yet
echo "------ Checking if PostgreSQL is running, loop count ${COUNT} ------"
set +e
pg_isready -q
RUNNING=$?
set -e
if [ $RUNNING -eq 0 ]; then
echo "PostgreSQL is running. Post upgrade tasks will start shortly"
else
echo "PostgreSQL is not yet running, lets wait then try again..."
sleep 3
fi
COUNT=$((COUNT+1))
done
if [ $RUNNING -ne 0 ]; then
echo "PostgreSQL did not start before timeout expired"
exit 2
fi
# Get the list of databases in the database cluster
DB_LIST=$(echo 'SELECT datname FROM pg_catalog.pg_database WHERE datistemplate IS FALSE' | psql --username="${POSTGRES_USER}" -1t --csv "${POSTGRES_DB}")
# Update query planner statistics
echo "----------------------------"
echo "Updating query planner stats"
echo "----------------------------"
for DATABASE in ${DB_LIST}; do
echo "VACUUM (ANALYZE, VERBOSE, INDEX_CLEANUP FALSE)" | psql --username="${POSTGRES_USER}" -t --csv "${DATABASE}"
done
echo "-------------------------------------"
echo "Finished updating query planner stats"
echo "-------------------------------------"
if [ "x${PGAUTO_REINDEX}" != "xno" ]; then
# Reindex the databases
echo "------------------------"
echo "Reindexing the databases"
echo "------------------------"
if [[ "$PGTARGET" -le 15 ]]; then
reindexdb --all --username="${POSTGRES_USER}"
else
reindexdb --all --concurrently --username="${POSTGRES_USER}"
fi
echo "-------------------------------"
echo "End of reindexing the databases"
echo "-------------------------------"
fi
# If "one shot" mode was requested, then shut down PostgreSQL
if [ "x${PGAUTO_ONESHOT}" = "xyes" ]; then
echo "****************************************************************************************************"
echo "'One shot' automatic upgrade was requested, so exiting now that the post upgrade tasks have finished"
echo "****************************************************************************************************"
pg_ctl stop -D "${PGDATA}"
else
echo "*************************************************************************************************"
echo "Post upgrade tasks have finished successfully. PostgreSQL should now be fully updated and online"
echo "*************************************************************************************************"
fi
# Run a sync before exiting, just to ensure everything is flushed to disk before docker terminates the process
sync