Minor change as for inserts we already know the schedule id so use the sequence.
Also missing index on schedule so was causing a table scan
Fixed primary key on schedule as was getting duplicate key violations on insert