INSERT brings a block with exactly the same data, it treats it as a retry and skips it. Deleting the rows does not remove that hash, so the second INSERT reports success but writes nothing. To insert the data again, give the INSERT its own insert_deduplication_token.
Example
Deduplication is on by default for replicated tables, including the tables of a ClickHouse Cloud service. A plainMergeTree table keeps no deduplication log unless you set non_replicated_deduplication_window, which this example does so that you can reproduce the behavior on a single server:
INSERT repeats the first one byte for byte, so it is skipped. The rows 1 and 2 are not in the table.
Confirm that the insert was deduplicated
The skipped block is recorded insystem.part_log as a NewPart event with the error INSERT_WAS_DEDUPLICATED:
DuplicatedInsertedBlocks profile event of the INSERT query in system.query_log is greater than zero as well.
Insert the data again
Pass a deduplication token that you have not used before. With a token, ClickHouse compares the token instead of the hash of the data:INSERT writes the rows twice:
Since ClickHouse 26.2,
deduplicate_insert is enable by default, and SET insert_deduplicate = 0 alone no longer turns deduplication off. On earlier releases use insert_deduplicate = 0.What decides whether a block is a duplicate
- The hash covers a whole inserted block. An
INSERTwith different rows, a different number of rows, or one changed value is not a duplicate. In the example, inserting only the row(1, 'new')would have worked. ALTER TABLE ... DELETEandDELETE FROMleave the stored hashes in place.TRUNCATE TABLEandALTER TABLE ... DROP PARTITIONremove the hashes of the data they drop, so the same data can be inserted again after them.- Replicated tables keep the hashes of the most recent
replicated_deduplication_windowblocks (10000 by default) and drop hashes older thanreplicated_deduplication_window_seconds(one hour by default). An identical block inserted after its hash has left the window is accepted. - Non-replicated
MergeTreetables deduplicate only ifnon_replicated_deduplication_windowis set. It is0by default.
Another cause: reading from a replica that is behind
If the service has several replicas and theDELETE, the INSERT and the SELECT do not all go to the same replica, the SELECT can run on a replica that has not received the new rows yet. They appear a moment later without any action. See How to achieve data read consistency in ClickHouse for ways to read your own writes.