subjects
is declared as an array of text. Your SELECT returns a record type column, not an array.
The following should work:
select id, first_name, array[subject1, subject2, subject3] from students
However, you don't need a type, nor a FOR loop, nor PL/pgSQL for this:
create function collect_students()
returns table (student_id numeric, first_name text, subjects text[])
as $function$
select id, first_name, array[subject1, subject2, subject3]
from students
$function$
language sql;
If you do want to use PL/pgSQL you still don't need a (slow) FOR loop. A simple return query select ...;
will do.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…