需求:
1 2 3 4 => 1-2 2-3 3-4 4-null
实现
deallocate getZipper;
--params:array,array_size,start
prepare getZipper (int[],int,int) as
with recursive cte(id,index,currentval,nextval) as (
values(0,$3,$1[$3],$1[$3+1])
union all
select (id+1),(index+1),$1[(index+1)],$1[(index+2)]
from cte where id<$2
)select currentval,nextval from cte where currentval is not null;
execute getZipper(array[1,2,3,4],4,1);