arraymath

Array math and operators that work element by element on the contents of arrays

Overview

PackageVersionCategoryLicenseLanguage
pg_arraymath1.1FUNCMITC
IDExtensionBinLibLoadCreateTrustRelocSchema
4770arraymathNoYesNoYesNoYes-
Relatedaggs_for_arrays aggs_for_vecs intarray first_last_agg floatvec

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.11817161514pg_arraymath-
RPMPIGSTY1.11817161514pg_arraymath_$v-
DEBPIGSTY1.11817161514postgresql-$v-pg-arraymath-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
d12.x86_64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
d12.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
d13.x86_64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
d13.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
u22.x86_64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
u22.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
u24.x86_64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
u24.aarch64
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1
PIGSTY 1.1

Build

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

pig build pkg pg_arraymath         # build RPM / DEB packages

Install

You can install pg_arraymath 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_arraymath;          # Install for current active PG version
pig ext install -y pg_arraymath -v 18  # PG 18
pig ext install -y pg_arraymath -v 17  # PG 17
pig ext install -y pg_arraymath -v 16  # PG 16
pig ext install -y pg_arraymath -v 15  # PG 15
pig ext install -y pg_arraymath -v 14  # PG 14
dnf install -y pg_arraymath_18       # PG 18
dnf install -y pg_arraymath_17       # PG 17
dnf install -y pg_arraymath_16       # PG 16
dnf install -y pg_arraymath_15       # PG 15
dnf install -y pg_arraymath_14       # PG 14
apt install -y postgresql-18-pg-arraymath   # PG 18
apt install -y postgresql-17-pg-arraymath   # PG 17
apt install -y postgresql-16-pg-arraymath   # PG 16
apt install -y postgresql-15-pg-arraymath   # PG 15
apt install -y postgresql-14-pg-arraymath   # PG 14

Create Extension:

CREATE EXTENSION arraymath;

Usage

arraymath: element-by-element array operations for PostgreSQL

Provides element-by-element operators and utility functions for integer, float, and numeric arrays.

CREATE EXTENSION arraymath;

Operators

All operators are prefixed with @ to indicate element-by-element behavior. Works with array-vs-array (same length or cycling) and array-vs-scalar.

OperatorDescriptionReturns
@=Element-by-element equalityboolean[]
@<Element-by-element less thanboolean[]
@>Element-by-element greater thanboolean[]
@<=Element-by-element less than or equalsboolean[]
@>=Element-by-element greater than or equalsboolean[]
@+Element-by-element additionsame type
@-Element-by-element subtractionsame type
@*Element-by-element multiplicationsame type
@/Element-by-element divisionsame type

Functions

FunctionDescription
array_sum(anyarray)Sum of all elements
array_avg(anyarray)Average of all elements
array_min(anyarray)Minimum element
array_max(anyarray)Maximum element
array_median(anyarray)Median element
array_sort(anyarray)Sort ascending
array_rsort(anyarray)Sort descending

Examples

-- Array vs scalar
SELECT ARRAY[1,2,3,4] @< 4;             -- {t,t,t,f}
SELECT ARRAY[3.4,5.6,7.6] @* 8.1;       -- {27.54,45.36,61.56}

-- Array vs array (cycling shorter array)
SELECT ARRAY[1,2,3,4,5,6] @* ARRAY[1,2]; -- {1,4,3,8,5,12}
SELECT ARRAY[1,2,3] @= ARRAY[3,2,1];     -- {f,t,f}

-- Utility functions
SELECT array_sort(ARRAY[9,1,8,2,7]);     -- {1,2,7,8,9}
SELECT array_sum(ARRAY[1,2,3,4,5]);      -- 15
SELECT array_median(ARRAY[1,2,3,4,5]);   -- 3

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