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

# reorder_chunk()

> Reorder rows in a chunk

export const PG = 'Postgres';

export const CHUNK_CAP = 'Chunk';

export const CHUNK = 'chunk';

export const HYPERTABLE_CAP = 'Hypertable';

export const HYPERTABLE = 'hypertable';

<Tag>Community</Tag>

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

Reorder a single {CHUNK}'s heap to follow the order of an index. This function
acts similarly to the [PostgreSQL CLUSTER command][postgres-cluster] , however
it uses lower lock levels so that, unlike with the CLUSTER command, the {CHUNK}
and {HYPERTABLE} are able to be read for most of the process. It does use a bit
more disk space during the operation.

This command can be particularly useful when data is often queried in an order
different from that in which it was originally inserted. For example, data is
commonly inserted into a {HYPERTABLE} in loose time order (for example, many devices
concurrently sending their current state), but one might typically query the
{HYPERTABLE} about a *specific* device. In such cases, reordering a {CHUNK} using an
index on `(device_id, time)` can lead to significant performance improvement for
these types of queries.

One can call this function directly on individual {CHUNK}s of a {HYPERTABLE}, but
using [add\_reorder\_policy][add_reorder_policy] is often much more convenient.

## Samples

Reorder a chunk on an index:

```sql theme={"dark"}
SELECT reorder_chunk('_timescaledb_internal._hyper_1_10_chunk', '_timescaledb_internal.conditions_device_id_time_idx');
```

## Arguments

The syntax is:

```sql theme={"dark"}
SELECT reorder_chunk(
    chunk = '<chunk_name>',
    index = '<index_name>',
    verbose = true | false
);
```

| Name      | Type     | Default | Required | Description                                                                  |
| --------- | -------- | ------- | -------- | ---------------------------------------------------------------------------- |
| `chunk`   | REGCLASS | -       | ✔        | Name of the {CHUNK} to reorder.                                              |
| `index`   | REGCLASS | -       | ✖        | The name of the index (on either the {HYPERTABLE} or {CHUNK}) to order by.   |
| `verbose` | BOOLEAN  | `FALSE` | ✖        | Setting to true displays messages about the progress of the reorder command. |

## Returns

This function returns void.

[add_reorder_policy]: /api-reference/timescaledb/hypertables/add_reorder_policy

[postgres-cluster]: https://www.postgresql.org/docs/current/sql-cluster.html

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