arraymath
Array math and operators that work element by element on the contents of arrays
Repository
pramsey/pgsql-arraymath
https://github.com/pramsey/pgsql-arraymath
Source
pgsql-arraymath-1.1.tar.gz
pgsql-arraymath-1.1.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pg_arraymath | 1.1 | FUNC | MIT | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4770 | arraymath | No | Yes | No | Yes | No | Yes | - |
| Related | aggs_for_arrays aggs_for_vecs intarray first_last_agg floatvec |
|---|
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.1 | 1817161514 | pg_arraymath | - |
| RPM | PIGSTY | 1.1 | 1817161514 | pg_arraymath_$v | - |
| DEB | PIGSTY | 1.1 | 1817161514 | postgresql-$v-pg-arraymath | - |
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.
| Operator | Description | Returns |
|---|---|---|
@= | Element-by-element equality | boolean[] |
@< | Element-by-element less than | boolean[] |
@> | Element-by-element greater than | boolean[] |
@<= | Element-by-element less than or equals | boolean[] |
@>= | Element-by-element greater than or equals | boolean[] |
@+ | Element-by-element addition | same type |
@- | Element-by-element subtraction | same type |
@* | Element-by-element multiplication | same type |
@/ | Element-by-element division | same type |
Functions
| Function | Description |
|---|---|
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
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.