.../articles/
On Data Compression in TimescaleDB

On Data Compression in TimescaleDB

2021.09.30

Environment

PostgreSQL 13 TimescaleDB 2.4.2

Enabling compression on the target table

First, we enable compression on the target table that's been converted into a hypertable. As a quick note, "hypertable-ized" means converting a PostgreSQL table into the hypertable format so it can be treated as a time-series table. Compression is enabled with ALTER TABLE.

ALTER TABLE iot_data SET (timescaledb.compress,timescaledb.compress_segmentby = 'sensor_id');
> Query 1 OK: ALTER TABLE

Manual compression

Narrowing down which chunks to compress

Next, we use show_chunks() to narrow down the chunks to compress. I won't go into detail about chunks, but think of them as boxes that hold time-series data, with multiple chunks existing per hypertable. Running the query below returns the chunk names, so make a note of them.

-- get the names of chunks containing data older than 14 days
SELECT show_chunks('iot_data', older_than => INTERVAL '14 days');
> _timescaledb_internal._hyper_19_210_chunk
> _timescaledb_internal._hyper_19_211_chunk
> ...
> ...

Running the compression query

Using the chunk names noted in the previous step, running the query below performs the compression. If you're compressing a chunk that already has a fair amount of data stored in it, this can take a while, so be patient. You can check whether compression has completed with chunk_compression_stats().

-- query that performs the compression
SELECT compress_chunk('_timescaledb_internal._hyper_19_210_chunk');

-- query to check whether a chunk has been compressed
SELECT * FROM chunk_compression_stats('iot_data')

Automating compression with a policy

There's also a feature that lets you define a policy to automatically compress data once a certain amount of time has passed.

-- register a policy to automatically compress data older than 14 days
SELECT add_compression_policy('iot_data', INTERVAL '14 days');

-- remove a configured compression policy
SELECT remove_compression_policy('iot_data');

What changes with compression

Benefits

  • Saves disk space You can check table-level compression info with hypertable_compression_stats(). In our own data, we confirmed a disk usage change from 73.1GB → 1.93GB.
  • Faster searches? According to the documentation, some queries can see improved speed, which we were hoping for, but in the queries we tested locally, we didn't see any benefit — no improvement, but no degradation either. We'd like to know what kind of query actually benefits from this.

Drawbacks

  • Updates and deletes are difficult Once compressed, you can't run updates or deletes; to update, you'd need to decompress at the chunk level, which is quite costly. When we tested an update query, it failed with cannot update/delete rows from chunk "_hyper_19_210_chunk" as it is compressed.

    Also, the documentation says inserts are only partially supported, and when we tested it, the query failed with insert into a compressed chunk that has primary or unique constraint is not supported. In this version, it seems you can't even insert into a table that has a primary key or unique key constraint.

    We're hoping this gets supported in a future version.

References

Compression https://docs.timescale.com/api/latest/compression/ Hypertable & Chunks https://docs.timescale.com/api/latest/hypertable/

written by

.../article/

Articles

All articles

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

From Firebase to Vercel, Contentful to microCMS — a migration log written with Claude Code

We moved our corporate site's hosting and CMS, and made it bilingual along the way. The constraints we only found by running against real data were more useful than the migration itself, so this post focuses on where we got stuck.

Can't Read POST Data with Firebase Functions × Remix?

Can't Read POST Data with Firebase Functions × Remix?

How to read POST data from a Remix action when running on Firebase Functions.

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Generative AI for Executives and Leaders: An Approach to Self-Driven DX

Building a structure where executives and leaders themselves can identify issues and evaluate solutions using generative AI. We introduce how combining this with our hands-on support dramatically improves both the quality and speed of digital transformation.

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Deploying a Monorepo Next.js App (App Router) to AWS Amplify

Notes on the obstacles we hit while deploying a Next.js app managed in a monorepo to AWS Amplify.

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Keeping Production Running Smoothly with Remote Work and Online Meetings [Documentation]

Many production companies have adopted remote work as a result of the pandemic, and we are one of them.

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

Designing an E-Commerce Site That Sells: How to Find Great Reference Examples

There is no single formula for e-commerce design that sells. Driving revenue requires a solid concept, and getting to that concept requires thorough research.

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

Productivity Tools We Recommend as a Production Company, Including Services That Work Well Solo

With remote work becoming the norm during the COVID-19 pandemic, our team now works from home most days of the week.

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We Released Thought Recorder, a Figma Plugin for Keeping a Commit History of Your Designs

We hope this helps web designers who work in Figma. Read on for how to use it.

How to Build an E-Commerce Site, and Which Platforms We Recommend

How to Build an E-Commerce Site, and Which Platforms We Recommend

Shopping online for fashion, appliances, and even groceries is now routine. With the pandemic accelerating the shift, we receive a steady stream of questions about which platform to use and how much it costs.

Generating FastAPI Schema Classes from OpenAPI

Generating FastAPI Schema Classes from OpenAPI

We chose FastAPI, a relatively modern framework, for a Python API project. FastAPI can generate an OpenAPI definition from your backend code, but here we do the opposite: generating FastAPI schema classes from an OpenAPI definition prepared in advance.

View all articles

Contact us