ALTER TABLE, and SELECT.
If the table to convert already contains data, set migrate_data to TRUE.
However, this may take a long time and there are limitations when the table contains foreign
key constraints.
You cannot run create_hypertable() on a table that is already partitioned using
declarative partitioning or inheritance. The time column must be defined
as NOT NULL. If this is not already specified on table creation, create_hypertable automatically adds
this constraint on the table when it is executed.
This page describes the generalized API introduced in v2.13.
The old interface for create_hypertable is also available.
Samples
Before you callcreate_hypertable, you create a standard relational table. For example:
- Time partition a hypertable by time range
- Time partition a hypertable using composite columns and immutable functions
- Time partition a hypertable using ISO formatting
- Time partition a hypertable using UUIDv7
Time partition a hypertable by time range
The following examples show different ways to create a hypertable:-
Convert with range partitioning on the
timecolumn: -
Convert with a set_chunk_time_interval of 24 hours:
Either:
or:
-
With range partitioning on the
timecolumn, do not raise a warning ifconditionsis already a hypertable:
If you call
SELECT * FROM create_hypertable(...) the return value is formatted as a table with column headings.Time partition a hypertable using composite columns and immutable functions
The following example shows how to time partition themeasurements relational table on a composite
column type using a range partitioning function.
-
Create the report type, then an immutable function that converts the column value into a supported column value:
-
Create the hypertable using the immutable function:
Time partition a hypertable using ISO formatting
The following example shows how to time partition theevents table on a jsonb (event) column
type, which has a top level started key that contains an ISO 8601 formatted timestamp:
Time partition a hypertable using UUIDv7
-
Create a table with a UUIDv7 column:
- Postgres 17 and lower
- Postgres v18
-
Partition the table based on the timestamps embedded within the UUID values:
Arguments
The syntax forcreate_hypertable is:
Dimension info
To create a_timescaledb_internal.dimension_info instance, you call add_dimension
to an existing hypertable.
Samples
s must always have a primary range dimension, followed by an arbitrary number of additional dimensions that can be either range or hash, Typically this is just one hash. For example:jsonb, you can specify a function to the partition_func argument
of the dimension build to extract a compatible data type. Look in the example section below.
Custom partitioning
By default, calls ‘s internal hash function for the given type. You use a custom partitioning function for value types that do not have a native hash function. You can specify a custom partitioning function for both range and hash partitioning. A partitioning function should take aanyelement argument as the only parameter and return a positive integer hash value. This hash value is
not a partition identifier, but rather the inserted value’s position in the dimension’s key space, which is then
divided across the partitions.
by_range()
Create a by-range dimension builder. You can partitionby_range on it’s own.
Samples
-
Partition on time using
CREATE TABLEThe simplest usage is to partition on a time column:When you create a using CREATE TABLE … WITH …, the default partitioning column is automatically the first column with a timestamp data type. Also, creates a columnstore policy that automatically converts your data to the , after an interval equal to the value of the chunk_interval, defined throughcompress_afterin the policy. This columnar format enables fast scanning and aggregation, optimizing performance for analytical workloads while also saving significant storage space. In the conversion, s are compressed by up to 98%, and organized for efficient, large-scale queries. You can customize this policy later using alter_job. However, to changeafterorcreated_before, the compression settings, or the the policy is acting on, you must remove the columnstore policy and add a new one. You can also manually convert s in a to the . This is the default partition, you do not need to add it explicitly. -
Extract time from a non-time column using
create_hypertableIf you have a table with a non-time column containing the time, such as a JSON column, add a partition function to extract the time:
Arguments
If the column to be partitioned is a:
-
TIMESTAMP,TIMESTAMPTZ, orDATE: specifypartition_intervaleither as anINTERVALtype or an integer value in microseconds. -
Another integer type: specify
partition_intervalas an integer that reflects the column’s underlying semantics. For example, if this column is in UNIX time, specifypartition_intervalin milliseconds.
by_hash()
The main purpose of hash partitioning is to enable parallelization across multiple disks within the same time interval. Every distinct item in hash partitioning is hashed to one of N buckets. By default, uses flexible range intervals to manage sizes.Parallelizing disk I/O
You use Parallel I/O in the following scenarios:- Two or more concurrent queries should be able to read from different disks in parallel.
- A single query should be able to use query parallelization to read from multiple disks in parallel.
- RAID: use a RAID setup across multiple physical disks, and expose a single logical disk to the . That is, using a single tablespace. Best practice is to use RAID when possible, as you do not need to manually manage tablespaces in the database.
- Multiple tablespaces: for each physical disk, add a separate tablespace to the database. allows you to add multiple tablespaces to a single . However, although under the hood, a ‘s s are spread across the tablespaces associated with that . When using multiple tablespaces, a best practice is to also add a second hash-partitioned dimension to your and to have at least one hash partition per disk. While a single time dimension would also work, it would mean that the first is written to one tablespace, the second to another, and so on, and thus would parallelize only if a query’s time range exceeds a single .
Samples
Arguments
Returns
by_range and by-hash return an opaque _timescaledb_internal.dimension_info instance, holding the
dimension information used by this function.
Returns
On failure, an error is returned: