SETVAL in a Window Function
Your usage of setval() is not what one usually expects.  It is used for resetting the sequence.  Feeding new values to IDs and other stuff is done by nextval().  Its function is described so:


  nextval()   Advance sequence and return new value


Important to note that this is done so that two concurrent transactions would never get the same number when calling nextval().  Also, it is not really suitable for producing gapless series - the reason is already cited in your question.