Skip to content

first_last

Aggregate functions to return first or last “n” elements in a group

first_last · Aggregate functions to return first or last “n” elements in a group

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
101366first_lastfirst_last0.1.1FUNCPostgreSQLSQL

Usage

Sources:

first_last — This extension provides four aggregates: first(anyelement) first(anyelement, int4) last(anyelement) last(anyelement, int4). Use it when SQL needs these specialized functions or aggregates. Use the pinned upstream revision linked above as the API boundary and test it on the target PostgreSQL build.

Core Workflow

CREATE EXTENSION first_last;

Install the extension in the intended database, run the smallest upstream example above when available, and verify the installed version and returned values before integrating it into application SQL.

Important Objects

  • agg_first(IN p_state anyarray, IN p_new_element anyelement, IN p_limit int4) is an extension function and returns anyarray.
  • agg_first(IN p_state anyelement, IN p_new_element anyelement) is an extension function and returns anyelement.
  • agg_last(IN p_state anyarray, IN p_new_element anyelement, IN p_limit int4) is an extension function and returns anyarray.
  • agg_last(IN p_state anyelement, IN p_new_element anyelement) is an extension function and returns anyelement.
  • first is an aggregate exposed by the extension.
  • last is an aggregate exposed by the extension.

Requirements and Caveats

  • The reviewed control file declares default version 0.1.1.
  • The control file marks the extension as non-relocatable.
  • The control file does not require superuser-only installation.
  • Confirm privileges, supported PostgreSQL versions, upgrade behavior, and failure cases against the pinned source before production use.

Table reader