weighted_statistics

High-performance weighted statistics functions for sparse data

Overview

PackageVersionCategoryLicenseLanguage
pg_weighted_statistics1.0.0FUNCPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
4680weighted_statisticsNoYesNoYesYesYes-

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.0.01817161514pg_weighted_statistics-
RPMPIGSTY1.0.01817161514pg_weighted_statistics_$v-
DEBPIGSTY1.0.01817161514postgresql-$v-weighted-statistics-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
el8.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
el9.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
el9.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
el10.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
el10.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
d12.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
d12.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
d13.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
d13.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
u22.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
u22.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
u24.x86_64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
u24.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0

Build

You can build the RPM / DEB packages for pg_weighted_statistics using pig build:

pig build pkg pg_weighted_statistics         # build RPM / DEB packages

Install

You can install pg_weighted_statistics directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pg_weighted_statistics;          # Install for current active PG version
pig ext install -y pg_weighted_statistics -v 18  # PG 18
pig ext install -y pg_weighted_statistics -v 17  # PG 17
pig ext install -y pg_weighted_statistics -v 16  # PG 16
pig ext install -y pg_weighted_statistics -v 15  # PG 15
pig ext install -y pg_weighted_statistics -v 14  # PG 14
dnf install -y pg_weighted_statistics_18       # PG 18
dnf install -y pg_weighted_statistics_17       # PG 17
dnf install -y pg_weighted_statistics_16       # PG 16
dnf install -y pg_weighted_statistics_15       # PG 15
dnf install -y pg_weighted_statistics_14       # PG 14
apt install -y postgresql-18-weighted-statistics   # PG 18
apt install -y postgresql-17-weighted-statistics   # PG 17
apt install -y postgresql-16-weighted-statistics   # PG 16
apt install -y postgresql-15-weighted-statistics   # PG 15
apt install -y postgresql-14-weighted-statistics   # PG 14

Create Extension:

CREATE EXTENSION weighted_statistics;

Usage

weighted_statistics: weighted statistical functions for PostgreSQL

High-performance C extension providing weighted statistical functions with automatic sparse data handling (when sum(weights) < 1.0).

CREATE EXTENSION weighted_statistics;

Functions

FunctionDescription
weighted_mean(values[], weights[])Weighted mean
weighted_variance(values[], weights[], ddof)Weighted variance (ddof: 0=population, 1=sample)
weighted_std(values[], weights[], ddof)Weighted standard deviation
weighted_quantile(values[], weights[], quantiles[])Empirical CDF quantiles
wquantile(values[], weights[], quantiles[])Type 7 (Hyndman-Fan) quantiles
whdquantile(values[], weights[], quantiles[])Harrell-Davis quantiles
weighted_median(values[], weights[])50th percentile shortcut (empirical CDF)

Examples

-- Weighted mean
SELECT weighted_mean(ARRAY[1.0, 2.0, 3.0], ARRAY[0.2, 0.3, 0.5]);
-- 2.3

-- Weighted quantiles
SELECT weighted_quantile(ARRAY[10.0, 20.0, 30.0], ARRAY[0.3, 0.4, 0.3], ARRAY[0.25, 0.5, 0.75]);
-- {15.0, 20.0, 25.0}

-- Sparse data (auto-adds implicit zeros when sum(weights) < 1.0)
SELECT weighted_mean(ARRAY[10, 20], ARRAY[0.2, 0.3]);
-- 8.0  (equivalent to weighted_mean(ARRAY[10, 20, 0, 0], ARRAY[0.2, 0.3, 0.25, 0.25]))

-- Multiple statistics
SELECT weighted_mean(vals, weights),
       weighted_std(vals, weights, 1),
       weighted_quantile(vals, weights, ARRAY[0.05, 0.95])
FROM (SELECT array_agg(val) AS vals, array_agg(weight) AS weights FROM data) t;

Last Modified 2026-03-12: add pg extension catalog (95749bf)