difference between Streaming Replication and logical replication
Both Streaming Replication and Logical Replication are methods used in PostgreSQL to copy data from one database server (the Primary or Publisher) to one or more other servers (the Standby or Subscriber). They achieve similar goals but use fundamentally different technologies under the hood.
1. Streaming Replication (Physical) 💾
Streaming Replication is the traditional and most common method. It works by copying the raw, low-level changes of the database files, which are recorded in the Write-Ahead Log (WAL).
How it Works:
- WAL Ship: The Primary server continuously streams its WAL records (the internal, binary log of every disk block change) directly over a network connection to the Standby server.
- Physical Copy: The Standby receives these binary records and applies them, effectively rebuilding the exact same data files, byte-for-byte, as the Primary.
- Use Case: It is primarily used for high availability and disaster recovery. If the Primary fails, the Standby can be promoted to take its place.
Key Characteristics:
- Whole Server: Replication happens at the database cluster level. You are replicating the entire PostgreSQL instance, including all databases, user accounts, and configuration settings.
- Speed: Very fast because it copies the data in its raw, binary format.
- Read-Only: The Standby is typically read-only. You can query it, but you cannot make changes to the data unless you promote it to become the new Primary.
- Same Version: Both the Primary and Standby must be running the exact same version of PostgreSQL because they are sharing the same physical data structure.
2. Logical Replication 📝
Logical Replication is a newer, more flexible approach (introduced in PostgreSQL 10). Instead of copying physical files, it copies the logical changes (the SQL operations, like $\text{INSERT}$ or $\text{UPDATE}$).
How it Works:
- Decode: The Primary server reads the WAL and decodes the binary changes back into logical operations (like, "Table X had a new row inserted with data A, B, C...").
- Publish/Subscribe: The Primary (Publisher) sends these logical operations to the Standby (Subscriber).
- Apply SQL: The Subscriber executes those logical operations as if a user had run the SQL query, applying the changes to its own tables.
- Use Case: It's used for selective data distribution, upgrades, and migration between different systems.
Key Characteristics:
- Selective: You can choose to replicate individual tables, schemas, or databases, not the whole server.
- Flexible: The Subscriber database can have a different schema or PostgreSQL version (within reasonable limits) because it's only applying the resulting data changes, not the underlying file structure.
- Writable: The Subscriber is typically writable, meaning it can be used for other purposes or receive data from other sources (though this can create conflicts if not managed carefully).
- Filtering: You can filter which actions (e.g., only $\text{INSERT}$ and $\text{UPDATE}$, but not $\text{DELETE}$) get replicated.
Summary of Differences
| Feature | Streaming (Physical) Replication | Logical Replication |
|---|---|---|
| Data Format | Raw, binary $\text{WAL}$ files (physical changes). | Decoded $\text{SQL}$ statements (logical changes). |
| Scope | Entire PostgreSQL Server (all databases). | Selectable (tables, schemas, or single databases). |
| Standby State | Typically Read-Only (great for load balancing reads). | Typically Writable (can be used for other applications). |
| Version Match | Required (must be the exact same version). | Flexible (can replicate between different minor/major versions). |
| Main Use | High Availability ($\text{HA}$), Disaster Recovery, Read Scaling. | Data Migration, Selective Distribution, Cross-Version Upgrades. |
Published on: Oct 01, 2025, 02:27 AM