Home  Postgress   Difference ...

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:

  1. 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.
  2. Physical Copy: The Standby receives these binary records and applies them, effectively rebuilding the exact same data files, byte-for-byte, as the Primary.
  3. 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:


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:

  1. 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...").
  2. Publish/Subscribe: The Primary (Publisher) sends these logical operations to the Standby (Subscriber).
  3. Apply SQL: The Subscriber executes those logical operations as if a user had run the SQL query, applying the changes to its own tables.
  4. Use Case: It's used for selective data distribution, upgrades, and migration between different systems.

Key Characteristics:


Summary of Differences

FeatureStreaming (Physical) ReplicationLogical Replication
Data FormatRaw, binary $\text{WAL}$ files (physical changes).Decoded $\text{SQL}$ statements (logical changes).
ScopeEntire PostgreSQL Server (all databases).Selectable (tables, schemas, or single databases).
Standby StateTypically Read-Only (great for load balancing reads).Typically Writable (can be used for other applications).
Version MatchRequired (must be the exact same version).Flexible (can replicate between different minor/major versions).
Main UseHigh Availability ($\text{HA}$), Disaster Recovery, Read Scaling.Data Migration, Selective Distribution, Cross-Version Upgrades.
Published on: Oct 01, 2025, 02:27 AM  
 

Comments

Add your comment