> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-poc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# merge_chunks()

> Merge two or more chunks into one chunk

export const CHUNK = 'chunk';

export const HYPERTABLE = 'hypertable';

<Icon icon="tag" iconType="duotone" /> Since [2.18.0][tsdb-2.18.0]

Merge two or more {CHUNK}s into one.

The partition boundaries for the new {CHUNK} is the union of all partitions of the merged {CHUNK}s.
The new {CHUNK} retains the name, constraints, and triggers of the *first* {CHUNK} in the partition order.

You can only merge {CHUNK}s that have directly adjacent partitions. It is not possible to merge
{CHUNK}s that have another {CHUNK}, or an empty range between them in any of the partitioning
dimensions.

{CHUNK} merging has the following limitations. You cannot:

* Merge {CHUNK}s with tiered data
* Write to {CHUNK}s that are being merged

## Concurrent mode

When a merge is executed using the `concurrently` option, other processes can
simultaneously read from the {CHUNK}s being merged and insert into other {CHUNK}s.
The merge happens across two transactions: the first one rewrites the {CHUNK}s
into a temporary relation without taking any locks that prevent reads, while
the second transaction locks out all other operations before swapping the old
relations for the new one. The second operation completes quickly so it
should not significantly affect other operations.

## Samples

* Merge two {CHUNK}s:

  ```sql theme={"dark"}
  CALL merge_chunks('_timescaledb_internal._hyper_1_1_chunk', '_timescaledb_internal._hyper_1_2_chunk');
  ```

* Merge more than two {CHUNK}s:

  ```sql theme={"dark"}
  CALL merge_chunks('{_timescaledb_internal._hyper_1_1_chunk, _timescaledb_internal._hyper_1_2_chunk, _timescaledb_internal._hyper_1_3_chunk}');
  ```

* Merge two {CHUNK}s concurrently, allowing reads:

  ```sql theme={"dark"}
  CALL merge_chunks('_timescaledb_internal._hyper_1_1_chunk', '_timescaledb_internal._hyper_1_2_chunk', concurrently => true);
  ```

## Arguments

The syntax is:

```sql theme={"dark"}
-- Merge two chunks:
CALL merge_chunks(
    chunk1 = '<chunk_name_1>',
    chunk2 = '<chunk_name_2>',
    concurrently = true | false
);

-- Merge multiple chunks:
CALL merge_chunks(
    chunks = ARRAY['<chunk_name_1>', '<chunk_name_2>', ...]::REGCLASS[]
);

-- Merge multiple chunks concurrently:
CALL merge_chunks_concurrently(
    chunks = ARRAY['<chunk_name_1>', '<chunk_name_2>', ...]::REGCLASS[]
);
```

You can merge either two {CHUNK}s, or an arbitrary number of {CHUNK}s specified as an array of {CHUNK} identifiers.
When you call `merge_chunks`, you must specify either `chunk1` and `chunk2`, or `chunks`. You cannot use both
arguments.

The `concurrently` option is only available for the two-chunk overload. To merge an array of {CHUNK}s concurrently,
use `merge_chunks_concurrently()` instead.

| Name               | Type        | Default | Required | Description                                                                                      |
| ------------------ | ----------- | ------- | -------- | ------------------------------------------------------------------------------------------------ |
| `chunk1`, `chunk2` | REGCLASS    | -       | ✖        | The two {CHUNK}s to merge in partition order                                                     |
| `chunks`           | REGCLASS\[] | -       | ✖        | The array of {CHUNK}s to merge in partition order                                                |
| `concurrently`     | BOOLEAN     | `false` | ✖        | Set to `true` to allow reads on the merging {CHUNK}s. Only available for the two-chunk overload. |

## Returns

This procedure does not return a value. Upon successful completion, the specified {CHUNK}s are merged into a single {CHUNK}.

[tsdb-2.18.0]: https://github.com/timescale/timescaledb/releases/tag/2.18.0
