In most cases, an automated compression policy is sufficient. However, if you want more control over compression, you can also manually compress specific chunks.


Compression alters data on your disk, so always back up before you start.

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. At the psql prompt, select all chunks in the table example that are older than three days:

    SELECT show_chunks('example', older_than => INTERVAL '3 days');
  2. This returns a list of chunks. Take a note of the chunk names:


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

  1. At the psql prompt, compress the chunk:

    SELECT compress_chunk( '<chunk_name>');
  2. Check the results of the compression with this command:

    SELECT *
    FROM chunk_compression_stats('example');

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

    _timescaledb_internal_hyper_1_1_chunkCompressed8192 bytes16 kB8192 bytes32 kB8192 bytes16 kB8192 bytes32 kB
  3. Repeat for all chunks you want to compress.

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:

SELECT compress_chunk(i, if_not_compressed => true)
FROM show_chunks(
now()::timestamp - INTERVAL '1 week',
now()::timestamp - INTERVAL '3 weeks'
) i;

In TimescaleDB 2.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, use this command, specifying the interval to use:

SELECT compress_chunk_time_interval('<time_interval>',if_not_compressed => true)
FROM show_chunks(
now()::timestamp - INTERVAL '1 week',
) ;

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.

Found an issue on this page?

Report an issue!


Related Content