Skip to content

Rehearse the Emergent PostgreSQL 19 restore

Emergent uses the stock CloudNativePG PostgreSQL 19 trixie image with a read-only extension image containing PGMQ, Supabase Vault and pg_net. The PG18 Barman backup is physical. Recover it on PG18 first, then transfer the application database to a fresh PG19 instance with PostgreSQL’s logical dump tools. The two rehearsal clusters have their own PVCs, namespaces and service names.

  1. Select a completed Emergent backup and record its Barman backup ID, source timeline and end LSN. Recover a separate PG18 CNPG cluster with the source’s PG18 base and extension images. Set bootstrap.recovery.database: supabase, owner: supabase_admin, and a recoveryTarget with the chosen backupID, targetImmediate: true and source targetTLI. Reference the existing backup through an external Barman ObjectStore using the original server name. Give the rehearsal no archive destination or backup schedule. Confirm the recovery job selected the intended backup and the PG18 cluster became ready.
  2. Keep the recovered source quiescent. Preload only Vault, use the same projected root key and executable key script, and limit network access to recovery dependencies. Exclude external applications, cron and the pg_net worker from this source. Keep the rehearsal networks isolated and access the databases via the operator socket.
  3. Build the extension image. For single-node dev101, import its local tag into the node’s containerd image store and use pullPolicy: Never. Create a separate PG19 CNPG cluster with a neutral bootstrap database and owner, leaving supabase and its application roles available for restore. Configure the following under the new cluster’s spec.postgresql. Supply the original Vault key and executable script through spec.projectedVolumeTemplate.
extensions:
- name: emergent
image:
reference: localhost/emergent-pg19-extensions:rehearsal
pullPolicy: Never
ld_library_path: [system]
shared_preload_libraries: [supabase_vault, pg_net]
parameters:
vault.getkey_script: /projected/postgresql-custom/pgsodium_getkey.sh
pg_net.database_name: rehearsal_bootstrap

Pointing pg_net at the neutral bootstrap database keeps archived application HTTP requests quiescent. Check the PG19 pod’s actual read-only volume mount at /extensions/emergent, normal extension control path, preloads and network isolation before importing data.

Run these Bash commands from the repository root with kubectl configured for the rehearsal cluster. Use the current primary pods for both databases. The commands assume fresh namespaces and the neutral PG19 bootstrap above.

Terminal window
set -euo pipefail
stage=(kubectl -n emergent-pg18-backup-rehearsal exec emergent-pg18-backup-1 -c postgres --)
target=(kubectl -n emergent-pg19-restore-rehearsal exec -i pg19-target-1 -c postgres --)
psql_target=(psql -X -q -U postgres -v ON_ERROR_STOP=1)
test "$("${target[@]}" "${psql_target[@]}" -Atd postgres -c \
"SELECT count(*) FROM pg_database WHERE datname = 'supabase'")" = 0
"${stage[@]}" psql -XAt -U postgres -d supabase -c \
"SELECT extname, extversion FROM pg_extension ORDER BY extname"

The tested restore profile expects source PGMQ 1.11.1. Compare the two role inventories before transferring globals. Fresh CNPG instances share their built-in manager roles. The chart-managed target also reconciles supabase_admin and its password from supabase-superuser, matching the existing deployment. Omit duplicate CREATE ROLE statements for roles already present on the target, and retain the source role attributes, settings, owners and memberships. Restoring other application passwords from the deployment’s secret store is a separate cutover step. If an earlier rehearsal created an extra app role on the PG18 staging instance, exclude that role’s CREATE and ALTER statements after verifying it was absent from the source database cluster.

Terminal window
existing_roles=$("${target[@]}" "${psql_target[@]}" -At -d postgres -c \
"SELECT rolname FROM pg_roles WHERE rolname !~ '^pg_' ORDER BY rolname")
"${stage[@]}" pg_dumpall -U postgres -l supabase --roles-only --no-role-passwords |
awk -v existing="$existing_roles" 'BEGIN { split(existing, names, "\n"); for (i in names) roles[names[i]] = 1 }
/^CREATE ROLE [[:alnum:]_]+;$/ { role = $3; sub(/;$/, "", role); if (role in roles) next }
{ print }' |
"${target[@]}" "${psql_target[@]}" --single-transaction -d postgres
"${stage[@]}" pg_dump -U postgres -Fc -d supabase |
"${target[@]}" env 'PGOPTIONS=-c extension_control_path=/extensions/emergent/restore/share:$system:/extensions/emergent/share' \
pg_restore -U postgres --create --exit-on-error --dbname=postgres

The restore connection selects the historical PGMQ control file. --create restores the database owner, locale, database ACLs and database settings from the archive. A failed restore can leave a partially populated destination. Retry on a fresh disposable target.

Run the post-restore SQL in a normal connection. It verifies the historical PGMQ version, runs the upstream upgrade against the restored queues, tests their metrics and privileges as pg_monitor, and clears the old PostgREST role’s supautils, safeupdate login preload. Its transaction rolls back if a check fails.

Terminal window
"${target[@]}" "${psql_target[@]}" -d supabase < infra/utils/cloudnativepg/pg19/finish-restore.sql

Run the data comparison against both quiescent databases before starting Auth, Storage or application workers. Save results privately. It covers every user heap, including empty tables and partition leaves, both halves of a row fingerprint summed independently, sequence values and large objects. First compare these outputs byte for byte. Then verify owners, grants and row policies, and exercise the service-owned migrations and APIs.

