跳转到主要内容

argm

argmax, argmin, and anyold aggregate functions

argm : argmax, argmin, and anyold aggregate functions

Overview

ID Extension Package Version Category License Language
4755 argm argm 1.1.1 FUNC PostgreSQL C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--s-d-r No Yes No Yes yes no
fix pg16+ varlena header with patch

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT PIGSTY 1.1.1 18 17 16 15 14 argm -
RPM PIGSTY 1.1.1 18 17 16 15 14 argm_$v -
DEB PIGSTY 1.1.1 18 17 16 15 14 postgresql-$v-argm -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
el8.aarch64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
el9.x86_64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
el9.aarch64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
el10.x86_64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
el10.aarch64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
d12.x86_64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
d12.aarch64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
d13.x86_64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
d13.aarch64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
u22.x86_64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
u22.aarch64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
u24.x86_64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
u24.aarch64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
u26.x86_64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
u26.aarch64 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1 PIGSTY 1.1.1
Package Version OS ORG SIZE File URL
argm_18 1.1.1 el8.x86_64 pigsty 18.3 KiB argm_18-1.1.1-1PIGSTY.el8.x86_64.rpm
argm_18 1.1.1 el8.aarch64 pigsty 18.4 KiB argm_18-1.1.1-1PIGSTY.el8.aarch64.rpm
argm_18 1.1.1 el9.x86_64 pigsty 18.2 KiB argm_18-1.1.1-1PIGSTY.el9.x86_64.rpm
argm_18 1.1.1 el9.aarch64 pigsty 18.3 KiB argm_18-1.1.1-1PIGSTY.el9.aarch64.rpm
argm_18 1.1.1 el10.x86_64 pigsty 18.2 KiB argm_18-1.1.1-1PIGSTY.el10.x86_64.rpm
argm_18 1.1.1 el10.aarch64 pigsty 18.5 KiB argm_18-1.1.1-1PIGSTY.el10.aarch64.rpm
postgresql-18-argm 1.1.1 d12.x86_64 pigsty 19.1 KiB postgresql-18-argm_1.1.1-1PIGSTY~bookworm_amd64.deb
postgresql-18-argm 1.1.1 d12.aarch64 pigsty 19.3 KiB postgresql-18-argm_1.1.1-1PIGSTY~bookworm_arm64.deb
postgresql-18-argm 1.1.1 d13.x86_64 pigsty 19.2 KiB postgresql-18-argm_1.1.1-1PIGSTY~trixie_amd64.deb
postgresql-18-argm 1.1.1 d13.aarch64 pigsty 19.3 KiB postgresql-18-argm_1.1.1-1PIGSTY~trixie_arm64.deb
postgresql-18-argm 1.1.1 u22.x86_64 pigsty 20.0 KiB postgresql-18-argm_1.1.1-1PIGSTY~jammy_amd64.deb
postgresql-18-argm 1.1.1 u22.aarch64 pigsty 20.2 KiB postgresql-18-argm_1.1.1-1PIGSTY~jammy_arm64.deb
postgresql-18-argm 1.1.1 u24.x86_64 pigsty 20.1 KiB postgresql-18-argm_1.1.1-1PIGSTY~noble_amd64.deb
postgresql-18-argm 1.1.1 u24.aarch64 pigsty 20.3 KiB postgresql-18-argm_1.1.1-1PIGSTY~noble_arm64.deb
postgresql-18-argm 1.1.1 u26.x86_64 pigsty 19.9 KiB postgresql-18-argm_1.1.1-1PIGSTY~resolute_amd64.deb
postgresql-18-argm 1.1.1 u26.aarch64 pigsty 20.0 KiB postgresql-18-argm_1.1.1-1PIGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
argm_17 1.1.1 el8.x86_64 pigsty 18.3 KiB argm_17-1.1.1-1PIGSTY.el8.x86_64.rpm
argm_17 1.1.1 el8.aarch64 pigsty 18.4 KiB argm_17-1.1.1-1PIGSTY.el8.aarch64.rpm
argm_17 1.1.1 el9.x86_64 pigsty 18.2 KiB argm_17-1.1.1-1PIGSTY.el9.x86_64.rpm
argm_17 1.1.1 el9.aarch64 pigsty 18.3 KiB argm_17-1.1.1-1PIGSTY.el9.aarch64.rpm
argm_17 1.1.1 el10.x86_64 pigsty 18.2 KiB argm_17-1.1.1-1PIGSTY.el10.x86_64.rpm
argm_17 1.1.1 el10.aarch64 pigsty 18.5 KiB argm_17-1.1.1-1PIGSTY.el10.aarch64.rpm
postgresql-17-argm 1.1.1 d12.x86_64 pigsty 19.1 KiB postgresql-17-argm_1.1.1-1PIGSTY~bookworm_amd64.deb
postgresql-17-argm 1.1.1 d12.aarch64 pigsty 19.3 KiB postgresql-17-argm_1.1.1-1PIGSTY~bookworm_arm64.deb
postgresql-17-argm 1.1.1 d13.x86_64 pigsty 19.1 KiB postgresql-17-argm_1.1.1-1PIGSTY~trixie_amd64.deb
postgresql-17-argm 1.1.1 d13.aarch64 pigsty 19.4 KiB postgresql-17-argm_1.1.1-1PIGSTY~trixie_arm64.deb
postgresql-17-argm 1.1.1 u22.x86_64 pigsty 20.9 KiB postgresql-17-argm_1.1.1-1PIGSTY~jammy_amd64.deb
postgresql-17-argm 1.1.1 u22.aarch64 pigsty 21.1 KiB postgresql-17-argm_1.1.1-1PIGSTY~jammy_arm64.deb
postgresql-17-argm 1.1.1 u24.x86_64 pigsty 20.1 KiB postgresql-17-argm_1.1.1-1PIGSTY~noble_amd64.deb
postgresql-17-argm 1.1.1 u24.aarch64 pigsty 20.2 KiB postgresql-17-argm_1.1.1-1PIGSTY~noble_arm64.deb
postgresql-17-argm 1.1.1 u26.x86_64 pigsty 20.0 KiB postgresql-17-argm_1.1.1-1PIGSTY~resolute_amd64.deb
postgresql-17-argm 1.1.1 u26.aarch64 pigsty 20.1 KiB postgresql-17-argm_1.1.1-1PIGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
argm_16 1.1.1 el8.x86_64 pigsty 18.3 KiB argm_16-1.1.1-1PIGSTY.el8.x86_64.rpm
argm_16 1.1.1 el8.aarch64 pigsty 18.4 KiB argm_16-1.1.1-1PIGSTY.el8.aarch64.rpm
argm_16 1.1.1 el9.x86_64 pigsty 18.2 KiB argm_16-1.1.1-1PIGSTY.el9.x86_64.rpm
argm_16 1.1.1 el9.aarch64 pigsty 18.3 KiB argm_16-1.1.1-1PIGSTY.el9.aarch64.rpm
argm_16 1.1.1 el10.x86_64 pigsty 18.2 KiB argm_16-1.1.1-1PIGSTY.el10.x86_64.rpm
argm_16 1.1.1 el10.aarch64 pigsty 18.4 KiB argm_16-1.1.1-1PIGSTY.el10.aarch64.rpm
postgresql-16-argm 1.1.1 d12.x86_64 pigsty 19.1 KiB postgresql-16-argm_1.1.1-1PIGSTY~bookworm_amd64.deb
postgresql-16-argm 1.1.1 d12.aarch64 pigsty 19.3 KiB postgresql-16-argm_1.1.1-1PIGSTY~bookworm_arm64.deb
postgresql-16-argm 1.1.1 d13.x86_64 pigsty 19.1 KiB postgresql-16-argm_1.1.1-1PIGSTY~trixie_amd64.deb
postgresql-16-argm 1.1.1 d13.aarch64 pigsty 19.4 KiB postgresql-16-argm_1.1.1-1PIGSTY~trixie_arm64.deb
postgresql-16-argm 1.1.1 u22.x86_64 pigsty 20.9 KiB postgresql-16-argm_1.1.1-1PIGSTY~jammy_amd64.deb
postgresql-16-argm 1.1.1 u22.aarch64 pigsty 21.1 KiB postgresql-16-argm_1.1.1-1PIGSTY~jammy_arm64.deb
postgresql-16-argm 1.1.1 u24.x86_64 pigsty 20.1 KiB postgresql-16-argm_1.1.1-1PIGSTY~noble_amd64.deb
postgresql-16-argm 1.1.1 u24.aarch64 pigsty 20.2 KiB postgresql-16-argm_1.1.1-1PIGSTY~noble_arm64.deb
postgresql-16-argm 1.1.1 u26.x86_64 pigsty 20.0 KiB postgresql-16-argm_1.1.1-1PIGSTY~resolute_amd64.deb
postgresql-16-argm 1.1.1 u26.aarch64 pigsty 20.0 KiB postgresql-16-argm_1.1.1-1PIGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
argm_15 1.1.1 el8.x86_64 pigsty 18.3 KiB argm_15-1.1.1-1PIGSTY.el8.x86_64.rpm
argm_15 1.1.1 el8.aarch64 pigsty 18.4 KiB argm_15-1.1.1-1PIGSTY.el8.aarch64.rpm
argm_15 1.1.1 el9.x86_64 pigsty 18.2 KiB argm_15-1.1.1-1PIGSTY.el9.x86_64.rpm
argm_15 1.1.1 el9.aarch64 pigsty 18.3 KiB argm_15-1.1.1-1PIGSTY.el9.aarch64.rpm
argm_15 1.1.1 el10.x86_64 pigsty 18.1 KiB argm_15-1.1.1-1PIGSTY.el10.x86_64.rpm
argm_15 1.1.1 el10.aarch64 pigsty 18.4 KiB argm_15-1.1.1-1PIGSTY.el10.aarch64.rpm
postgresql-15-argm 1.1.1 d12.x86_64 pigsty 19.1 KiB postgresql-15-argm_1.1.1-1PIGSTY~bookworm_amd64.deb
postgresql-15-argm 1.1.1 d12.aarch64 pigsty 19.3 KiB postgresql-15-argm_1.1.1-1PIGSTY~bookworm_arm64.deb
postgresql-15-argm 1.1.1 d13.x86_64 pigsty 19.1 KiB postgresql-15-argm_1.1.1-1PIGSTY~trixie_amd64.deb
postgresql-15-argm 1.1.1 d13.aarch64 pigsty 19.5 KiB postgresql-15-argm_1.1.1-1PIGSTY~trixie_arm64.deb
postgresql-15-argm 1.1.1 u22.x86_64 pigsty 20.9 KiB postgresql-15-argm_1.1.1-1PIGSTY~jammy_amd64.deb
postgresql-15-argm 1.1.1 u22.aarch64 pigsty 21.1 KiB postgresql-15-argm_1.1.1-1PIGSTY~jammy_arm64.deb
postgresql-15-argm 1.1.1 u24.x86_64 pigsty 20.1 KiB postgresql-15-argm_1.1.1-1PIGSTY~noble_amd64.deb
postgresql-15-argm 1.1.1 u24.aarch64 pigsty 20.2 KiB postgresql-15-argm_1.1.1-1PIGSTY~noble_arm64.deb
postgresql-15-argm 1.1.1 u26.x86_64 pigsty 20.0 KiB postgresql-15-argm_1.1.1-1PIGSTY~resolute_amd64.deb
postgresql-15-argm 1.1.1 u26.aarch64 pigsty 20.1 KiB postgresql-15-argm_1.1.1-1PIGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
argm_14 1.1.1 el8.x86_64 pigsty 18.3 KiB argm_14-1.1.1-1PIGSTY.el8.x86_64.rpm
argm_14 1.1.1 el8.aarch64 pigsty 18.4 KiB argm_14-1.1.1-1PIGSTY.el8.aarch64.rpm
argm_14 1.1.1 el9.x86_64 pigsty 18.1 KiB argm_14-1.1.1-1PIGSTY.el9.x86_64.rpm
argm_14 1.1.1 el9.aarch64 pigsty 18.2 KiB argm_14-1.1.1-1PIGSTY.el9.aarch64.rpm
argm_14 1.1.1 el10.x86_64 pigsty 18.1 KiB argm_14-1.1.1-1PIGSTY.el10.x86_64.rpm
argm_14 1.1.1 el10.aarch64 pigsty 18.4 KiB argm_14-1.1.1-1PIGSTY.el10.aarch64.rpm
postgresql-14-argm 1.1.1 d12.x86_64 pigsty 19.0 KiB postgresql-14-argm_1.1.1-1PIGSTY~bookworm_amd64.deb
postgresql-14-argm 1.1.1 d12.aarch64 pigsty 19.3 KiB postgresql-14-argm_1.1.1-1PIGSTY~bookworm_arm64.deb
postgresql-14-argm 1.1.1 d13.x86_64 pigsty 19.0 KiB postgresql-14-argm_1.1.1-1PIGSTY~trixie_amd64.deb
postgresql-14-argm 1.1.1 d13.aarch64 pigsty 19.3 KiB postgresql-14-argm_1.1.1-1PIGSTY~trixie_arm64.deb
postgresql-14-argm 1.1.1 u22.x86_64 pigsty 20.9 KiB postgresql-14-argm_1.1.1-1PIGSTY~jammy_amd64.deb
postgresql-14-argm 1.1.1 u22.aarch64 pigsty 21.1 KiB postgresql-14-argm_1.1.1-1PIGSTY~jammy_arm64.deb
postgresql-14-argm 1.1.1 u24.x86_64 pigsty 20.0 KiB postgresql-14-argm_1.1.1-1PIGSTY~noble_amd64.deb
postgresql-14-argm 1.1.1 u24.aarch64 pigsty 20.2 KiB postgresql-14-argm_1.1.1-1PIGSTY~noble_arm64.deb
postgresql-14-argm 1.1.1 u26.x86_64 pigsty 20.0 KiB postgresql-14-argm_1.1.1-1PIGSTY~resolute_amd64.deb
postgresql-14-argm 1.1.1 u26.aarch64 pigsty 20.0 KiB postgresql-14-argm_1.1.1-1PIGSTY~resolute_arm64.deb

