Skip to content

Commit 8d2ccb3

Browse files
committed
chore: stash code
1 parent ecff047 commit 8d2ccb3

File tree

2 files changed

+171
-12
lines changed

2 files changed

+171
-12
lines changed

ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fi
5252
SERVER_ENCODING=$(run_sql -A -t -c "SHOW server_encoding;")
5353

5454
POSTGRES_CONFIG_PATH="/etc/postgresql/postgresql.conf"
55-
PGBINOLD="/usr/lib/postgresql/bin"
55+
PGBINOLD="$(pg_config --bindir)"
5656

5757
PG_UPGRADE_BIN_DIR="/tmp/pg_upgrade_bin/$PGVERSION"
5858
NIX_INSTALLER_PATH="/tmp/persistent/nix-installer"
@@ -242,6 +242,10 @@ 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
245249

246250
PGDATANEW="$MOUNT_POINT/pgdata"
247251

@@ -255,7 +259,12 @@ function initiate_upgrade {
255259
if [ -n "$IS_LOCAL_UPGRADE" ]; then
256260
mkdir -p "$PG_UPGRADE_BIN_DIR"
257261
mkdir -p /tmp/persistent/
258-
echo "a7189a68ed4ea78c1e73991b5f271043636cf074" > "$PG_UPGRADE_BIN_DIR/nix_flake_version"
262+
# Use NIX_FLAKE_VERSION if set, otherwise use default
263+
if [ -n "$NIX_FLAKE_VERSION" ]; then
264+
echo "$NIX_FLAKE_VERSION" > "$PG_UPGRADE_BIN_DIR/nix_flake_version"
265+
else
266+
echo "a7189a68ed4ea78c1e73991b5f271043636cf074" > "$PG_UPGRADE_BIN_DIR/nix_flake_version"
267+
fi
259268
tar -czf "/tmp/persistent/pg_upgrade_bin.tar.gz" -C "/tmp/pg_upgrade_bin" .
260269
rm -rf /tmp/pg_upgrade_bin/
261270
fi
@@ -320,6 +329,7 @@ function initiate_upgrade {
320329
# upgrade job outputs a log in the cwd; needs write permissions
321330
mkdir -p /tmp/pg_upgrade/
322331
chown -R postgres:postgres /tmp/pg_upgrade/
332+
chmod 0700 /tmp/pg_upgrade/
323333
cd /tmp/pg_upgrade/
324334

325335
# Fixing erros generated by previous dpkg executions (package upgrades et co)
@@ -417,25 +427,51 @@ $(cat /etc/postgresql/pg_hba.conf)" > /etc/postgresql/pg_hba.conf
417427
run_sql -c "select pg_reload_conf();"
418428
fi
419429

430+
# Build the base upgrade command
420431
UPGRADE_COMMAND=$(cat <<EOF
421-
time ${PGBINNEW}/pg_upgrade \
432+
${PGBINNEW}/pg_upgrade \
422433
--old-bindir="${PGBINOLD}" \
423434
--new-bindir=${PGBINNEW} \
424-
--old-datadir=${PGDATAOLD} \
435+
--old-datadir="${PGDATAOLD}" \
425436
--new-datadir=${PGDATANEW} \
426437
--username=supabase_admin \
427-
--jobs="${WORKERS}" -r \
428-
--old-options='-c config_file=${POSTGRES_CONFIG_PATH}' \
429-
--old-options="-c shared_preload_libraries='${SHARED_PRELOAD_LIBRARIES}'" \
430-
--new-options="-c data_directory=${PGDATANEW}" \
431-
--new-options="-c shared_preload_libraries='${SHARED_PRELOAD_LIBRARIES}'" \
432-
--new-options="-c max_slot_wal_keep_size=-1"
438+
--jobs="${WORKERS}" -r
433439
EOF
434440
)
435441

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+
436466
if [ "$IS_NIX_BASED_SYSTEM" = "true" ]; then
437-
UPGRADE_COMMAND=". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && $UPGRADE_COMMAND"
438-
fi
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
439475

440476
if [[ "$PGVERSION" =~ ^17.* ]]; then
441477
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
@@ -457,6 +493,14 @@ EOF
457493
CI_stop_postgres
458494
fi
459495

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+
503+
# Start the old PostgreSQL instance with version-specific options
460504
if [[ "$PGVERSION" =~ ^17.* ]]; then
461505
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
462506
else
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)