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

# attach_chunk()

> Attach a chunk to a hypertable

export const CHUNK = 'chunk';

export const HYPERTABLE_CAP = 'Hypertable';

export const HYPERTABLE = 'hypertable';

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

<Tag>Community</Tag>

Attach a {HYPERTABLE} as a {CHUNK} in another {HYPERTABLE} at a given slice in a dimension.

![Hypertable structure][hypertable-structure]

The schema, name, existing constraints, and indexes of `chunk` do not change, even
if a constraint conflicts with a {CHUNK} constraint in `hypertable`.

The `hypertable` you attach `chunk` to does not need to have the same dimension columns as the
{HYPERTABLE} you previously [detached `chunk`][hypertable-detach-chunk] from.

While attaching `chunk` to `hypertable`:

* Dimension columns in `chunk` are set as `NOT NULL`.
* Any foreign keys in `hypertable` are created in `chunk`.

You cannot:

* Attaching a {CHUNK} that is still attached to another {HYPERTABLE}. First call
  [detach\_chunk][hypertable-detach-chunk].
* Attaching foreign tables are not supported.

## Samples

Attach a {HYPERTABLE} as a {CHUNK} in another {HYPERTABLE} for a specific slice in a dimension:

```sql theme={"dark"}
CALL attach_chunk('ht', '_timescaledb_internal._hyper_1_2_chunk', '{"device_id": [0, 1000]}');
```

## Arguments

The syntax is:

```sql theme={"dark"}
CALL attach_chunk(
    hypertable = '<hypertable_name>',
    chunk = '<chunk_name>',
    slices = '<jsonb_slices>'
);
```

| Name         | Type     | Default | Required | Description                                                                                                                                  |
| ------------ | -------- | ------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `hypertable` | REGCLASS | -       | ✔        | Name of the {HYPERTABLE} to attach `chunk` to.                                                                                               |
| `chunk`      | REGCLASS | -       | ✔        | Name of the {CHUNK} to attach.                                                                                                               |
| `slices`     | JSONB    | -       | ✔        | The slice `chunk` will occupy in `hypertable`. `slices` cannot clash with the slice already occupied by an existing {CHUNK} in `hypertable`. |

## Returns

This function returns void.

[hypertable-detach-chunk]: /api-reference/timescaledb/hypertables/detach_chunk

[hypertable-structure]: https://assets.timescale.com/docs/images/hypertable-structure.png

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