Creating Replication Slots to Enable CDC
Function Description
Open-source PostgreSQL uses replication slots to enable Change Data Capture (CDC). Replication slots are the core for logical replication. They can:
- Ensure that WAL logs of the primary database are not cleared until all logs are consumed by secondary databases or subscribers.
- Record subscribers' consumption positions (LSNs) to prevent data loss or repeated consumption.
- Parse WAL logs into readable change data (such as INSERT, UPDATE, and DELETE operations) using logical decoding plugins (like test_decoding and wal2json) to implement CDC.
This topic describes how to enable CDC for an RDS for PostgreSQL instance.
Prerequisites
- You have purchased an ECS. For details, see Purchasing an ECS in Custom Config Mode. Ensure that the ECS is in the same region, VPC, and security group as your RDS for PostgreSQL instance.
- You have purchased an RDS for PostgreSQL instance and installed a PostgreSQL client on the ECS. For details, see Buying a DB Instance and Connecting to It Using a PostgreSQL Client.
- You have connected to the RDS for PostgreSQL instance through the ECS. For details, see Connecting to a DB Instance from a Linux ECS over a Private Network.
Precautions
- You can enable CDC and consume captured data only on the primary instance. Read replicas do not support this feature.
- RDS for PostgreSQL supports logical replication slot failover. A primary/standby switchover does not affect CDC. For more information, see Failover Slot for Logical Subscriptions.
- Before enabling CDC, modify RDS for PostgreSQL instance parameters and reboot the instance. To prevent impact on workloads, modify parameters during off-peak hours.
Enabling CDC
Disabling CDC
RDS for PostgreSQL instances with CDC enabled require more space for storing WAL logs. If CDC is no longer needed, to prevent replication slots from running out of storage space, disable CDC by performing the following operations:
- Connect to the RDS for PostgreSQL instance using the account root.
psql -h <instance-address> -p 5432 -U root -d testdb
- (Optional) Check for the replication slot.
SELECT slot_name FROM pg_replication_slots WHERE slot_name = 'cdc_replication_slot';
- Delete the replication slot. This operation cannot be undone. Exercise caution when performing this operation.
SELECT pg_drop_replication_slot('cdc_replication_slot');
- Verify that CDC is disabled.
SELECT slot_name FROM pg_replication_slots;
If the query result does not contain cdc_replication_slot, the feature has been disabled.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot