Fungsi berikut menghasilkan semua kombinasi ukuran yang diminta sebagai kumpulan baris dengan satu kombinasi per baris:
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;
Fungsi ini polimorfik dalam tipe array.