> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to check whether inserts were deduplicated

> Find inserts that ClickHouse skipped as duplicates: the DuplicatedInsertedBlocks event in system.query_log and the INSERT_WAS_DEDUPLICATED entries of system.part_log.

When an `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 `DuplicatedInsertedBlocks` profile event in [`system.query_log`](https://clickhouse.com/docs/reference/system-tables/query_log) is greater than zero;
* per table, [`system.part_log`](https://clickhouse.com/docs/reference/system-tables/part_log) has a `NewPart` entry with the error `INSERT_WAS_DEDUPLICATED`.

Asynchronous inserts are deduplicated by the flush that writes them and are recorded differently: see [Asynchronous inserts](#asynchronous-inserts).

## Example

`Replicated*MergeTree` tables keep a deduplication log by default. A plain `MergeTree` table does so only when [`non_replicated_deduplication_window`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/other#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:

```sql theme={null}
CREATE TABLE events
(
    id UInt64,
    message String
)
ENGINE = MergeTree
ORDER BY id
SETTINGS non_replicated_deduplication_window = 100;

CREATE TABLE events_per_message
(
    message String,
    events UInt64
)
ENGINE = SummingMergeTree
ORDER BY message
SETTINGS non_replicated_deduplication_window = 100;

CREATE MATERIALIZED VIEW events_mv TO events_per_message AS
SELECT
    message,
    count() AS events
FROM events
GROUP BY message;

INSERT INTO events SETTINGS async_insert = 0 VALUES (1, 'first'), (2, 'second');
INSERT INTO events SETTINGS async_insert = 0 VALUES (1, 'first'), (2, 'second');
INSERT INTO events SETTINGS async_insert = 0 VALUES (3, 'third');

SELECT count() FROM events;
```

```text theme={null}
   ┌─count()─┐
1. │       3 │
   └─────────┘
```

All three inserts succeeded, but the table has three rows, not five: the second insert repeated the first one and was skipped.

## Find deduplicated inserts in the query log

```sql theme={null}
SYSTEM FLUSH LOGS;

SELECT
    query,
    written_rows,
    ProfileEvents['DuplicatedInsertedBlocks'] AS duplicated_blocks
FROM system.query_log
WHERE type = 'QueryFinish'
    AND query_kind = 'Insert'
    AND has(tables, 'default.events')
    AND event_time >= now() - INTERVAL 10 MINUTE
ORDER BY event_time_microseconds;
```

```text theme={null}
   ┌─query────────────────────────────────────────────────┬─written_rows─┬─duplicated_blocks─┐
1. │ INSERT INTO events SETTINGS async_insert = 0 VALUES  │            4 │                 0 │
2. │ INSERT INTO events SETTINGS async_insert = 0 VALUES  │            4 │                 4 │
3. │ INSERT INTO events SETTINGS async_insert = 0 VALUES  │            2 │                 0 │
   └──────────────────────────────────────────────────────┴──────────────┴───────────────────┘
```

A non-zero `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 in `system.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):

```sql theme={null}
SELECT
    database,
    table,
    countIf(error = 0) AS new_parts,
    countIf(errorCodeToName(error) = 'INSERT_WAS_DEDUPLICATED') AS deduplicated_blocks
FROM system.part_log
WHERE event_type = 'NewPart'
    AND event_time >= now() - INTERVAL 10 MINUTE
GROUP BY
    database,
    table
ORDER BY
    database,
    table;
```

```text theme={null}
   ┌─database─┬─table──────────────┬─new_parts─┬─deduplicated_blocks─┐
1. │ default  │ events             │         2 │                   1 │
2. │ default  │ events_per_message │         2 │                   1 │
   └──────────┴────────────────────┴───────────┴─────────────────────┘
```

`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:

```sql theme={null}
SELECT
    q.query,
    p.table,
    errorCodeToName(p.error) AS result
FROM system.part_log AS p
INNER JOIN system.query_log AS q ON q.query_id = p.query_id
WHERE p.event_type = 'NewPart'
    AND q.type = 'QueryFinish'
    AND p.event_time >= now() - INTERVAL 10 MINUTE
ORDER BY
    p.event_time_microseconds,
    p.table;
```

```text theme={null}
   ┌─query────────────────────────────────────────────────┬─table──────────────┬─result──────────────────┐
1. │ INSERT INTO events SETTINGS async_insert = 0 VALUES  │ events             │ OK                      │
2. │ INSERT INTO events SETTINGS async_insert = 0 VALUES  │ events_per_message │ OK                      │
3. │ INSERT INTO events SETTINGS async_insert = 0 VALUES  │ events             │ INSERT_WAS_DEDUPLICATED │
4. │ INSERT INTO events SETTINGS async_insert = 0 VALUES  │ events_per_message │ INSERT_WAS_DEDUPLICATED │
5. │ INSERT INTO events SETTINGS async_insert = 0 VALUES  │ events             │ OK                      │
6. │ INSERT INTO events SETTINGS async_insert = 0 VALUES  │ events_per_message │ OK                      │
   └──────────────────────────────────────────────────────┴────────────────────┴─────────────────────────┘
```

The server log has the same information as text. For a non-replicated table the message is `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`](https://clickhouse.com/docs/reference/system-tables/text_log) with `message LIKE 'Block with ID%ignoring it%'`.

## Asynchronous inserts

With `async_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.

```sql theme={null}
SELECT
    event_time,
    ProfileEvents['SelfDuplicatedAsyncInserts'] AS duplicates_within_flush,
    ProfileEvents['DuplicatedAsyncInserts'] AS duplicates_of_earlier_inserts
FROM system.query_log
WHERE type = 'QueryFinish'
    AND query_kind = 'AsyncInsertFlush'
    AND has(tables, 'default.events')
    AND event_time >= now() - INTERVAL 10 MINUTE
    AND (duplicates_within_flush > 0 OR duplicates_of_earlier_inserts > 0)
ORDER BY event_time_microseconds;
```

`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](https://clickhouse.com/docs/concepts/features/operations/insert/deduplicating-inserts-on-retries.md#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 different [`insert_deduplication_token`](https://clickhouse.com/docs/reference/settings/session-settings/insert#insert_deduplication_token), or disable deduplication for the query with [`deduplicate_insert = 'disable'`](https://clickhouse.com/docs/reference/settings/session-settings/deduplicate-insert#deduplicate_insert). How blocks are identified, how long they are remembered and how materialized views take part is described in [Deduplicating inserts on retries](https://clickhouse.com/docs/concepts/features/operations/insert/deduplicating-inserts-on-retries.md).
