> ## 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 troubleshoot failed asynchronous inserts

> Use system.asynchronous_insert_log to find asynchronous inserts that failed to parse or to flush, read the exception, and see which other inserts of the batch were lost.

Every `INSERT` that goes through the asynchronous insert queue is recorded in [`system.asynchronous_insert_log`](https://clickhouse.com/docs/reference/system-tables/asynchronous_insert_log) with a status and, if it failed, the exception. This matters most with `wait_for_async_insert = 0`: the client receives a successful response as soon as the data is buffered, before it is parsed and written, so this table and the server log are the only places where the failure is reported.

## Check whether asynchronous inserts fail

Two counters in [`system.events`](https://clickhouse.com/docs/reference/system-tables/events) show the number of asynchronous `INSERT` queries and the number of failed ones since the server started. A counter that is still zero is not listed.

```sql theme={null}
SELECT event, value
FROM system.events
WHERE event IN ('AsyncInsertQuery', 'FailedAsyncInsertQuery');
```

For the details, group the log by table and status:

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

SELECT
    toStartOfHour(event_time) AS hour,
    database,
    table,
    status,
    count() AS inserts,
    sum(rows) AS rows,
    formatReadableSize(sum(bytes)) AS size,
    any(exception) AS example_exception
FROM system.asynchronous_insert_log
WHERE event_date >= today() - 1
GROUP BY ALL
ORDER BY hour DESC, database, table, status;
```

Each row of the log is one `INSERT` query sent by a client. The `status` column has three values:

| Status | Meaning |
| - | - |
| `Ok` | The data of the query was written to the table. |
| `ParsingError` | The data of this query could not be parsed. Nothing from this query was inserted. The other queries collected into the same batch are not affected. |
| `FlushError` | The data was parsed, but writing the batch to the table failed. |

Log entries are written with a delay of a few seconds. `SYSTEM FLUSH LOGS` writes them immediately.

## Read the error of a failed insert

```sql theme={null}
SELECT
    event_time,
    database,
    table,
    format,
    status,
    rows,
    bytes,
    query_id,
    flush_query_id,
    exception
FROM system.asynchronous_insert_log
WHERE event_date >= today() - 1
    AND status != 'Ok'
ORDER BY event_time DESC
LIMIT 20
FORMAT Vertical;
```

* `exception` is the full error message.
* `query_id` is the ID of the `INSERT` query that the client sent, which you can match with the logs of the client.
* `flush_query_id` is the ID of the background query that wrote the batch. Use it to find the flush in [`system.query_log`](https://clickhouse.com/docs/reference/system-tables/query_log) (`WHERE query_id = '...'`), which has the duration, the number of written rows and the profile events of the flush.

ClickHouse collects the inserts that have the same query text, settings and user into one batch and writes the batch as a whole. When the write fails, every insert of the batch gets the status `FlushError` with the same exception. Before you send the data again, check the target table: a write that fails in a materialized view has already stored its rows in the table itself. The following query shows how many inserts each failed flush took with it:

```sql theme={null}
SELECT
    flush_query_id,
    count() AS inserts_in_batch,
    sum(rows) AS rows,
    any(exception) AS exception
FROM system.asynchronous_insert_log
WHERE event_date >= today() - 1
    AND status = 'FlushError'
GROUP BY flush_query_id
ORDER BY max(event_time) DESC
LIMIT 20;
```

## Typical causes

**`ParsingError`**: the data does not match the format named in the query or the types of the columns, for example a string in a numeric field or invalid JSON. Fix the producer; the rejected data is not kept anywhere on the server.

**`FlushError`**: any error that is raised while the batch is written. Common ones:

* `TOO_MANY_PARTS`: the table has too many active parts and rejects new inserts. See [Resolving the "Too many parts" exception](https://clickhouse.com/docs/resources/support-center/knowledge-base/troubleshooting/exception-too-many-parts.md).
* `ACCESS_DENIED`, with the message `Not enough privileges. To execute this query, it's necessary to have the grant ...`. The `INSERT` grant on the target table is checked when the insert is received, so a client that lacks it gets the error immediately. Access is checked again when the batch is written, and that check also covers the materialized views attached to the table: if the definer of a materialized view lacks a grant on the table that the view reads from or writes to, the insert into the source table fails at flush time. The message names the user and the missing grant. Compare it with the output of [`SHOW GRANTS`](https://clickhouse.com/docs/reference/statements/show#show-grants) for that user and add the grant with [`GRANT`](https://clickhouse.com/docs/reference/statements/grant). The same happens if a grant is revoked between the moment the insert is received and the flush.

## Make the client see the error

With `wait_for_async_insert = 1`, which is the default, the `INSERT` query returns only after its batch is written, and a parsing or flush error is returned to the client, which can then retry or report it. If your clients use [`wait_for_async_insert = 0`](https://clickhouse.com/docs/reference/settings/session-settings/wait-for#wait_for_async_insert), they cannot detect these failures, and you have to monitor `system.asynchronous_insert_log` instead. The trade-offs of the two modes are described in the "Error handling" section of [Asynchronous inserts](https://clickhouse.com/docs/concepts/features/operations/insert/asyncinserts.md).

## Where the log is available

* On a self-managed server the table exists if the server configuration has the [`asynchronous_insert_log`](https://clickhouse.com/docs/reference/settings/server-settings/settings/asynchronous#asynchronous_insert_log) section. The default `config.xml` includes it.
* The log is local to each server. On a cluster, read it from all replicas with the [`clusterAllReplicas`](https://clickhouse.com/docs/reference/functions/table-functions/cluster) table function. In ClickHouse Cloud the cluster is named `default`: `FROM clusterAllReplicas(default, system.asynchronous_insert_log)`. See [System tables in ClickHouse Cloud](https://clickhouse.com/docs/reference/system-tables/overview.md#system-tables-in-clickhouse-cloud).
* Inserts that are still waiting in the queue are not in the log yet. They are listed in [`system.asynchronous_inserts`](https://clickhouse.com/docs/reference/system-tables/asynchronous_inserts).
