New procedures to `merge_chunks` are introduced that can merge an arbitrary number of chunks if the right conditions apply. Basic checks are done to ensure that the chunks can be merged from a partitioning perspective. Some more advanced cases that are potentially mergeable are not supported at this time (e.g., chunks with non-adjacent partitioning) and merging of compressed chunks. Merging compressed chunks requires additional work, although the same basic rewrite approach should work also on the internal compressed relations. Still, one needs to handle merges of a compressed chunk and a non-compressed chunk, or two compressed chunks with different compression settings, partially compressed chunks, and so forth. This is left for a future enhancement. Currently, the merge defaults to taking an AccessExclusive lock on the merged chunks to prevent deadlocks and concurrent modifications. Weaker locking is supported via an anonymous settings variable, and it is used in tests to illustrate various deadlock scenarios. Alternative locking approaches, including multi-transactional merges, can be considered in the future. The actual merging is done by rewriting all the data from multiple chunks into a (temporary) merged heap using the same approach as that implemented to support VACUUM FULL and CLUSTER. Then this new heap is swapped into one of the original relations while the rest are dropped. This approach is MVCC compliant and implements correct visibility under higher isolation levels, while also cleaning up garbage tuples.
TimescaleDB is a PostgreSQL extension for high-performance real-time analytics on time-series and event data
Install TimescaleDB
Install from a Docker container:
-
Run the TimescaleDB container:
docker run -d --name timescaledb -p 5432:5432 -e POSTGRES_PASSWORD=password timescale/timescaledb:latest-pg17
-
Connect to a database:
docker exec -it timescaledb psql -d "postgres://postgres:password@localhost/postgres"
See other installation options or try Timescale Cloud for free.
Create a hypertable
You create a regular table and then convert it into a hypertable. A hypertable automatically partitions data into chunks based on your configuration.
-- Create timescaledb extension
CREATE EXTENSION IF NOT EXISTS timescaledb;
-- Create a regular SQL table
CREATE TABLE conditions (
time TIMESTAMPTZ NOT NULL,
location TEXT NOT NULL,
temperature DOUBLE PRECISION NULL,
humidity DOUBLE PRECISION NULL
);
-- Convert the table into a hypertable that is partitioned by time
SELECT create_hypertable('conditions', by_range('time'));
See more:
Enable columnstore
TimescaleDB's hypercore is a hybrid row-columnar store that boosts analytical query performance on your time-series and event data, while reducing data size by more than 90%. This keeps your queries operating at lightning speed and ensures low storage costs as you scale. Data is inserted in row format in the rowstore and converted to columnar format in the columnstore based on your configuration.
-
Configure the columnstore on a hypertable:
ALTER TABLE conditions SET ( timescaledb.compress, timescaledb.compress_segmentby = 'location' );
-
Create a policy to automatically convert chunks in row format that are older than seven days to chunks in the columnar format:
SELECT add_compression_policy('conditions', INTERVAL '7 days');
See more:
Insert and query data
Insert and query data in a hypertable via regular SQL commands. For example:
-
Insert data into a hypertable named
conditions
:INSERT INTO conditions VALUES (NOW(), 'office', 70.0, 50.0), (NOW(), 'basement', 66.5, 60.0), (NOW(), 'garage', 77.0, 65.2);
-
Return the number of entries written to the table conditions in the last 12 hours:
SELECT COUNT(*) FROM conditions WHERE time > NOW() - INTERVAL '12 hours';
See more:
Create time buckets
Time buckets enable you to aggregate data in hypertables by time interval and calculate summary values.
For example, calculate the average daily temperature in a table named conditions
. The table has a time
and temperature
columns:
SELECT
time_bucket('1 day', time) AS bucket,
AVG(temperature) AS avg_temp
FROM
conditions
GROUP BY
bucket
ORDER BY
bucket ASC;
See more:
Create continuous aggregates
Continuous aggregates are designed to make queries on very large datasets run faster. They continuously and incrementally refresh a query in the background, so that when you run such query, only the data that has changed needs to be computed, not the entire dataset. This is what makes them different from regular PostgreSQL materialized views, which cannot be incrementally materialized and have to be rebuilt from scratch every time you want to refresh it.
For example, create a continuous aggregate view for daily weather data in two simple steps:
-
Create a materialized view:
CREATE MATERIALIZED VIEW conditions_summary_daily WITH (timescaledb.continuous) AS SELECT location, time_bucket(INTERVAL '1 day', time) AS bucket, AVG(temperature), MAX(temperature), MIN(temperature) FROM conditions GROUP BY location, bucket;
-
Create a policy to refresh the view every hour:
SELECT add_continuous_aggregate_policy( 'conditions_summary_daily', start_offset => INTERVAL '1 month', end_offset => INTERVAL '1 day', schedule_interval => INTERVAL '1 hour' );
See more:
Want TimescaleDB hosted and managed for you? Try Timescale Cloud
Timescale Cloud is a cloud-based PostgreSQL platform for resource-intensive workloads. We help you build faster, scale further, and stay under budget. A Timescale Cloud service is a single optimized 100% PostgreSQL database instance that you use as is, or extend with capabilities specific to your business needs. The available capabilities are:
- Time-series and analytics: PostgreSQL with TimescaleDB. The PostgreSQL you know and love, supercharged with functionality for storing and querying time-series data at scale for analytics and other use cases. Get faster time-based queries with hypertables, continuous aggregates, and columnar storage. Save on storage with native compression, data retention policies, and bottomless data tiering to Amazon S3.
- AI and vector: PostgreSQL with vector extensions. Use PostgreSQL as a vector database with purpose built extensions for building AI applications from start to scale. Get fast and accurate similarity search with the pgvector and pgvectorscale extensions. Create vector embeddings and perform LLM reasoning on your data with the pgai extension.
- PostgreSQL: the trusted industry-standard RDBMS. Ideal for applications requiring strong data consistency, complex relationships, and advanced querying capabilities. Get ACID compliance, extensive SQL support, JSON handling, and extensibility through custom functions, data types, and extensions. All services include all the cloud tooling you'd expect for production use: automatic backups, high availability, read replicas, data forking, connection pooling, tiered storage, usage-based storage, and much more.
Check build status
Linux/macOS | Linux i386 | Windows | Coverity | Code Coverage | OpenSSF |
---|---|---|---|---|---|
Get involved
We welcome contributions to TimescaleDB! See Contributing and Code style guide for details.
Learn about Timescale
Timescale is PostgreSQL made powerful. To learn more about the company and its products, visit timescale.com.