recompress_chunk()

Recompresses a compressed chunk that had more data inserted after compression.

  1. recompress_chunk(
  2. chunk REGCLASS,
  3. if_not_compressed BOOLEAN = false
  4. )

You can also recompress chunks by running the job associated with your compression policy. recompress_chunk gives you more fine-grained control by allowing you to target a specific chunk.

important

recompress_chunk is implemented as an SQL procedure and not a function. Call the procedure with CALL. Don’t use a SELECT statement.

note

recompress_chunk only works on chunks that have previously been compressed. To compress a chunk for the first time, use compress_chunk.

Required arguments

NameTypeDescription
chunkREGCLASSThe chunk to be recompressed. Must include the schema, for example _timescaledb_internal, if it is not in the search path.

Optional arguments

NameTypeDescription
if_not_compressedBOOLEANIf true, prints a notice instead of erroring if the chunk is already compressed. Defaults to false.

Sample usage

Recompress the chunk timescaledb_internal._hyper_1_2_chunk:

  1. CALL recompress_chunk('_timescaledb_internal._hyper_1_2_chunk');

Troubleshooting

In TimescaleDB 2.6.0 and above, recompress_chunk is implemented as a procedure. Previously, it was implemented as a function. If you are upgrading to TimescaleDB 2.6.0 or above, therecompress_chunk function could cause an error. For example, trying to run SELECT recompress_chunk(i.show_chunks, true) FROM... gives the following error:

  1. ERROR: recompress_chunk(regclass, boolean) is a procedure

To fix the error, use CALL instead of SELECT. You might also need to write a procedure to replace the full functionality in your SELECT statement. For example:

  1. DO $$
  2. DECLARE chunk regclass;
  3. BEGIN
  4. FOR chunk IN SELECT format('%I.%I', chunk_schema, chunk_name)::regclass
  5. FROM timescaledb_information.chunks
  6. WHERE is_compressed = true
  7. LOOP
  8. RAISE NOTICE 'Recompressing %', chunk::text;
  9. CALL recompress_chunk(chunk, true);
  10. END LOOP;
  11. END
  12. $$;