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

# CREATE INDEX (Transaction Per Chunk)

> Create a hypertable index using a separate transaction for each chunk

export const CHUNK_CAP = 'Chunk';

export const CHUNK = 'chunk';

export const HYPERTABLE_CAP = 'Hypertable';

export const HYPERTABLE = 'hypertable';

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

```SQL theme={"dark"}
CREATE INDEX ... WITH (timescaledb.transaction_per_chunk, ...);
```

This option extends [`CREATE INDEX`][postgres-createindex] with the ability to
use a separate transaction for each {CHUNK} it creates an index on, instead of
using a single transaction for the entire {HYPERTABLE}. This allows `INSERT`s, and
other operations to be performed concurrently during most of the duration of the
`CREATE INDEX` command. While the index is being created on an individual {CHUNK},
it functions as if a regular `CREATE INDEX` were called on that {CHUNK}, however
other {CHUNK}s are completely unblocked.

This version of `CREATE INDEX` can be used as an alternative to
`CREATE INDEX CONCURRENTLY`, which is not currently supported on {HYPERTABLE}s.

<Warning>
  * Not supported for `CREATE UNIQUE INDEX`.
  * If the operation fails partway through, indexes might not be created on all
    {HYPERTABLE} {CHUNK}s. If this occurs, the index on the root table of the {HYPERTABLE}
    is marked as invalid. You can check this by running `\d+` on the {HYPERTABLE}. The
    index still works, and is created on new {CHUNK}s, but if you want to ensure all
    {CHUNK}s have a copy of the index, drop and recreate it.

    You can also use the following query to find all invalid indexes:

    ```SQL theme={"dark"}
    SELECT * FROM pg_index i WHERE i.indisvalid IS FALSE;
    ```
</Warning>

## Samples

Create an anonymous index:

```SQL theme={"dark"}
CREATE INDEX ON conditions(time, device_id)
    WITH (timescaledb.transaction_per_chunk);
```

Alternatively:

```SQL theme={"dark"}
CREATE INDEX ON conditions USING brin(time, location)
    WITH (timescaledb.transaction_per_chunk);
```

## Returns

The `CREATE INDEX` command does not return a value. Upon successful completion, an index is created on the {HYPERTABLE} and all its {CHUNK}s.

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

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