tdigest

Provides tdigest aggregate function.

Overview

PackageVersionCategoryLicenseLanguage
tdigest1.4.3FUNCApache-2.0C
IDExtensionBinLibLoadCreateTrustRelocSchema
4700tdigestNoYesNoYesNoYes-
Relatedpg_idkit pgx_ulid pg_uuidv7 pg_hashids sequential_uuids topn quantile lower_quantile

Version

TypeRepoVersionPG VerPackageDeps
EXTPGDG1.4.31817161514tdigest-
RPMPGDG1.4.21817161514tdigest_$v-
DEBPGDG1.4.31817161514postgresql-$v-tdigest-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
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
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
u24.aarch64
PGDG 1.4.3
PGDG 1.4.3
PGDG 1.4.3
PGDG 1.4.3
PGDG 1.4.3

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

tdigest: t-digest percentile estimation for PostgreSQL

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

FunctionDescription
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

FunctionDescription
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

FunctionDescription
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

FunctionDescription
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 roughly 1/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 Modified 2026-03-12: add pg extension catalog (95749bf)