cms_topn
cms_topn · Count-min sketch data type and aggregates for approximate frequency and top-N queries
Overview
Usage
Sources:
cms_topn 1.0.0 stores an approximate Count-Min Sketch together with a bounded list of frequent-item candidates. It is useful for compact frequency estimates, approximate top-N reporting, and merging summaries from separate partitions when exact aggregation is too expensive.
Core Workflow
Build one summary per group with cms_topn_add_agg, query candidate frequencies with topn, and merge compatible summaries with cms_topn_union_agg.
The typed null passed to topn determines the SQL type of its item column. Use the same input type and sketch parameters for summaries that will be merged.
Main Objects
cms_topn(n, error_bound, confidence)constructs an empty summary; defaults are0.001and0.99.cms_topn_add(summary, value)adds one value; aggregate overloads ofcms_topn_add_aggbuild summaries from a column.cms_topn_frequency(summary, value)returns an estimated frequency.topn(summary, typed_null)returns candidateitemand estimatedfrequencyrows.cms_topn_unionandcms_topn_union_aggcombine compatible summaries;cms_topn_inforeports sketch dimensions and metadata.
Smaller error bounds increase the sketch width; higher confidence increases its depth. Both improve statistical guarantees at a memory and CPU cost.
Approximation and Compatibility Boundaries
A Count-Min Sketch can overestimate frequency because of hash collisions, and the retained candidate list does not make the answer exact. Validate acceptable error with production-like distributions, especially heavy skew, many distinct values, and merged summaries. Do not use approximate counts for billing, integrity constraints, or other exact decisions.
The source and tests date from 2015. The binary custom type stores internal type-dependent data, so do not assume dump/restore, replication, architecture, or PostgreSQL-major compatibility without testing the exact build. Treat serialized summaries as derived data that can be rebuilt from source rows.