Good afternoon. How to expand the second dimension of a two-dimensional array by standard means? The size of the array is not known in advance, so array_fill (null :: text, array [2,3]) is not applicable. I expand the first dimension with the standard "||". experimental code:
DO
$$declare
p_StrArr text[][];
begin
-- p_StrArr:=array[['a1', 'b1', 'c1'], ['a2', 'b2', 'c2'] ];
-- p_StrArr:=array_fill(null::text, array[2,3]);
p_StrArr[1][1]:='a1';
p_StrArr:=p_StrArr || array['a2'];
p_StrArr:=p_StrArr || array['a3'];
-- if you uncomment, then
-- ERROR: array subscript out of range
-- p_StrArr[1][2]:='b1';
raise notice 'p_Str=%', p_StrArr;
raise notice 'p_Str=%', p_StrArr[1][1];
raise notice 'p_Str=%', p_StrArr[2][1];
raise notice 'c1=%', array_length(p_StrArr,1);
raise notice 'c2=%', array_length(p_StrArr,2);
END$$;
the solution through a temporary array seems resource-intensive to me:
DO
$$declare
p_StrArr text[][];
p_TmpArr text[][];
begin
p_StrArr:=array[['a1', 'b1', 'c1'], ['a2', 'b2', 'c2'] ];
raise notice 'p_Str=%', p_StrArr;
p_TmpArr:=array_fill(null::text,
array[array_length(p_StrArr,1),array_length(p_StrArr,2)+1]);
for i in 1..array_length(p_StrArr,1) loop
for j in 1..array_length(p_StrArr,2) loop
p_TmpArr[i][j]:=p_StrArr[i][j];
end loop;
end loop;
p_StrArr:=p_TmpArr;
p_StrArr[1][4]:='d1';
raise notice 'p_Str=%', p_StrArr;
raise notice 'c1=%', array_length(p_StrArr,1);
raise notice 'c2=%', array_length(p_StrArr,2);
END$$;
is there a standard means?