ddsketch

Provides ddsketch aggregate function

Overview

PackageVersionCategoryLicenseLanguage
ddsketch1.0.1FUNCPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
4650ddsketchNoYesNoYesNoYes-
Relatedomnisketch quantile lower_quantile topn count_distinct hll first_last_agg

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.0.11817161514ddsketch-
RPMPIGSTY1.0.11817161514ddsketch_$v-
DEBPIGSTY1.0.11817161514postgresql-$v-ddsketch-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
u22.x86_64
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
u22.aarch64
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
u24.x86_64
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
u24.aarch64
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1
PIGSTY 1.0.1

Build

You can build the RPM / DEB packages for ddsketch using pig build:

pig build pkg ddsketch         # build RPM / DEB packages

Install

You can install ddsketch directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install ddsketch;          # Install for current active PG version
pig ext install -y ddsketch -v 18  # PG 18
pig ext install -y ddsketch -v 17  # PG 17
pig ext install -y ddsketch -v 16  # PG 16
pig ext install -y ddsketch -v 15  # PG 15
pig ext install -y ddsketch -v 14  # PG 14
dnf install -y ddsketch_18       # PG 18
dnf install -y ddsketch_17       # PG 17
dnf install -y ddsketch_16       # PG 16
dnf install -y ddsketch_15       # PG 15
dnf install -y ddsketch_14       # PG 14
apt install -y postgresql-18-ddsketch   # PG 18
apt install -y postgresql-17-ddsketch   # PG 17
apt install -y postgresql-16-ddsketch   # PG 16
apt install -y postgresql-15-ddsketch   # PG 15
apt install -y postgresql-14-ddsketch   # PG 14

Create Extension:

CREATE EXTENSION ddsketch;

Usage

ddsketch: DDSketch quantile estimation for PostgreSQL

Implements DDSketch, a fully-mergeable quantile sketch with relative-error guarantees. Much faster than percentile_cont and supports parallelism.

CREATE EXTENSION ddsketch;

Direct Aggregation Functions

FunctionDescription
ddsketch_percentile(value, alpha, nbuckets, quantile)Estimate a single percentile
ddsketch_percentile(value, alpha, nbuckets, quantiles[])Estimate multiple percentiles
ddsketch_percentile_of(value, alpha, nbuckets, value)Estimate percentile rank of a value
ddsketch_percentile_of(value, alpha, nbuckets, values[])Estimate percentile ranks of multiple values

Pre-aggregation Functions

FunctionDescription
ddsketch(value, alpha, nbuckets)Build a ddsketch from values
ddsketch_percentile(sketch, quantile)Estimate percentile from a pre-built sketch
ddsketch_percentile(sketch, quantiles[])Estimate multiple percentiles from a pre-built sketch

Utility Functions

FunctionDescription
ddsketch_count(sketch)Return the number of items in the sketch
ddsketch_sum(sketch, low, high)Trimmed sum within a value range
ddsketch_avg(sketch, low, high)Trimmed average within a value range

Parameters

  • alpha – controls accuracy and sketch size (lower = more accurate, larger)
  • nbuckets – maximum number of buckets (each 8 bytes)

Examples

-- Instead of: SELECT percentile_cont(0.95) WITHIN GROUP (ORDER BY a) FROM t;
SELECT ddsketch_percentile(a, 0.05, 1024, 0.95) FROM t;

-- Multiple percentiles at once
SELECT ddsketch_percentile(a, 0.05, 1024, ARRAY[0.5, 0.95, 0.99]) FROM t;

-- Pre-aggregate for fast repeated queries
CREATE TABLE p AS SELECT a, b, ddsketch(c, 0.05, 1024) AS d FROM t GROUP BY a, b;

-- Query pre-aggregated data (~1.5ms vs ~7s for exact)
SELECT a, ddsketch_percentile(d, 0.95) FROM p GROUP BY a ORDER BY a;

Last Modified 2026-03-12: add pg extension catalog (95749bf)