topn
type for top-n JSONB
Repository
citusdata/postgresql-topn
https://github.com/citusdata/postgresql-topn
Source
postgresql-topn-2.7.0.tar.gz
postgresql-topn-2.7.0.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
topn | 2.7.0 | FUNC | AGPL-3.0 | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4600 | topn | No | Yes | No | Yes | No | No | - |
| Related | count_distinct quantile lower_quantile first_last_agg omnisketch ddsketch aggs_for_arrays aggs_for_vecs |
|---|
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PGDG | 2.7.0 | 1817161514 | topn | - |
| RPM | PGDG | 2.7.0 | 1817161514 | topn_$v | - |
| DEB | PIGSTY | 2.7.0 | 1817161514 | postgresql-$v-topn | - |
Build
You can build the DEB packages for topn using pig build:
pig build pkg topn # build DEB packages
Install
You can install topn directly. First, make sure the PGDG repository is added and enabled:
pig repo add pgdg -u # Add PGDG repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install topn; # Install for current active PG version
pig ext install -y topn -v 18 # PG 18
pig ext install -y topn -v 17 # PG 17
pig ext install -y topn -v 16 # PG 16
pig ext install -y topn -v 15 # PG 15
pig ext install -y topn -v 14 # PG 14
dnf install -y topn_18 # PG 18
dnf install -y topn_17 # PG 17
dnf install -y topn_16 # PG 16
dnf install -y topn_15 # PG 15
dnf install -y topn_14 # PG 14
apt install -y postgresql-18-topn # PG 18
apt install -y postgresql-17-topn # PG 17
apt install -y postgresql-16-topn # PG 16
apt install -y postgresql-15-topn # PG 15
apt install -y postgresql-14-topn # PG 14
Create Extension:
CREATE EXTENSION topn;
Usage
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
| Function | Description |
|---|---|
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
| Function | Description |
|---|---|
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;
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.