Sub topic 14
When to Choose Logical Replication
Assume you know basic SQL, PostgreSQL roles, primary keys, psql, and the purpose of the write-ahead log (WAL). You should also understand that a publisher sends changes and a subscriber applies them.
Logical replication is a good choice when you need to copy selected row changes between independent PostgreSQL databases. It is especially useful when the two databases need different roles, versions, schemas, or workloads.
Choose logical replication when
You need an online major-version upgrade
Logical replication can connect PostgreSQL versions that are compatible for the planned migration, including many major-version upgrade paths. You create the new database, copy existing data, and stream changes from the old database while applications continue running.
At cutover time, briefly stop writes, let the subscriber catch up, and redirect the application. This can reduce downtime compared with a dump and restore.
Test the complete process first. Check extensions, data types, sequences, permissions, application connections, and unsupported objects. Logical replication copies table changes, not every database feature.
You need only part of a database
A physical standby mirrors the whole cluster. Logical replication can publish selected tables, operations, rows, or columns. This fits cases such as:
- Sending order data to a reporting database.
- Sharing a tenant's data with another service.
- Feeding a warehouse from operational tables.
- Moving only selected tables to a new application database.
Filtering reduces the data sent, but it does not automatically solve privacy or security requirements. Review row filters and column choices carefully. A copied column may still reveal sensitive information indirectly.
You need independent systems
A physical standby is tightly coupled to its primary. A logical subscriber has its own database and can often be queried, backed up, or upgraded independently. The subscriber can also contain tables that are not replicated.
This independence helps when a reporting workload should not compete with the production database. It also helps when teams need separate ownership of downstream databases.
Compare the alternatives
| Requirement | Logical replication | Physical replication |
|---|---|---|
| Copy selected tables | Yes | No, copies the cluster |
| Support different major versions | Often | Usually no |
| Read-only standby failover | Less direct | Strong fit |
| Replicate DDL automatically | No | Yes, as part of the physical state |
| Filter rows or columns | Yes, where supported | No |
| Maintain an independent writable database | Possible, with conflict controls | No during normal standby use |
Choose physical replication when your main goal is high availability or disaster recovery for the entire cluster. Physical replication usually has fewer moving parts for an exact standby. Choose logical replication when selection, independence, or migration flexibility matters more.
Check the limits before committing
Logical replication does not replicate schema changes automatically. A deployment that adds or changes a column must update the subscriber safely. It also does not automatically keep sequences synchronized, and some objects require separate migration or backup procedures.
Apply conflicts can stop a subscription. Common causes include writing different values to the subscriber, missing rows, unique-key collisions, and incompatible schemas. A design that allows writes on both sides needs an explicit conflict strategy. Logical replication is not automatically multi-primary replication.
Replication slots can retain WAL when a subscriber is stopped. Monitor slot activity and retained WAL, and define an incident procedure before production use.
Use logical replication when its selective, loosely coupled stream solves a clear problem. Do not choose it merely because it is newer or because it avoids planning a migration. For a complete standby, physical replication is usually simpler. For periodic copies with no near-real-time requirement, backups or batch data movement may be easier to operate.
> callout warning > Before production cutover, rehearse initial synchronization, catch-up, application validation, sequence handling, rollback, and subscriber cleanup.
Exercise
You need a complete, read-only standby for automatic failover. You also need the standby to match every database object and schema change. Which replication type is usually the better starting point, and why?
Show answer
Physical replication is usually better because it mirrors the entire cluster and keeps the standby closely aligned with the primary. Logical replication is better when you need selected data, independent databases, or a cross-version migration.