Wu Ivy <ivywuyzl@gmail.com> writes:
> fprintf(fp, (i == tupdesc->natts) ? "%s\n" : "%s," , SPI_getvalue(tuple, tupdesc, i));
That (specifically the SPI_getvalue call) is what's leaking memory.
You could improve matters by pfree'ing the result string after each
such call.
Just to add insult to injury, it's also looking up the column datatype's
output function afresh on every call. There could perhaps be some leakage
involved in those lookups too, though I'd bet the main problem is the
result strings.
regards, tom lane