declare "i" to be an integer, not serial, in your table2 since you're using nextval to grab the sequence value by hand.
You need to declare v_index_id in your function, like this:
v_index_id table2.i%TYPE;
(picks up the type from table2's i column automatically)
Other than that I don't see any error in your function but I could be wrong, of course ...