Följande funktion producerar alla kombinationer av den begärda storleken som en uppsättning rader med en kombination per rad:
create or replace function get_combinations(source anyarray, size int) returns setof anyarray as $$
with recursive combinations(combination, indices) as (
select source[i:i], array[i] from generate_subscripts(source, 1) i
union all
select c.combination || source[j], c.indices || j
from combinations c, generate_subscripts(source, 1) j
where j > all(c.indices) and
array_length(c.combination, 1) < size
)
select combination from combinations
where array_length(combination, 1) = size;
$$ language sql;
Denna funktion är polymorf i arraytypen.