> ## 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 "Part number exceeded 10000 while writing to S3" during merges or OPTIMIZE FINAL

> Why a merge or OPTIMIZE FINAL on an S3 disk fails with INVALID_CONFIG_PARAMETER "Part number exceeded", what the limit on the size of one file is, and how to get below it.

The error means that a single file of a data part became too large to be uploaded to S3 with the configured multipart upload settings. The word "part" in the message is a part of an S3 multipart upload, not a ClickHouse data part. To fix it, keep the data parts smaller (do not run `OPTIMIZE ... FINAL` on very large partitions, keep `max_bytes_to_merge_at_max_space_in_pool` at its default), or change the upload settings of the disk so that upload parts can grow.

## The error

The message has this form (code 318, `INVALID_CONFIG_PARAMETER`). The placeholders stand for the number of bytes written so far and for the effective settings:

```text theme={null}
Part number exceeded 10000 while writing <bytes> bytes to S3. Check min_upload_part_size = <...>, max_upload_part_size = <...>, upload_part_size_multiply_factor = <...>, upload_part_size_multiply_parts_count_threshold = <...>, max_single_part_upload_size = <...>
```

It appears when a background merge or an `OPTIMIZE` writes its result to a disk backed by S3. A background merge that fails is retried and fails again, so the parts it was supposed to merge stay unmerged. [How to find failed merges and mutations](https://clickhouse.com/docs/resources/support-center/knowledge-base/troubleshooting/find-failed-merges-and-mutations.md) shows where to look for such failures.

## Why it happens

On an S3 disk, every file of a data part is stored as a separate object. In a `Wide` part each column has its own data file, so the largest objects are the data files of the largest columns. A file larger than `max_single_part_upload_size` is sent as a multipart upload, and a multipart upload may have at most `max_part_number` parts. The default of 10000 is the limit of AWS S3 itself.

The size of the upload parts is chosen as follows:

* The first part has the size `max_single_part_upload_size` (32 MiB by default).
* The following parts start at `min_upload_part_size` (16 MiB by default). After every `upload_part_size_multiply_parts_count_threshold` parts (500 by default), the size is multiplied by `upload_part_size_multiply_factor` (2 by default), up to `max_upload_part_size` (5 GiB by default).
* If `strict_upload_part_size` is set, every part has exactly that size and nothing grows.

The largest file that fits is the sum of these part sizes. You can compute it for your settings. The values below are the defaults:

```sql theme={null}
WITH
    10000 AS max_part_number,
    16777216 AS min_upload_part_size,
    5368709120 AS max_upload_part_size,
    2 AS upload_part_size_multiply_factor,
    500 AS upload_part_size_multiply_parts_count_threshold,
    33554432 AS max_single_part_upload_size
SELECT formatReadableSize(sum(
    if(part_number = 1,
        greatest(min_upload_part_size, least(max_single_part_upload_size, max_upload_part_size)),
        least(
            min_upload_part_size * toUInt64(pow(upload_part_size_multiply_factor, intDiv(part_number - 1, upload_part_size_multiply_parts_count_threshold))),
            max_upload_part_size)))) AS max_object_size
FROM
(
    SELECT number + 1 AS part_number
    FROM numbers(max_part_number)
);
```

```text theme={null}
   ┌─max_object_size─┐
1. │ 30.75 TiB       │
   └─────────────────┘
```

With the defaults the limit is far above the size of any reasonable data part. It becomes reachable when the upload parts do not grow. With `upload_part_size_multiply_factor = 1` the same query returns 156.27 GiB, and with `strict_upload_part_size` the limit is `max_part_number` multiplied by that size, which is 156.25 GiB for 16 MiB.

So the error needs two things at once: upload settings that keep the upload parts small, and a data part with a very large column file. Background merges limit the total size of the parts they merge by [`max_bytes_to_merge_at_max_space_in_pool`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/max-bytes#max_bytes_to_merge_at_max_space_in_pool) (150 GiB by default), which is roughly the largest part they can produce. Very large files come from raising that setting, or from `OPTIMIZE ... FINAL`, which ignores it and merges a whole partition into one part.

## Find the large parts and columns

Compare the settings printed in the error message with the size of the largest column files of the table. This query lists them, with the name of your database and table in place of `db` and `events`:

```sql theme={null}
SELECT
    partition,
    name AS part,
    column,
    formatReadableSize(column_data_compressed_bytes) AS column_size
FROM system.parts_columns
WHERE database = 'db' AND table = 'events' AND active
ORDER BY column_data_compressed_bytes DESC
LIMIT 10;
```

A merge fails when the column file of its result would exceed the limit, that is, roughly when the sizes of that column in the source parts add up to more than the limit.

## Keep the data parts smaller

* Do not run `OPTIMIZE TABLE ... FINAL` on partitions of this size. See [Avoid `OPTIMIZE FINAL`](https://clickhouse.com/docs/concepts/best-practices/avoid-optimize-final).

* If `max_bytes_to_merge_at_max_space_in_pool` was raised for the table, return it to the default, or lower it further so that the failing merge is no longer selected:

  ```sql theme={null}
  ALTER TABLE db.events RESET SETTING max_bytes_to_merge_at_max_space_in_pool;

  ALTER TABLE db.events MODIFY SETTING max_bytes_to_merge_at_max_space_in_pool = 53687091200;
  ```

* If you do need one part per partition, choose a partitioning key that keeps partitions smaller than the limit.

## Or allow larger objects

On a self-managed server, change the upload settings. For data written by merges they are taken from the configuration of the S3 disk, where each setting has the prefix `s3_`:

```xml theme={null}
<clickhouse>
    <storage_configuration>
        <disks>
            <s3_disk>
                <type>s3</type>
                <endpoint>https://example-bucket.s3.amazonaws.com/data/</endpoint>
                <!-- credentials and the other parameters of your disk -->
                <s3_upload_part_size_multiply_factor>2</s3_upload_part_size_multiply_factor>
                <s3_upload_part_size_multiply_parts_count_threshold>500</s3_upload_part_size_multiply_parts_count_threshold>
                <s3_max_upload_part_size>5368709120</s3_max_upload_part_size>
            </s3_disk>
        </disks>
    </storage_configuration>
</clickhouse>
```

The values shown are the defaults. Look in your disk configuration for other values of these settings and for `s3_strict_upload_part_size`. If the storage requires parts of equal size and you use `s3_strict_upload_part_size`, raise it until `s3_max_part_number` multiplied by it is larger than your largest column file. Larger upload parts need more memory for every file that is being written, see [`s3_max_inflight_parts_for_one_file`](https://clickhouse.com/docs/reference/settings/session-settings/s3-max#s3_max_inflight_parts_for_one_file).

The same error can come from a query that writes one very large file to S3, for example `INSERT INTO FUNCTION s3(...)`. There the query-level settings apply: increase [`s3_max_upload_part_size`](https://clickhouse.com/docs/reference/settings/session-settings/s3-max#s3_max_upload_part_size) or [`s3_upload_part_size_multiply_factor`](https://clickhouse.com/docs/reference/settings/session-settings/s3-upload#s3_upload_part_size_multiply_factor), as described for [`s3_max_part_number`](https://clickhouse.com/docs/reference/settings/session-settings/s3-max#s3_max_part_number).

## ClickHouse Cloud

In ClickHouse Cloud the server configuration is managed for you and [system settings](https://clickhouse.com/docs/products/cloud/guides/cloud-compatibility.md#system-settings) are not configurable, so keep the data parts smaller as described above. If background merges keep failing with this error, contact ClickHouse support.
