Skip to content

Commit 784f998

Browse files
authored
chore: update pg upgrade scripts (#485)
* chore: update pg upgrade scripts
1 parent ee6a90d commit 784f998

File tree

4 files changed

+84
-25
lines changed

4 files changed

+84
-25
lines changed

ansible/files/admin_api_scripts/pg_upgrade_complete.sh

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,70 @@
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 -eEuo pipefail
1617

1718
run_sql() {
18-
STATEMENT=$1
19-
psql -h localhost -U supabase_admin -d postgres -c "$STATEMENT"
19+
psql -h localhost -U supabase_admin -d postgres "$@"
20+
}
21+
22+
cleanup() {
23+
UPGRADE_STATUS=${1:-"failed"}
24+
EXIT_CODE=${?:-0}
25+
26+
echo "${UPGRADE_STATUS}" > /tmp/pg-upgrade-status
27+
28+
exit $EXIT_CODE
2029
}
2130

2231
function complete_pg_upgrade {
32+
if [ -f /tmp/pg-upgrade-status ]; then
33+
echo "Upgrade job already started. Bailing."
34+
exit 0
35+
fi
36+
37+
echo "running" > /tmp/pg-upgrade-status
38+
2339
mount -a -v
2440

2541
# copying custom configurations
2642
cp -R /data/conf/* /etc/postgresql-custom/
43+
chown -R postgres:postgres /var/lib/postgresql/data
44+
chown -R postgres:postgres /data/pgdata
2745

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

3148
for EXTENSION in "${EXTENSIONS_TO_REENABLE[@]}"; do
32-
run_sql "CREATE EXTENSION IF NOT EXISTS ${EXTENSION} CASCADE;"
49+
run_sql -c "CREATE EXTENSION IF NOT EXISTS ${EXTENSION} CASCADE;"
3350
done
34-
35-
sleep 5
36-
service postgresql restart
51+
52+
if [ -d /data/sql ]; then
53+
for FILE in /data/sql/*.sql; do
54+
if [ -f "$FILE" ]; then
55+
run_sql -f $FILE
56+
fi
57+
done
58+
fi
3759

3860
sleep 5
3961
service postgresql restart
62+
63+
start_vacuum_analyze
64+
65+
echo "Upgrade job completed"
66+
}
67+
68+
function start_vacuum_analyze {
69+
su -c 'vacuumdb --all --analyze-in-stages' -s $SHELL postgres
70+
cleanup "complete"
4071
}
4172

42-
set -euo pipefail
73+
trap cleanup ERR
4374

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

ansible/files/admin_api_scripts/pg_upgrade_initiate.sh

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ cleanup() {
2828
UPGRADE_STATUS=${1:-"failed"}
2929
EXIT_CODE=${?:-0}
3030

31+
if [ -d "${MOUNT_POINT}/pgdata/pg_upgrade_output.d/" ]; then
32+
cp -R "${MOUNT_POINT}/pgdata/pg_upgrade_output.d/" /var/log/
33+
fi
34+
3135
if [ -L /var/lib/postgresql ]; then
3236
rm /var/lib/postgresql
3337
mv /var/lib/postgresql.bak /var/lib/postgresql
@@ -42,9 +46,6 @@ cleanup() {
4246
done
4347

4448
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
4849

4950
umount $MOUNT_POINT
5051
echo "${UPGRADE_STATUS}" > /tmp/pg-upgrade-status
@@ -53,11 +54,19 @@ cleanup() {
5354
}
5455

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

59+
# awk NF==3 prints lines with exactly 3 fields, which are the block devices currently not mounted anywhere
60+
# excluding nvme0 since it is the root disk
61+
BLOCK_DEVICE=$(lsblk -dprno name,size,mountpoint,type | grep "disk" | grep -v "nvme0" | awk 'NF==3 { print $1; }')
62+
63+
if [ -x "$(command -v blockdev)" ]; then
64+
blockdev --rereadpt "$BLOCK_DEVICE"
65+
fi
66+
5967
mkdir -p "$MOUNT_POINT"
6068
mount "$BLOCK_DEVICE" "$MOUNT_POINT"
69+
resize2fs "$BLOCK_DEVICE"
6170

6271
SHARED_PRELOAD_LIBRARIES=$(cat /etc/postgresql/postgresql.conf | grep shared_preload_libraries | sed "s/shared_preload_libraries = '\(.*\)'.*/\1/")
6372
PGDATAOLD=$(cat /etc/postgresql/postgresql.conf | grep data_directory | sed "s/data_directory = '\(.*\)'.*/\1/")
@@ -73,6 +82,12 @@ function initiate_upgrade {
7382
cp /root/pg_upgrade_pgsodium_getkey.sh "$PGSHARENEW/extension/pgsodium_getkey"
7483
chmod +x "$PGSHARENEW/extension/pgsodium_getkey"
7584

85+
if [ -f "$MOUNT_POINT/pgsodium_root.key" ]; then
86+
cp "$MOUNT_POINT/pgsodium_root.key" /etc/postgresql-custom/pgsodium_root.key
87+
chown postgres:postgres /etc/postgresql-custom/pgsodium_root.key
88+
chmod 600 /etc/postgresql-custom/pgsodium_root.key
89+
fi
90+
7691
chown -R postgres:postgres "/tmp/pg_upgrade_bin/$PGVERSION"
7792

7893
for EXTENSION in "${EXTENSIONS_TO_DISABLE[@]}"; do
@@ -90,7 +105,9 @@ function initiate_upgrade {
90105
WORKERS=$(nproc | awk '{ print ($1 == 1 ? 1 : $1 - 1) }')
91106

92107
# upgrade job outputs a log in the cwd; needs write permissions
93-
cd /tmp
108+
mkdir -p /tmp/pg_upgrade
109+
chown -R postgres:postgres /tmp/pg_upgrade
110+
cd /tmp/pg_upgrade
94111

95112
UPGRADE_COMMAND=$(cat <<EOF
96113
time ${PGBINNEW}/pg_upgrade \
@@ -108,17 +125,28 @@ EOF
108125
mv /var/lib/postgresql /var/lib/postgresql.bak
109126
ln -s /tmp/pg_upgrade_bin/15/share /var/lib/postgresql
110127

128+
if [ ! -L /var/lib/postgresql.bak/data ]; then
129+
if [ -L /var/lib/postgresql/data ]; then
130+
rm /var/lib/postgresql/data
131+
fi
132+
ln -s /var/lib/postgresql.bak/data /var/lib/postgresql/data
133+
fi
134+
111135
systemctl stop postgresql
112136
su -c "$UPGRADE_COMMAND" -s $SHELL postgres
113137

114138
# copying custom configurations
115-
mkdir -p $MOUNT_POINT/conf
116-
cp -R /etc/postgresql-custom/* $MOUNT_POINT/conf/
139+
mkdir -p "$MOUNT_POINT/conf"
140+
cp -R /etc/postgresql-custom/* "$MOUNT_POINT/conf/"
141+
142+
# copy sql files generated by pg_upgrade
143+
mkdir -p "$MOUNT_POINT/sql"
144+
cp /tmp/pg_upgrade/*.sql "$MOUNT_POINT/sql/" || true
117145

118146
cleanup "complete"
119147
}
120148

121149
trap cleanup ERR
122150

123-
initiate_upgrade >> /var/log/pg-upgrade-initiate.log 2>&1
124-
echo "Upgrade initiate job completed "
151+
initiate_upgrade >> /var/log/pg-upgrade-initiate.log 2>&1 &
152+
echo "Upgrade initiate job completed"
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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

1212
systemctl stop postgresql
13-
umount /data
1413

14+
cp /etc/postgresql-custom/pgsodium_root.key /data/pgsodium_root.key
15+
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.33"
1+
postgres-version = "15.1.0.35"

0 commit comments

Comments
 (0)