Skip to content

topn

topn : type for top-n JSONB

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
4600
topn
topn
2.7.0
FUNC
AGPL-3.0
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d--
No
Yes
No
Yes
no
no
Relationships
See Also
count_distinct
quantile
lower_quantile
first_last_agg
omnisketch
ddsketch
aggs_for_arrays
aggs_for_vecs

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PGDG
2.7.0
18
17
16
15
14
topn-
RPM
PGDG
2.7.0
18
17
16
15
14
topn_$v-
DEB
PIGSTY
2.7.0
18
17
16
15
14
postgresql-$v-topn-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
el8.aarch64
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
el9.x86_64
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
el9.aarch64
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
el10.x86_64
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
el10.aarch64
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
PGDG 2.7.0
d12.x86_64
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
d12.aarch64
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
d13.x86_64
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
d13.aarch64
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
u22.x86_64
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
u22.aarch64
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
u24.x86_64
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
u24.aarch64
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
u26.x86_64
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
u26.aarch64
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0
PIGSTY 2.7.0

Source

pig build pkg topn;		# build deb

Install

Make sure PGDG repo available:

pig repo add pgdg -u    # add pgdg repo and update cache

Install this extension with pig:

pig install topn;		# install via package name, for the active PG version

pig install topn -v 18;   # install for PG 18
pig install topn -v 17;   # install for PG 17
pig install topn -v 16;   # install for PG 16
pig install topn -v 15;   # install for PG 15
pig install topn -v 14;   # install for PG 14

Create this extension with:

CREATE EXTENSION topn;

Usage

topn: top-N values approximation for PostgreSQL

Provides approximate top-N value tracking using an approximation algorithm that keeps a predefined number of frequent items and counters. Supports materialization, incremental updates, and merging across time intervals.

CREATE EXTENSION topn;

Data Type

Uses JSONB to store the frequent items and their frequencies.

Aggregates

FunctionDescription
topn_add_agg(text)Aggregate that creates a JSONB counter from a text column
topn_union_agg(jsonb)Aggregate that merges multiple JSONB counter lists

Functions

FunctionDescription
topn(jsonb, n)Return the top-N elements and frequencies as rows
topn_add(jsonb, text)Add a text value to a JSONB counter
topn_union(jsonb, jsonb)Merge two JSONB counter lists

Configuration

  • topn.number_of_counters – number of counters to track (default: 1000)

Examples

-- Materialize top products by date
CREATE TABLE popular_products (
  review_date date UNIQUE,
  agg_data jsonb
);

INSERT INTO popular_products
SELECT review_date, topn_add_agg(product_id)
FROM customer_reviews GROUP BY review_date;

-- Get the top-1 product for each day
SELECT review_date, (topn(agg_data, 1)).*
FROM popular_products ORDER BY review_date;

-- Top 10 across a time range (merging daily summaries)
SELECT (topn(topn_union_agg(agg_data), 10)).*
FROM popular_products
WHERE review_date >= '2000-01-01' AND review_date < '2000-02-01'
ORDER BY 2 DESC;
Last updated on