Plain `MergeTree`: `REPLACE PARTITION` and `TRUNCATE` in a transaction keep the deduplication block IDs of removed parts, so a later identical `INSERT` is silently dropped
Company or project name
No response
Describe what's wrong
With non_replicated_deduplication_window > 0, MergeTreeDeduplicationLog has to forget the block IDs of parts that are removed. Otherwise a later INSERT of the same data is deduplicated against a part that no longer exists.
It returns success and writes nothing. The non-transactional DROP PART, DROP PARTITION, DETACH and TRUNCATE do this in StorageMergeTree::renameAndCommitEmptyParts, and the transactional DROP/DETACH in StorageMergeTree::dropPartsImpl.
Existing tests depend on it, for example 03755_insert_select_duduplication.sh re-inserts the same data after each TRUNCATE. Some removal paths do not:
REPLACE PARTITION:StorageMergeTree::replacePartitionFromremoves the destination's parts withremovePartsInRangeFromWorkingSetand never callsMergeTreeDeduplicationLog::dropPart. The loss is permanent until the window evicts the IDs:- The empty part that covers the replaced range is rolled back right after it is written (
rollback_tx_guard.resetinMergeTreeData::removePartsInRangeFromWorkingSetAndGetPartsToRemoveFromZooKeeper), so it never becomesActive, and the empty-part cleanup never sees it. - A later
DROP PARTITIONorTRUNCATEdoesn't work because the new parts have higher block numbers and never contain the old ranges.
This has been the behaviour since the deduplication log was introduced (2021-04-02).
ReplicatedMergeTreeclears the blocks in this case (clearBlocksInPartitioninStorageReplicatedMergeTree::replacePartitionFrom).- The empty part that covers the replaced range is rolled back right after it is written (
TRUNCATEinside a transaction**: the transactional branch ofStorageMergeTree::truncateonly callsremovePartsFromWorkingSet, andCOMMITdoes not touch the log. This is a regression from #52945 (2023-08-03). Before ittruncatealways went throughrenameAndCommitEmptyParts. That PR added the erasure to the new transactionalDROPpath (dropPartsImpl) but not to the new transactionalTRUNCATEbranch. Transactions are experimental, so this can be further investigated.MOVE PARTITION ... TO TABLE, source side: the source parts are covered by empty parts without touching the log. The IDs are erased only when the cleanup thread later drops those empty parts (clearEmptyParts→dropPartNoWaitNoThrow):- With default settings that happens within roughly 0 to 310 s.
- It never happens with
remove_empty_parts = 0. - The IDs also stay if a merge combines an empty part with non-empty neighbours before the cleanup reaches it.
#101039 fixes this on its branch. That PR is large and unrelated.
A related ordering problem affects the paths that do erase the IDs: renameAndCommitEmptyParts, dropPartNoWaitNoThrow and dropPartsImpl. Each removes the part from the working set first, and only afterwards, without lockParts, erases its IDs. Nothing orders an INSERT against that window:
INSERTtakes a shared table lock andlockParts.ALTER ... DROP PART/PARTITIONandTRUNCATEof aMergeTreetake only a shared table lock.
The window grows with the number of dropped parts times the window size. For a transactional DETACH it also includes copying the parts to detached/. Two sessions are enough to hit it, and the retry pattern hits it:
- A client gets an unknown status for its
INSERT. - It checks the table, as the
UNKNOWN_STATUS_OF_INSERTmessage from #122543 advises, and sees the rows gone because aDROP PARTITIONhas just removed the part. - It retries, and the retry is deduplicated against the removed part while the
DROPis still erasing IDs.
The rows are lost although the client did what the server told it to.
On the renameAndCommitEmptyParts and dropPartNoWaitNoThrow paths, the IDs are also left behind when the server is killed between the removal and the erasure, or when the log write throws. It can throw on an I/O error, or with ABORTED when a concurrent DETACH TABLE/DROP TABLE shuts the log down. The transactional path is not affected, because any exception rolls the whole transaction back. The empty covering part usually repairs this later when the cleanup thread drops it, but not with remove_empty_parts = 0, and not if the empty part is first merged with non-empty parts.
In the opposite direction, a DROP inside a transaction erases the IDs before COMMIT. After ROLLBACK the parts come back without their IDs, so a retry of the original insert duplicates rows. The transactional TRUNCATE + ROLLBACK keeps them, because it never erased them. So the two transactional paths disagree in both directions.
Does it reproduce on the most recent release?
Yes
How to reproduce
1 affects every release since the deduplication log was introduced in 2021, and 2 every release since #52945.
The transactional TRUNCATE case cannot run on the fiddle, because it needs allow_experimental_transactions in the server config and Keeper. It is shown separately below.
REPLACE PARTITION:
CREATE TABLE dst (p UInt8, x UInt32) ENGINE = MergeTree PARTITION BY p ORDER BY x
SETTINGS non_replicated_deduplication_window = 100;
CREATE TABLE src AS dst;
INSERT INTO dst VALUES (1, 1);
INSERT INTO src VALUES (1, 2);
ALTER TABLE dst REPLACE PARTITION 1 FROM src;
INSERT INTO dst VALUES (1, 1);
SELECT x FROM dst ORDER BY x;TRUNCATE in a transaction (needs allow_experimental_transactions in the server config and Keeper for the transaction log). Both inserts run outside the transaction:
CREATE TABLE t (x UInt32) ENGINE = MergeTree ORDER BY x
SETTINGS non_replicated_deduplication_window = 100;
INSERT INTO t VALUES (1);
BEGIN TRANSACTION;
TRUNCATE TABLE t;
COMMIT;
INSERT INTO t VALUES (1);
SELECT count() FROM t;MOVE PARTITION (remove_empty_parts = 0 keeps the cleanup thread from repairing the log, which makes the result deterministic):
CREATE TABLE a (p UInt8, x UInt32) ENGINE = MergeTree PARTITION BY p ORDER BY x
SETTINGS non_replicated_deduplication_window = 100, remove_empty_parts = 0;
CREATE TABLE b (p UInt8, x UInt32) ENGINE = MergeTree PARTITION BY p ORDER BY x;
INSERT INTO a VALUES (1, 1);
ALTER TABLE a MOVE PARTITION 1 TO TABLE b;
INSERT INTO a VALUES (1, 1);
SELECT count() FROM a;Expected behavior
The second INSERT writes its row:
dstreturns1, 2, andareturns1, as withReplicatedMergeTree.treturns1, as with the same script using a non-transactionalTRUNCATE.
Actual Behavior:
The second INSERT returns success and writes nothing: dst returns 2, t returns 0 and a returns 0. The server log shows Block with ID ... already exists as part ...; ignoring it, naming a part that has been removed.
Error message and/or stacktrace
None. The second INSERT silently succeeds.
Related issues and pull requests
Caused by: #52945 Related: #122543 Related: #122275 Related: #101039 Related: #122290 Related: #111577
Additional context
Lock order: MergeTreeDeduplicationLog::state_mutex is never held while lockParts is taken, because the log class has no reference to the storage, so calling dropPart under lockParts cannot deadlock on master. https://github.com/ClickHouse/ClickHouse/pull/101039 adds a callback into the log and runs its erasure outside lockParts under leader_election.
Also, StorageMergeTree::dropPartNoWaitNoThrow throws despite its name when the log write fails (ABORTED, or an I/O error while erasing the IDs of another empty part). Through clearEmptyParts, that exception reaches the end of DROP PART, DROP PARTITION and TRUNCATE, which then report failure after they have already removed the data. It is rare, but the error is misleading.
Source: ClickHouse/ClickHouse