Skip to content

collection

Associative-array data types for high-performance PL/pgSQL workflows

pgcollection · Associative-array data types for high-performance PL/pgSQL workflows

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
3700collectionpgcollection2.1.3TYPEApache-2.0C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-rNoYesNoYesyesno
PGXN name: pgcollection.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY2.1.319 18 17 16 15pgcollection-
RPMPIGSTY2.1.319 18 17 16 15pgcollection_$v-
DEBPIGSTY2.1.319 18 17 16 15postgresql-$v-collection-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
el8.aarch64N/APIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
el9.x86_64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
el9.aarch64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
el10.x86_64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
el10.aarch64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
d12.x86_64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
d12.aarch64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
d13.x86_64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
d13.aarch64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
u22.x86_64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
u22.aarch64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
u24.x86_64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
u24.aarch64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
u26.x86_64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
u26.aarch64PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3PIGSTY 2.1.3
PackageVersionOSORGSIZEFile URL
pgcollection_192.1.3el9.x86_64pigsty158.1 KiBpgcollection_19-2.1.3-1PGSTY.el9.x86_64.rpm
pgcollection_192.1.3el9.aarch64pigsty156.4 KiBpgcollection_19-2.1.3-1PGSTY.el9.aarch64.rpm
pgcollection_192.1.3el10.x86_64pigsty161.6 KiBpgcollection_19-2.1.3-1PGSTY.el10.x86_64.rpm
pgcollection_192.1.3el10.aarch64pigsty159.0 KiBpgcollection_19-2.1.3-1PGSTY.el10.aarch64.rpm
postgresql-19-collection2.1.3d12.x86_64pigsty143.5 KiBpostgresql-19-collection_2.1.3-1PGSTY~bookworm_amd64.deb
postgresql-19-collection2.1.3d12.aarch64pigsty140.2 KiBpostgresql-19-collection_2.1.3-1PGSTY~bookworm_arm64.deb
postgresql-19-collection2.1.3d13.x86_64pigsty144.2 KiBpostgresql-19-collection_2.1.3-1PGSTY~trixie_amd64.deb
postgresql-19-collection2.1.3d13.aarch64pigsty140.6 KiBpostgresql-19-collection_2.1.3-1PGSTY~trixie_arm64.deb
postgresql-19-collection2.1.3u22.x86_64pigsty159.3 KiBpostgresql-19-collection_2.1.3-1PGSTY~jammy_amd64.deb
postgresql-19-collection2.1.3u22.aarch64pigsty157.6 KiBpostgresql-19-collection_2.1.3-1PGSTY~jammy_arm64.deb
postgresql-19-collection2.1.3u24.x86_64pigsty152.1 KiBpostgresql-19-collection_2.1.3-1PGSTY~noble_amd64.deb
postgresql-19-collection2.1.3u24.aarch64pigsty149.7 KiBpostgresql-19-collection_2.1.3-1PGSTY~noble_arm64.deb
postgresql-19-collection2.1.3u26.x86_64pigsty150.1 KiBpostgresql-19-collection_2.1.3-1PGSTY~resolute_amd64.deb
postgresql-19-collection2.1.3u26.aarch64pigsty147.6 KiBpostgresql-19-collection_2.1.3-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pgcollection_182.1.3el8.x86_64pigsty157.2 KiBpgcollection_18-2.1.3-1PGSTY.el8.x86_64.rpm
pgcollection_182.1.3el8.aarch64pigsty153.2 KiBpgcollection_18-2.1.3-1PGSTY.el8.aarch64.rpm
pgcollection_182.1.3el9.x86_64pigsty158.1 KiBpgcollection_18-2.1.3-1PGSTY.el9.x86_64.rpm
pgcollection_182.1.3el9.aarch64pigsty156.7 KiBpgcollection_18-2.1.3-1PGSTY.el9.aarch64.rpm
pgcollection_182.1.3el10.x86_64pigsty161.5 KiBpgcollection_18-2.1.3-1PGSTY.el10.x86_64.rpm
pgcollection_182.1.3el10.aarch64pigsty159.1 KiBpgcollection_18-2.1.3-1PGSTY.el10.aarch64.rpm
postgresql-18-collection2.1.3d12.x86_64pigsty143.9 KiBpostgresql-18-collection_2.1.3-1PGSTY~bookworm_amd64.deb
postgresql-18-collection2.1.3d12.aarch64pigsty140.4 KiBpostgresql-18-collection_2.1.3-1PGSTY~bookworm_arm64.deb
postgresql-18-collection2.1.3d13.x86_64pigsty144.4 KiBpostgresql-18-collection_2.1.3-1PGSTY~trixie_amd64.deb
postgresql-18-collection2.1.3d13.aarch64pigsty140.8 KiBpostgresql-18-collection_2.1.3-1PGSTY~trixie_arm64.deb
postgresql-18-collection2.1.3u22.x86_64pigsty159.4 KiBpostgresql-18-collection_2.1.3-1PGSTY~jammy_amd64.deb
postgresql-18-collection2.1.3u22.aarch64pigsty158.1 KiBpostgresql-18-collection_2.1.3-1PGSTY~jammy_arm64.deb
postgresql-18-collection2.1.3u24.x86_64pigsty152.3 KiBpostgresql-18-collection_2.1.3-1PGSTY~noble_amd64.deb
postgresql-18-collection2.1.3u24.aarch64pigsty150.1 KiBpostgresql-18-collection_2.1.3-1PGSTY~noble_arm64.deb
postgresql-18-collection2.1.3u26.x86_64pigsty149.8 KiBpostgresql-18-collection_2.1.3-1PGSTY~resolute_amd64.deb
postgresql-18-collection2.1.3u26.aarch64pigsty147.6 KiBpostgresql-18-collection_2.1.3-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pgcollection_172.1.3el8.x86_64pigsty157.0 KiBpgcollection_17-2.1.3-1PGSTY.el8.x86_64.rpm
pgcollection_172.1.3el8.aarch64pigsty153.0 KiBpgcollection_17-2.1.3-1PGSTY.el8.aarch64.rpm
pgcollection_172.1.3el9.x86_64pigsty158.0 KiBpgcollection_17-2.1.3-1PGSTY.el9.x86_64.rpm
pgcollection_172.1.3el9.aarch64pigsty156.4 KiBpgcollection_17-2.1.3-1PGSTY.el9.aarch64.rpm
pgcollection_172.1.3el10.x86_64pigsty161.3 KiBpgcollection_17-2.1.3-1PGSTY.el10.x86_64.rpm
pgcollection_172.1.3el10.aarch64pigsty158.8 KiBpgcollection_17-2.1.3-1PGSTY.el10.aarch64.rpm
postgresql-17-collection2.1.3d12.x86_64pigsty143.9 KiBpostgresql-17-collection_2.1.3-1PGSTY~bookworm_amd64.deb
postgresql-17-collection2.1.3d12.aarch64pigsty140.3 KiBpostgresql-17-collection_2.1.3-1PGSTY~bookworm_arm64.deb
postgresql-17-collection2.1.3d13.x86_64pigsty144.1 KiBpostgresql-17-collection_2.1.3-1PGSTY~trixie_amd64.deb
postgresql-17-collection2.1.3d13.aarch64pigsty140.5 KiBpostgresql-17-collection_2.1.3-1PGSTY~trixie_arm64.deb
postgresql-17-collection2.1.3u22.x86_64pigsty175.6 KiBpostgresql-17-collection_2.1.3-1PGSTY~jammy_amd64.deb
postgresql-17-collection2.1.3u22.aarch64pigsty174.2 KiBpostgresql-17-collection_2.1.3-1PGSTY~jammy_arm64.deb
postgresql-17-collection2.1.3u24.x86_64pigsty152.3 KiBpostgresql-17-collection_2.1.3-1PGSTY~noble_amd64.deb
postgresql-17-collection2.1.3u24.aarch64pigsty149.7 KiBpostgresql-17-collection_2.1.3-1PGSTY~noble_arm64.deb
postgresql-17-collection2.1.3u26.x86_64pigsty149.6 KiBpostgresql-17-collection_2.1.3-1PGSTY~resolute_amd64.deb
postgresql-17-collection2.1.3u26.aarch64pigsty147.5 KiBpostgresql-17-collection_2.1.3-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pgcollection_162.1.3el8.x86_64pigsty156.0 KiBpgcollection_16-2.1.3-1PGSTY.el8.x86_64.rpm
pgcollection_162.1.3el8.aarch64pigsty151.8 KiBpgcollection_16-2.1.3-1PGSTY.el8.aarch64.rpm
pgcollection_162.1.3el9.x86_64pigsty156.7 KiBpgcollection_16-2.1.3-1PGSTY.el9.x86_64.rpm
pgcollection_162.1.3el9.aarch64pigsty155.4 KiBpgcollection_16-2.1.3-1PGSTY.el9.aarch64.rpm
pgcollection_162.1.3el10.x86_64pigsty160.3 KiBpgcollection_16-2.1.3-1PGSTY.el10.x86_64.rpm
pgcollection_162.1.3el10.aarch64pigsty157.5 KiBpgcollection_16-2.1.3-1PGSTY.el10.aarch64.rpm
postgresql-16-collection2.1.3d12.x86_64pigsty142.5 KiBpostgresql-16-collection_2.1.3-1PGSTY~bookworm_amd64.deb
postgresql-16-collection2.1.3d12.aarch64pigsty139.0 KiBpostgresql-16-collection_2.1.3-1PGSTY~bookworm_arm64.deb
postgresql-16-collection2.1.3d13.x86_64pigsty142.9 KiBpostgresql-16-collection_2.1.3-1PGSTY~trixie_amd64.deb
postgresql-16-collection2.1.3d13.aarch64pigsty139.3 KiBpostgresql-16-collection_2.1.3-1PGSTY~trixie_arm64.deb
postgresql-16-collection2.1.3u22.x86_64pigsty173.6 KiBpostgresql-16-collection_2.1.3-1PGSTY~jammy_amd64.deb
postgresql-16-collection2.1.3u22.aarch64pigsty172.2 KiBpostgresql-16-collection_2.1.3-1PGSTY~jammy_arm64.deb
postgresql-16-collection2.1.3u24.x86_64pigsty150.8 KiBpostgresql-16-collection_2.1.3-1PGSTY~noble_amd64.deb
postgresql-16-collection2.1.3u24.aarch64pigsty148.4 KiBpostgresql-16-collection_2.1.3-1PGSTY~noble_arm64.deb
postgresql-16-collection2.1.3u26.x86_64pigsty148.5 KiBpostgresql-16-collection_2.1.3-1PGSTY~resolute_amd64.deb
postgresql-16-collection2.1.3u26.aarch64pigsty146.1 KiBpostgresql-16-collection_2.1.3-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pgcollection_152.1.3el8.x86_64pigsty156.6 KiBpgcollection_15-2.1.3-1PGSTY.el8.x86_64.rpm
pgcollection_152.1.3el8.aarch64pigsty152.4 KiBpgcollection_15-2.1.3-1PGSTY.el8.aarch64.rpm
pgcollection_152.1.3el9.x86_64pigsty157.6 KiBpgcollection_15-2.1.3-1PGSTY.el9.x86_64.rpm
pgcollection_152.1.3el9.aarch64pigsty156.0 KiBpgcollection_15-2.1.3-1PGSTY.el9.aarch64.rpm
pgcollection_152.1.3el10.x86_64pigsty160.5 KiBpgcollection_15-2.1.3-1PGSTY.el10.x86_64.rpm
pgcollection_152.1.3el10.aarch64pigsty157.9 KiBpgcollection_15-2.1.3-1PGSTY.el10.aarch64.rpm
postgresql-15-collection2.1.3d12.x86_64pigsty143.3 KiBpostgresql-15-collection_2.1.3-1PGSTY~bookworm_amd64.deb
postgresql-15-collection2.1.3d12.aarch64pigsty139.8 KiBpostgresql-15-collection_2.1.3-1PGSTY~bookworm_arm64.deb
postgresql-15-collection2.1.3d13.x86_64pigsty143.8 KiBpostgresql-15-collection_2.1.3-1PGSTY~trixie_amd64.deb
postgresql-15-collection2.1.3d13.aarch64pigsty140.1 KiBpostgresql-15-collection_2.1.3-1PGSTY~trixie_arm64.deb
postgresql-15-collection2.1.3u22.x86_64pigsty174.1 KiBpostgresql-15-collection_2.1.3-1PGSTY~jammy_amd64.deb
postgresql-15-collection2.1.3u22.aarch64pigsty173.0 KiBpostgresql-15-collection_2.1.3-1PGSTY~jammy_arm64.deb
postgresql-15-collection2.1.3u24.x86_64pigsty151.0 KiBpostgresql-15-collection_2.1.3-1PGSTY~noble_amd64.deb
postgresql-15-collection2.1.3u24.aarch64pigsty148.9 KiBpostgresql-15-collection_2.1.3-1PGSTY~noble_arm64.deb
postgresql-15-collection2.1.3u26.x86_64pigsty148.7 KiBpostgresql-15-collection_2.1.3-1PGSTY~resolute_amd64.deb
postgresql-15-collection2.1.3u26.aarch64pigsty146.3 KiBpostgresql-15-collection_2.1.3-1PGSTY~resolute_arm64.deb

