aggs_for_vecs

Aggregate functions for array inputs

Overview

PackageVersionCategoryLicenseLanguage
aggs_for_vecs1.4.1FUNCMITC
IDExtensionBinLibLoadCreateTrustRelocSchema
4740aggs_for_vecsNoYesNoYesNoYes-
Relatedaggs_for_arrays first_last_agg arraymath floatvec vector topn

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.4.11817161514aggs_for_vecs-
RPMPIGSTY1.4.11817161514aggs_for_vecs_$v-
DEBPIGSTY1.4.11817161514postgresql-$v-aggs-for-vecs-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
d12.aarch64
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
d13.x86_64
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
d13.aarch64
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
u22.x86_64
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
u22.aarch64
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
u24.x86_64
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
u24.aarch64
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1
PIGSTY 1.4.1

Build

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

pig build pkg aggs_for_vecs         # build RPM / DEB packages

Install

You can install aggs_for_vecs 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 aggs_for_vecs;          # Install for current active PG version
pig ext install -y aggs_for_vecs -v 18  # PG 18
pig ext install -y aggs_for_vecs -v 17  # PG 17
pig ext install -y aggs_for_vecs -v 16  # PG 16
pig ext install -y aggs_for_vecs -v 15  # PG 15
pig ext install -y aggs_for_vecs -v 14  # PG 14
dnf install -y aggs_for_vecs_18       # PG 18
dnf install -y aggs_for_vecs_17       # PG 17
dnf install -y aggs_for_vecs_16       # PG 16
dnf install -y aggs_for_vecs_15       # PG 15
dnf install -y aggs_for_vecs_14       # PG 14
apt install -y postgresql-18-aggs-for-vecs   # PG 18
apt install -y postgresql-17-aggs-for-vecs   # PG 17
apt install -y postgresql-16-aggs-for-vecs   # PG 16
apt install -y postgresql-15-aggs-for-vecs   # PG 15
apt install -y postgresql-14-aggs-for-vecs   # PG 14

Create Extension:

CREATE EXTENSION aggs_for_vecs;

Usage

aggs_for_vecs: aggregate functions for arrays (vector/row-based)

Provides aggregate functions that operate on arrays as vectors, computing position-wise statistics across multiple rows. Supports SMALLINT, INTEGER, BIGINT, REAL, and DOUBLE PRECISION.

CREATE EXTENSION aggs_for_vecs;

Aggregate Functions

FunctionDescription
vec_to_count(anyarray)Count of non-nulls in each position
vec_to_sum(anyarray)Sum in each position
vec_to_min(anyarray)Minimum in each position
vec_to_max(anyarray)Maximum in each position
vec_to_mean(anyarray)Average in each position (returns FLOAT[])
vec_to_weighted_mean(values, weights)Weighted average in each position
vec_to_var_samp(anyarray)Sample variance in each position
vec_to_first(anyarray)First non-null in each position (use with ORDER BY)
vec_to_last(anyarray)Last non-null in each position (use with ORDER BY)
hist_2d(x, y, ...)2-D histogram
hist_md(vals, indexes, ...)N-dimensional histogram

Non-Aggregate Functions

FunctionDescription
vec_add(l, r)Element-wise addition
vec_sub(l, r)Element-wise subtraction
vec_mul(l, r)Element-wise multiplication
vec_div(l, r)Element-wise division
vec_elements(array, indexes)Select elements at given indexes
pad_vec(array, length)Extend array to given length with NULLs
vec_coalesce(array, default)Replace NULLs with default
vec_trim_scale(numeric[])Trim trailing zeros from NUMERIC elements
vec_without_outliers(vals, mins, maxs)Replace outliers with NULL

Examples

-- Position-wise minimum across rows
SELECT vec_to_min(vals) FROM (VALUES
  (ARRAY[1,2,3,4]),
  (ARRAY[5,0,-5,0]),
  (ARRAY[3,6,0,9])
) AS t(vals);
-- {1,0,-5,0}

-- Position-wise average
SELECT vec_to_mean(vals) FROM my_table;

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