5252SERVER_ENCODING=$( run_sql -A -t -c " SHOW server_encoding;" )
5353
5454POSTGRES_CONFIG_PATH=" /etc/postgresql/postgresql.conf"
55- PGBINOLD=" $( pg_config --bindir ) "
55+ PGBINOLD=" /usr/lib/postgresql/bin "
5656
5757PG_UPGRADE_BIN_DIR=" /tmp/pg_upgrade_bin/$PGVERSION "
5858NIX_INSTALLER_PATH=" /tmp/persistent/nix-installer"
@@ -242,10 +242,6 @@ function initiate_upgrade {
242242 fi
243243
244244 PGDATAOLD=$( cat " $POSTGRES_CONFIG_PATH " | grep data_directory | sed " s/data_directory = '\(.*\)'.*/\1/" )
245- # Resolve symlink if it exists
246- if [ -L " $PGDATAOLD " ]; then
247- PGDATAOLD=$( readlink -f " $PGDATAOLD " )
248- fi
249245
250246 PGDATANEW=" $MOUNT_POINT /pgdata"
251247
@@ -259,12 +255,12 @@ function initiate_upgrade {
259255 if [ -n " $IS_LOCAL_UPGRADE " ]; then
260256 mkdir -p " $PG_UPGRADE_BIN_DIR "
261257 mkdir -p /tmp/persistent/
262- # Use NIX_FLAKE_VERSION if set, otherwise use default
263- if [ -n " $NIX_FLAKE_VERSION " ]; then
258+ if [ -n " $NIX_FLAKE_VERSION " ]; then
264259 echo " $NIX_FLAKE_VERSION " > " $PG_UPGRADE_BIN_DIR /nix_flake_version"
265260 else
266261 echo " a7189a68ed4ea78c1e73991b5f271043636cf074" > " $PG_UPGRADE_BIN_DIR /nix_flake_version"
267262 fi
263+
268264 tar -czf " /tmp/persistent/pg_upgrade_bin.tar.gz" -C " /tmp/pg_upgrade_bin" .
269265 rm -rf /tmp/pg_upgrade_bin/
270266 fi
@@ -329,7 +325,6 @@ function initiate_upgrade {
329325 # upgrade job outputs a log in the cwd; needs write permissions
330326 mkdir -p /tmp/pg_upgrade/
331327 chown -R postgres:postgres /tmp/pg_upgrade/
332- chmod 0700 /tmp/pg_upgrade/
333328 cd /tmp/pg_upgrade/
334329
335330 # Fixing erros generated by previous dpkg executions (package upgrades et co)
@@ -415,6 +410,7 @@ function initiate_upgrade {
415410 fi
416411 else
417412 su -c " $PGBINNEW /initdb -L $PGSHARENEW -D $PGDATANEW / --username=supabase_admin" -s " $SHELL " postgres
413+
418414 fi
419415
420416 # This line avoids the need to supply the supabase_admin password on the old
@@ -427,51 +423,39 @@ $(cat /etc/postgresql/pg_hba.conf)" > /etc/postgresql/pg_hba.conf
427423 run_sql -c " select pg_reload_conf();"
428424 fi
429425
430- # Build the base upgrade command
426+ TMP_CONFIG=" /tmp/pg_upgrade/postgresql.conf"
427+ cp " $POSTGRES_CONFIG_PATH " " $TMP_CONFIG "
428+
429+ # Check if max_slot_wal_keep_size exists in the config
430+ if grep -q " max_slot_wal_keep_size" " $TMP_CONFIG " ; then
431+ # Find and replace the existing setting
432+ sed -i ' s/^\s*max_slot_wal_keep_size\s*=.*$/max_slot_wal_keep_size = -1/' " $TMP_CONFIG "
433+ else
434+ # Add the setting if not found
435+ echo " max_slot_wal_keep_size = -1" >> " $TMP_CONFIG "
436+ fi
437+
438+ chown postgres:postgres " $TMP_CONFIG "
439+
431440 UPGRADE_COMMAND=$( cat << EOF
432- ${PGBINNEW} /pg_upgrade \
441+ time ${PGBINNEW} /pg_upgrade \
433442 --old-bindir="${PGBINOLD} " \
434443 --new-bindir=${PGBINNEW} \
435- --old-datadir=" ${PGDATAOLD} " \
444+ --old-datadir=${PGDATAOLD} \
436445 --new-datadir=${PGDATANEW} \
437446 --username=supabase_admin \
438- --jobs="${WORKERS} " -r
447+ --jobs="${WORKERS} " -r \
448+ --old-options="-c config_file=$TMP_CONFIG " \
449+ --old-options="-c shared_preload_libraries='${SHARED_PRELOAD_LIBRARIES} '" \
450+ --new-options="-c data_directory=${PGDATANEW} " \
451+ --new-options="-c config_file=$TMP_CONFIG " \
452+ --new-options="-c shared_preload_libraries='${SHARED_PRELOAD_LIBRARIES} '"
439453EOF
440454 )
441455
442- # Add common options including config file
443- UPGRADE_COMMAND=" $UPGRADE_COMMAND \
444- --old-options=\" -c shared_preload_libraries='${SHARED_PRELOAD_LIBRARIES} '\" \
445- --old-options=\" -c data_directory=${PGDATAOLD} \" \
446- --new-options=\" -c data_directory=${PGDATANEW} \" \
447- --new-options=\" -c shared_preload_libraries='${SHARED_PRELOAD_LIBRARIES} '\" "
448-
449- # Add remaining version-specific options
450- if [[ " $PGVERSION " =~ ^17.* ]]; then
451- # Create temporary config file for old instance
452- TMP_CONFIG=" /tmp/pg_upgrade/postgresql.conf"
453- cp " $POSTGRES_CONFIG_PATH " " $TMP_CONFIG "
454- echo " max_slot_wal_keep_size = -1" >> " $TMP_CONFIG "
455- chown postgres:postgres " $TMP_CONFIG "
456-
457- UPGRADE_COMMAND=" $UPGRADE_COMMAND \
458- --old-options='-c config_file=$TMP_CONFIG ' \
459- --new-options='-c max_slot_wal_keep_size=-1' \
460- --new-options='-c unix_socket_directories=/tmp/pg_upgrade'"
461- else
462- UPGRADE_COMMAND=" $UPGRADE_COMMAND \
463- --old-options='-c config_file=${POSTGRES_CONFIG_PATH} '"
464- fi
465-
466456 if [ " $IS_NIX_BASED_SYSTEM " = " true" ]; then
467- # Ensure nix environment is loaded for both old and new instances
468- UPGRADE_COMMAND=" . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && PGBINOLD=${PGBINOLD} $UPGRADE_COMMAND "
469- fi
470-
471- # Set max_slot_wal_keep_size in environment for PG17 upgrades
472- if [[ " $PGVERSION " =~ ^17.* ]]; then
473- export PGDATA_UPGRADE_MAX_SLOT_WAL_KEEP_SIZE=-1
474- fi
457+ UPGRADE_COMMAND=" . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && $UPGRADE_COMMAND "
458+ fi
475459
476460 if [[ " $PGVERSION " =~ ^17.* ]]; then
477461 GRN_PLUGINS_DIR=/var/lib/postgresql/.nix-profile/lib/groonga/plugins LC_ALL=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 --check" -s " $SHELL " postgres
@@ -493,20 +477,14 @@ EOF
493477 CI_stop_postgres
494478 fi
495479
496- # Create and set permissions on socket directory for PG17 upgrades
497- if [[ " $PGVERSION " =~ ^17.* ]]; then
498- mkdir -p /tmp/pg_upgrade
499- chown postgres:postgres /tmp/pg_upgrade
500- chmod 0700 /tmp/pg_upgrade
501- fi
502-
503480 # Start the old PostgreSQL instance with version-specific options
504481 if [[ " $PGVERSION " =~ ^17.* ]]; then
505482 GRN_PLUGINS_DIR=/var/lib/postgresql/.nix-profile/lib/groonga/plugins LC_ALL=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
506483 else
507484 GRN_PLUGINS_DIR=/var/lib/postgresql/.nix-profile/lib/groonga/plugins LC_ALL=en_US.UTF-8 LC_CTYPE=$SERVER_LC_CTYPE LC_COLLATE=$SERVER_LC_COLLATE LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8 LOCALE_ARCHIVE=/usr/lib/locale/locale-archive su -pc " $UPGRADE_COMMAND " -s " $SHELL " postgres
508485 fi
509486
487+ # copying custom configurations
510488 echo " 11. Copying custom configurations"
511489 mkdir -p " $MOUNT_POINT /conf"
512490 cp -R /etc/postgresql-custom/* " $MOUNT_POINT /conf/"
0 commit comments