tdigest
tdigest
tdigest : Provides tdigest aggregate function.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 4700 | tdigest | tdigest | 1.4.3 | FUNC | Apache-2.0 | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d-r | No | Yes | No | Yes | yes | no |
| Relationships | |
|---|---|
| See Also | pg_idkit pgx_ulid pg_uuidv7 pg_hashids sequential_uuids topn quantile lower_quantile |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PGDG | 1.4.3 | 18 17 16 15 14 | tdigest | - |
| RPM | PGDG | 1.4.2 | 18 17 16 15 14 | tdigest_$v | - |
| DEB | PGDG | 1.4.3 | 18 17 16 15 14 | postgresql-$v-tdigest | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PGDG 1.4.2 | PGDG 1.4.2 | PGDG 1.4.1 | PGDG 1.4.1 | PGDG 1.4.1 |
el8.aarch64 | PGDG 1.4.2 | PGDG 1.4.2 | PGDG 1.4.1 | PGDG 1.4.1 | PGDG 1.4.1 |
el9.x86_64 | PGDG 1.4.2 | PGDG 1.4.2 | PGDG 1.4.1 | PGDG 1.4.1 | PGDG 1.4.1 |
el9.aarch64 | PGDG 1.4.2 | PGDG 1.4.2 | PGDG 1.4.1 | PGDG 1.4.1 | PGDG 1.4.1 |
el10.x86_64 | PGDG 1.4.2 | PGDG 1.4.2 | PGDG 1.4.2 | PGDG 1.4.2 | PGDG 1.4.2 |
el10.aarch64 | PGDG 1.4.2 | PGDG 1.4.2 | PGDG 1.4.2 | PGDG 1.4.2 | PGDG 1.4.2 |
d12.x86_64 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 |
d12.aarch64 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 |
d13.x86_64 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 |
d13.aarch64 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 |
u22.x86_64 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 |
u22.aarch64 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 |
u24.x86_64 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 |
u24.aarch64 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 | PGDG 1.4.3 |
Source
Install
Make sure PGDG repo available:
pig repo add pgdg -u # add pgdg repo and update cacheInstall this extension with pig:
pig install tdigest; # install via package name, for the active PG version
pig install tdigest -v 18; # install for PG 18
pig install tdigest -v 17; # install for PG 17
pig install tdigest -v 16; # install for PG 16
pig install tdigest -v 15; # install for PG 15
pig install tdigest -v 14; # install for PG 14Create this extension with:
CREATE EXTENSION tdigest;Usage
Implements t-digest for on-line accumulation of rank-based statistics such as quantiles and trimmed means. Much faster than percentile_cont, supports parallelism, and allows pre-aggregation.
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;Last updated on