Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Postgres and Data

Followers, Rollback and Upgrades

Understand Heroku Postgres followers, forks, rollback and failover, how to read replication lag, and how a major PostgreSQL version upgrade is planned and carried out.

Standard and higher tiers add features that make the database more resilient and more flexible: followers, forks, rollback, and managed upgrades. This lesson explains what each one is, when to use it and what can go wrong. The commands appear as read-only text because the Heroku CLI cannot run on this site; the Python stand-ins let you practice the concepts with plain data.

Followers: read-only copies

A follower is a second database that continuously copies changes from a leader (also called primary). It is read-only. You create one with a flag on the add-on command:

Terminal
heroku addons:create heroku-postgresql:standard-0 --follow DATABASE_URL --app my-app
heroku pg:info --app my-app

The new database receives its own attachment name and URL. Typical uses:

  • Read scaling. Send reports and heavy SELECT queries to the follower so they do not slow down the leader.
  • Upgrades and moves. Build a follower on a new plan or version, wait for it to catch up, then promote it (below).
  • Safety. A follower is a warm copy to fall back on.

Followers are asynchronous: they are slightly behind. The difference is the replication lag. A page that writes a row and then reads it from a follower might not see it yet. For that reason, read-your-own-writes traffic stays on the leader.

Python Stand-in: read from a lagging follower

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The follower returns None at ticks 0 and 1 and paid from tick 2. Real lag is usually small but spikes under heavy writes, so monitor it (pg:info on a follower shows how far behind it is).

flowchart LR
    APP["App writes"] --> L[("Leader")]
    L -.->|replication| F1[("Follower<br/>reports")]
    L -.->|replication| F2[("Follower<br/>standby")]
    REP["Reporting jobs"] --> F1

Writes go to the leader; followers copy asynchronously and serve reads.

High availability on Premium

On Premium, Private and Shield plans Heroku can keep a standby copy and fail over to it automatically when the primary fails. After a failover your DATABASE_URL keeps working; connections drop for a short time, so apps must reconnect. Test that your app survives dropped connections before you rely on it.

Forks and rollback

A fork is an independent copy of a database at a point in time. Unlike a follower it does not keep following. You can fork the current state, or (with continuous protection on Standard and above) roll back by creating a new database as it was at a given time:

Terminal
heroku addons:create heroku-postgresql:standard-0 --fork DATABASE_URL --app my-app
heroku addons:create heroku-postgresql:standard-0 --rollback DATABASE_URL --to "2026-03-20 14:30+00" --app my-app

Rollback is the tool for "someone ran a wrong UPDATE at 14:31". You do not rewind production; you create a new database as it was at 14:30, inspect it, and then either copy the lost rows back or promote the new database. Dyno data written after the chosen time is not in the copy, so promoting it loses newer good writes too. Decide with care and tell users.

Python Stand-in: find the last good moment

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output shows minute 9 as the target and insert user 3 as a good write that must be replayed. That is the real cost of rollback.

Upgrades

PostgreSQL ships a major version every year, and old versions go out of support. Heroku announces deprecations well in advance and, in time, upgrades databases that are still on an end-of-life version. Doing it yourself, on your schedule, is better.

For a major upgrade, Heroku provides pg:upgrade, which works with a follower: you create a follower, prepare, then run the upgrade during a maintenance window:

Terminal
heroku addons:create heroku-postgresql:standard-0 --follow DATABASE_URL --app my-app
heroku pg:upgrade HEROKU_POSTGRESQL_AQUA_URL --version 17 --app my-app
heroku maintenance:on --app my-app
heroku pg:upgrade --confirm my-app --app my-app
heroku maintenance:off --app my-app

Plan details (flags, supported versions, downtime) change between releases, so follow the current Dev Center article. The general routine is stable:

  1. Read the PostgreSQL release notes for incompatible changes.
  2. Test the new version: fork production into a copy, upgrade the copy and run the test suite and key queries against it.
  3. Check extensions (some may not exist on the new version).
  4. Take a backup (backups).
  5. Schedule a maintenance window; turn on maintenance mode so writes stop.
  6. Upgrade, verify, turn maintenance off.
  7. Keep the old database for a few days before removing it.

Essential plans can usually be upgraded by creating a new database and restoring a backup into it, which also needs a short downtime.

Maintenance windows

Heroku applies minor security patches to databases in a weekly maintenance window you can choose (heroku pg:maintenance:window). During maintenance your database may restart briefly. Choose a low-traffic hour, and make sure the app reconnects on its own.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a follower database?

    A follower asynchronously copies a leader's changes and serves reads.
  2. 2. Why should read-your-own-writes traffic stay on the leader?

    Followers are asynchronous, so they can be a little behind the leader.
  3. 3. What does a rollback create?

    Rollback creates a new database from continuous protection data; you then inspect it and promote or copy rows back.
  4. 4. What should you do before a major PostgreSQL upgrade?

    Testing on a fork reveals extension and query incompatibilities before they hit production.

Summary

  • Followers are asynchronous read-only copies for reporting and upgrades; watch replication lag.
  • Premium tiers add automatic failover; apps must tolerate dropped connections.
  • Forks copy a moment in time; rollback builds a new database at an earlier moment, at the cost of newer writes.
  • Upgrade deliberately: test on a fork, back up, use a maintenance window and keep the old database a few days.

Next: Scaling Dynos Horizontally and Vertically