try
CREATE OR REPLACE FUNCTION last_fields(anyarray, int)
RETURNS anyarray LANGUAGE sql AS $$
SELECT ($1)[array_upper($1,1) - $2 + 1: array_upper($1,1)];
$$;
postgres=# select last_fields(ARRAY[1,2,3,4,5], 2);
last_fields
─────────────
{4,5}
(1 row)