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:- WAL Level: The database must be configured with
wal_level = logical
to capture logical changes - Publication: Creates a logical replication stream on the source database
- Replication Slot: Maintains position in the WAL stream and ensures data consistency
- Subscription/Consumer: External tools consume the logical replication stream
- 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
Basic configuration requirements
PlanetScale cluster parameters
To enable logical replication on your PlanetScale for Postgres cluster, configure these parameters in the Cluster configuration > Parameters tab:Parameter | Required Value | Description |
---|---|---|
wal_level | logical | Enables logical replication in the WAL |
max_replication_slots | ≥ 1 | Sets the maximum number of replication slots |
max_wal_senders | ≥ 2 | Sets the maximum number of WAL sender processes |
max_slot_wal_keep_size | > 4GB | Amount of WAL data to retain for replication slots |
Verify configuration
Connect to your database and verify the WAL level:logical
:
Production CDC setup - Critical failover configuration
Before deploying CDC to production, you must configure logical replication failover. Without this, any database failover will break your CDC streams and require manual intervention to restore data pipelines.
Create failover-enabled replication slots
When creating logical replication slots, you must enable thefailover
option:
Configure PlanetScale cluster for failover
You must enable these parameters in your cluster configuration:- Navigate to Cluster configuration > Parameters
- Configure these failover settings:
Setting | Required Value | Description |
---|---|---|
sync_replication_slots | on | Enables synchronization of replication slots to standby servers |
hot_standby_feedback | on | Prevents query conflicts during replication |
Logical slot name | your_slot_name | The exact name of your logical replication slot |
If your CDC tool doesn’t display the slot name, you can find it by running:
CDC tool considerations
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.Additional CDC setup considerations
User permissions and security
For production CDC deployments, create a dedicated replication user with minimal privileges:Publications and table selection
Some CDC tools require you to create publications to specify which tables to replicate:Remember to update your publication when adding new tables that should be replicated. Publications don’t automatically include new tables unless created with
FOR ALL TABLES
.Replica identity configuration
For complete change tracking, set replica identity to FULL for tables that need full row data: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:Metric Category | Key Indicators for CDC | What to Monitor |
---|---|---|
WAL archival rate | Success/Failed counts | Monitor failed WAL archival attempts that could impact CDC streams |
WAL archive age | Seconds behind | Age of oldest unarchived WAL - should be under 60 seconds for healthy CDC |
WAL storage | Storage usage in MB | Track WAL disk usage; high usage may indicate CDC consumers falling behind |
Replication lag | Lag in seconds | Monitor delay between primary and replicas; high lag may indicate CDC issues |
Transaction rate | Transactions per second | Track database workload intensity affecting CDC processing |
Memory | RSS and Memory mapped | Monitor memory pressure that could impact logical decoding performance |
For detailed information about interpreting these metrics, see the Cluster Metrics documentation.
Key CDC monitoring indicators
- WAL archival failures: Any failed WAL archival can break CDC streams
- High WAL storage usage: May indicate slow CDC consumers not advancing replication slots
- Increasing replication lag: Could signal CDC consumer performance issues
- High memory usage: Logical decoding can be memory-intensive for high-volume changes
Monitoring replication lag
Check replication slot lag:WAL retention and disk usage
Monitor WAL retention to prevent disk space issues:Common issues
Issue: WAL disk space growing rapidlyCause: 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
- Always enable failover: Never deploy CDC to production without
failover = true
on replication slots and proper PlanetScale cluster configuration - Verify configuration: Double-check that both your CDC tool and PlanetScale settings are properly configured before going live
- Test failover scenarios: Test actual failover events in staging environments to ensure your configuration works
- Regular monitoring: Monitor replication lag, WAL retention, and slot synchronization status
- Slot cleanup: Remove unused logical replication slots to prevent WAL accumulation
- 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