collection
collection
pgcollection : Memory optimized data type to be used inside of plpglsql func
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 3630 | collection | pgcollection | 2.0.0 | TYPE | Apache-2.0 | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d-r | No | Yes | No | Yes | yes | no |
| Relationships | |
|---|---|
| See Also | prefix semver unit pgpdf pglite_fusion md5hash asn1oid roaringbitmap |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 2.0.0 | 18 17 16 15 14 | pgcollection | - |
| RPM | PIGSTY | 2.0.0 | 18 17 16 15 14 | pgcollection_$v | - |
| DEB | PIGSTY | 2.0.0 | 18 17 16 15 14 | postgresql-$v-collection | - |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
el8.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
el9.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
el9.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
el10.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
el10.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
d12.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
d12.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
d13.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
d13.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
u22.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
u22.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
u24.x86_64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
u24.aarch64 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 | PIGSTY 2.0.0 |
Source
pig build pkg pgcollection; # build rpm/debInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall this extension with pig:
pig install pgcollection; # install via package name, for the active PG version
pig install collection; # install by extension name, for the current active PG version
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
pig install collection -v 14; # install for PG 14Create this extension with:
CREATE EXTENSION collection;Usage
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 ordericollection: 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
| Function | Description |
|---|---|
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 updated on