Let’s say I’ve created a composite type in Postgresql:
CREATE TYPE custom_type AS
(x integer
y integer);
I need to use it in a function as an array:
...
DECLARE
customVar custom_type[];
BEGIN
....
My question is: how do I access custom_type’s specific components?
For example, I want to (re)assign ‘x’ for the third element in custom_type array…
postgres=> create type pt as (x int, y int); CREATE TYPE postgres=> create or replace function fx() returns void as $$ declare a pt[] = ARRAY[(10,20),(30,40)]; declare xx pt; begin for i in array_lower(a, 1) .. array_upper(a,1) loop xx = a[i]; xx.x := xx.x + 1; a[i] := xx; raise notice '%', a[i].x; end loop; end; $$ language plpgsql; CREATE FUNCTION postgres=> select fx(); NOTICE: 11 NOTICE: 31 fx ──── (1 row)Significant limit for target of assign statement is possibility to refer only one level nested properties. This limit can be bypassed by auxiliary variables – it is not too friendly – and internal implementation is too simple, but it is fast and enough for typical stored procedure usage although it is not strong in comparison with generic programming languages.