count_distinct

An alternative to COUNT(DISTINCT …) aggregate, usable with HashAggregate

Overview

PackageVersionCategoryLicenseLanguage
count_distinct3.0.2FUNCBSD 2-ClauseC
IDExtensionBinLibLoadCreateTrustRelocSchema
4630count_distinctNoYesNoYesNoYes-
Relatedtopn hll omnisketch ddsketch quantile lower_quantile first_last_agg aggs_for_arrays

no pg14 on el8/9 pgdg

Version

TypeRepoVersionPG VerPackageDeps
EXTMIXED3.0.21817161514count_distinct-
RPMPIGSTY3.0.21817161514count_distinct_$v-
DEBPIGSTY3.0.21817161514postgresql-$v-count-distinct-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
d12.aarch64
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
d13.x86_64
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
d13.aarch64
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
u22.x86_64
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
u22.aarch64
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
u24.x86_64
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
u24.aarch64
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2
PIGSTY 3.0.2

Build

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

pig build pkg count_distinct         # build RPM / DEB packages

Install

You can install count_distinct 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 count_distinct;          # Install for current active PG version
pig ext install -y count_distinct -v 18  # PG 18
pig ext install -y count_distinct -v 17  # PG 17
pig ext install -y count_distinct -v 16  # PG 16
pig ext install -y count_distinct -v 15  # PG 15
pig ext install -y count_distinct -v 14  # PG 14
dnf install -y count_distinct_18       # PG 18
dnf install -y count_distinct_17       # PG 17
dnf install -y count_distinct_16       # PG 16
dnf install -y count_distinct_15       # PG 15
dnf install -y count_distinct_14       # PG 14
apt install -y postgresql-18-count-distinct   # PG 18
apt install -y postgresql-17-count-distinct   # PG 17
apt install -y postgresql-16-count-distinct   # PG 16
apt install -y postgresql-15-count-distinct   # PG 15
apt install -y postgresql-14-count-distinct   # PG 14

Create Extension:

CREATE EXTENSION count_distinct;

Usage

count_distinct: alternative to COUNT(DISTINCT …) with better performance

Provides an alternative to COUNT(DISTINCT ...) that avoids sorting and supports parallel aggregation.

CREATE EXTENSION count_distinct;

Functions

FunctionDescription
count_distinct(value anyelement)Count distinct values (alternative to COUNT(DISTINCT ...))
array_agg_distinct(value anyelement)Aggregate distinct values into an array
count_distinct_elements(value anyarray)Count distinct elements within input arrays
array_agg_distinct_elements(value anyarray)Aggregate distinct elements from input arrays

Examples

CREATE TABLE test_table (id INT, val INT);
INSERT INTO test_table
SELECT mod(i, 1000), (1000 * random())::int
FROM generate_series(1, 10000000) s(i);

-- Instead of:  SELECT id, COUNT(DISTINCT val) FROM test_table GROUP BY 1;
-- Use:
SELECT id, count_distinct(val) FROM test_table GROUP BY 1;

-- Aggregate distinct values into an array
SELECT id, array_agg_distinct(val) FROM test_table GROUP BY 1;

-- Count distinct elements across arrays
SELECT count_distinct_elements(ARRAY[1, 2, 2, 3]);

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