Logical Decoding and Logical Replication in PostgreSQL

Introduction

PostgreSQL, a powerful open-source relational database, offers multiple replication strategies to support high availability, scalability, and integration. Among them, logical decoding and logical replication provide fine-grained, row-level control over data streaming, unlike traditional physical replication.

Understanding the Core Concepts

What Is Logical Decoding?

Logical decoding is a PostgreSQL mechanism that extracts changes from the Write-Ahead Log (WAL) and converts them into a human-readable format. Instead of copying raw bytes like physical replication, it captures INSERT, UPDATE, and DELETE operations.

What Is Logical Replication?

Introduced in PostgreSQL 10, logical replication enables targeted, near-real-time data synchronization using a publisher-subscriber model.

Key Features:

  • Publisher: Sends row-level changes from selected tables.
  • Subscriber: Receives and applies those changes.
  • Row-Level control: Focused on specific SQL operations.
  • Selective replication: Supports replicating only chosen tables or columns.

How Logical Decoding Works?

Logical decoding is the process of extracting row-level changes from WAL.

To enable it:

Enable the logical WAL level

ALTER SYSTEM SET wal_level = logical;
SELECT pg_reload_conf();

Create a replication slot:

SELECT pg_create_logical_replication_slot('my_slot', 'wal2json');

Key Components:

  • WAL (Write-Ahead Log): Captures every data change in PostgreSQL.
  • Logical Decoding: Converts WAL entries into meaningful row-level changes.
  • Replication Slot: Ensures that WAL files are retained until fully consumed.
  • Popular Logical Decoding Plugins such as pgoutput or wal2json that control how WAL changes are decoded and presented

Logical Decoding Plugins

Plugins define how changes are decoded and formattedPlugins define how changes are decoded and formatted.

Built-in plugins
  • test_decoding – simple text output, for testing/logging.
  • pgoutput – used internally by PostgreSQL’s built-in logical replication.
Popular third-party plugins
  • wal2json – outputs JSON (great for streaming changes to Kafka, Debezium, ETL).
  • decoderbufs – outputs Protocol Buffers (efficient for binary pipelines).
  • bottledwater -integrates with Apache Kafka using Avro format.
  • pglogical_output – used by the older pglogical extension

Use Cases of Logical Decoding

  • Change Data Capture (CDC): Stream changes to Kafka, Debezium, etc.
  • Cross-version replication: Migrate from PG 12 → PG 16 with no downtime.
  • Analytics: Send only new changes to Snowflake, BigQuery, or data lakes.
  • Auditing: Track who changed what in real-time.

Limitations & Considerations

  • Works only on tables with primary keys (for UPDATE/DELETE).
  • Does not fully replicate schema changes (DDL).
  • Needs monitoring – unconsumed slots can bloat WAL files.

FAQs

What is the difference between logical and physical replication in PostgreSQL?
Logical replication works at the row/SQL operation level, allowing selective table replication. Physical replication copies the entire database at the byte level.

Do I need primary keys for logical replication?
Yes. Logical decoding only supports tables with primary keys for accurate row identification

Can logical replication handle schema changes?
Not fully. DDL changes (e.g., ALTER TABLE) are not replicated and must be manually applied.

Stay tuned for what’s next

This is just the beginning of our deep dive into PostgreSQL’s powerful data replication ecosystem. In the next blogs, we’ll explore advanced logical replication strategies, conflict handling, performance tuning tips, and real-world architecture patterns for integrating with modern data platforms like Kafka, Flink, and cloud data warehouses. Whether you’re building a resilient microservices infrastructure or a high-throughput analytics pipeline, the upcoming articles will equip you with the tools and techniques to go further. Subscribe or follow to ensure you don’t miss the next chapter!

Author

+ posts

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top