arraymath
arraymath
pg_arraymath : Array math and operators that work element by element on the contents of arrays
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 4770 | arraymath
|
pg_arraymath
|
1.1 |
FUNC
|
MIT
|
C
|
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d-r
|
No
|
Yes
|
No
|
Yes
|
yes
|
no
|
| Relationships | |
|---|---|
| See Also | aggs_for_arrays
aggs_for_vecs
intarray
first_last_agg
floatvec
|
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY
|
1.1 |
18
17
16
15
14
|
pg_arraymath |
- |
| RPM | PIGSTY
|
1.1 |
18
17
16
15
14
|
pg_arraymath_$v |
- |
| DEB | PIGSTY
|
1.1 |
18
17
16
15
14
|
postgresql-$v-pg-arraymath |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
el8.aarch64
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
el9.x86_64
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
el9.aarch64
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
el10.x86_64
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
PIGSTY 1.1
|
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
|
Source
pig build pkg pg_arraymath; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pg_arraymath; # install via package name, for the active PG version
pig install arraymath; # install by extension name, for the current active PG version
pig install arraymath -v 18; # install for PG 18
pig install arraymath -v 17; # install for PG 17
pig install arraymath -v 16; # install for PG 16
pig install arraymath -v 15; # install for PG 15
pig install arraymath -v 14; # install for PG 14Create this extension with:
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]); -- 3Last updated on