Skip to content

Commit 16cd5c9

Browse files
authored
chore: update pg_upgrade initiate.sh to support nix-based upgrades (#1057)
1 parent 12852b2 commit 16cd5c9

File tree

2 files changed

+80
-18
lines changed

2 files changed

+80
-18
lines changed

ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,20 @@ function apply_auth_scheme_updates {
161161

162162
function start_vacuum_analyze {
163163
echo "complete" > /tmp/pg-upgrade-status
164-
su -c 'vacuumdb --all --analyze-in-stages' -s "$SHELL" postgres
164+
if ! command -v nix &> /dev/null; then
165+
su -c 'vacuumdb --all --analyze-in-stages' -s "$SHELL" postgres
166+
else
167+
su -c '. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && vacuumdb --all --analyze-in-stages' -s "$SHELL" postgres
168+
fi
165169
echo "Upgrade job completed"
166170
}
167171

168172
trap cleanup ERR
169173

174+
echo "C.UTF-8 UTF-8" > /etc/locale.gen
175+
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
176+
locale-gen
177+
170178
if [ -z "$IS_CI" ]; then
171179
complete_pg_upgrade >> $LOG_FILE 2>&1 &
172180
else

ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ SCRIPT_DIR=$(dirname -- "$0";)
2929
source "$SCRIPT_DIR/common.sh"
3030

3131
IS_CI=${IS_CI:-}
32-
LOG_FILE="/var/log/pg-upgrade-initiate.log"
32+
IS_LOCAL_UPGRADE=${IS_LOCAL_UPGRADE:-}
33+
IS_NIX_UPGRADE=${IS_NIX_UPGRADE:-}
34+
IS_NIX_BASED_SYSTEM="false"
3335

3436
PGVERSION=$1
3537
MOUNT_POINT="/data_migration"
38+
LOG_FILE="/var/log/pg-upgrade-initiate.log"
3639

3740
POST_UPGRADE_EXTENSION_SCRIPT="/tmp/pg_upgrade/pg_upgrade_extensions.sql"
3841
OLD_PGVERSION=$(run_sql -A -t -c "SHOW server_version;")
@@ -41,6 +44,15 @@ POSTGRES_CONFIG_PATH="/etc/postgresql/postgresql.conf"
4144
PGBINOLD="/usr/lib/postgresql/bin"
4245
PGLIBOLD="/usr/lib/postgresql/lib"
4346

47+
PG_UPGRADE_BIN_DIR="/tmp/pg_upgrade_bin/$PGVERSION"
48+
49+
if [ -L "$PGBINOLD/pg_upgrade" ]; then
50+
BINARY_PATH=$(readlink -f "$PGBINOLD/pg_upgrade")
51+
if [[ "$BINARY_PATH" == *"nix"* ]]; then
52+
IS_NIX_BASED_SYSTEM="true"
53+
fi
54+
fi
55+
4456
# If upgrading from older major PG versions, disable specific extensions
4557
if [[ "$OLD_PGVERSION" =~ ^14.* ]]; then
4658
EXTENSIONS_TO_DISABLE+=("${PG14_EXTENSIONS_TO_DISABLE[@]}")
@@ -107,7 +119,7 @@ cleanup() {
107119
echo "Removing SUPERUSER grant from postgres"
108120
run_sql -c "ALTER USER postgres WITH NOSUPERUSER;"
109121

110-
if [ -z "$IS_CI" ]; then
122+
if [ -z "$IS_CI" ] && [ -z "$IS_LOCAL_UPGRADE" ]; then
111123
echo "Unmounting data disk from ${MOUNT_POINT}"
112124
umount $MOUNT_POINT
113125
fi
@@ -175,18 +187,50 @@ function initiate_upgrade {
175187
PGDATAOLD=$(cat "$POSTGRES_CONFIG_PATH" | grep data_directory | sed "s/data_directory = '\(.*\)'.*/\1/")
176188

177189
PGDATANEW="$MOUNT_POINT/pgdata"
178-
PG_UPGRADE_BIN_DIR="/tmp/pg_upgrade_bin/$PGVERSION"
179-
PGBINNEW="$PG_UPGRADE_BIN_DIR/bin"
180-
PGLIBNEW="$PG_UPGRADE_BIN_DIR/lib"
181-
PGSHARENEW="$PG_UPGRADE_BIN_DIR/share"
182190

183191
# running upgrade using at least 1 cpu core
184192
WORKERS=$(nproc | awk '{ print ($1 == 1 ? 1 : $1 - 1) }')
193+
194+
# To make nix-based upgrades work for testing, create a pg binaries tarball with the following contents:
195+
# - nix_flake_version - a7189a68ed4ea78c1e73991b5f271043636cf074
196+
# Where the value is the commit hash of the nix flake that contains the binaries
197+
198+
if [ -n "$IS_LOCAL_UPGRADE" ]; then
199+
mkdir -p "$PG_UPGRADE_BIN_DIR"
200+
mkdir -p /tmp/persistent/
201+
echo "a7189a68ed4ea78c1e73991b5f271043636cf074" > "$PG_UPGRADE_BIN_DIR/nix_flake_version"
202+
tar -czf "/tmp/persistent/pg_upgrade_bin.tar.gz" -C "/tmp/pg_upgrade_bin" .
203+
rm -rf /tmp/pg_upgrade_bin/
204+
fi
185205

186206
echo "1. Extracting pg_upgrade binaries"
187207
mkdir -p "/tmp/pg_upgrade_bin"
188208
tar zxf "/tmp/persistent/pg_upgrade_bin.tar.gz" -C "/tmp/pg_upgrade_bin"
189209

210+
PGSHARENEW="$PG_UPGRADE_BIN_DIR/share"
211+
212+
if [ -f "$PG_UPGRADE_BIN_DIR/nix_flake_version" ]; then
213+
IS_NIX_UPGRADE="true"
214+
NIX_FLAKE_VERSION=$(cat "$PG_UPGRADE_BIN_DIR/nix_flake_version")
215+
216+
if [ "$IS_NIX_BASED_SYSTEM" = "false" ]; then
217+
echo "1.1. Nix is not installed; installing."
218+
219+
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --no-confirm \
220+
--extra-conf "substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com" \
221+
--extra-conf "trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI=% cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
222+
fi
223+
224+
echo "1.2. Installing flake revision: $NIX_FLAKE_VERSION"
225+
# shellcheck disable=SC1091
226+
source /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
227+
PG_UPGRADE_BIN_DIR=$(nix build "github:supabase/postgres/${NIX_FLAKE_VERSION}#psql_15/bin" --no-link --print-out-paths --extra-experimental-features nix-command --extra-experimental-features flakes)
228+
PGSHARENEW="$PG_UPGRADE_BIN_DIR/share/postgresql"
229+
fi
230+
231+
PGBINNEW="$PG_UPGRADE_BIN_DIR/bin"
232+
PGLIBNEW="$PG_UPGRADE_BIN_DIR/lib"
233+
190234
# copy upgrade-specific pgsodium_getkey script into the share dir
191235
chmod +x "$SCRIPT_DIR/pgsodium_getkey.sh"
192236
mkdir -p "$PGSHARENEW/extension"
@@ -220,7 +264,7 @@ function initiate_upgrade {
220264
locale-gen
221265
fi
222266

223-
if [ -z "$IS_CI" ]; then
267+
if [ -z "$IS_CI" ] && [ -z "$IS_LOCAL_UPGRADE" ]; then
224268
# awk NF==3 prints lines with exactly 3 fields, which are the block devices currently not mounted anywhere
225269
# excluding nvme0 since it is the root disk
226270
echo "5. Determining block device to mount"
@@ -254,13 +298,17 @@ function initiate_upgrade {
254298
echo "8. Granting SUPERUSER to postgres user"
255299
run_sql -c "ALTER USER postgres WITH SUPERUSER;"
256300

257-
if [ -d "/usr/share/postgresql/${PGVERSION}" ]; then
258-
mv "/usr/share/postgresql/${PGVERSION}" "/usr/share/postgresql/${PGVERSION}.bak"
259-
fi
260-
ln -s "$PGSHARENEW" "/usr/share/postgresql/${PGVERSION}"
301+
if [ -z "$IS_NIX_UPGRADE" ]; then
302+
if [ -d "/usr/share/postgresql/${PGVERSION}" ]; then
303+
mv "/usr/share/postgresql/${PGVERSION}" "/usr/share/postgresql/${PGVERSION}.bak"
304+
fi
305+
306+
ln -s "$PGSHARENEW" "/usr/share/postgresql/${PGVERSION}"
307+
cp --remove-destination "$PGLIBNEW"/*.control "$PGSHARENEW/extension/"
308+
cp --remove-destination "$PGLIBNEW"/*.sql "$PGSHARENEW/extension/"
261309

262-
cp --remove-destination "$PGLIBNEW"/*.control "$PGSHARENEW/extension/"
263-
cp --remove-destination "$PGLIBNEW"/*.sql "$PGSHARENEW/extension/"
310+
export LD_LIBRARY_PATH="${PGLIBNEW}"
311+
fi
264312

265313
# This is a workaround for older versions of wrappers which don't have the expected
266314
# naming scheme, containing the version in their library's file name
@@ -287,8 +335,6 @@ function initiate_upgrade {
287335
fi
288336
fi
289337

290-
export LD_LIBRARY_PATH="${PGLIBNEW}"
291-
292338
echo "9. Creating new data directory, initializing database"
293339
chown -R postgres:postgres "$MOUNT_POINT/"
294340
rm -rf "${PGDATANEW:?}/"
@@ -308,6 +354,10 @@ function initiate_upgrade {
308354
EOF
309355
)
310356

357+
if [ "$IS_NIX_BASED_SYSTEM" = "true" ]; then
358+
UPGRADE_COMMAND=". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && $UPGRADE_COMMAND"
359+
fi
360+
311361
su -c "$UPGRADE_COMMAND --check" -s "$SHELL" postgres
312362

313363
echo "10. Stopping postgres; running pg_upgrade"
@@ -324,7 +374,11 @@ EOF
324374
CI_stop_postgres
325375
fi
326376

327-
su -c "$UPGRADE_COMMAND" -s "$SHELL" postgres
377+
if [ "$IS_NIX_BASED_SYSTEM" = "true" ]; then
378+
LC_ALL=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8 LOCALE_ARCHIVE=/usr/lib/locale/locale-archive su -pc "$UPGRADE_COMMAND" -s "$SHELL" postgres
379+
else
380+
su -c "$UPGRADE_COMMAND" -s "$SHELL" postgres
381+
fi
328382

329383
# copying custom configurations
330384
echo "11. Copying custom configurations"
@@ -349,7 +403,7 @@ EOF
349403
trap cleanup ERR
350404

351405
echo "running" > /tmp/pg-upgrade-status
352-
if [ -z "$IS_CI" ]; then
406+
if [ -z "$IS_CI" ] && [ -z "$IS_LOCAL_UPGRADE" ]; then
353407
initiate_upgrade >> "$LOG_FILE" 2>&1 &
354408
echo "Upgrade initiate job completed"
355409
else

0 commit comments

Comments
 (0)