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

# Manual compression

> Manually compress specific chunks of a hypertable for more granular control

export const HYPERTABLE = 'hypertable';

export const TIMESCALE_DB = 'TimescaleDB';

<Icon icon="archive" iconType="duotone" /> Old API since [2.18.0][tsdb-2.18.0]. Superseded by [hypercore][hypercore].
However, compression APIs are still supported, you do not need to migrate to the hypercore APIs.

[hypercore]: /manage-data/capabilities/hypercore

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

In most cases, an [automated compression policy][add_compression_policy] is sufficient to automatically compress your
chunks. However, if you want more control, you can also use manual synchronous compression of specific chunks.

Before you start, you need a list of chunks to compress. In this example, you use a {HYPERTABLE} called `example`, and
compress chunks older than three days.

1. **Select chunks to compress**

   At the psql prompt, select all chunks in the table `example` that are older than three days:

   ```sql theme={"dark"}
   SELECT show_chunks('example', older_than => INTERVAL '3 days');
   ```

   This returns a list of chunks. Take note of the chunks' names:

   |   | show\_chunks                                |
   | - | ------------------------------------------- |
   | 1 | \_timescaledb\_internal\_hyper\_1\_2\_chunk |
   | 2 | \_timescaledb\_internal\_hyper\_1\_3\_chunk |

When you are happy with the list of chunks, you can use the chunk names to manually compress each one.

1. **Compress the chunk**

   At the psql prompt, compress the chunk:

   ```sql theme={"dark"}
   SELECT compress_chunk( '<chunk_name>');
   ```

2. **Check the compression results**

   Check the results of the compression with this command:

   ```sql theme={"dark"}
   SELECT *
   FROM chunk_compression_stats('example');
   ```

   The results show the chunks for the given {HYPERTABLE}, their compression status, and some other statistics:

   | chunk\_schema           | chunk\_name           | compression\_status | before\_compression\_table\_bytes | before\_compression\_index\_bytes | before\_compression\_toast\_bytes | before\_compression\_total\_bytes | after\_compression\_table\_bytes | after\_compression\_index\_bytes | after\_compression\_toast\_bytes | after\_compression\_total\_bytes | node\_name |
   | ----------------------- | --------------------- | ------------------- | --------------------------------- | --------------------------------- | --------------------------------- | --------------------------------- | -------------------------------- | -------------------------------- | -------------------------------- | -------------------------------- | ---------- |
   | \_timescaledb\_internal | \_hyper\_1\_1\_chunk  | Compressed          | 8192 bytes                        | 16 kB                             | 8192 bytes                        | 32 kB                             | 8192 bytes                       | 16 kB                            | 8192 bytes                       | 32 kB                            |            |
   | \_timescaledb\_internal | \_hyper\_1\_20\_chunk | Uncompressed        |                                   |                                   |                                   |                                   |                                  |                                  |                                  |                                  |            |

3. **Repeat for all chunks**

   Repeat for all chunks you want to compress.

## Manually compress chunks in a single command

Alternatively, you can select the chunks and compress them in a single command by using the output of the `show_chunks`
command to compress each one. For example, use this command to compress chunks between one and three weeks old if they
are not already compressed:

```sql theme={"dark"}
SELECT compress_chunk(i, if_not_compressed => true)
    FROM show_chunks(
        'example',
        now()::timestamp - INTERVAL '1 week',
        now()::timestamp - INTERVAL '3 weeks'
    ) i;
```

## Roll up uncompressed chunks when compressing

In {TIMESCALE_DB} v2.9 and later, you can roll up multiple uncompressed chunks into a previously compressed chunk as part
of your compression procedure. This allows you to have much smaller uncompressed chunk intervals, which reduces the disk
space used for uncompressed data. For example, if you have multiple smaller uncompressed chunks in your data, you can
roll them up into a single compressed chunk.

To roll up your uncompressed chunks into a compressed chunk, alter the compression settings to set the compress chunk
time interval and run compression operations to roll up the chunks while compressing.

<Info>
  The default setting of `compress_orderby` is `'time DESC'` (the descending or DESC command is used to sort the data
  returned in ascending order), which causes chunks to be re-compressed many times during the rollup, possibly leading to
  a steep performance penalty. Set `timescaledb.compress_orderby = 'time ASC'` to avoid this penalty.
</Info>

```sql theme={"dark"}
ALTER TABLE example SET (timescaledb.compress_chunk_time_interval = '<time_interval>',
                            timescaledb.compress_orderby = 'time ASC');
SELECT compress_chunk(c, if_not_compressed => true)
    FROM show_chunks(
        'example',
        now()::timestamp - INTERVAL '1 week'
    ) c;
```

The time interval you choose must be a multiple of the uncompressed chunk interval. For example, if your uncompressed
chunk interval is one week, your `<time_interval>` of the compressed chunk could be two weeks or six weeks, but not one
month.

[add_compression_policy]: /api-reference/timescaledb/compression/add_compression_policy
