Skip to content

probabilistic_counter

A C data type and aggregates for approximate distinct counting using the Flajolet-Martin probabilistic counting algorithm.

distinct_estimators · A C data type and aggregates for approximate distinct counting using the Flajolet-Martin probabilistic counting algorithm.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100039probabilistic_counterdistinct_estimators1.3.3FUNCBSD-2-ClauseC

PostgreSQL · 10, 11, 12, 13, 14, 15, 16, 17, 18

Usage

Sources:

probabilistic_counter implements the Flajolet-Martin probabilistic counter as a probabilistic_estimator state type and aggregate. It trades exactness for a compact, configurable distinct-count estimate.

Core Workflow

CREATE EXTENSION probabilistic_counter;

SELECT probabilistic_distinct(i, 4, 32)
FROM generate_series(1, 100000) AS s(i);

The explicit arguments are the state byte count and number of salts. The one-argument probabilistic_distinct(anyelement) overload uses 4 bytes and 32 salts.

API

  • probabilistic_init(int, int) creates a probabilistic_estimator; probabilistic_size(int, int) reports its size.
  • probabilistic_add_item(probabilistic_estimator, anyelement) updates a state, and probabilistic_get_estimate(probabilistic_estimator) returns the estimate.
  • probabilistic_reset(probabilistic_estimator) clears a state, while length(probabilistic_estimator) reports its storage size.
  • probabilistic_distinct(anyelement, int, int) is the configurable aggregate; probabilistic_distinct(anyelement) uses the defaults.

Operational Notes

The version 1.3.3 control file is relocatable and declares no prerequisite extension or preload requirement. The upstream repository is archived and read-only. Larger states can improve behavior at the cost of memory; stored states can also contribute to MVCC bloat when updated frequently. Test the estimator on representative distributions and retain exact counting for correctness-sensitive work.

Table reader