Project: YugabyteDB (App Definition and Development / Database)
File: async-transactional-tables.md

title: Handling DDLs in transactional xCluster headerTitle: Handling DDLs linkTitle: Handling DDLs description: How to handle DDLs when using transactional xCluster replication between universes headContent: Handling DDLs in transactional xCluster menu: stable: parent: async-replication-transactional identifier: async-transactional-tables weight: 50 type: docs When DDL operations are performed to databases in transactional xCluster replication (such as creating, altering, or dropping tables or partitions), the statements must be executed on both the Primary/Source and Standby/Target and the xCluster configuration must be updated. You should perform these actions in a specific order, depending on the type of DDL, as indicated in the table below. | DDL | Step 1 | Step 2 | Step 3 | | : | : | : | : | | CREATE TABLE | Execute DDL on Primary | Execute DDL on Standby | Add the table to replication | | CREATE TABLE foo PARTITION OF bar | Execute DDL on Primary | Execute DDL on Standby | Add the table to replication | | DROP TABLE | Remove the table from replication | Execute DDL on Standby | Execute DDL on Primary | | CREATE INDEX | Execute DDL on Primary | Execute DDL on Standby | - | | DROP INDEX | Execute DDL on Standby | Execute DDL on Primary | - | | ALTER TABLE or INDEX | Execute DDL on Standby | Execute DDL on Primary | - | To ensure that data is protected at all times, set up replication on a new table before inserting any into it. If a table already has data before adding it to replication, then adding the table to replication can result in a backup and restore of the entire database. Add tables to replication in the following sequence: Create the table on the Primary. Create the table on the Standby. Add the table to the replication. For instructions on adding tables to replication, refer to . Remove tables from replication in the following sequence: Remove the table from replication. For instructions on removing tables from replication, refer to

Drop the table from both Primary and Standby databases separately. Indexes are automatically added to replication in an atomic fashion after you create the indexes separately on Primary and Standby. You do not have to stop the writes on the Primary. Note: The Create Index DDL may kill some in-flight transactions. This is a temporary error. Retry any failed transactions. Add indexes to replication in the following sequence: Create an index on the Primary. Wait for index backfill to finish. Create the same index on Standby. Wait for index backfill to finish. For instructions on monitoring backfill, refer to . When an index is dropped it is automatically removed from replication. Remove indexes from replication in the following sequence: Drop the index on the Standby universe. Drop the index on the Primary universe. Adding a table partition is similar to adding a table. The caveat is that the parent table (if not already) along with each new partition has to be added to the replication, as DDL changes are not replicated automatically. Each partition is treated as a separate table and is added to replication separately (like a table). For example, you can create a table with partitions as follows: ```sql CREATE TABLE order_changes ( order_id int, change_date date, type text, description text) PARTITION BY RANGE (change_date); ``` ```sql CREATE TABLE orderchangesdefault PARTITION OF order_changes DEFAULT; ``` Create a new partition: ```sql CREATE TABLE orderchanges202301 PARTITION OF orderchanges FOR VALUES FROM ('2023-01-01') TO ('2023-03-30'); ``` Assume the parent table and default partition are included in the replication stream. To add a table partition to the replication, follow the same steps for . To remove a table partition from replication, follow the same steps as . You can alter the schema of tables and indexes without having to stop writes on the Primary. Note: The ALTER DDL may kill some in-flight transactions. This is a temporary error. Retry any failed transactions. Alter the schema of tables and indexes in the following sequence: Alter the index on the Standby universe. Alter the index on the Primary universe.