Terminal window
umask 077
mkdir -p "$PWD/tmp/agents"
checkdir=$(mktemp -d "$PWD/tmp/agents/pg19-compare.XXXXXXXX")
kubectl -n emergent-pg18-backup-rehearsal exec -i emergent-pg18-backup-1 -c postgres -- \
psql -XqAt -v ON_ERROR_STOP=1 -U postgres -d supabase \
< infra/utils/cloudnativepg/pg19/compare-data.sql > "$checkdir/pg18"
"${target[@]}" "${psql_target[@]}" -At -d supabase \
< infra/utils/cloudnativepg/pg19/compare-data.sql > "$checkdir/pg19"
cmp "$checkdir/pg18" "$checkdir/pg19"

Check the erasure restore procedure against receipts newer than the backup before reconnecting any application. A live cutover also needs a final write boundary, a fresh export, application login credentials, the intended pg_net worker database, and a verified backup destination for the new cluster.

Recovered Emergent Barman backup 20261121T023000 through end LSN 1/D1000050 (02:31:26 UTC) in an isolated PG18 CNPG cluster on dev101. The destination ran stock CNPG PostgreSQL 19.0 with the local 11 MB extension image mounted read-only. Before and after the PGMQ upgrade, all 323 user tables (2,480,931 rows), 15 sequences and large-object checks matched the recovered source. All 303 row policies matched. All five queues exposed metrics to pg_monitor and denied it access to message contents after the upgrade.

The backup contained zero Vault rows. Encryption and decryption passed with a rolled-back sentinel using the projected key. A separate synthetic PG18 ciphertext fixture verified decryption on PG19 across restart. The check for erasure receipts since the backup started returned zero.

On this restored copy, the chart-pinned Auth and Storage images ran their migrations as their respective nonsuperuser roles. Auth migration records advanced from 77 to 84 and Storage from 61 to 82. All 31 pre-existing Auth and Storage data tables retained their row counts and fingerprints over the original columns. Both health endpoints, the Auth user list and the Storage bucket list returned HTTP 200. Storage used a disposable file backend for this API smoke. The database backup covers bucket and object metadata; Garage object contents have a separate backup lifecycle.

The active primary is emergent/emergent-postgres (PostgreSQL 19.0). Its configuration is in the PostgreSQL chart and dev values. Helmfile reconciles the new database during both the data-only and full app installs. The live chart uses the private registry payload by manifest digest sha256:ac3329b42cd6d2de9af43d2ee33d785619305cc4bb2ea68f385385103f5e4518.

The old and new clusters exchanged ownership of the CNPG-managed supabasedb-svc while its UID and ClusterIP 10.101.5.80 stayed the same. The workstation alias dev-pg-emergent now points to that stable IP. Auth, Storage, Studio, Meta, the Bun API and workers, and Circuits use the stable endpoint. The active cluster reconciles the existing supabase_admin login from supabase-superuser.

Database Completed Barman backup (UTC) End LSN Barman server path
PG18 before maintenance 20261121T135900, 14:00:25 1/E3000158 s3://db-backups-emergent/supabase-cluster
PG19 after final restore 20261121T141137, 14:12:58 0/3702DB90 s3://db-backups-emergent/pg19/emergent-postgres

The transfer streamed the final live database after scaling down writers, draining Circuits, rejecting network connections to the source database and acquiring share locks on the source tables. PostgreSQL transferred application role verifiers directly between sockets. The final copy had 329 tables, 2,480,959 rows and 15 sequences. Row fingerprints, sequence states, large objects, application relation ownership and ACLs, and all 303 row policies matched. PGMQ metrics and message-body privileges passed for all five queues.

The first PG19 archive attempt failed because Garage required the S3 region garage. After configuring the Barman sidecar, the unopened target needed an immediate shutdown during a restart because its shutdown checkpoint was waiting on failed archiving. CNPG recovered on the same PVC. The data and policy comparisons passed again after restart, before switching traffic. The replacement backup completed and its end WAL was archived. PG19’s backup schedule is daily at 02:45 UTC.

Circuits retained its durable-streams pod, gracefully stopped the engine, and detected the new PostgreSQL system identifier on restart. Its expected source reset retired 36 cached shapes. The new circuits logical slot became active and a fresh shape backfilled against PG19. Existing clients can re-subscribe. All paused workloads returned to their original replica counts. Live checks covered authenticated Auth and Storage reads, Studio and Meta SQL, the public Bun functions route, content runtime, LRS and worker connections.

supabase-cluster is hibernated with its original 10 GiB PVC, supabase has ALLOW_CONNECTIONS false in that cluster, and its ScheduledBackup is suspended. PG18’s former default RW Service is specific to the retained cluster. Use supabasedb-svc for applications and host tools. The PG18 Vault key script ConfigMap still comes from the common-data release; retain that script and the root key when eventually retiring the old resources.

After PG19 accepted writes, the PG18 PVC became a point-in-time recovery option. Switching the Service back directly would lose subsequent writes. Prefer recovery from the PG19 backup plus archived WAL for a PG19 incident. For an intentional recovery onto PG18, first stop writers and plan how to preserve intervening writes. A separately inspected PG18 copy can be rehydrated by removing its cnpg.io/hibernation annotation and enabling database connections from the maintenance database. Keep application routing on the selected primary throughout recovery.