Darko Prenosil <darko(dot)prenosil(at)finteh(dot)hr> writes:
> Anyone knows what I'm doing wrong ?
Well, when you didn't show us the text of the function, no.
However, a reasonable bet would be that you used SPI inside the function
and did not use it correctly, leaving the SPI state corrupted when
control got back to plpgsql.
regards, tom lane