count_distinct
count_distinct
count_distinct : An alternative to COUNT(DISTINCT …) aggregate, usable with HashAggregate
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 4630 | count_distinct | count_distinct | 3.0.2 | FUNC | BSD 2-Clause | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d-r | No | Yes | No | Yes | yes | no |
| Relationships | |
|---|---|
| See Also | topn hll omnisketch ddsketch quantile lower_quantile first_last_agg aggs_for_arrays |
no pg14 on el8/9 pgdg
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | MIXED | 3.0.2 | 18 17 16 15 14 | count_distinct | - |
| RPM | PIGSTY | 3.0.2 | 18 17 16 15 14 | count_distinct_$v | - |
| DEB | PIGSTY | 3.0.2 | 18 17 16 15 14 | postgresql-$v-count-distinct | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 |
el8.aarch64 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 |
el9.x86_64 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 |
el9.aarch64 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 |
el10.x86_64 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 |
el10.aarch64 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 | PIGSTY 3.0.2 |
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 |
Source
pig build pkg count_distinct; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install count_distinct; # install via package name, for the active PG version
pig install count_distinct -v 18; # install for PG 18
pig install count_distinct -v 17; # install for PG 17
pig install count_distinct -v 16; # install for PG 16
pig install count_distinct -v 15; # install for PG 15
pig install count_distinct -v 14; # install for PG 14Create this extension with:
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
| Function | Description |
|---|---|
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 updated on