> ## 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.

# Resolving TOO_MANY_PARTS (error 252) caused by too many partitions

> Tell apart the messages of error 252 TOO_MANY_PARTS, recognize the two that are caused by the partition key and not by slow merges, and fix the partition key.

Error 252 (`TOO_MANY_PARTS`) is raised for several different limits, and only the message text tells you which one. Two of the messages point to the partition key and not to slow merges: `Too many parts (N) in all partitions in total` and `Too many partitions for single INSERT block`. Parts are merged only within one partition, so a table can never have fewer active parts than partitions. If the two numbers are close, merges have nothing left to do, and the fix is a coarser partition key.

## Which limit was reached

| Message starts with | Limit | Default | What is counted |
| - | - | - | - |
| `Too many parts (N with average size of S) in table` ... `Merges are processing significantly slower than inserts` | [`parts_to_throw_insert`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/parts-to#parts_to_throw_insert) | 3000 | Active parts in one partition |
| `Too many parts (N) in all partitions in total in table` | [`max_parts_in_total`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/max-parts#max_parts_in_total) | 100000 | Active parts in the whole table |
| `Too many partitions for single INSERT block (more than N)` | [`max_partitions_per_insert_block`](https://clickhouse.com/docs/reference/settings/session-settings/max-partitions#max_partitions_per_insert_block) | 100 | Partitions that one inserted block is split into |
| `Too many inactive parts (N) in table` | `inactive_parts_to_throw_insert` | 0 (disabled) | Inactive parts in one partition |
| `Too many dead blobs in queue (N) on disks of table` | `dead_blobs_to_throw_insert` | 1000000 | Blobs pending removal on the disks of the table |
| `Cannot move N parts at once` | `max_parts_to_move` | 1000 | Parts moved by one `MOVE PARTITION` |

The first row is the classic case of too many small inserts, covered in [Resolving the "Too many parts" exception](https://clickhouse.com/docs/resources/support-center/knowledge-base/troubleshooting/exception-too-many-parts.md). The rest of this article is about the second and the third row.

To see which limits your inserts reached, group the failed queries by message:

```sql theme={null}
SELECT
    multiIf(
        exception LIKE '%in all partitions in total%', 'max_parts_in_total',
        exception LIKE '%Too many partitions for single INSERT block%', 'max_partitions_per_insert_block',
        exception LIKE '%Merges are processing significantly slower than inserts%', 'parts_to_throw_insert',
        'other') AS limit_reached,
    count() AS failed_queries
FROM system.query_log
WHERE exception_code = 252 AND event_date >= today() - 1
GROUP BY limit_reached
ORDER BY limit_reached;
```

## Example

The table is partitioned by day and tenant. To keep the example small, `max_parts_in_total` is lowered to 100.

```sql theme={null}
CREATE TABLE metrics
(
    event_time DateTime,
    tenant_id UInt32,
    metric LowCardinality(String),
    value Float64
)
ENGINE = MergeTree
PARTITION BY (toDate(event_time), tenant_id)
ORDER BY (tenant_id, metric, event_time)
SETTINGS max_parts_in_total = 100;

INSERT INTO metrics
SELECT
    toDateTime('2026-01-01 00:00:00') + toIntervalDay(number % 40) + toIntervalSecond(number),
    intDiv(number, 40) % 3,
    'cpu',
    number
FROM numbers(1200);
```

The data covers 40 days and 3 tenants, that is 120 partitions, and the insert is rejected:

```text theme={null}
Code: 252. DB::Exception: Too many partitions for single INSERT block (more than 100). The limit is controlled by 'max_partitions_per_insert_block' setting. Large number of partitions is a common misconception. It will lead to severe negative performance impact, including slow server startup, slow INSERT queries and slow SELECT queries. Recommended total number of partitions for a table is under 1000..10000. Please note, that partitioning is not intended to speed up SELECT queries (ORDER BY key is sufficient to make range queries fast). Partitions are intended for data manipulation (DROP PARTITION, etc). (TOO_MANY_PARTS)
```

Lifting the limit for this query lets the insert through, and it creates one part for each partition:

```sql theme={null}
INSERT INTO metrics
SETTINGS max_partitions_per_insert_block = 0
SELECT
    toDateTime('2026-01-01 00:00:00') + toIntervalDay(number % 40) + toIntervalSecond(number),
    intDiv(number, 40) % 3,
    'cpu',
    number
FROM numbers(1200);
```

The table now has 120 parts, more than its `max_parts_in_total`, so the next insert fails on the other limit:

```sql theme={null}
INSERT INTO metrics VALUES ('2026-03-01 00:00:00', 1, 'cpu', 1);
```

```text theme={null}
Code: 252. DB::Exception: Too many parts (120) in all partitions in total in table 'default.metrics (<table UUID>)'. This indicates wrong choice of partition key. The threshold can be modified with 'max_parts_in_total' setting in <merge_tree> element in config.xml or with per-table setting. (TOO_MANY_PARTS)
```

After these two failures the query on `system.query_log` returns:

```text theme={null}
   ┌─limit_reached───────────────────┬─failed_queries─┐
1. │ max_partitions_per_insert_block │              1 │
2. │ max_parts_in_total              │              1 │
   └─────────────────────────────────┴────────────────┘
```

## Compare the number of parts with the number of partitions

```sql theme={null}
SELECT
    count() AS active_parts,
    uniqExact(partition_id) AS partitions,
    sum(rows) AS rows
FROM system.parts
WHERE database = currentDatabase() AND `table` = 'metrics' AND active;
```

```text theme={null}
   ┌─active_parts─┬─partitions─┬─rows─┐
1. │          120 │        120 │ 1200 │
   └──────────────┴────────────┴──────┘
```

There is one part per partition and ten rows per part. Merges can't reduce this: `OPTIMIZE TABLE metrics FINAL` leaves 120 parts. When `active_parts` is close to `partitions`:

* Don't look for the cause in merge throughput or insert batching. The number of partitions is the lower bound for the number of parts.
* Raising `max_parts_in_total` only buys time until the number of partitions grows to the new value.
* Check which elements of the partition key multiply the count. A tuple key produces up to one partition for every combination of its elements. Here it is 40 days times 3 tenants:

```sql theme={null}
SELECT
    uniqExact(toDate(event_time)) AS days,
    uniqExact(tenant_id) AS tenants
FROM metrics;
```

```text theme={null}
   ┌─days─┬─tenants─┐
1. │   40 │       3 │
   └──────┴─────────┘
```

## Fix the partition key

Compare the partition key with the sorting key. In the example `tenant_id` is the first column of `ORDER BY`, so the primary index already narrows a query to one tenant, and repeating the column in `PARTITION BY` only multiplies the number of partitions. A daily partition is also finer than this amount of data needs. See [Choosing a partitioning key](https://clickhouse.com/docs/concepts/best-practices/partitioning-keys.md#choose-a-low-cardinality-partitioning-key) for guidance.

The partition key of an existing table can't be changed: there is no `ALTER TABLE ... MODIFY PARTITION BY`. Create a new table, copy the data, and swap the tables:

```sql theme={null}
CREATE TABLE metrics_new
(
    event_time DateTime,
    tenant_id UInt32,
    metric LowCardinality(String),
    value Float64
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(event_time)
ORDER BY (tenant_id, metric, event_time);

INSERT INTO metrics_new SELECT * FROM metrics;

EXCHANGE TABLES metrics AND metrics_new;
```

The same query on `system.parts` now returns:

```text theme={null}
   ┌─active_parts─┬─partitions─┬─rows─┐
1. │            2 │          2 │ 1200 │
   └──────────────┴────────────┴──────┘
```

Writers that insert during the copy have to be paused or pointed to the new table, and you can drop `metrics_new` (the old data) when the result is verified. If old partitions are no longer needed, a `TTL` or `ALTER TABLE ... DROP PARTITION` also lowers the count, but it doesn't change how fast new partitions appear.

## When one INSERT spans many partitions of a well-partitioned table

`Too many partitions for single INSERT block` can also appear with a reasonable key, for example when a backfill loads several months into a table partitioned by day. The options are:

* Split the load so that each `INSERT` covers fewer partitions, for example one month per query.
* Raise `max_partitions_per_insert_block` for that query, or set it to 0 to remove the limit, as in the example above.
* Set [`throw_on_max_partitions_per_insert_block`](https://clickhouse.com/docs/reference/settings/session-settings/throw-on#throw_on_max_partitions_per_insert_block) to 0 to log a warning and continue.

These are query-level settings: set them in the `INSERT` query, in the session, or in the settings profile of the user that runs the insert. A `SETTINGS` clause inside the `SELECT` of a materialized view doesn't apply to the write into the view's target table, so set them on the `INSERT` into the source table.

Every partition in an inserted block becomes a separate part. Lifting the limit is fine for a bounded backfill into a table with a coarse key. On a table that is already close to `max_parts_in_total` it moves the failure from one message to the other, as in the example.

## Related pages

* [Resolving the "Too many parts" exception](https://clickhouse.com/docs/resources/support-center/knowledge-base/troubleshooting/exception-too-many-parts.md)
* [Choosing a partitioning key](https://clickhouse.com/docs/concepts/best-practices/partitioning-keys.md)
* [Custom partitioning key](https://clickhouse.com/docs/reference/engines/table-engines/mergetree-family/custom-partitioning-key.md)
