Skip to content

unnest_ordinality

Legacy array unnest function that returns each element with its ordinal position.

unnest_ordinality · Legacy array unnest function that returns each element with its ordinal position.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100693unnest_ordinalityunnest_ordinality1.0.1FUNCPostgreSQLC

PostgreSQL · 10, 11, 12, 13, 14, 15, 16, 17, 18

Usage

Sources:

unnest_ordinality is a legacy set-returning function that expands any PostgreSQL array into (element_number, element) rows. It predates PostgreSQL’s built-in WITH ORDINALITY syntax and is deprecated for maintained server versions.

Core Workflow

CREATE EXTENSION unnest_ordinality;

SELECT *
FROM unnest_ordinality(ARRAY['a', NULL, 'c']);

-- Preferred on PostgreSQL 9.4 and later:
SELECT ordinality::integer AS element_number, element
FROM unnest(ARRAY['a', NULL, 'c']) WITH ORDINALITY AS u(element, ordinality);

The extension function returns element_number integer starting at 1 and element anyelement. It preserves NULL elements and flattens multidimensional arrays in storage order. The numbers are sequential ordinals, not the source array’s declared subscripts; custom lower bounds and dimensional coordinates are not preserved. An empty array returns no rows.

Deprecation and Performance Boundaries

The C function deconstructs the entire source array and materializes every output row in a tuplestore before returning. Large or toasted arrays therefore require memory and temporary-file capacity proportional to the expanded result. It uses old backend tuple and array APIs and was mainly tested with PostgreSQL 9.2.

Use built-in unnest(...) WITH ORDINALITY on PostgreSQL 9.4 or later. Keep unnest_ordinality only for a pinned legacy compatibility requirement, and migrate SQL before upgrading or removing the old binary.

Table reader