Skip to content

fix(clickhouse): drop orphaned shard table after mirror resync exchange#4490

Merged
ilidemi merged 1 commit into
PeerDB-io:mainfrom
andreyzhelnin-st:fix-orphaned-shard-after-resync-main
Jun 24, 2026
Merged

fix(clickhouse): drop orphaned shard table after mirror resync exchange#4490
ilidemi merged 1 commit into
PeerDB-io:mainfrom
andreyzhelnin-st:fix-orphaned-shard-after-resync-main

Conversation

@andreyzhelnin-st

Copy link
Copy Markdown
Contributor

Problem

After a mirror resync on a clustered ClickHouse destination, the old local shard table (e.g. itunes_apps_shard) is left behind as an orphan.

During resync, RenameTables does:

  1. EXCHANGE TABLES itunes_apps AND itunes_apps_resync — swaps the two Distributed tables
  2. DROP TABLE itunes_apps_resync — drops the now-old Distributed shell

But this only handles the Distributed layer. The local shard that the original itunes_apps pointed to (itunes_apps_shard) has no Distributed table referencing it anymore and is never dropped, leaving it as dead storage.

Fix

Before the EXCHANGE, resolve the shard table name from the original Distributed table's engine_full definition using the existing getDistributedShardTable helper. After the exchange and Distributed drop succeed, explicitly drop that shard table.

If the shard lookup fails (e.g. non-Distributed engine, single-node deployment), we log a warning and continue — the swap itself is not blocked.

Test plan

  • Run a mirror resync on a clustered ClickHouse destination
  • Confirm itunes_apps_shard (or equivalent old shard) is gone after resync completes
  • Confirm itunes_apps Distributed table still points to the new timestamped shard and data is intact

After EXCHANGE TABLES during resync, the original Distributed table is
dropped but the local shard it referenced (e.g. itunes_apps_shard) was
never cleaned up. Resolve the shard name from the Distributed table
definition before the exchange, then drop it after the exchange succeeds.
@ilidemi

ilidemi commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Thanks for the fix!

@ilidemi
ilidemi merged commit 7f72af5 into PeerDB-io:main Jun 24, 2026
19 of 21 checks passed
@andreyzhelnin-st
andreyzhelnin-st deleted the fix-orphaned-shard-after-resync-main branch June 25, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants