Principle:2ndQuadrant Pglogical Logical Replication
| Knowledge Sources | |
|---|---|
| Domains | Database, Replication |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Method of replicating data changes at the logical level by decoding write-ahead log (WAL) records into row-level change events, rather than copying physical disk blocks.
Description
Logical replication decodes the database's write-ahead log into a stream of logical change events (INSERT, UPDATE, DELETE) that can be sent to a subscriber and applied independently of the physical storage layout. This contrasts with physical (streaming) replication, which copies byte-level page changes and requires identical system identifiers.
The key advantage is that the provider and subscriber can run different PostgreSQL major versions, have different physical layouts, and selectively replicate only specific tables or rows. The subscriber remains a fully writable, independent database that can also serve as a provider for further replication, enabling multi-master and cascading topologies.
Logical replication addresses the limitation of physical replication which requires exact binary compatibility and full-database copying. By operating at the row level, it enables selective replication (only certain tables), schema divergence (different indexes, partitioning), and conflict detection (since the subscriber may have local writes).
Usage
Use logical replication when:
- The subscriber must be independently writable (multi-master, bi-directional replication)
- Only a subset of tables or rows should be replicated
- Provider and subscriber run different PostgreSQL versions
- The subscriber needs different physical characteristics (indexes, tablespaces)
- Cascading or fan-out replication topologies are required
Prefer physical replication when byte-level consistency is required (hot standby, read replicas) and version heterogeneity is unnecessary.
Theoretical Basis
Logical replication builds on three foundations:
1. Write-Ahead Log (WAL) as the change source:
All data modifications are first written to the WAL before being applied to data files. The reorder buffer reconstructs transactions from WAL records in commit order.
2. Output plugin abstraction:
An output plugin transforms reordered WAL changes into a wire format. The plugin receives callbacks for each transaction boundary and each row change:
// Abstract algorithm (NOT real implementation)
void decode_change(ctx, txn, relation, change) {
if (!is_in_replication_set(relation)) return;
if (!passes_row_filter(change)) return;
write_protocol_message(change);
}
3. Apply-side idempotency and conflict handling:
The subscriber applies changes via normal SQL executor paths (heap insert/update/delete or SPI), enabling trigger firing, constraint checking, and conflict detection. Each applied transaction is tagged with a replication origin to prevent infinite loops and to detect conflicts.
Practical Guide
Configuring Logical Replication
- Set wal_level = logical on the provider
- Set max_replication_slots and max_worker_processes sufficiently high
- Create a replication slot with the logical output plugin
- On the subscriber, create a subscription pointing to the provider's replication slot
Key Considerations
- Replication identity (PRIMARY KEY or REPLICA IDENTITY) is required for UPDATE/DELETE
- Schema changes (DDL) are not automatically replicated by logical decoding
- Sequences require separate replication mechanisms
- Large initial datasets require an initial synchronization phase before streaming begins
Related Pages
Implemented By
- Implementation:2ndQuadrant_Pglogical_Output_Plugin_Init
- Implementation:2ndQuadrant_Pglogical_Apply_Work
- Implementation:2ndQuadrant_Pglogical_Protocol_API
- Implementation:2ndQuadrant_Pglogical_Create_Node
- Implementation:2ndQuadrant_Pglogical_Create_Subscription