tdigest
Provides tdigest aggregate function.
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
tdigest | 1.4.4 | FUNC | Apache-2.0 | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4700 | tdigest | No | Yes | No | Yes | No | Yes | - |
| Related | pg_idkit pgx_ulid pg_uuidv7 pg_hashids sequential_uuids topn quantile lower_quantile |
|---|
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PGDG | 1.4.4 | 1817161514 | tdigest | - |
| RPM | PGDG | 1.4.4 | 1817161514 | tdigest_$v | - |
| DEB | PGDG | 1.4.4 | 1817161514 | postgresql-$v-tdigest | - |
Install
You can install tdigest directly. First, make sure the PGDG repository is added and enabled:
pig repo add pgdg -u # Add PGDG repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install tdigest; # Install for current active PG version
pig ext install -y tdigest -v 18 # PG 18
pig ext install -y tdigest -v 17 # PG 17
pig ext install -y tdigest -v 16 # PG 16
pig ext install -y tdigest -v 15 # PG 15
pig ext install -y tdigest -v 14 # PG 14
dnf install -y tdigest_18 # PG 18
dnf install -y tdigest_17 # PG 17
dnf install -y tdigest_16 # PG 16
dnf install -y tdigest_15 # PG 15
dnf install -y tdigest_14 # PG 14
apt install -y postgresql-18-tdigest # PG 18
apt install -y postgresql-17-tdigest # PG 17
apt install -y postgresql-16-tdigest # PG 16
apt install -y postgresql-15-tdigest # PG 15
apt install -y postgresql-14-tdigest # PG 14
Create Extension:
CREATE EXTENSION tdigest;
Usage
Sources:
tdigest implements an approximate, mergeable t-digest for online rank statistics such as quantiles, percentile ranks, and trimmed means. It supports parallel aggregation and storing pre-aggregated digests for later rollups.
CREATE EXTENSION tdigest;
Direct Aggregation Functions
| Function | Description |
|---|---|
tdigest_percentile(value, compression, quantile) | Estimate a single percentile |
tdigest_percentile(value, compression, quantiles[]) | Estimate multiple percentiles |
tdigest_percentile_of(value, compression, value) | Estimate percentile rank of a value |
tdigest_percentile_of(value, compression, values[]) | Estimate percentile ranks of multiple values |
Pre-aggregation Functions
| Function | Description |
|---|---|
tdigest(value, compression) | Build a t-digest from values |
tdigest_percentile(digest, quantile) | Estimate percentile from a pre-built digest |
tdigest_percentile(digest, quantiles[]) | Estimate multiple percentiles from a pre-built digest |
Incremental Update Functions
| Function | Description |
|---|---|
tdigest_add(digest, value) | Add a single value to an existing digest |
tdigest_add(digest, values[]) | Add an array of values to an existing digest |
tdigest_union(digest, digest) | Merge two digests |
Utility Functions
| Function | Description |
|---|---|
tdigest_count(digest) | Return the number of items in the digest |
tdigest_sum(digest, low, high) | Trimmed sum within a value range |
tdigest_avg(digest, low, high) | Trimmed average within a value range |
Parameters
compression– controls accuracy (higher = more accurate, larger digest). Error is roughly1/compression.
Examples
-- Instead of: SELECT percentile_cont(0.95) WITHIN GROUP (ORDER BY a) FROM t;
SELECT tdigest_percentile(a, 100, 0.95) FROM t;
-- Multiple percentiles
SELECT tdigest_percentile(a, 100, ARRAY[0.5, 0.95, 0.99]) FROM t;
-- Pre-aggregate for fast repeated queries
CREATE TABLE p AS SELECT a, b, tdigest(c, 100) AS d FROM t GROUP BY a, b;
-- Query pre-aggregated data (~1.5ms vs ~7s for exact)
SELECT a, tdigest_percentile(d, 0.95) FROM p GROUP BY a ORDER BY a;
Caveats
- Results are estimates. Validate the chosen compression against exact
percentile_contresults on representative data before setting accuracy targets. - Higher compression usually improves tail accuracy but increases state size and CPU cost.
- Stored digests can be merged across groups and time windows. Version
1.4.4fixes combining digests created with different parameters, so use that patch level when heterogeneous states may meet. - Version
1.4.4also strengthens text-input parsing and validation and adds PostgreSQL 19 build/test coverage; malformed serialized digests that older builds accepted may now be rejected.
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.