Skip to content

Commit 5fdac5b

Browse files
committed
chore: patch pg_cron post-upgrade; generate locales pre-upgrade; remove libpq symlinking
1 parent 2d0bb05 commit 5fdac5b

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ function cleanup {
2424
exit "$EXIT_CODE"
2525
}
2626

27+
function execute_patches {
28+
# Patching pg_cron ownership as it resets during upgrade
29+
RESULT=$(run_sql -A -t -c "select count(*) > 0 as pg_is_owner from pg_extension where extname = 'pg_cron' and extowner::regrole::text = 'postgres';")
30+
31+
if [ "$RESULT" = "t" ]; then
32+
QUERY=$(cat <<EOF
33+
begin;
34+
create temporary table cron_job as select * from cron.job;
35+
create temporary table cron_job_run_details as select * from cron.job_run_details;
36+
drop extension pg_cron;
37+
create extension pg_cron schema pg_catalog;
38+
insert into cron.job select * from cron_job;
39+
insert into cron.job_run_details select * from cron_job_run_details;
40+
select setval('cron.jobid_seq', coalesce(max(jobid), 0) + 1, false) from cron.job;
41+
select setval('cron.runid_seq', coalesce(max(runid), 0) + 1, false) from cron.job_run_details;
42+
update cron.job set username = 'postgres' where username = 'supabase_admin';
43+
commit;
44+
EOF
45+
)
46+
47+
run_sql -c "$QUERY"
48+
fi
49+
}
50+
2751
function complete_pg_upgrade {
2852
if [ -f /tmp/pg-upgrade-status ]; then
2953
echo "Upgrade job already started. Bailing."
@@ -45,9 +69,12 @@ function complete_pg_upgrade {
4569
echo "4. Running generated SQL files"
4670
retry 3 run_generated_sql
4771

72+
echo "4.1. Applying patches"
73+
execute_patches || true
74+
4875
run_sql -c "ALTER USER postgres WITH NOSUPERUSER;"
4976

50-
echo "4.1. Applying authentication scheme updates"
77+
echo "4.2. Applying authentication scheme updates"
5178
retry 3 apply_auth_scheme_updates
5279

5380
sleep 5

ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ cleanup() {
8585
fi
8686
fi
8787

88-
if [ -f "/usr/lib/postgresql/lib/aarch64/libpq.so.5.bak" ]; then
89-
rm /usr/lib/postgresql/lib/aarch64/libpq.so.5
90-
mv /usr/lib/postgresql/lib/aarch64/libpq.so.5.bak /usr/lib/postgresql/lib/aarch64/libpq.so.5
91-
fi
92-
9388
if [ "$IS_DRY_RUN" = false ]; then
9489
echo "Restarting postgresql"
9590
systemctl enable postgresql
@@ -183,16 +178,6 @@ function initiate_upgrade {
183178

184179
chown -R postgres:postgres "/tmp/pg_upgrade_bin/$PGVERSION"
185180

186-
# Make latest libpq available to pg_upgrade
187-
mkdir -p /usr/lib/aarch64-linux-gnu
188-
if [ -f "/usr/lib/aarch64-linux-gnu/libpq.so.5" ]; then
189-
mv /usr/lib/aarch64-linux-gnu/libpq.so.5 /usr/lib/aarch64-linux-gnu/libpq.so.5.bak
190-
fi
191-
if [ -f "${PG_UPGRADE_BIN_DIR}/libpq.so.5" ]; then
192-
cp "${PG_UPGRADE_BIN_DIR}/libpq.so.5" "${PGLIBNEW}/libpq.so.5"
193-
fi
194-
ln -s "${PGLIBNEW}/libpq.so.5" /usr/lib/aarch64-linux-gnu/libpq.so.5
195-
196181
# upgrade job outputs a log in the cwd; needs write permissions
197182
mkdir -p /tmp/pg_upgrade/
198183
chown -R postgres:postgres /tmp/pg_upgrade/
@@ -208,15 +193,21 @@ function initiate_upgrade {
208193
apt-get update && apt --fix-broken install -y libprotobuf-c1
209194
fi
210195

196+
echo "4. Setup locale if required"
197+
if [[ ! "$(cat /etc/locale.gen | grep "^en_US.UTF-8")" ]]; then
198+
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
199+
locale-gen
200+
fi
201+
211202
if [ "$IS_DRY_RUN" = false ]; then
212203
# awk NF==3 prints lines with exactly 3 fields, which are the block devices currently not mounted anywhere
213204
# excluding nvme0 since it is the root disk
214-
echo "4. Determining block device to mount"
205+
echo "5. Determining block device to mount"
215206
BLOCK_DEVICE=$(lsblk -dprno name,size,mountpoint,type | grep "disk" | grep -v "nvme0" | awk 'NF==3 { print $1; }')
216207
echo "Block device found: $BLOCK_DEVICE"
217208

218209
mkdir -p "$MOUNT_POINT"
219-
echo "5. Mounting block device"
210+
echo "6. Mounting block device"
220211

221212
sleep 5
222213
e2fsck -pf "$BLOCK_DEVICE"
@@ -234,10 +225,10 @@ function initiate_upgrade {
234225
chmod 600 /etc/postgresql-custom/pgsodium_root.key
235226
fi
236227

237-
echo "6. Disabling extensions and generating post-upgrade script"
228+
echo "7. Disabling extensions and generating post-upgrade script"
238229
handle_extensions
239230

240-
echo "7. Granting SUPERUSER to postgres user"
231+
echo "8. Granting SUPERUSER to postgres user"
241232
run_sql -c "ALTER USER postgres WITH SUPERUSER;"
242233

243234
if [ -d "/usr/share/postgresql/${PGVERSION}" ]; then
@@ -275,7 +266,7 @@ function initiate_upgrade {
275266

276267
export LD_LIBRARY_PATH="${PGLIBNEW}"
277268

278-
echo "8. Creating new data directory, initializing database"
269+
echo "9. Creating new data directory, initializing database"
279270
chown -R postgres:postgres "$MOUNT_POINT/"
280271
rm -rf "${PGDATANEW:?}/"
281272
su -c "$PGBINNEW/initdb -L $PGSHARENEW -D $PGDATANEW/" -s "$SHELL" postgres
@@ -297,7 +288,7 @@ EOF
297288
if [ "$IS_DRY_RUN" = true ]; then
298289
UPGRADE_COMMAND="$UPGRADE_COMMAND --check"
299290
else
300-
echo "9. Stopping postgres; running pg_upgrade"
291+
echo "10. Stopping postgres; running pg_upgrade"
301292

302293
# Extra work to ensure postgres is actually stopped
303294
# Mostly needed for PG12 projects with odd systemd unit behavior
@@ -312,20 +303,20 @@ EOF
312303
su -c "$UPGRADE_COMMAND" -s "$SHELL" postgres
313304

314305
# copying custom configurations
315-
echo "10. Copying custom configurations"
306+
echo "11. Copying custom configurations"
316307
mkdir -p "$MOUNT_POINT/conf"
317308
cp -R /etc/postgresql-custom/* "$MOUNT_POINT/conf/"
318309

319310
# removing wal-g config as to allow it to be explicitly enabled on the new instance
320311
rm -f "$MOUNT_POINT/conf/wal-g.conf"
321312

322313
# copy sql files generated by pg_upgrade
323-
echo "11. Copying sql files generated by pg_upgrade"
314+
echo "12. Copying sql files generated by pg_upgrade"
324315
mkdir -p "$MOUNT_POINT/sql"
325316
cp /tmp/pg_upgrade/*.sql "$MOUNT_POINT/sql/" || true
326317
chown -R postgres:postgres "$MOUNT_POINT/sql/"
327318

328-
echo "12. Cleaning up"
319+
echo "13. Cleaning up"
329320
cleanup "complete"
330321
}
331322

0 commit comments

Comments
 (0)