Skip to content

Commit 1e29a31

Browse files
committed
chore: pg scripts update
1 parent f619af5 commit 1e29a31

File tree

4 files changed

+103
-19
lines changed

4 files changed

+103
-19
lines changed

ansible/files/admin_api_scripts/pg_upgrade_complete.sh

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,67 @@
66
## extensions, containing regtypes referencing system OIDs.
77

88
# Extensions to be reenabled after pg_upgrade.
9-
# Running an upgrade with these extensions enabled will result in errors due to
9+
# Running an upgrade with these extensions enabled will result in errors due to
1010
# them depending on regtypes referencing system OIDs. Thus they have been disabled
1111
# beforehand.
1212
EXTENSIONS_TO_REENABLE=(
1313
"pg_graphql"
1414
)
1515

16+
set -euo pipefail
1617

1718
run_sql() {
1819
STATEMENT=$1
1920
psql -h localhost -U supabase_admin -d postgres -c "$STATEMENT"
2021
}
2122

23+
cleanup() {
24+
UPGRADE_STATUS=${1:-"failed"}
25+
EXIT_CODE=${?:-0}
26+
27+
echo "${UPGRADE_STATUS}" > /tmp/pg-upgrade-status
28+
29+
exit $EXIT_CODE
30+
}
31+
2232
function complete_pg_upgrade {
33+
echo "running" > /tmp/pg-upgrade-status
34+
2335
mount -a -v
2436

2537
# copying custom configurations
2638
cp -R /data/conf/* /etc/postgresql-custom/
39+
chown -R postgres:postgres /var/lib/postgresql/data
40+
chown -R postgres:postgres /data/pgdata
2741

2842
service postgresql start
29-
su -c 'vacuumdb --all --analyze-in-stages' -s $SHELL postgres
3043

3144
for EXTENSION in "${EXTENSIONS_TO_REENABLE[@]}"; do
3245
run_sql "CREATE EXTENSION IF NOT EXISTS ${EXTENSION} CASCADE;"
3346
done
34-
35-
sleep 5
36-
service postgresql restart
3747

3848
sleep 5
3949
service postgresql restart
50+
51+
if [[ $(systemctl is-active gotrue) == "inactive" ]]; then
52+
echo "starting gotrue"
53+
systemctl start --no-block gotrue || true
54+
fi
55+
56+
if [[ $(systemctl is-active postgrest) == "inactive" ]]; then
57+
echo "starting postgrest"
58+
systemctl start --no-block postgrest || true
59+
fi
60+
61+
echo "Upgrade job completed"
4062
}
4163

42-
set -euo pipefail
64+
function start_vacuum_analyze {
65+
su -c 'vacuumdb --all --analyze-in-stages' -s $SHELL postgres
66+
cleanup "complete"
67+
}
68+
69+
trap cleanup ERR
4370

44-
complete_pg_upgrade >> /var/log/pg-upgrade-complete.log 2>&1
45-
echo "Upgrade job completed"
71+
complete_pg_upgrade >>/var/log/pg-upgrade-complete.log 2>&1
72+
start_vacuum_analyze >>/var/log/pg-upgrade-complete.log 2>&1 &

ansible/files/admin_api_scripts/pg_upgrade_initiate.sh

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,38 @@ run_sql() {
2424
psql -h localhost -U supabase_admin -d postgres -c "$STATEMENT"
2525
}
2626

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+
2751
cleanup() {
2852
UPGRADE_STATUS=${1:-"failed"}
2953
EXIT_CODE=${?:-0}
3054

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+
3159
if [ -L /var/lib/postgresql ]; then
3260
rm /var/lib/postgresql
3361
mv /var/lib/postgresql.bak /var/lib/postgresql
@@ -42,9 +70,8 @@ cleanup() {
4270
done
4371

4472
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
4875

4976
umount $MOUNT_POINT
5077
echo "${UPGRADE_STATUS}" > /tmp/pg-upgrade-status
@@ -53,11 +80,17 @@ cleanup() {
5380
}
5481

5582
function initiate_upgrade {
56-
BLOCK_DEVICE=$(lsblk -dpno name | grep -v "/dev/nvme[0-1]")
5783
echo "running" > /tmp/pg-upgrade-status
5884

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+
5991
mkdir -p "$MOUNT_POINT"
6092
mount "$BLOCK_DEVICE" "$MOUNT_POINT"
93+
resize2fs "$BLOCK_DEVICE"
6194

6295
SHARED_PRELOAD_LIBRARIES=$(cat /etc/postgresql/postgresql.conf | grep shared_preload_libraries | sed "s/shared_preload_libraries = '\(.*\)'.*/\1/")
6396
PGDATAOLD=$(cat /etc/postgresql/postgresql.conf | grep data_directory | sed "s/data_directory = '\(.*\)'.*/\1/")
@@ -73,6 +106,12 @@ function initiate_upgrade {
73106
cp /root/pg_upgrade_pgsodium_getkey.sh "$PGSHARENEW/extension/pgsodium_getkey"
74107
chmod +x "$PGSHARENEW/extension/pgsodium_getkey"
75108

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+
76115
chown -R postgres:postgres "/tmp/pg_upgrade_bin/$PGVERSION"
77116

78117
for EXTENSION in "${EXTENSIONS_TO_DISABLE[@]}"; do
@@ -108,6 +147,13 @@ EOF
108147
mv /var/lib/postgresql /var/lib/postgresql.bak
109148
ln -s /tmp/pg_upgrade_bin/15/share /var/lib/postgresql
110149

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+
111157
systemctl stop postgresql
112158
su -c "$UPGRADE_COMMAND" -s $SHELL postgres
113159

@@ -120,5 +166,5 @@ EOF
120166

121167
trap cleanup ERR
122168

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"
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
#! /usr/bin/env bash
2-
## This script is runs in advance of the database version upgrade, on the newly
3-
## launched instance which will eventually be promoted to become the primary
2+
## This script is runs in advance of the database version upgrade, on the newly
3+
## launched instance which will eventually be promoted to become the primary
44
## database instance once the upgrade successfully completes, terminating the
55
## previous (source) instance.
6-
## The following commands safely stop the Postgres service and unmount
6+
## The following commands safely stop the Postgres service and unmount
77
## the data disk off the newly launched instance, to be re-attached to the
88
## source instance and run the upgrade there.
99

1010
set -euo pipefail
1111

12+
if [[ $(systemctl is-active gotrue) == "active" ]]; then
13+
echo "stopping gotrue"
14+
systemctl stop gotrue || true
15+
fi
16+
17+
if [[ $(systemctl is-active postgrest) == "active" ]]; then
18+
echo "stopping postgrest"
19+
systemctl stop postgrest || true
20+
fi
21+
1222
systemctl stop postgresql
13-
umount /data
1423

24+
cp /etc/postgresql-custom/pgsodium_root.key /data/pgsodium_root.key
25+
umount /data

common.vars.pkr.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
postgres-version = "15.1.0.27"
1+
postgres-version = "15.1.0.28"

0 commit comments

Comments
 (0)