---
title: "PostgreSQL Logical Replication and CDC"
description: "An in-depth guide to setting up and managing logical replication and change data capture in PostgreSQL"
keywords: ["postgresql", "replication", "cdc", "logical replication", "debezium"]
category: "databases"
---

To set up logical replication in PostgreSQL, you first need to configure a publication, which is a set of tables that will be replicated, and a subscription, which is the receiving end of the replication. The CREATE PUBLICATION command is used to define a publication, specifying the database and tables to be replicated, with options to include or exclude specific tables. The CREATE SUBSCRIPTION command is then used to define a subscription, pointing to the publication and the connection details of the downstream database. For example, the command CREATE PUBLICATION my_publication FOR TABLE users, orders; would create a publication named "my_publication" that replicates the "users" and "orders" tables, while the command CREATE SUBSCRIPTION my_subscription CONNECTION 'host=standby_server port=5432 dbname=mydb' PUBLICATION my_publication; would create a subscription named "my_subscription" that receives the replicated data.

The pgoutput logical replication plugin is a built-in plugin in PostgreSQL 10 and later versions that provides the necessary functionality for logical replication. It must be enabled by setting the wal_level parameter to "logical" in the postgresql.conf file, which controls the amount of information written to the write-ahead log (WAL) for replication purposes. This parameter must be set to "logical" on both the publisher and subscriber databases for logical replication to function correctly.

When it comes to conflict resolution, PostgreSQL's logical replication provides several options. By default, if a conflict is detected (e.g., a row is updated on both the publisher and subscriber), the subscriber's update will be skipped, and a warning will be logged. Alternatively, you can configure the replication to use the "REPLICA" option, which means the subscriber's update will always take precedence, or the "ORIGIN" option, which means the publisher's update will always take precedence.

The initial table synchronization process is an important step in setting up logical replication. This involves creating a snapshot of the tables being replicated on the publisher and transferring that data to the subscriber. PostgreSQL provides the pg_basebackup command to create a consistent backup of the entire database cluster, which can then be used to initialize the subscriber database. Once the initial sync is complete, the subscription can be created, and ongoing changes will be replicated.

It's important to note that logical replication in PostgreSQL does not support the replication of DDL (Data Definition Language) statements, such as CREATE TABLE or ALTER TABLE. This means that any schema changes made on the publisher will not be automatically propagated to the subscriber. To work around this limitation, you can use tools like Debezium, which is a popular open-source CDC (Change Data Capture) solution that integrates with PostgreSQL's logical replication features.

Debezium is a distributed platform that captures row-level changes in your PostgreSQL databases and makes them available as events that can be consumed by applications. Debezium uses the pgoutput logical replication plugin to capture changes and can handle both DML (Data Manipulation Language) and DDL operations, providing a more comprehensive CDC solution compared to PostgreSQL's native logical replication. Debezium also offers advanced features like data transformation, filtering, and integration with various messaging and streaming platforms.

To monitor the status of logical replication in PostgreSQL, you can query the pg_replication_slots view, which provides information about the active replication slots, including the slot name, the replication plugin, the total bytes of WAL data retained, and the oldest transaction ID that must be kept. This information can be useful for managing and troubleshooting logical replication setups.When configuring logical replication in PostgreSQL, it's important to understand the concept of replication slots. A replication slot is a logical construct that ensures that the necessary WAL (Write-Ahead Log) data is retained on the publisher to satisfy the subscriber's needs. Each subscription creates a corresponding replication slot on the publisher, and this slot is used to track the progress of the replication. The pg_create_logical_replication_slot command can be used to manually create a replication slot, specifying the plugin to be used (e.g., pgoutput) and the slot name. The pg_drop_replication_slot command can be used to remove a replication slot when it's no longer needed.

One of the key benefits of logical replication in PostgreSQL is its ability to handle changes to the schema, known as DDL (Data Definition Language) operations. While PostgreSQL's native logical replication does not support DDL replication, the Debezium CDC connector can capture and replicate these changes. Debezium monitors the PostgreSQL server's transaction log and generates events for any DDL operations, such as CREATE TABLE, ALTER TABLE, or DROP TABLE, which can then be consumed by downstream applications or systems. This allows for a more comprehensive and flexible replication solution that can adapt to schema changes over time.

When working with logical replication and CDC in PostgreSQL, it's important to monitor the health and performance of the replication process. The pg_stat_replication view provides detailed information about the current state of replication, including the replication lag (the difference between the current position on the subscriber and the position on the publisher), the number of transactions and bytes replicated, and the connection status. This information can be used to identify any issues or bottlenecks in the replication process and take appropriate actions, such as scaling the resources or tuning the replication parameters.

Another important aspect of managing logical replication and CDC in PostgreSQL is the handling of failover and failback scenarios. In the event of a failure on the publisher, the subscriber can be promoted to become the new publisher, and the original publisher can be demoted to become a subscriber. This process requires careful coordination and the use of tools like pg_basebackup and pg_rewind to ensure data consistency and minimal downtime. Additionally, when the original publisher is restored, the failback process can be initiated to synchronize the data and resume normal replication operations.

