pingcap / pingcap/tidb

Feature Request: Allow EXCHANGE PARTITION when only one table has TiFlash replicas

Open
#67,809 1 comment 0 reactions 0 assignees View on GitHub
contribution first-time-contributor
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Summary

`ALTER TABLE ... EXCHANGE PARTITION` currently fails with error 1736 ("Tables have different definitions") if the partitioned table has TiFlash replicas and the non-partitioned swap table does not (or has a different replica count). This forces workarounds that are either complex or slow.

## Use Case

A common ETL pattern for partitioned tables:

1. Create a temp table (no TiFlash replicas)
2. Load data into it via TiDB Lightning (local backend)
3. `EXCHANGE PARTITION` to atomically swap the data into the target partition
4. Drop the temp table

This pattern is efficient because the exchange is a pure metadata operation (~1 s regardless of data size). When the partitioned table has TiFlash replicas, the pattern breaks.

## Current Behavior

```sql
-- partitioned table has TIFLASH REPLICA 3, swap table has TIFLASH REPLICA 0
ALTER TABLE t EXCHANGE PARTITION p WITH TABLE swap WITHOUT VALIDATION;
-- ERROR 1736: Tables have different definitions
```

Workaround requires setting TIFLASH REPLICA 3 on the swap table and polling until `AVAILABLE = 1` before the exchange — adding 30–60 s of latency per partition load and making parallel loads more complex.

## Expected Behavior

`EXCHANGE PARTITION` should succeed when one table has TiFlash replicas and the other does not (or has a different count), provided the row/column definitions match. After the exchange, TiDB should propagate the partitioned table's TiFlash replica policy to the newly swapped-in partition data automatically, the same way it handles newly added partitions.

## Why This Makes Sense

TiFlash replicas are a storage policy, not a schema definition. The current check conflates storage configuration with structural compatibility. A newly created partition (via `ADD PARTITION`) has no TiFlash data yet and replicates asynchronously — the same behavior could apply to an exchanged partition.

## Environment

- TiDB version: v8.5.0
- Partition type: LIST COLUMNS

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.