argm

argmax, argmin, and anyold aggregate functions

Overview

PackageVersionCategoryLicenseLanguage
argm1.1.1FUNCPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
4755argmNoYesNoYesNoYes-
Relatedtablefunc first_last_agg extra_window_functions pg_duckdb

fix pg16+ varlena header with patch

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.1.11817161514argm-
RPMPIGSTY1.1.11817161514argm_$v-
DEBPIGSTY1.1.11817161514postgresql-$v-argm-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
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
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
u26.aarch64

Build

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

pig build pkg argm         # build RPM / DEB packages

Install

You can install argm 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 argm;          # Install for current active PG version
pig ext install -y argm -v 18  # PG 18
pig ext install -y argm -v 17  # PG 17
pig ext install -y argm -v 16  # PG 16
pig ext install -y argm -v 15  # PG 15
pig ext install -y argm -v 14  # PG 14
dnf install -y argm_18       # PG 18
dnf install -y argm_17       # PG 17
dnf install -y argm_16       # PG 16
dnf install -y argm_15       # PG 15
dnf install -y argm_14       # PG 14
apt install -y postgresql-18-argm   # PG 18
apt install -y postgresql-17-argm   # PG 17
apt install -y postgresql-16-argm   # PG 16
apt install -y postgresql-15-argm   # PG 15
apt install -y postgresql-14-argm   # PG 14

Create Extension:

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.


Last Modified: 2026-07-30: extension update 2026-07-30 (7219c44)