collection

Memory optimized data type to be used inside of plpglsql func

Overview

PackageVersionCategoryLicenseLanguage
pgcollection1.1.1TYPEApache-2.0C
IDExtensionBinLibLoadCreateTrustRelocSchema
3630collectionNoYesNoYesNoYes-
Relatedprefix semver unit pgpdf pglite_fusion md5hash asn1oid roaringbitmap

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY1.1.11817161514pgcollection-
RPMPIGSTY1.1.11817161514pgcollection_$v-
DEBPIGSTY1.1.11817161514postgresql-$v-collection-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
d13.x86_64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
d13.aarch64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
u22.x86_64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
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
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
u24.aarch64
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1
PIGSTY 1.1.1

Build

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

pig build pkg pgcollection         # build RPM / DEB packages

Install

You can install pgcollection 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 pgcollection;          # Install for current active PG version
pig ext install -y pgcollection -v 18  # PG 18
pig ext install -y pgcollection -v 17  # PG 17
pig ext install -y pgcollection -v 16  # PG 16
pig ext install -y pgcollection -v 15  # PG 15
pig ext install -y pgcollection -v 14  # PG 14
dnf install -y pgcollection_18       # PG 18
dnf install -y pgcollection_17       # PG 17
dnf install -y pgcollection_16       # PG 16
dnf install -y pgcollection_15       # PG 15
dnf install -y pgcollection_14       # PG 14
apt install -y postgresql-18-collection   # PG 18
apt install -y postgresql-17-collection   # PG 17
apt install -y postgresql-16-collection   # PG 16
apt install -y postgresql-15-collection   # PG 15
apt install -y postgresql-14-collection   # PG 14

Create Extension:

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;

Last Modified 2026-03-12: add pg extension catalog (95749bf)