Skip to content

Commit d7fffb3

Browse files
authored
feat: if upgrade 17 -> 17 modify upgrade process (#1583)
* feat: if upgrade 17 -> 17 or 17-orioledb -> 17-orioledb do not run these queries * feat: handle all cases of SERVER_LC_COLLATE and SERVER_LC_CTYPE * fix: explixit set on 17/oriole * feat: handling max_slot_wal_keep_size for pg 17 was needed as well * feat: binary upgrades require max_slot_wal_keep_size to be -1 during binary upgrade * fix: Better to override that during the upgrade process by specifying the option directly when pg_upgrade is executed (-c) * fix: cover only pg 17 * fix: rm oriole handling * fix: do not need max_slot_wal_keep_size on old version * fix: temp config on new-options too * fix: remove unbound var * chore: remove complete.sh change that should not have been committed * chore: bump for testing * chore: stash code * feat: working pg 17 upgrade * feat: pg 15 handling * feat: rm oriole handling, refine 15 -> 17 config * feat: make sure old pg stops if not force stop * chore: bump version * chore: cleanup + bump version for test * fix: rollback to working version with fix from divit * chore: version bump
1 parent 4018cac commit d7fffb3

File tree

3 files changed

+166
-11
lines changed

3 files changed

+166
-11
lines changed

ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ LOG_FILE="/var/log/pg-upgrade-initiate.log"
4141

4242
POST_UPGRADE_EXTENSION_SCRIPT="/tmp/pg_upgrade/pg_upgrade_extensions.sql"
4343
POST_UPGRADE_POSTGRES_PERMS_SCRIPT="/tmp/pg_upgrade/pg_upgrade_postgres_perms.sql"
44-
OLD_PGVERSION=$(run_sql -A -t -c "SHOW server_version;")
44+
OLD_PGVERSION=$(pg_config --version | sed 's/PostgreSQL \([0-9]*\.[0-9]*\).*/\1/')
45+
46+
# Skip locale settings if both versions are PostgreSQL 17+
47+
if ! [[ "$OLD_PGVERSION" =~ ^17.* && "$PGVERSION" =~ ^17.* ]]; then
48+
SERVER_LC_COLLATE=$(run_sql -A -t -c "SHOW lc_collate;")
49+
SERVER_LC_CTYPE=$(run_sql -A -t -c "SHOW lc_ctype;")
50+
fi
4551

46-
SERVER_LC_COLLATE=$(run_sql -A -t -c "SHOW lc_collate;")
47-
SERVER_LC_CTYPE=$(run_sql -A -t -c "SHOW lc_ctype;")
4852
SERVER_ENCODING=$(run_sql -A -t -c "SHOW server_encoding;")
4953

5054
POSTGRES_CONFIG_PATH="/etc/postgresql/postgresql.conf"
@@ -251,7 +255,12 @@ function initiate_upgrade {
251255
if [ -n "$IS_LOCAL_UPGRADE" ]; then
252256
mkdir -p "$PG_UPGRADE_BIN_DIR"
253257
mkdir -p /tmp/persistent/
254-
echo "a7189a68ed4ea78c1e73991b5f271043636cf074" > "$PG_UPGRADE_BIN_DIR/nix_flake_version"
258+
if [ -n "$NIX_FLAKE_VERSION" ]; then
259+
echo "$NIX_FLAKE_VERSION" > "$PG_UPGRADE_BIN_DIR/nix_flake_version"
260+
else
261+
echo "a7189a68ed4ea78c1e73991b5f271043636cf074" > "$PG_UPGRADE_BIN_DIR/nix_flake_version"
262+
fi
263+
255264
tar -czf "/tmp/persistent/pg_upgrade_bin.tar.gz" -C "/tmp/pg_upgrade_bin" .
256265
rm -rf /tmp/pg_upgrade_bin/
257266
fi
@@ -394,9 +403,14 @@ function initiate_upgrade {
394403
rm -rf "${PGDATANEW:?}/"
395404

396405
if [ "$IS_NIX_UPGRADE" = "true" ]; then
397-
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 -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && $PGBINNEW/initdb --encoding=$SERVER_ENCODING --lc-collate=$SERVER_LC_COLLATE --lc-ctype=$SERVER_LC_CTYPE -L $PGSHARENEW -D $PGDATANEW/ --username=supabase_admin" -s "$SHELL" postgres
406+
if [[ "$PGVERSION" =~ ^17.* ]]; then
407+
LC_ALL=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8 LOCALE_ARCHIVE=/usr/lib/locale/locale-archive su -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && $PGBINNEW/initdb --encoding=$SERVER_ENCODING --locale-provider=icu --icu-locale=en_US.UTF-8 -L $PGSHARENEW -D $PGDATANEW/ --username=supabase_admin" -s "$SHELL" postgres
408+
else
409+
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 -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && $PGBINNEW/initdb --encoding=$SERVER_ENCODING --lc-collate=$SERVER_LC_COLLATE --lc-ctype=$SERVER_LC_CTYPE -L $PGSHARENEW -D $PGDATANEW/ --username=supabase_admin" -s "$SHELL" postgres
410+
fi
398411
else
399412
su -c "$PGBINNEW/initdb -L $PGSHARENEW -D $PGDATANEW/ --username=supabase_admin" -s "$SHELL" postgres
413+
400414
fi
401415

402416
# This line avoids the need to supply the supabase_admin password on the old
@@ -409,6 +423,20 @@ $(cat /etc/postgresql/pg_hba.conf)" > /etc/postgresql/pg_hba.conf
409423
run_sql -c "select pg_reload_conf();"
410424
fi
411425

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+
# Add the setting if not found
431+
echo "max_slot_wal_keep_size = -1" >> "$TMP_CONFIG"
432+
433+
# Remove db_user_namespace if upgrading from PG15
434+
if [[ "$OLD_PGVERSION" =~ ^15.* && "$PGVERSION" =~ ^17.* ]]; then
435+
sed -i '/^db_user_namespace/d' "$TMP_CONFIG"
436+
fi
437+
438+
chown postgres:postgres "$TMP_CONFIG"
439+
412440
UPGRADE_COMMAND=$(cat <<EOF
413441
time ${PGBINNEW}/pg_upgrade \
414442
--old-bindir="${PGBINOLD}" \
@@ -417,17 +445,23 @@ $(cat /etc/postgresql/pg_hba.conf)" > /etc/postgresql/pg_hba.conf
417445
--new-datadir=${PGDATANEW} \
418446
--username=supabase_admin \
419447
--jobs="${WORKERS}" -r \
420-
--old-options='-c config_file=${POSTGRES_CONFIG_PATH}' \
448+
--old-options="-c config_file=$TMP_CONFIG" \
421449
--old-options="-c shared_preload_libraries='${SHARED_PRELOAD_LIBRARIES}'" \
422450
--new-options="-c data_directory=${PGDATANEW}" \
451+
--new-options="-c config_file=$TMP_CONFIG" \
423452
--new-options="-c shared_preload_libraries='${SHARED_PRELOAD_LIBRARIES}'"
424453
EOF
425454
)
426455

427456
if [ "$IS_NIX_BASED_SYSTEM" = "true" ]; then
428457
UPGRADE_COMMAND=". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && $UPGRADE_COMMAND"
429458
fi
430-
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 --check" -s "$SHELL" postgres
459+
460+
if [[ "$PGVERSION" =~ ^17.* ]]; then
461+
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
462+
else
463+
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 --check" -s "$SHELL" postgres
464+
fi
431465

432466
echo "10. Stopping postgres; running pg_upgrade"
433467
# Extra work to ensure postgres is actually stopped
@@ -439,11 +473,17 @@ EOF
439473

440474
sleep 3
441475
systemctl stop postgresql
476+
442477
else
443478
CI_stop_postgres
444479
fi
445480

446-
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
481+
# Start the old PostgreSQL instance with version-specific options
482+
if [[ "$PGVERSION" =~ ^17.* ]]; then
483+
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
484+
else
485+
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
486+
fi
447487

448488
# copying custom configurations
449489
echo "11. Copying custom configurations"

ansible/vars.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ postgres_major:
99

1010
# Full version strings for each major version
1111
postgres_release:
12-
postgresorioledb-17: "17.0.1.079-orioledb"
13-
postgres17: "17.4.1.029"
14-
postgres15: "15.8.1.086"
12+
postgresorioledb-17: "17.0.1.080-orioledb"
13+
postgres17: "17.4.1.030"
14+
postgres15: "15.8.1.087"
1515

1616
# Non Postgres Extensions
1717
pgbouncer_release: "1.19.0"
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Testing PostgreSQL Upgrade Scripts
2+
3+
This document describes how to test changes to the PostgreSQL upgrade scripts on a running machine.
4+
5+
## Prerequisites
6+
7+
- A running PostgreSQL instance
8+
- Access to the Supabase Postgres repository
9+
- Permissions to run GitHub Actions workflows
10+
- ssh access to the ec2 instance
11+
12+
## Development Workflow
13+
14+
1. **Make Changes to Upgrade Scripts**
15+
- Make your changes to the scripts in `ansible/files/admin_api_scripts/pg_upgrade_scripts/`
16+
- Commit and push your changes to your feature branch
17+
- For quick testing, you can also edit the script directly on the server at `/etc/adminapi/pg_upgrade_scripts/initiate.sh`
18+
19+
2. **Publish Script Changes** (Only needed for deploying to new instances)
20+
- Go to [publish-nix-pgupgrade-scripts.yml](https://github.com/supabase/postgres/actions/workflows/publish-nix-pgupgrade-scripts.yml)
21+
- Click "Run workflow"
22+
- Select your branch
23+
- Run the workflow
24+
25+
3. **Publish Binary Flake Version** (Only needed for deploying to new instances)
26+
- Go to [publish-nix-pgupgrade-bin-flake-version.yml](https://github.com/supabase/postgres/actions/workflows/publish-nix-pgupgrade-bin-flake-version.yml)
27+
- Click "Run workflow"
28+
- Select your branch
29+
- Run the workflow
30+
- Note: Make sure the flake version includes the PostgreSQL version you're testing (e.g., 17)
31+
32+
4. **Test on Running Machine**
33+
ssh into the machine
34+
```bash
35+
# Stop PostgreSQL
36+
sudo systemctl stop postgresql
37+
38+
# Run the upgrade script in local mode with your desired flake version
39+
sudo NIX_FLAKE_VERSION="your-flake-version-here" IS_LOCAL_UPGRADE=true /etc/adminapi/pg_upgrade_scripts/initiate.sh 17
40+
```
41+
Note: This will use the version of the script that exists at `/etc/adminapi/pg_upgrade_scripts/initiate.sh` on the server.
42+
The script should be run as the ubuntu user with sudo privileges. The script will handle switching to the postgres user when needed.
43+
44+
In local mode:
45+
- The script at `/etc/adminapi/pg_upgrade_scripts/initiate.sh` will be used (your edited version)
46+
- Only the PostgreSQL binaries will be downloaded from the specified flake version
47+
- No new upgrade scripts will be downloaded
48+
- You can override the flake version by setting the NIX_FLAKE_VERSION environment variable
49+
- If NIX_FLAKE_VERSION is not set, it will use the default flake version
50+
51+
5. **Monitor Progress**
52+
```bash
53+
# Watch the upgrade log
54+
tail -f /var/log/pg-upgrade-initiate.log
55+
```
56+
57+
6. **Check Results**
58+
In local mode, the script will:
59+
- Create a new data directory at `/data_migration/pgdata`
60+
- Run pg_upgrade to test the upgrade process
61+
- Generate SQL files in `/data_migration/sql/` for any needed post-upgrade steps
62+
- Log the results in `/var/log/pg-upgrade-initiate.log`
63+
64+
To verify success:
65+
```bash
66+
# Check the upgrade log for completion
67+
grep "Upgrade complete" /var/log/pg-upgrade-initiate.log
68+
69+
# Check for any generated SQL files
70+
ls -l /data_migration/sql/
71+
72+
# Check the new data directory
73+
ls -l /data_migration/pgdata/
74+
```
75+
76+
Note: The instance will not be upgraded to the new version in local mode. This is just a test run to verify the upgrade process works correctly.
77+
78+
## Important Notes
79+
80+
- The `IS_LOCAL_UPGRADE=true` flag makes the script run in the foreground and skip disk mounting steps
81+
- The script will use the existing data directory
82+
- All output is logged to `/var/log/pg-upgrade-initiate.log`
83+
- The script will automatically restart PostgreSQL after completion or failure
84+
- For testing, you can edit the script directly on the server - the GitHub Actions workflows are only needed for deploying to new instances
85+
- Run the script as the ubuntu user with sudo privileges - the script will handle user switching internally
86+
- Local mode is for testing only - it will not actually upgrade the instance
87+
- The Nix flake version must include the PostgreSQL version you're testing (e.g., 17)
88+
- In local mode, only the PostgreSQL binaries are downloaded from the flake - the upgrade scripts are used from the local filesystem
89+
- You can override the flake version by setting the NIX_FLAKE_VERSION environment variable when running the script
90+
91+
## Troubleshooting
92+
93+
If the upgrade fails:
94+
1. Check the logs at `/var/log/pg-upgrade-initiate.log`
95+
2. Look for any error messages in the PostgreSQL logs
96+
3. The script will attempt to clean up and restore the original state
97+
4. If you see an error about missing Nix flake attributes, make sure the flake version includes the PostgreSQL version you're testing
98+
99+
Common Errors:
100+
- `error: flake 'github:supabase/postgres/...' does not provide attribute 'packages.aarch64-linux.psql_17/bin'`
101+
- This means the Nix flake version doesn't include PostgreSQL 17 binaries
102+
- You need to specify a flake version that includes your target version
103+
- You can find valid flake versions by looking at the commit history of the publish-nix-pgupgrade-bin-flake-version.yml workflow
104+
105+
## Cleanup
106+
107+
After testing:
108+
1. The script will automatically clean up temporary files
109+
2. PostgreSQL will be restarted
110+
3. The original configuration will be restored
111+
112+
## References
113+
114+
- [publish-nix-pgupgrade-scripts.yml](https://github.com/supabase/postgres/actions/workflows/publish-nix-pgupgrade-scripts.yml)
115+
- [publish-nix-pgupgrade-bin-flake-version.yml](https://github.com/supabase/postgres/actions/workflows/publish-nix-pgupgrade-bin-flake-version.yml)

0 commit comments

Comments
 (0)