Sub topic 1
Physical versus Logical Replication
Prerequisites
This chapter assumes that you know basic SQL, including tables and primary keys. You should also know how to run psql, edit postgresql.conf, and describe a write-ahead log (WAL). A WAL records database changes before PostgreSQL writes the corresponding data pages to disk.
Two Ways to Copy Database Changes
PostgreSQL has two main replication models:
- Physical replication copies the database cluster at the storage level. A standby receives WAL records and replays them to produce the same database files as the primary.
- Logical replication copies the meaning of changes, such as “insert this row” or “update this column.” The subscriber applies those changes to its own tables.
Both models use WAL on the publisher, but they consume it differently.
flowchart TD
P[Publisher database] --> W[Write-ahead log]
W --> PR[Physical standby<br/>replays storage changes]
W --> LD[Logical decoding<br/>produces row changes]
LD --> S[Logical subscriber<br/>applies row changes]The physical standby is intended to remain a copy of the whole source cluster. The logical subscriber receives only the changes selected by its publication and subscription.
Physical Replication
Physical replication is the usual choice for high availability and disaster recovery. A standby follows the primary closely and can be promoted if the primary fails.
Physical replication has these properties:
- It copies all databases in the PostgreSQL cluster, not selected tables.
- It preserves indexes, table storage, and other physical database structures.
- The standby normally remains read-only while it follows the primary.
- The primary and standby use compatible PostgreSQL versions and platform assumptions.
- A physical standby cannot normally accept independent writes without breaking its relationship with the primary.
Because the standby replays WAL records, it does not need to understand each change as a SQL-level operation. This makes physical replication efficient and faithful to the source.
Logical Replication
Logical replication is useful when the destination needs a different view of the data. A publisher exposes selected tables through a publication. A subscriber connects through a subscription and applies the selected row changes.
Logical replication can:
- Replicate only some tables.
- Support different databases on the source and destination.
- Send selected operations, such as inserts only.
- Support compatible major-version upgrades.
- Let the subscriber contain additional tables or indexes.
- Feed reporting, integration, or migration systems.
The destination must have compatible table definitions before it can apply changes. Logical replication does not make the subscriber an exact copy of the source cluster. It transfers data changes, not every database object or configuration setting.
Choosing Between Them
| Requirement | Physical replication | Logical replication |
|---|---|---|
| Whole-cluster disaster recovery | Strong fit | Poor fit |
| Selected tables only | Not supported directly | Strong fit |
| Read-only high-availability standby | Strong fit | Possible, but not its main role |
| Major-version migration | Usually requires another method | Strong fit |
| Independent destination schema | Limited | Flexible |
| Exact storage-level copy | Yes | No |
Choose physical replication when the main goal is a current standby that can replace the primary. Choose logical replication when you need selective data movement, a migration path, or a destination that is not an identical copy.
A Simple Decision Process
Ask these questions:
- Do you need every database and table in the cluster?
- Must the destination be ready for failover?
- Do you need to select tables, columns, rows, or operations?
- Must the source and destination run different PostgreSQL major versions?
- Will the destination have its own writes or schema?
The first two answers usually point to physical replication. The later answers usually point to logical replication. In either case, plan for monitoring, lag, storage, permissions, and recovery before putting the system into production.
Exercise
You need a reporting database that receives only the orders and customers tables while the source database remains available for normal writes. Which replication model is the better starting point, and why?
Show answer
Logical replication is the better starting point because it can publish selected tables and apply their row changes to a separate destination without copying the entire cluster.