Overall, PostgreSQL's logical replication and CDC capabilities provide a powerful and flexible solution for data distribution and real-time data integration. By understanding the various configuration options, monitoring tools, and best practices, PostgreSQL administrators can set up and manage robust and reliable replication and CDC systems to meet the evolving needs of their applications and data infrastructure.When setting up logical replication in PostgreSQL, it's important to consider the potential impact on the publisher's performance. The replication process can generate additional load on the publisher, as it needs to write the changes to the write-ahead log (WAL) and manage the replication slots. To mitigate this impact, PostgreSQL provides several configuration parameters that can be tuned, such as max_wal_senders, which controls the maximum number of concurrent WAL sender processes, and max_replication_slots, which sets the maximum number of replication slots that can be active at the same time. Additionally, the use of dedicated replication servers or the implementation of cascading replication topologies can help distribute the replication load and improve overall system performance.

Another important consideration when working with logical replication in PostgreSQL is the handling of conflicts that may arise between the publisher and subscriber databases. While PostgreSQL's default conflict resolution behavior is to skip the subscriber's update and log a warning, this may not always be the desired outcome. To address this, PostgreSQL provides the REPLICA and ORIGIN conflict resolution strategies, which can be configured at the subscription level. The REPLICA strategy ensures that the subscriber's update always takes precedence, while the ORIGIN strategy ensures that the publisher's update always takes precedence. This flexibility allows administrators to choose the conflict resolution approach that best fits their specific use case and data consistency requirements.

When it comes to monitoring the health and status of logical replication in PostgreSQL, the pg_stat_replication view is a valuable tool. This view provides detailed information about the current state of replication, including the replication lag, the number of transactions and bytes replicated, and the connection status. Additionally, the pg_stat_subscription view can be used to monitor the status of individual subscriptions, including the current position of the subscriber and the lag compared to the publisher. These monitoring tools can be integrated with alerting and monitoring systems to proactively detect and address any issues that may arise during the replication process.

In addition to the built-in monitoring capabilities, PostgreSQL also provides the ability to create custom monitoring solutions using the pg_stat_get_wal_sender_status and pg_stat_get_subscription_status functions. These functions can be used to retrieve detailed information about the state of replication, including the current position of the subscriber, the number of transactions and bytes replicated, and the status of the replication process. This information can be used to build custom dashboards, alerts, and reporting tools to better understand and manage the logical replication and CDC infrastructure.

Overall, the combination of PostgreSQL's logical replication and CDC capabilities, along with the various configuration options, monitoring tools, and conflict resolution strategies, provides a robust and flexible solution for data distribution, real-time data integration, and change data capture. By understanding and leveraging these features, PostgreSQL administrators can design and implement highly available, scalable, and resilient data replication and CDC systems to meet the evolving needs of their applications and data infrastructure.When working with logical replication in PostgreSQL, it's important to consider the potential impact on the publisher's resources, such as CPU, memory, and disk I/O. The replication process can generate additional load on the publisher, as it needs to write the changes to the write-ahead log (WAL) and manage the replication slots. To mitigate this impact, PostgreSQL provides several configuration parameters that can be tuned, such as max_wal_senders, which controls the maximum number of concurrent WAL sender processes, and max_replication_slots, which sets the maximum number of replication slots that can be active at the same time. Additionally, the use of dedicated replication servers or the implementation of cascading replication topologies can help distribute the replication load and improve overall system performance.

Another important aspect of managing logical replication in PostgreSQL is the handling of changes to the database schema, known as DDL (Data Definition Language) operations. While PostgreSQL's native logical replication does not support the replication of DDL changes, the Debezium CDC connector can capture and replicate these changes. Debezium monitors the PostgreSQL server's transaction log and generates events for any DDL operations, such as CREATE TABLE, ALTER TABLE, or DROP TABLE, which can then be consumed by downstream applications or systems. This allows for a more comprehensive and flexible replication solution that can adapt to schema changes over time.

To monitor the status and health of logical replication in PostgreSQL, administrators can use the pg_replication_slots view, which provides information about the active replication slots, including the slot name, the replication plugin, the total bytes of WAL data retained, and the oldest transaction ID that must be kept. This information can be useful for managing and troubleshooting logical replication setups, as it allows you to identify any issues with replication slots, such as slots that are not being used or slots that are retaining too much WAL data.

In addition to the built-in monitoring capabilities, PostgreSQL also provides the ability to create custom monitoring solutions using the pg_stat_get_wal_sender_status and pg_stat_get_subscription_status functions. These functions can be used to retrieve detailed information about the state of replication, including the current position of the subscriber, the number of transactions and bytes replicated, and the status of the replication process. This information can be used to build custom dashboards, alerts, and reporting tools to better understand and manage the logical replication and CDC infrastructure.

Finally, it's important to consider the handling of failover and failback scenarios when working with logical replication and CDC in PostgreSQL. In the event of a failure on the publisher, the subscriber can be promoted to become the new publisher, and the original publisher can be demoted to become a subscriber. This process requires careful coordination and the use of tools like pg_basebackup and pg_rewind to ensure data consistency and minimal downtime. Additionally, when the original publisher is restored, the failback process can be initiated to synchronize the data and resume normal replication operations.