Source

pig build pkg pgcollection;		# 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 pgcollection;		# install by package name
pig install collection;		# install by extension name

pig install collection -v 19;   # install for PG 19
pig install collection -v 18;   # install for PG 18
pig install collection -v 17;   # install for PG 17
pig install collection -v 16;   # install for PG 16
pig install collection -v 15;   # install for PG 15

Create this extension with:

CREATE EXTENSION collection;

Usage

collection: key-value collection data types for PL/pgSQL

The collection extension provides two memory-optimized collection data types for use within PL/pgSQL functions.

CREATE EXTENSION collection;

Data Types

  • collection: Key-value pairs with text keys (max 32,767 chars), stored in creation order
  • icollection: Key-value pairs with 64-bit integer keys, enabling sparse arrays

Both types support type modifiers to specify element types:

DECLARE
    c1  collection('date');
    ic1 icollection('int4');

Subscript Access

DO $$
DECLARE t_capital collection;
BEGIN
    t_capital['USA'] := 'Washington, D.C.';
    t_capital['Japan'] := 'Tokyo';
    RAISE NOTICE '%', t_capital['USA'];  -- Washington, D.C.
END $$;

Core Functions

FunctionDescription
add(coll, key, value)Add element
count(coll)Element count
delete(coll, key)Remove element
exist(coll, key)Check key existence
find(coll, key)Retrieve value
first(coll)Move iterator to start
last(coll)Move iterator to end
next(coll)Advance iterator
prev(coll)Reverse iterator
key(coll)Current key
value(coll)Current value
copy(coll)Create copy
sort(coll)Sort by keys
keys_to_table(coll)All keys as set
values_to_table(coll)All values as set
to_table(coll)Keys and values as table

Iterator Example

DO $$
DECLARE t_capital collection;
BEGIN
    t_capital['USA'] := 'Washington, D.C.';
    t_capital['United Kingdom'] := 'London';
    t_capital['Japan'] := 'Tokyo';

    t_capital := first(t_capital);
    WHILE NOT isnull(t_capital) LOOP
        RAISE NOTICE 'Capital of % is %', key(t_capital), value(t_capital);
        t_capital := next(t_capital);
    END LOOP;
END $$;

Sparse Arrays (icollection)

icollection supports non-contiguous integer indices and distinguishes between NULL values and uninitialized keys:

DECLARE sparse icollection;
BEGIN
    sparse[1] := 'first';
    sparse[1000000] := 'millionth';  -- no memory wasted on gaps
END;

Table reader