Source

pig build pkg argm;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

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

Install this extension with pig:

pig install argm;		# install via package name, for the active PG version

pig install argm -v 18;   # install for PG 18
pig install argm -v 17;   # install for PG 17
pig install argm -v 16;   # install for PG 16
pig install argm -v 15;   # install for PG 15
pig install argm -v 14;   # install for PG 14

Create this extension with:

CREATE EXTENSION argm;

Usage

Sources:

argm provides the polymorphic aggregates argmax, argmin, and anyold. They return a value from a selected row while grouping, avoiding a join or window-function pass when the row can be chosen by one or more sortable keys.

Core Workflow

CREATE EXTENSION argm;

SELECT customer_id,
       argmax(order_id, total, ordered_at) AS largest_order
FROM orders
GROUP BY customer_id;

argmax(value, key...) returns the value belonging to the lexicographically greatest key tuple. argmin selects the least tuple. Additional keys break ties without building a composite value:

SELECT device_id,
       argmax(reading, measured_at, sequence_no) AS latest_reading
FROM measurements
GROUP BY device_id;

Use anyold(value) when any member of a group is acceptable:

SELECT account_id, anyold(display_name)
FROM account_aliases
GROUP BY account_id;

Important Objects

  • argmax(value, key [, key ...]) selects the value associated with the greatest key tuple.
  • argmin(value, key [, key ...]) selects the value associated with the least key tuple.
  • anyold(value) returns an arbitrary non-null value from the aggregate state.

The aggregates accept any value type; key types must have ordering support. The SQL definitions are parallel-safe and include combine and serialization functions for partial aggregation.

Semantics and Caveats

Key tuples use one ordering direction and one collation for the whole tuple, with null keys sorted last. If complete key tuples tie, the chosen value is unspecified; add a stable final key when deterministic results matter. As with other PostgreSQL aggregates, empty input produces NULL.

argm 1.1.x requires PostgreSQL 9.6 or newer. The extension is relocatable. Upgrading from 1.0.3 to 1.1.x requires dropping and recreating the extension because the aggregate state changed; the 1.1.0-to-1.1.1 upgrade does not change the public SQL surface.