Skip to main content

What is logical replication?

Logical replication is a PostgreSQL feature that replicates data objects and their changes at the logical level, rather than the physical level. It works by:
  1. WAL Level: The database must be configured with wal_level = logical to capture logical changes
  2. Publication: Creates a logical replication stream on the source database
  3. Replication Slot: Maintains position in the WAL stream and ensures data consistency
  4. Subscription/Consumer: External tools consume the logical replication stream
This setup enables:
  • Replication of individual transactions and row changes
  • Selective replication of specific tables or databases
  • Cross-version replication between different PostgreSQL versions
  • CDC integration with external tools and data pipelines

Configuration requirements

PlanetScale cluster parameters

To enable logical replication on your PlanetScale Postgres cluster, configure these parameters in the Clusters > Parameters tab: In addition, for production environments, configure the following to ensure that your CDC stream is maintained during any switchover or failover. Without these settings, manual intervention will be required to restore data pipelines after these events. If your CDC client sleeps for long periods, a failover can proceed before slots are synced, which risks losing the logical slot. Use logical_slot_sync_timeout to bound how long failover waits for slot sync in that scenario. For more details, see Postgres High Availability with CDC. Also, in the Cluster configuration > Parameters tab of the dashboard UI, under the Failover section, add a comma-delimited list of the replication slot(s) you will create to preserve during any switchover or failover events. Be sure to apply the queue of configuration changes before proceeding.

Verify configuration

After setting these configuration parameters in the dashboard, you can verify them in the CLI. For example, to verify the WAL level:
The result should show logical:

CDC tool configuration

Ensure your CDC tool is configured properly:
  • Airbyte: Ensure replication slots are created with failover support (Setup Guide)
  • AWS DMS: Manually create failover-enabled replication slots before configuring DMS (Setup Guide)
  • ClickHouse: See ClickPipes documentation for PlanetScale configuration (Setup Guide)
  • Debezium: Configure connector to use failover-enabled replication slots (Setup Guide)
  • Fivetran: Create your own replication slot with failover = true (Setup Guide)
Some CDC tools create replication slots automatically. You must verify that any auto-created slots have failover = true enabled, or manually create the slots yourself with the proper configuration.

Create and manage users

For production CDC deployments, create a dedicated replication role. You can do this through PlanetScale (API, dashboard, CLI, or Terraform) or with SQL as the default user.

Create a role with REPLICATION via PlanetScale

You can create a PlanetScale user-defined role with the REPLICATION attribute enabled. When creating the role:
  • Include the postgres inherited role.
  • Enable the REPLICATION attribute on the role.
In the dashboard, select WITH REPLICATION when creating a role (Settings > Roles, or the Connect dialog). Through the API, pass with_replication: true when creating a role with inherited_roles that includes postgres. Roles created this way appear in your database settings and can be managed through the dashboard, API, and CLI. Use this role’s credentials to connect from your subscriber/consumer side.

Create a user with SQL

Alternatively, log in as the default user and create a dedicated replication user with SQL:
The WITH REPLICATION clause allows the user to connect to the server using the replication protocol, to create and use replication slots, to stream WAL files, and to perform the logical decoding operations. You will configure this user to connect from your subscriber/consumer side. Because of the edge connection settings, to log in as this user, add the branch ID after the username, like this:
Some target systems, like Fivetran, will need to match on the exact login username for some operations after it has logged in. As a workaround for those cases, also create a user with the name + branch ID, like this:
To find the branch ID, look at the Settings > Roles page and observe the branch ID in roles that were created in the UI. Roles created via SQL do not appear on the Settings > Roles page and must be managed directly in Postgres.

Create and manage replication streams

Create a replication slot

Using the dedicated replication role, create logical replication slots with the failover option enabled to preserve the slots during any switchover or failover events:

Create initial publication

Some CDC tools require you to create publications to specify which tables to replicate. You will need to do this as the owner of the tables or the default role. This example uses the default role.

Add tables to a publication

Currently, tables must be added to the publication individually or as a comma-delimited list. Remember to update your publication when adding new tables that should be replicated.

Replica identity configuration

For complete change tracking of both row values before and after changes (as well as to support any tables without a primary key), set the replica identity to FULL:

Verify publications

Issue the following to see active publications with tables. Do this as the default user.

Monitoring and troubleshooting

PlanetScale metrics for CDC monitoring

PlanetScale provides built-in metrics that are essential for monitoring your CDC setup. Access these through your Metrics dashboard to track replication health and performance:
For detailed information about interpreting these metrics, see the Cluster Metrics documentation.

Monitoring replication lag

Check replication slot lag. The replication_lag column shows how much WAL data the publisher is keeping because the subscriber has not confirmed or processed it yet. This value should be kept well below max_wal_size.

WAL retention and disk usage

Monitor WAL retention to prevent disk space issues. This is another way to see similar information, and will include any PlanetScale HA replicas.

Common issues

Issue: WAL disk space growing rapidly
Cause: Inactive or slow CDC consumers
Solution: Remove unused slots or troubleshoot slow consumers
Issue: Failover breaks CDC stream
Cause: Replication slot not properly synchronized
Solution: Verify failover configuration and slot synchronization status

Best practices

  1. Always enable failover: Never deploy CDC to production without failover = true on replication slots and proper PlanetScale cluster configuration
  2. Verify configuration: Double-check that both your CDC tool and PlanetScale settings are properly configured before going live
  3. Test failover scenarios: Test actual failover events in staging environments to ensure your configuration works
  4. Regular monitoring: Monitor replication lag, WAL retention, and slot synchronization status
  5. Slot cleanup: Remove unused logical replication slots to prevent WAL accumulation
  6. CDC client resilience: Ensure CDC clients can handle connection interruptions gracefully

Security considerations

  • Logical replication exposes table data - ensure proper access controls
  • Use dedicated database users with minimal required privileges for CDC
  • Consider network security when streaming to external systems
  • Monitor for unauthorized replication slots
For more information about cluster configuration parameters, see the Cluster configuration parameters documentation.