Back to Yugabyte Db

postgresql-hll extension

docs/content/v2024.1/additional-features/pg-extensions/extension-postgresql-hll.md

2026.1.0.0-b251.6 KB
Original Source

The postgresql-hll extension adds a new data type hll, which is a HyperLogLog data structure. HyperLogLog is a fixed-size, set-like structure used for distinct value counting with tunable precision. For example, in 1280 bytes hll can estimate the count of tens of billions of distinct values with only a few percent error.

First, enable the extension:

sql
CREATE EXTENSION "hll";

To run the helloworld example from the postgresql-hll repository, connect using ysqlsh and run the following:

sql
CREATE TABLE helloworld (id integer, set hll);
output
CREATE TABLE

Insert an empty HLL as follows:

sql
INSERT INTO helloworld(id, set) VALUES (1, hll_empty());
output
INSERT 0 1

Add a hashed integer to the HLL as follows:

sql
UPDATE helloworld SET set = hll_add(set, hll_hash_integer(12345)) WHERE id = 1;
output
UPDATE 1

Add a hashed string to the HLL as follows:

sql
UPDATE helloworld SET set = hll_add(set, hll_hash_text('hello world')) WHERE id = 1;
output
UPDATE 1

Get the cardinality of the HLL as follows:

sql
SELECT hll_cardinality(set) FROM helloworld WHERE id = 1;
output
 hll_cardinality
-----------------
               2
(1 row)

For a more advanced example, see the Data Warehouse Use Case.