@@ -24,10 +24,38 @@ run_sql() {
24
24
psql -h localhost -U supabase_admin -d postgres -c " $STATEMENT "
25
25
}
26
26
27
+ function shutdown_services {
28
+ if [[ $( systemctl is-active gotrue) == " active" ]]; then
29
+ echo " stopping gotrue"
30
+ systemctl stop gotrue || true
31
+ fi
32
+
33
+ if [[ $( systemctl is-active postgrest) == " active" ]]; then
34
+ echo " stopping postgrest"
35
+ systemctl stop postgrest || true
36
+ fi
37
+ }
38
+
39
+ function start_services {
40
+ if [[ $( systemctl is-active gotrue) == " inactive" ]]; then
41
+ echo " starting gotrue"
42
+ systemctl start gotrue || true
43
+ fi
44
+
45
+ if [[ $( systemctl is-active postgrest) == " inactive" ]]; then
46
+ echo " starting postgrest"
47
+ systemctl start postgrest || true
48
+ fi
49
+ }
50
+
27
51
cleanup () {
28
52
UPGRADE_STATUS=${1:- " failed" }
29
53
EXIT_CODE=${?:- 0}
30
54
55
+ if [ -d " ${MOUNT_POINT} /pgdata/pg_upgrade_output.d/" ]; then
56
+ cp -R " ${MOUNT_POINT} /pgdata/pg_upgrade_output.d/" /var/log/
57
+ fi
58
+
31
59
if [ -L /var/lib/postgresql ]; then
32
60
rm /var/lib/postgresql
33
61
mv /var/lib/postgresql.bak /var/lib/postgresql
@@ -42,9 +70,8 @@ cleanup() {
42
70
done
43
71
44
72
run_sql " ALTER USER postgres WITH NOSUPERUSER;"
45
- if [ -d " ${MOUNT_POINT} /pgdata/pg_upgrade_output.d/" ]; then
46
- cp -R " ${MOUNT_POINT} /pgdata/pg_upgrade_output.d/" /var/log/
47
- fi
73
+
74
+ start_services
48
75
49
76
umount $MOUNT_POINT
50
77
echo " ${UPGRADE_STATUS} " > /tmp/pg-upgrade-status
@@ -53,11 +80,17 @@ cleanup() {
53
80
}
54
81
55
82
function initiate_upgrade {
56
- BLOCK_DEVICE=$( lsblk -dpno name | grep -v " /dev/nvme[0-1]" )
57
83
echo " running" > /tmp/pg-upgrade-status
58
84
85
+ shutdown_services
86
+
87
+ # awk NF==3 prints lines with exactly 3 fields, which are the block devices currently not mounted anywhere
88
+ # excluding nvme0 since it is the root disk
89
+ BLOCK_DEVICE=$( lsblk -dprno name,size,mountpoint,type | grep " disk" | grep -v " nvme0" | awk ' NF==3 { print $1; }' )
90
+
59
91
mkdir -p " $MOUNT_POINT "
60
92
mount " $BLOCK_DEVICE " " $MOUNT_POINT "
93
+ resize2fs " $BLOCK_DEVICE "
61
94
62
95
SHARED_PRELOAD_LIBRARIES=$( cat /etc/postgresql/postgresql.conf | grep shared_preload_libraries | sed " s/shared_preload_libraries = '\(.*\)'.*/\1/" )
63
96
PGDATAOLD=$( cat /etc/postgresql/postgresql.conf | grep data_directory | sed " s/data_directory = '\(.*\)'.*/\1/" )
@@ -73,6 +106,12 @@ function initiate_upgrade {
73
106
cp /root/pg_upgrade_pgsodium_getkey.sh " $PGSHARENEW /extension/pgsodium_getkey"
74
107
chmod +x " $PGSHARENEW /extension/pgsodium_getkey"
75
108
109
+ if [ -f " $MOUNT_POINT /pgsodium_root.key" ]; then
110
+ cp " $MOUNT_POINT /pgsodium_root.key" /etc/postgresql-custom/pgsodium_root.key
111
+ chown postgres:postgres /etc/postgresql-custom/pgsodium_root.key
112
+ chmod 600 /etc/postgresql-custom/pgsodium_root.key
113
+ fi
114
+
76
115
chown -R postgres:postgres " /tmp/pg_upgrade_bin/$PGVERSION "
77
116
78
117
for EXTENSION in " ${EXTENSIONS_TO_DISABLE[@]} " ; do
108
147
mv /var/lib/postgresql /var/lib/postgresql.bak
109
148
ln -s /tmp/pg_upgrade_bin/15/share /var/lib/postgresql
110
149
150
+ if [ ! -L /var/lib/postgresql.bak/data ]; then
151
+ if [ -L /var/lib/postgresql/data ]; then
152
+ rm /var/lib/postgresql/data
153
+ fi
154
+ ln -s /var/lib/postgresql.bak/data /var/lib/postgresql/data
155
+ fi
156
+
111
157
systemctl stop postgresql
112
158
su -c " $UPGRADE_COMMAND " -s $SHELL postgres
113
159
120
166
121
167
trap cleanup ERR
122
168
123
- initiate_upgrade >> /var/log/pg-upgrade-initiate.log 2>&1
124
- echo " Upgrade initiate job completed "
169
+ initiate_upgrade >> /var/log/pg-upgrade-initiate.log 2>&1 &
170
+ echo " Upgrade initiate job completed"
0 commit comments