@@ -29,10 +29,13 @@ SCRIPT_DIR=$(dirname -- "$0";)
29
29
source " $SCRIPT_DIR /common.sh"
30
30
31
31
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"
33
35
34
36
PGVERSION=$1
35
37
MOUNT_POINT=" /data_migration"
38
+ LOG_FILE=" /var/log/pg-upgrade-initiate.log"
36
39
37
40
POST_UPGRADE_EXTENSION_SCRIPT=" /tmp/pg_upgrade/pg_upgrade_extensions.sql"
38
41
OLD_PGVERSION=$( run_sql -A -t -c " SHOW server_version;" )
@@ -41,6 +44,15 @@ POSTGRES_CONFIG_PATH="/etc/postgresql/postgresql.conf"
41
44
PGBINOLD=" /usr/lib/postgresql/bin"
42
45
PGLIBOLD=" /usr/lib/postgresql/lib"
43
46
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
+
44
56
# If upgrading from older major PG versions, disable specific extensions
45
57
if [[ " $OLD_PGVERSION " =~ ^14.* ]]; then
46
58
EXTENSIONS_TO_DISABLE+=(" ${PG14_EXTENSIONS_TO_DISABLE[@]} " )
@@ -107,7 +119,7 @@ cleanup() {
107
119
echo " Removing SUPERUSER grant from postgres"
108
120
run_sql -c " ALTER USER postgres WITH NOSUPERUSER;"
109
121
110
- if [ -z " $IS_CI " ]; then
122
+ if [ -z " $IS_CI " ] && [ -z " $IS_LOCAL_UPGRADE " ] ; then
111
123
echo " Unmounting data disk from ${MOUNT_POINT} "
112
124
umount $MOUNT_POINT
113
125
fi
@@ -175,18 +187,50 @@ function initiate_upgrade {
175
187
PGDATAOLD=$( cat " $POSTGRES_CONFIG_PATH " | grep data_directory | sed " s/data_directory = '\(.*\)'.*/\1/" )
176
188
177
189
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"
182
190
183
191
# running upgrade using at least 1 cpu core
184
192
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
185
205
186
206
echo " 1. Extracting pg_upgrade binaries"
187
207
mkdir -p " /tmp/pg_upgrade_bin"
188
208
tar zxf " /tmp/persistent/pg_upgrade_bin.tar.gz" -C " /tmp/pg_upgrade_bin"
189
209
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
+
190
234
# copy upgrade-specific pgsodium_getkey script into the share dir
191
235
chmod +x " $SCRIPT_DIR /pgsodium_getkey.sh"
192
236
mkdir -p " $PGSHARENEW /extension"
@@ -220,7 +264,7 @@ function initiate_upgrade {
220
264
locale-gen
221
265
fi
222
266
223
- if [ -z " $IS_CI " ]; then
267
+ if [ -z " $IS_CI " ] && [ -z " $IS_LOCAL_UPGRADE " ] ; then
224
268
# awk NF==3 prints lines with exactly 3 fields, which are the block devices currently not mounted anywhere
225
269
# excluding nvme0 since it is the root disk
226
270
echo " 5. Determining block device to mount"
@@ -254,13 +298,17 @@ function initiate_upgrade {
254
298
echo " 8. Granting SUPERUSER to postgres user"
255
299
run_sql -c " ALTER USER postgres WITH SUPERUSER;"
256
300
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/"
261
309
262
- cp --remove-destination " $PGLIBNEW " / * .control " $PGSHARENEW /extension/ "
263
- cp --remove-destination " $PGLIBNEW " / * .sql " $PGSHARENEW /extension/ "
310
+ export LD_LIBRARY_PATH= " ${PGLIBNEW} "
311
+ fi
264
312
265
313
# This is a workaround for older versions of wrappers which don't have the expected
266
314
# naming scheme, containing the version in their library's file name
@@ -287,8 +335,6 @@ function initiate_upgrade {
287
335
fi
288
336
fi
289
337
290
- export LD_LIBRARY_PATH=" ${PGLIBNEW} "
291
-
292
338
echo " 9. Creating new data directory, initializing database"
293
339
chown -R postgres:postgres " $MOUNT_POINT /"
294
340
rm -rf " ${PGDATANEW:? } /"
@@ -308,6 +354,10 @@ function initiate_upgrade {
308
354
EOF
309
355
)
310
356
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
+
311
361
su -c " $UPGRADE_COMMAND --check" -s " $SHELL " postgres
312
362
313
363
echo " 10. Stopping postgres; running pg_upgrade"
324
374
CI_stop_postgres
325
375
fi
326
376
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
328
382
329
383
# copying custom configurations
330
384
echo " 11. Copying custom configurations"
349
403
trap cleanup ERR
350
404
351
405
echo " running" > /tmp/pg-upgrade-status
352
- if [ -z " $IS_CI " ]; then
406
+ if [ -z " $IS_CI " ] && [ -z " $IS_LOCAL_UPGRADE " ] ; then
353
407
initiate_upgrade >> " $LOG_FILE " 2>&1 &
354
408
echo " Upgrade initiate job completed"
355
409
else
0 commit comments