You need the whole sequence of DECLARE ... BEGIN ... END in a plpgsqlfunction. Yes, errors are non-obvious with this thing.