Sub topic 15
When to use logical replication instead
This chapter assumes that you understand PostgreSQL transactions, WAL, basic SQL, and the physical streaming replication setup from earlier chapters.
PostgreSQL has two broad replication choices:
- Physical replication copies the database cluster at the storage level. A standby receives WAL records and replays the same page changes as the primary.
- Logical replication copies row-level changes from selected tables. A publisher exposes changes through a publication, and a subscriber applies them.
Choose logical replication when the subscriber should contain a selected or transformed view of the publisher. Choose physical replication when you need a close copy of the entire cluster for high availability or disaster recovery.
How logical replication differs
Logical replication uses PostgreSQL's logical decoding process. PostgreSQL reads committed WAL records, converts them into operations such as INSERT, UPDATE, and DELETE, and sends those operations to the subscriber.
The subscriber must have compatible tables. Logical replication does not automatically copy all database objects, such as roles, functions, indexes, or most schema changes.
-- Run on the publisher.
CREATE PUBLICATION app_changes
FOR TABLE public.orders, public.customers;
-- Run on the subscriber after creating compatible tables.
CREATE SUBSCRIPTION reporting_sub
CONNECTION 'host=primary.example.com port=5432 dbname=app user=replicator password=change-me'
PUBLICATION app_changes;Creating a subscription normally performs an initial table copy, then continues with changes that arrive afterward. Keep the publisher's connection credentials in a protected configuration system rather than embedding them in scripts.
Choosing between the methods
| Requirement | Better choice | Reason |
|---|---|---|
| Automatic failover for the whole database cluster | Physical | The standby can be promoted with the same cluster contents |
| Replicate only selected tables | Logical | Publications can include specific tables |
| Move data between supported PostgreSQL major versions | Logical | Physical streaming normally requires the same major version |
| Run reports on a complete read-only copy | Physical | The standby contains the whole database |
| Send data to another application or service | Logical | Row changes can cross database boundaries |
| Keep an exact disaster-recovery copy | Physical | WAL replay preserves cluster-level state |
Logical replication is not a replacement for a physical failover standby. A subscriber can lag, miss schema changes, or contain only part of the source data. It also does not replicate sequences, so generated identifiers require separate planning.
Common failure modes
Logical replication can stop when the subscriber cannot apply a change. Common causes include:
- The target table lacks a required column.
- A row violates a target-side constraint.
- An update or delete cannot identify a unique row.
- A target trigger changes the result unexpectedly.
- The publisher and subscriber use incompatible data types.
- A large transaction creates substantial apply delay.
For updates and deletes, a published table generally needs a primary key or another suitable replica identity. Without one, PostgreSQL may require REPLICA IDENTITY FULL, which can make changes more expensive because the old row must be identified using all columns.
A logical subscriber is also useful during migrations. You can replicate selected tables into a new database, validate the new system, briefly stop writes, catch up, and then redirect clients. Test conflict handling and DDL procedures before using this approach in production.
The decision model
Exercise
You need automatic failover for a complete PostgreSQL cluster and also need to send three tables to an analytics database. Which replication methods fit these requirements?
Show answer
Use physical streaming replication for the failover standby and logical replication for the selected analytics tables.