Skip to content

Use Read Replicas

Spread read traffic over a second service: create a read-only copy of a Managed PostgreSQL service, in the same zone or another one, and promote it to a standalone service if you need to.

Use a replica to:

  • run reporting, analytics or export queries without loading the primary,
  • serve reads from a zone closer to the clients,
  • keep a copy of the data in another zone that you can promote.

A replica is not a backup: a DROP TABLE on the primary reaches the replica too. To recover from a mistake, fork the service to a point in time, see Manage Backups and Restores.

How a Replica Works

A read replica is a separate Managed PostgreSQL service with its own name, zone, plan, connection URI and IP filter. A service integration of type read_replica links it to the primary. While the integration exists, the replica is read-only. Deleting the integration promotes the replica to a standalone primary and ends the replication for good.

Replicas are asynchronous, so a replica can return data that is slightly older than the primary. Send reads that must see a transaction’s own writes to the primary.

Create a Read Replica

Create the replica through the API, as a new service that declares a read_replica integration pointing at the primary. This also lets you set its IP filter at creation time:

exo api create-dbaas-service-pg REPLICA_NAME -z REPLICA_ZONE <<EOF
{
  "plan": "PLAN",
  "ip-filter": ["CIDR"],
  "integrations": [
    {
      "type": "read_replica",
      "source-service": "PRIMARY_NAME"
    }
  ]
}
EOF

Parameters:

  • REPLICA_NAME: name of the new replica service, for example my-postgresql-replica.
  • REPLICA_ZONE: zone the replica runs in, for example at-vie-1. It can differ from the zone of the primary.
  • PLAN: plan of the replica, for example startup-4. Hobbyist plans cannot be used for a replica.
  • CIDR: a network block allowed to connect to the replica, for example 203.0.113.0/24. List several entries if needed.
  • PRIMARY_NAME: name of the service to replicate from, for example my-postgresql.

Always set ip-filter in the creation payload. A replica created without one starts with an empty IP filter: it reaches running, but every connection attempt times out.

Use this API command rather than exo dbaas read-replica create, which rejects its --source-service flag in current CLI versions.

Cross-zone replicas are supported, for example a primary in ch-gva-2 and a replica in at-vie-1. The distance between zones adds to the replication lag, so measure it before you rely on the replica for reads that must be recent.

List and Inspect Replicas

List every replica of a primary, in all zones:

exo dbaas read-replica list PRIMARY_NAME

Parameters:

  • PRIMARY_NAME: name of the primary service, for example my-postgresql.

This command takes the name of the primary and has no -z flag: it searches every zone.

Show one replica:

exo dbaas read-replica show REPLICA_NAME -z REPLICA_ZONE

Parameters:

  • REPLICA_NAME: name of the replica service.
  • REPLICA_ZONE: zone of the replica.

The STATUS column reports the state of the integration. It can read starting while the replica is already streaming, so check replication from PostgreSQL, as shown below.

Connect to a Replica

The replica has its own connection URI:

exo dbaas show REPLICA_NAME -z REPLICA_ZONE --uri

Once connected, confirm which side you are on:

SELECT pg_is_in_recovery();

true means you are on a replica, false on a primary. Applications that split reads and writes can run this check at startup instead of trusting their configuration.

Measure Replication Lag

On the primary, list the replicas that are streaming:

SELECT application_name, client_addr, state, sync_state,
       pg_wal_lsn_diff(sent_lsn, replay_lsn) AS replay_bytes
FROM pg_stat_replication
WHERE application_name <> 'pghoard';

Each replica appears with an application_name made of its service name and a node number, for example my-postgresql-replica-1. replay_bytes is the amount of WAL, in bytes, that the replica has not replayed yet, and sync_state reads async.

The query excludes pghoard, a connection that pg_stat_replication always contains, even on a service without any replica: it is the platform’s WAL archiver, not a replica. Leave it out of replica monitoring.

Warning

Never drop the pghoard_local replication slot. It belongs to the platform’s WAL archiver.

To also see the lag as durations, query every column, in expanded output (\x in psql):

SELECT application_name,
       client_addr,
       state,
       sync_state,
       pg_wal_lsn_diff(sent_lsn, replay_lsn) AS replay_bytes,
       write_lag,
       flush_lag,
       replay_lag
FROM pg_stat_replication;

On the replica, read the lag from the receiving side:

SELECT pg_is_in_recovery() AS is_replica,
       pg_last_wal_receive_lsn(),
       pg_last_wal_replay_lsn(),
       now() - pg_last_xact_replay_timestamp() AS replica_lag;

replica_lag is the age of the last replayed transaction. When the primary receives no writes, it grows even though the replica is up to date, so read it together with replay_bytes on the primary: replay_bytes close to zero with a growing replica_lag means the primary is idle, not that the replica is behind.

Promote a Replica

Caution

Promotion is permanent. A promoted service cannot be attached to its former primary again, and the two services diverge from that moment. To replicate again, delete the promoted service and create a new replica, which copies the data from scratch.

Promotion turns the replica into a standalone service by deleting the read_replica integration:

exo dbaas read-replica promote REPLICA_NAME -z REPLICA_ZONE -f

-f skips the confirmation prompt. Without it, a non-interactive shell answers nothing and the replica is not promoted.

To promote through the API instead, read the integration ID on the replica and delete the integration:

exo api get-dbaas-service-pg REPLICA_NAME -z REPLICA_ZONE -q 'integrations'
exo api delete-dbaas-integration INTEGRATION_ID -z REPLICA_ZONE

Parameters:

  • INTEGRATION_ID: the id of the entry whose type is read_replica.

Pass -z on both commands: without it, exo api uses the default zone of your CLI account.

A promoted replica keeps its IP filter. Before sending traffic to it, review the settings that were less important on a replica, such as its plan, backup schedule and maintenance window.

Troubleshooting

SymptomCauseFix
Creation of a read replica not allowed under current planThe replica uses a Hobbyist plan.Choose a Startup plan or larger.
exo dbaas read-replica create rejects --source-serviceCurrent CLI versions do not accept the flag.Create the replica with exo api create-dbaas-service-pg, as shown above.
The replica is running, but every connection times outIt was created without ip-filter, so its filter is empty.Set the filter, see Restrict Connections to a Service.
STATUS reads starting for a long timeThe integration status can read starting while the replica is already streaming.Check pg_stat_replication on the primary.
cannot execute ... in a read-only transactionYou are writing to the replica.Send writes to the primary.
replica_lag keeps growingThe primary receives no writes.Check replay_bytes on the primary: close to zero means the replica is up to date.
The replica is not promoted, with no errorpromote ran without -f in a non-interactive shell.Add -f.

Related Pages

Last updated on