Skip to content

PostgreSQL Major Upgrade Runbook

A new major version (e.g. 18 → 19) cannot start on the previous version's data directory. Bumping the image tag and applying would leave PostgreSQL restart-looping, which takes down Matrix (and with it alert delivery through Hookshot), MAS logins, saimiris-gateway and peerlab-gateway.

The databases are small (about 200 MB, mostly synapse), so the upgrade is a dump and restore into a fresh data directory. The container uses the image's default PGDATA, /var/lib/postgresql/<major>/docker, on the /home/nxthdr/postgresql/data mount. The new version therefore initializes its own directory, and the previous one stays untouched as the rollback.

version data directory (host, under /home/nxthdr/postgresql/data)
17 pgdata_v17_backup
18 pgdata
19 onward <major>/docker

Downtime is a few minutes. Done for 18 → 19 on 2026-11-29.

Rehearse first

Restore a dump into a throwaway container of the new version, with no network and the real config files, and compare row counts with production:

ssh nxthdr@ams01.core.infra.nxthdr.dev
mkdir -p ~/pgupgrade && cd ~/pgupgrade
docker exec postgresql pg_dumpall -U postgres > rehearsal.sql
C=/home/nxthdr/postgresql/config
docker run -d --name pg-rehearsal --network none \
  -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=rehearsal -e POSTGRES_DB=saimiris \
  -v $C/postgresql.conf:/etc/postgresql/postgresql.conf:ro \
  -v $C/pg_hba.conf:/etc/postgresql/pg_hba.conf:ro \
  -v $C/pg_ident.conf:/etc/postgresql/pg_ident.conf:ro \
  postgres:<new> postgres -c config_file=/etc/postgresql/postgresql.conf \
    -c hba_file=/etc/postgresql/pg_hba.conf -c ident_file=/etc/postgresql/pg_ident.conf
docker exec -i pg-rehearsal psql -U postgres -q < rehearsal.sql   # only "already exists" errors
# compare row counts (query below) against `postgresql`, then:
docker rm -f pg-rehearsal && rm rehearsal.sql

Row counts of every table in a database (run for saimiris, peerlab_gateway, synapse and mas):

select format('%s.%s=%s', schemaname, relname,
  (xpath('/row/c/text()', query_to_xml(format('select count(*) c from %I.%I', schemaname, relname), false, true, '')))[1]::text)
from pg_stat_user_tables order by 1;

Because Synapse and MAS keep writing, a rehearsal against the live server shows a few rows of drift. The real cutover stops them first, so its counts must match exactly.

Cutover

  1. Off-host backup, from the repository: make backup-postgres.
  2. Stop the writers on coreams01. Alerts are not delivered to Matrix until step 7.
    docker stop synapse mas saimiris_gateway peerlab_gateway
    
  3. Dump the old version and record the row counts:
    cd ~/pgupgrade
    docker exec postgresql pg_dumpall -U postgres > pg<old>-final.sql
    for db in saimiris peerlab_gateway synapse mas; do
      docker exec postgresql psql -U postgres -d $db -Atc "<row count query>" > counts-$db-old.txt
    done
    
  4. Apply the new image (scoped):
    terraform apply -target=docker_image.postgresql -target=docker_container.postgresql
    
    The new container initializes <major>/docker, with the postgres role and the saimiris database.
  5. Restore once docker exec postgresql pg_isready -U postgres succeeds:
    docker exec -i postgresql psql -U postgres -q < pg<old>-final.sql
    
    The only expected errors are role "postgres" already exists and database "saimiris" already exists.
  6. Verify: run the row count query into counts-$db-new.txt and diff it with the old counts. They must be identical.
  7. Start the writers and check their logs:
    docker start synapse mas saimiris_gateway peerlab_gateway
    
    Then log in to Matrix and check that a message goes through.

Rollback

The old data directory is untouched. Revert the change (image tag back, and PGDATA set to the old directory, e.g. PGDATA=/var/lib/postgresql/pgdata for 18), then run the same scoped apply. Writes made on the new version are lost.

Cleanup

After a week without issues, delete the dump in ~/pgupgrade and the data directory from two versions back (pgdata_v17_backup after 18 → 19).