Learn Labs
Security & Ops

Upgrades and the Runbook

A rolling minor upgrade under load (one 3-second pause), changing settings without downtime, and the day-to-day procedures

Postgres ships a minor release about every three months, mostly security and bug fixes. Minor releases never change the data format, so upgrading is just "run the new binaries on the same data directory". With three nodes you can do it without an outage, if you do it in the right order.

The rolling upgrade drill

scripts/upgrade_drill.sh resets the cluster on 18.4 and upgrades it to 18.6 while labs/writer.py writes through PgBouncer the whole time. (The drill was meant to go from 18.5; Docker Hub has no 18.5 image, the tags jump from 18.4 to 18.6.)

./scripts/upgrade_drill.sh
pg-1 18.4 (Debian 18.4-1.pgdg13+1)
pg-2 18.4 (Debian 18.4-1.pgdg13+1)
pg-3 18.4 (Debian 18.4-1.pgdg13+1)
[t= 10s] upgrading replica pg-2
[t= 18s] pg-2 back, streaming
[t= 28s] upgrading replica pg-3
[t= 35s] pg-3 back, streaming
[t= 45s] switchover pg-1 -> pg-2
[t= 53s] upgrading the old primary pg-1
[t= 65s] done
writer: 303,082 acknowledged, 36 failed, 0 lost, 0 in doubt
writer: 3.0s without a successful write, starting at t=46s
pg-1 18.6 (Debian 18.6-1.pgdg13+2)
pg-2 18.6 (Debian 18.6-1.pgdg13+2)
pg-3 18.6 (Debian 18.6-1.pgdg13+2)
switchover at 45 spg-218.6new primarypg-318.6pg-1primary on 18.418.6writers

The order is the whole trick:

  1. Replicas first, one at a time. Recreate the container on the new image (PG_VERSION=18.6 docker compose up -d --no-deps pg-2), wait until it's streaming again, then the next. Writers don't notice: they only talk to the primary.
  2. Switch over to an upgraded replica. This is the only moment clients notice: 3.0 seconds of errors, the same as the switchover in the failover drill.
  3. Upgrade the old primary last. It comes back as a replica of the new one.

303,082 writes acknowledged, 0 lost, one 3-second gap. During the drill the cluster briefly ran 18.4 and 18.6 side by side, which is fine within a major version: a 18.4 primary streams to an 18.6 replica.

Major upgrades are different

18 to 19 changes the on-disk format, so new binaries can't open the old data directory. The options:

  • pg_upgrade --link: minutes of downtime, rewrites the catalog and hard-links the data files. Every replica must be rebuilt afterwards.
  • Logical replication to a new cluster on the new version, then switch the application over: seconds of downtime, but sequences, DDL and large objects need care.

Both deserve their own drill before a real one. The configuration lesson covers pg_upgrade.

Changing settings without downtime

Postgres parameters live in etcd, not in postgresql.conf:

docker exec -it pg-1 patronictl -c /etc/patroni/patroni.yml edit-config
# change, e.g.  work_mem: 16MB   (applied with a reload, no restart)
#               shared_buffers: 512MB   (needs a restart)
docker exec pg-1 patronictl -c /etc/patroni/patroni.yml list   # "Pending restart" column

Patroni pushes the change to every node and reloads. Settings that need a restart (shared_buffers, max_connections, shared_preload_libraries) show as pending; restart them the same way as an upgrade: replicas one at a time (patronictl restart pg-cluster pg-2), then a switchover, then the old primary. max_connections has one extra rule: replicas must have at least the primary's value, or they refuse to start as hot standbys. Raise it on replicas before the primary; lower it on the primary first.

Runbook

SituationDo
Planned maintenance on the primarypatronictl switchover, then work on the old primary as a replica
A replica is broken or far behindpatronictl reinit pg-cluster <node>: re-clone it from the primary
A replica is gone for gooddrop its slot (SELECT pg_drop_replication_slot('pg_3')) or it keeps WAL forever (up to max_slot_wal_keep_size)
Someone dropped a tablerestore to a separate node at the time before (the PITR lesson), copy the table back
PostgresWALArchivingFailingpgbackrest --stanza=main check on the primary; fix the repository before pg_wal fills the disk
etcd lost its majorityrestore etcd members first; the primary is read-only until then (unless failsafe_mode)
Rotate a role's passwordALTER ROLE ... PASSWORD once on the primary (it replicates); PgBouncer picks it up through auth_query
Renew TLS certificatesreplace the files in config/certs, then restart the nodes one at a time like an upgrade (the entrypoint copies the certificates when a node starts; Postgres itself re-reads them on a reload)

On this page