Logical replication

On this page 7

Logical replication is a form of database replication that transmits data changes as row-level operations, such as insert, update and delete on specific tables, instead of copying the physical storage blocks of the whole database. The receiving server applies those operations through its own SQL engine, so it can run a different major version, a different storage layout or even a different schema.

Logical replication is the counterpart of physical or streaming replication, which ships write-ahead log records byte for byte and produces an identical copy of the entire cluster. The logical approach is selective and flexible, which makes it the usual tool for upgrades, migrations, data distribution and change data capture.

How logical replication works

The source database still records every change in its transaction log, the write-ahead log in PostgreSQL or the binary log in MySQL. For logical replication, a decoding step reads that log and turns low-level records into a stream of committed row changes with table names, column values and transaction boundaries.

In PostgreSQL the model uses publications and subscriptions. A publication on the source names the tables and operations to share; a subscription on the target connects to it, performs an optional initial data copy and then streams changes from a replication slot. The slot remembers how far the subscriber has confirmed, so the source keeps the log segments the subscriber still needs.

The subscriber applies changes in commit order, one transaction at a time by default, and identifies the rows to update or delete through the table’s replica identity, normally its primary key. The table below lists the main building blocks in PostgreSQL and MySQL terms.

ComponentRolePostgreSQLMySQL
Change sourceOrdered record of committed changesWrite-ahead log with wal_level logicalBinary log in ROW format
DecodingTurns log records into row changesLogical decoding output pluginRow events in the binary log
SelectionChooses what to replicatePublicationReplication filters
Progress trackingRemembers the subscriber positionReplication slotGTID or binary log position
Apply processExecutes changes on the targetSubscription apply workerReplica SQL threads
Building blocks of logical replication

Logical versus physical replication

The two approaches answer different needs. Physical replication creates an exact standby suitable for failover; logical replication creates a selective, writable copy of chosen data that is not bound to the source’s physical format.

AspectLogical replicationPhysical replication
Unit of transferRow changes per tableLog records for data blocks
ScopeSelected tables or rowsEntire database cluster
Version requirementsCross-version possibleSame major version and platform
Target writabilityTarget accepts its own writesStandby is read-only
Schema changesNot replicated automatically in PostgreSQLReplicated as part of the log
Sequences and large objectsLimited or no supportFully replicated
Typical useUpgrades, integration, CDCHigh availability, read replicas
Comparison of logical and physical replication

Logical replication is usually asynchronous, so the subscriber trails the source by a measurable replication lag. PostgreSQL can make a logical subscriber synchronous, but that is rarely used because the apply path is slower than physical replay.

Typical uses of logical replication

Because it decouples source and target, logical replication solves problems that physical copies cannot. The most valuable is the near-zero-downtime major version upgrade: a new server is built on the target version, synchronized through logical replication and promoted when lag reaches zero.

ScenarioHow it is usedBenefit
Major version upgradeReplicate old version to new, then switchDowntime of seconds to minutes
Cloud or platform migrationReplicate into the new environmentCut over without a long freeze
Data consolidationSeveral sources publish into one reporting databaseCentral analytics without batch exports
Selective distributionShare only some tables with another teamSmaller, purpose-built copies
Change data captureDecoded stream feeds message queues or warehousesEvent-driven integration
Common scenarios and why logical replication fits

Since PostgreSQL 15, publications can also filter rows with a WHERE clause and limit column lists, which helps keep sensitive data out of downstream copies.

Operational limitations and pitfalls

Logical replication demands more care than physical replication, because more depends on schema and data discipline. The table below lists frequent problems.

ProblemCauseRemedy
Disk fills on the sourceInactive replication slot retains the logMonitor slots, drop abandoned ones, cap retained size
Updates fail on subscriberTable lacks a primary key or replica identityAdd a key or set a suitable identity
Apply stops on conflictDuplicate key or missing row on targetResolve the row, then resume or skip
Schema driftDDL applied only on the sourceApply schema changes on both sides first
Sequences out of step after cutoverSequence values not replicatedReset sequences before switching
Growing lag on large tablesSingle-threaded apply or huge transactionsUse parallel or streaming apply options
Frequent problems and their remedies

Writes on both sides are possible, but multi-primary setups built on logical replication need explicit conflict handling and careful key design. Without it, two servers editing the same row produce divergence that no tool resolves automatically.

Monitoring and tuning logical replication

Healthy logical replication depends on watching two positions: how far the source has written and how far each subscriber has confirmed. In PostgreSQL this information appears in views of replication slots and subscription statistics, and the difference in bytes or seconds is the lag to alert on.

  • Alert on retained log size per slot, not only on lag, because a stopped subscriber silently grows the backlog.
  • Keep transactions short: one transaction that updates millions of rows reaches the subscriber only after commit and can stall it.
  • Size the initial copy carefully; synchronizing large tables can take hours and holds its own slot.
  • Test failover: logical slots historically did not survive promotion of a physical standby, although recent PostgreSQL releases add slot synchronization.

In simple terms

Logical replication copies what changed, not how it is stored. Instead of photocopying every page of a ledger, the source sends short notes such as add this customer or change that price. The receiving database follows those notes in order.

Because the notes are simple, the receiver can be a newer system or keep only some tables. That makes it ideal for moving to a new database without stopping the business.

A practical example

An online retailer runs a 2 TB PostgreSQL database on an old major version and needs to upgrade without hours of downtime. The team builds a new server on the current version, restores the schema, and creates a publication for all tables on the old server and a subscription on the new one.

The initial copy takes about a day, after which changes stream continuously with lag under a second. On the night of the switch, the team stops application writes, waits for lag to reach zero, resets the sequences on the new server and points the application to it. Total write downtime is about five minutes, compared with the many hours a dump and restore would have required.