INSERT repeats a block of data that the table has already received, ClickHouse skips the block and still reports success, so that a retried insert does not create duplicates. If rows seem to be missing after an insert that succeeded, check whether the insert was deduplicated:
- per query, the
DuplicatedInsertedBlocksprofile event insystem.query_logis greater than zero; - per table,
system.part_loghas aNewPartentry with the errorINSERT_WAS_DEDUPLICATED.
Example
Replicated*MergeTree tables keep a deduplication log by default. A plain MergeTree table does so only when non_replicated_deduplication_window is set, which this example uses to stay self-contained. A materialized view is included to show that its target table is deduplicated as well. The inserts turn off async_insert so that each one is written by its own query:
Find deduplicated inserts in the query log
duplicated_blocks marks an insert of which at least one block was skipped, in the table itself or in the target of a materialized view. Treat the value as a flag and not as an exact number of blocks. written_rows is no help here: it counts the rows that the query sent to the tables, whether or not they were kept.
Find the affected tables in the part log
Every part that an insert creates is recorded insystem.part_log as a NewPart event. A block that was skipped as a duplicate is recorded too, with the error INSERT_WAS_DEDUPLICATED (code 389):
system.part_log also has the query_id of the insert, so you can join it with system.query_log to see which query was deduplicated in which table:
Block with ID ... already exists as part ...; ignoring it, and for a replicated table Block with ID ... already exists locally as part ...; ignoring it. or ... already exists on other replicas as part ...; ignoring it., both at the Information level. You can search for them in system.text_log with message LIKE 'Block with ID%ignoring it%'.
Asynchronous inserts
Withasync_insert = 1, which is the default since ClickHouse 26.2, the data of several INSERT queries is collected in a buffer and written by a separate flush. Deduplication happens during the flush, so it is recorded in the entry of the flush in system.query_log (query_kind = 'AsyncInsertFlush') and not in the entries of the INSERT queries, where DuplicatedInsertedBlocks stays zero. Two events count it:
SelfDuplicatedAsyncInserts: inserts that repeated another insert of the same flush;DuplicatedAsyncInserts: inserts that repeated data the table had already received before the flush.
system.part_log shows only one case: a flush in which every insert repeated earlier data is recorded with INSERT_WAS_DEDUPLICATED, as above. A flush that dropped some of its inserts as duplicates and wrote the rest is an ordinary NewPart entry without an error, so for asynchronous inserts check the two events. See Deduplication for asynchronous inserts.
If the deduplication is not wanted
Deduplication compares whole inserted blocks, so it also triggers when you intentionally insert the same data twice, for example in a test. To insert identical data on purpose, give each insert a differentinsert_deduplication_token, or disable deduplication for the query with deduplicate_insert = 'disable'. How blocks are identified, how long they are remembered and how materialized views take part is described in Deduplicating inserts on retries.