unnest_ordinality
unnest_ordinality · Legacy array unnest function that returns each element with its ordinal position.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100693 | unnest_ordinality | unnest_ordinality | 1.0.1 | FUNC | PostgreSQL | C |
PostgreSQL · 10, 11, 12, 13, 14, 15, 16, 17, 18
Usage
Sources:
- Official documentation at the reviewed commit
- SQL declaration
- Set-returning implementation
- Extension control file
- Official PGXN distribution
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
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.